blob: 0020b5675fa9700d86ef2f497a35c43a8d6e41b6 [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 Masonb888db22007-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 Masonb888db22007-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 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000757 /*
758 * here we're doing allocation and writeback of the
759 * compressed pages
760 */
761 btrfs_drop_extent_cache(inode, async_extent->start,
762 async_extent->start +
763 async_extent->ram_size - 1, 0);
764
David Sterba172ddd62011-04-21 00:48:27 +0200765 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000766 if (!em) {
767 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500768 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000769 }
Chris Mason771ed682008-11-06 22:02:51 -0500770 em->start = async_extent->start;
771 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500772 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400773 em->mod_start = em->start;
774 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500775
776 em->block_start = ins.objectid;
777 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500778 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400779 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500780 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800781 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500782 set_bit(EXTENT_FLAG_PINNED, &em->flags);
783 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400784 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500785
Chris Masond3977122009-01-05 21:25:51 -0500786 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400787 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400788 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400789 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500790 if (ret != -EEXIST) {
791 free_extent_map(em);
792 break;
793 }
794 btrfs_drop_extent_cache(inode, async_extent->start,
795 async_extent->start +
796 async_extent->ram_size - 1, 0);
797 }
798
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500799 if (ret)
800 goto out_free_reserve;
801
Li Zefan261507a02010-12-17 14:21:50 +0800802 ret = btrfs_add_ordered_extent_compress(inode,
803 async_extent->start,
804 ins.objectid,
805 async_extent->ram_size,
806 ins.offset,
807 BTRFS_ORDERED_COMPRESSED,
808 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100809 if (ret) {
810 btrfs_drop_extent_cache(inode, async_extent->start,
811 async_extent->start +
812 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500813 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100814 }
Chris Mason771ed682008-11-06 22:02:51 -0500815
Chris Mason771ed682008-11-06 22:02:51 -0500816 /*
817 * clear dirty, set writeback and unlock the pages.
818 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400819 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400820 async_extent->start +
821 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400822 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
823 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400824 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500825 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500826 async_extent->start,
827 async_extent->ram_size,
828 ins.objectid,
829 ins.offset, async_extent->pages,
830 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100831 if (ret) {
832 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
833 struct page *p = async_extent->pages[0];
834 const u64 start = async_extent->start;
835 const u64 end = start + async_extent->ram_size - 1;
836
837 p->mapping = inode->i_mapping;
838 tree->ops->writepage_end_io_hook(p, start, end,
839 NULL, 0);
840 p->mapping = NULL;
841 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
842 PAGE_END_WRITEBACK |
843 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100844 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100845 }
Chris Mason771ed682008-11-06 22:02:51 -0500846 alloc_hint = ins.objectid + ins.offset;
847 kfree(async_extent);
848 cond_resched();
849 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100850 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500851out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800852 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100853out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400854 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500855 async_extent->start +
856 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400857 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400858 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
859 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100860 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
861 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100862 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100863 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500864 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500865}
866
Josef Bacik4b46fce2010-05-23 11:00:55 -0400867static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
868 u64 num_bytes)
869{
870 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
871 struct extent_map *em;
872 u64 alloc_hint = 0;
873
874 read_lock(&em_tree->lock);
875 em = search_extent_mapping(em_tree, start, num_bytes);
876 if (em) {
877 /*
878 * if block start isn't an actual block number then find the
879 * first block in this inode and use that as a hint. If that
880 * block is also bogus then just don't worry about it.
881 */
882 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
883 free_extent_map(em);
884 em = search_extent_mapping(em_tree, 0, 0);
885 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
886 alloc_hint = em->block_start;
887 if (em)
888 free_extent_map(em);
889 } else {
890 alloc_hint = em->block_start;
891 free_extent_map(em);
892 }
893 }
894 read_unlock(&em_tree->lock);
895
896 return alloc_hint;
897}
898
Chris Mason771ed682008-11-06 22:02:51 -0500899/*
900 * when extent_io.c finds a delayed allocation range in the file,
901 * the call backs end up in this code. The basic idea is to
902 * allocate extents on disk for the range, and create ordered data structs
903 * in ram to track those extents.
904 *
905 * locked_page is the page that writepage had locked already. We use
906 * it to make sure we don't do extra locks or unlocks.
907 *
908 * *page_started is set to one if we unlock locked_page and do everything
909 * required to start IO on it. It may be clean and already done with
910 * IO when we return.
911 */
Josef Bacik00361582013-08-14 14:02:47 -0400912static noinline int cow_file_range(struct inode *inode,
913 struct page *locked_page,
914 u64 start, u64 end, int *page_started,
915 unsigned long *nr_written,
916 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500917{
Josef Bacik00361582013-08-14 14:02:47 -0400918 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500919 u64 alloc_hint = 0;
920 u64 num_bytes;
921 unsigned long ram_size;
922 u64 disk_num_bytes;
923 u64 cur_alloc_size;
924 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500925 struct btrfs_key ins;
926 struct extent_map *em;
927 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
928 int ret = 0;
929
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400930 if (btrfs_is_free_space_inode(inode)) {
931 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500932 ret = -EINVAL;
933 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400934 }
Chris Mason771ed682008-11-06 22:02:51 -0500935
Qu Wenruofda28322013-02-26 08:10:22 +0000936 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500937 num_bytes = max(blocksize, num_bytes);
938 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500939
Chris Mason4cb53002011-05-24 15:35:30 -0400940 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400941 if (num_bytes < 64 * 1024 &&
942 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400943 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400944
Chris Mason771ed682008-11-06 22:02:51 -0500945 if (start == 0) {
946 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400947 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
948 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500949 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400950 extent_clear_unlock_delalloc(inode, start, end, NULL,
951 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400952 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400953 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
954 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000955
Chris Mason771ed682008-11-06 22:02:51 -0500956 *nr_written = *nr_written +
957 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
958 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500959 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100960 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100961 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500962 }
963 }
Chris Masonc8b97812008-10-29 14:49:59 -0400964
965 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200966 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400967
Josef Bacik4b46fce2010-05-23 11:00:55 -0400968 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400969 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400970
Chris Masond3977122009-01-05 21:25:51 -0500971 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400972 unsigned long op;
973
Josef Bacik287a0ab2010-03-19 18:07:23 +0000974 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400975 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500976 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800977 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400978 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100979 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500980
David Sterba172ddd62011-04-21 00:48:27 +0200981 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000982 if (!em) {
983 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000984 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000985 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400986 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500987 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500988 ram_size = ins.offset;
989 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400990 em->mod_start = em->start;
991 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400992
Chris Masone6dcd2d2008-07-17 12:53:50 -0400993 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400994 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500995 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400996 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400997 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400998 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400999 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -04001000
Chris Masond3977122009-01-05 21:25:51 -05001001 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001002 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001003 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001004 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001005 if (ret != -EEXIST) {
1006 free_extent_map(em);
1007 break;
1008 }
1009 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001010 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001011 }
Liu Boace68ba2013-04-22 10:53:47 +00001012 if (ret)
1013 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001014
Chris Mason98d20f62008-04-14 09:46:10 -04001015 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001016 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001017 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001018 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001019 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001020
Yan Zheng17d217f2008-12-12 10:03:38 -05001021 if (root->root_key.objectid ==
1022 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1023 ret = btrfs_reloc_clone_csums(inode, start,
1024 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001025 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001026 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001027 }
1028
Chris Masond3977122009-01-05 21:25:51 -05001029 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001030 break;
Chris Masond3977122009-01-05 21:25:51 -05001031
Chris Masonc8b97812008-10-29 14:49:59 -04001032 /* we're not doing compressed IO, don't unlock the first
1033 * page (which the caller expects to stay locked), don't
1034 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001035 *
1036 * Do set the Private2 bit so we know this page was properly
1037 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001038 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001039 op = unlock ? PAGE_UNLOCK : 0;
1040 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001041
Josef Bacikc2790a22013-07-29 11:20:47 -04001042 extent_clear_unlock_delalloc(inode, start,
1043 start + ram_size - 1, locked_page,
1044 EXTENT_LOCKED | EXTENT_DELALLOC,
1045 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001046 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001047 num_bytes -= cur_alloc_size;
1048 alloc_hint = ins.objectid + ins.offset;
1049 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001050 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001051out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001052 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001053
Filipe Mananad9f85962014-08-25 10:43:00 +01001054out_drop_extent_cache:
1055 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001056out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001057 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001058out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001059 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001060 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1061 EXTENT_DELALLOC | EXTENT_DEFRAG,
1062 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1063 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001064 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001065}
Chris Masonc8b97812008-10-29 14:49:59 -04001066
Chris Mason771ed682008-11-06 22:02:51 -05001067/*
1068 * work queue call back to started compression on a file and pages
1069 */
1070static noinline void async_cow_start(struct btrfs_work *work)
1071{
1072 struct async_cow *async_cow;
1073 int num_added = 0;
1074 async_cow = container_of(work, struct async_cow, work);
1075
1076 compress_file_range(async_cow->inode, async_cow->locked_page,
1077 async_cow->start, async_cow->end, async_cow,
1078 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001079 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001080 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001081 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001082 }
Chris Mason771ed682008-11-06 22:02:51 -05001083}
1084
1085/*
1086 * work queue call back to submit previously compressed pages
1087 */
1088static noinline void async_cow_submit(struct btrfs_work *work)
1089{
1090 struct async_cow *async_cow;
1091 struct btrfs_root *root;
1092 unsigned long nr_pages;
1093
1094 async_cow = container_of(work, struct async_cow, work);
1095
1096 root = async_cow->root;
1097 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1098 PAGE_CACHE_SHIFT;
1099
Josef Bacik66657b32012-08-01 15:36:24 -04001100 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001101 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001102 waitqueue_active(&root->fs_info->async_submit_wait))
1103 wake_up(&root->fs_info->async_submit_wait);
1104
Chris Masond3977122009-01-05 21:25:51 -05001105 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001106 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001107}
Chris Masonc8b97812008-10-29 14:49:59 -04001108
Chris Mason771ed682008-11-06 22:02:51 -05001109static noinline void async_cow_free(struct btrfs_work *work)
1110{
1111 struct async_cow *async_cow;
1112 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001113 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001114 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001115 kfree(async_cow);
1116}
1117
1118static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1119 u64 start, u64 end, int *page_started,
1120 unsigned long *nr_written)
1121{
1122 struct async_cow *async_cow;
1123 struct btrfs_root *root = BTRFS_I(inode)->root;
1124 unsigned long nr_pages;
1125 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001126 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001127
Chris Masona3429ab2009-10-08 12:30:20 -04001128 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1129 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001130 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001131 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001132 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001133 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001134 async_cow->root = root;
1135 async_cow->locked_page = locked_page;
1136 async_cow->start = start;
1137
Wang Shilongf79707b2014-07-17 11:44:09 +08001138 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1139 !btrfs_test_opt(root, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001140 cur_end = end;
1141 else
1142 cur_end = min(end, start + 512 * 1024 - 1);
1143
1144 async_cow->end = cur_end;
1145 INIT_LIST_HEAD(&async_cow->extents);
1146
Liu Bo9e0af232014-08-15 23:36:53 +08001147 btrfs_init_work(&async_cow->work,
1148 btrfs_delalloc_helper,
1149 async_cow_start, async_cow_submit,
1150 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001151
Chris Mason771ed682008-11-06 22:02:51 -05001152 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1153 PAGE_CACHE_SHIFT;
1154 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1155
Qu Wenruoafe3d242014-02-28 10:46:07 +08001156 btrfs_queue_work(root->fs_info->delalloc_workers,
1157 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001158
1159 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1160 wait_event(root->fs_info->async_submit_wait,
1161 (atomic_read(&root->fs_info->async_delalloc_pages) <
1162 limit));
1163 }
1164
Chris Masond3977122009-01-05 21:25:51 -05001165 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001166 atomic_read(&root->fs_info->async_delalloc_pages)) {
1167 wait_event(root->fs_info->async_submit_wait,
1168 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1169 0));
1170 }
1171
1172 *nr_written += nr_pages;
1173 start = cur_end + 1;
1174 }
1175 *page_started = 1;
1176 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001177}
1178
Chris Masond3977122009-01-05 21:25:51 -05001179static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001180 u64 bytenr, u64 num_bytes)
1181{
1182 int ret;
1183 struct btrfs_ordered_sum *sums;
1184 LIST_HEAD(list);
1185
Yan Zheng07d400a2009-01-06 11:42:00 -05001186 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001187 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001188 if (ret == 0 && list_empty(&list))
1189 return 0;
1190
1191 while (!list_empty(&list)) {
1192 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1193 list_del(&sums->list);
1194 kfree(sums);
1195 }
1196 return 1;
1197}
1198
Chris Masond352ac62008-09-29 15:18:18 -04001199/*
1200 * when nowcow writeback call back. This checks for snapshots or COW copies
1201 * of the extents that exist in the file, and COWs the file as required.
1202 *
1203 * If no cow copies or snapshots exist, we write directly to the existing
1204 * blocks on disk
1205 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001206static noinline int run_delalloc_nocow(struct inode *inode,
1207 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001208 u64 start, u64 end, int *page_started, int force,
1209 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001210{
Chris Masonbe20aa92007-12-17 20:14:01 -05001211 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001212 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001213 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001214 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001215 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001216 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001217 u64 cow_start;
1218 u64 cur_offset;
1219 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001220 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001221 u64 disk_bytenr;
1222 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001223 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001224 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001225 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001226 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001227 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001228 int nocow;
1229 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001230 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001231 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001232
1233 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001234 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001235 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1236 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001237 EXTENT_DO_ACCOUNTING |
1238 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001239 PAGE_CLEAR_DIRTY |
1240 PAGE_SET_WRITEBACK |
1241 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001242 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001243 }
Li Zefan82d59022011-04-20 10:33:24 +08001244
Liu Bo83eea1f2012-07-10 05:28:39 -06001245 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001246
1247 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001248 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001249 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001250 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001251
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001252 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001253 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1254 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001255 EXTENT_DO_ACCOUNTING |
1256 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001257 PAGE_CLEAR_DIRTY |
1258 PAGE_SET_WRITEBACK |
1259 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001260 btrfs_free_path(path);
1261 return PTR_ERR(trans);
1262 }
1263
Josef Bacik74b21072011-04-13 12:02:53 -04001264 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001265
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 cow_start = (u64)-1;
1267 cur_offset = start;
1268 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001269 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001270 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001271 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001272 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001273 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1274 leaf = path->nodes[0];
1275 btrfs_item_key_to_cpu(leaf, &found_key,
1276 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001277 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001278 found_key.type == BTRFS_EXTENT_DATA_KEY)
1279 path->slots[0]--;
1280 }
1281 check_prev = 0;
1282next_slot:
1283 leaf = path->nodes[0];
1284 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1285 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001286 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001287 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001288 if (ret > 0)
1289 break;
1290 leaf = path->nodes[0];
1291 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001292
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 nocow = 0;
1294 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001295 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001296 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001297
Li Zefan33345d012011-04-20 10:31:50 +08001298 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001299 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1300 found_key.offset > end)
1301 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001302
Yan Zheng80ff3852008-10-30 14:20:02 -04001303 if (found_key.offset > cur_offset) {
1304 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001305 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001306 goto out_check;
1307 }
Chris Masonc31f8832008-01-08 15:46:31 -05001308
Yan Zheng80ff3852008-10-30 14:20:02 -04001309 fi = btrfs_item_ptr(leaf, path->slots[0],
1310 struct btrfs_file_extent_item);
1311 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001312
Josef Bacikcc95bef2013-04-04 14:31:27 -04001313 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001314 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1315 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001316 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001317 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001318 extent_end = found_key.offset +
1319 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001320 disk_num_bytes =
1321 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001322 if (extent_end <= start) {
1323 path->slots[0]++;
1324 goto next_slot;
1325 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001326 if (disk_bytenr == 0)
1327 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001328 if (btrfs_file_extent_compression(leaf, fi) ||
1329 btrfs_file_extent_encryption(leaf, fi) ||
1330 btrfs_file_extent_other_encoding(leaf, fi))
1331 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001332 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1333 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001334 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001335 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001336 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001337 found_key.offset -
1338 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001339 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001340 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001341 disk_bytenr += cur_offset - found_key.offset;
1342 num_bytes = min(end + 1, extent_end) - cur_offset;
1343 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001344 * if there are pending snapshots for this root,
1345 * we fall into common COW way.
1346 */
1347 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001348 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001349 if (!err)
1350 goto out_check;
1351 }
1352 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001353 * force cow if csum exists in the range.
1354 * this ensure that csum for a given extent are
1355 * either valid or do not exist.
1356 */
1357 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1358 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001359 nocow = 1;
1360 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1361 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001362 btrfs_file_extent_inline_len(leaf,
1363 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001364 extent_end = ALIGN(extent_end, root->sectorsize);
1365 } else {
1366 BUG_ON(1);
1367 }
1368out_check:
1369 if (extent_end <= start) {
1370 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001371 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001372 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001373 goto next_slot;
1374 }
1375 if (!nocow) {
1376 if (cow_start == (u64)-1)
1377 cow_start = cur_offset;
1378 cur_offset = extent_end;
1379 if (cur_offset > end)
1380 break;
1381 path->slots[0]++;
1382 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001383 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001384
David Sterbab3b4aa72011-04-21 01:20:15 +02001385 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001386 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001387 ret = cow_file_range(inode, locked_page,
1388 cow_start, found_key.offset - 1,
1389 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001390 if (ret) {
1391 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001392 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001393 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001394 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001395 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001396 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001397
Yan Zhengd899e052008-10-30 14:25:28 -04001398 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1399 struct extent_map *em;
1400 struct extent_map_tree *em_tree;
1401 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001402 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001403 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001404 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001405 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001406 em->len = num_bytes;
1407 em->block_len = num_bytes;
1408 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001409 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001410 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001411 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001412 em->mod_start = em->start;
1413 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001414 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001415 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001416 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001417 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001418 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001419 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001420 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001421 if (ret != -EEXIST) {
1422 free_extent_map(em);
1423 break;
1424 }
1425 btrfs_drop_extent_cache(inode, em->start,
1426 em->start + em->len - 1, 0);
1427 }
1428 type = BTRFS_ORDERED_PREALLOC;
1429 } else {
1430 type = BTRFS_ORDERED_NOCOW;
1431 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001432
1433 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001434 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001435 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001436
Yan, Zhengefa56462010-05-16 10:49:59 -04001437 if (root->root_key.objectid ==
1438 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1439 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1440 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001441 if (ret) {
1442 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001443 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001444 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001445 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001446 }
1447
Josef Bacikc2790a22013-07-29 11:20:47 -04001448 extent_clear_unlock_delalloc(inode, cur_offset,
1449 cur_offset + num_bytes - 1,
1450 locked_page, EXTENT_LOCKED |
1451 EXTENT_DELALLOC, PAGE_UNLOCK |
1452 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001453 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001454 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001455 cur_offset = extent_end;
1456 if (cur_offset > end)
1457 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001458 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001459 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001460
Josef Bacik17ca04a2012-05-31 15:58:55 -04001461 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001462 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001463 cur_offset = end;
1464 }
1465
Yan Zheng80ff3852008-10-30 14:20:02 -04001466 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001467 ret = cow_file_range(inode, locked_page, cow_start, end,
1468 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001469 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001470 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001471 }
1472
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001473error:
Miao Xiea698d0752012-09-20 01:51:59 -06001474 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001475 if (!ret)
1476 ret = err;
1477
Josef Bacik17ca04a2012-05-31 15:58:55 -04001478 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001479 extent_clear_unlock_delalloc(inode, cur_offset, end,
1480 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001481 EXTENT_DELALLOC | EXTENT_DEFRAG |
1482 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1483 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001484 PAGE_SET_WRITEBACK |
1485 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001486 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001487 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001488}
1489
Wang Shilong47059d92014-07-03 18:22:07 +08001490static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1491{
1492
1493 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1494 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1495 return 0;
1496
1497 /*
1498 * @defrag_bytes is a hint value, no spinlock held here,
1499 * if is not zero, it means the file is defragging.
1500 * Force cow if given extent needs to be defragged.
1501 */
1502 if (BTRFS_I(inode)->defrag_bytes &&
1503 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1504 EXTENT_DEFRAG, 0, NULL))
1505 return 1;
1506
1507 return 0;
1508}
1509
Chris Masond352ac62008-09-29 15:18:18 -04001510/*
1511 * extent_io.c call back to do delayed allocation processing
1512 */
Chris Masonc8b97812008-10-29 14:49:59 -04001513static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001514 u64 start, u64 end, int *page_started,
1515 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001516{
Chris Masonbe20aa92007-12-17 20:14:01 -05001517 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001518 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001519
Wang Shilong47059d92014-07-03 18:22:07 +08001520 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001521 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001522 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001523 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001524 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001525 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001526 } else if (!inode_need_compress(inode)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001527 ret = cow_file_range(inode, locked_page, start, end,
1528 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001529 } else {
1530 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1531 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001532 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001533 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001534 }
Chris Masonb888db22007-08-27 16:49:44 -04001535 return ret;
1536}
1537
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001538static void btrfs_split_extent_hook(struct inode *inode,
1539 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001540{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001541 u64 size;
1542
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001543 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001544 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001545 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001546
Josef Bacikdcab6a32015-02-11 15:08:59 -05001547 size = orig->end - orig->start + 1;
1548 if (size > BTRFS_MAX_EXTENT_SIZE) {
1549 u64 num_extents;
1550 u64 new_size;
1551
1552 /*
Josef Bacikba117212015-03-13 15:01:24 -04001553 * See the explanation in btrfs_merge_extent_hook, the same
1554 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001555 */
1556 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001557 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001558 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001559 new_size = split - orig->start;
1560 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1561 BTRFS_MAX_EXTENT_SIZE);
1562 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1563 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001564 return;
1565 }
1566
Josef Bacik9e0baf62011-07-15 15:16:44 +00001567 spin_lock(&BTRFS_I(inode)->lock);
1568 BTRFS_I(inode)->outstanding_extents++;
1569 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001570}
1571
1572/*
1573 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1574 * extents so we can keep track of new extents that are just merged onto old
1575 * extents, such as when we are doing sequential writes, so we can properly
1576 * account for the metadata space we'll need.
1577 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001578static void btrfs_merge_extent_hook(struct inode *inode,
1579 struct extent_state *new,
1580 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001581{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001582 u64 new_size, old_size;
1583 u64 num_extents;
1584
Josef Bacik9ed74f22009-09-11 16:12:44 -04001585 /* not delalloc, ignore it */
1586 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001587 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001588
Josef Bacik8461a3d2015-03-13 15:12:08 -04001589 if (new->start > other->start)
1590 new_size = new->end - other->start + 1;
1591 else
1592 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001593
1594 /* we're not bigger than the max, unreserve the space and go */
1595 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1596 spin_lock(&BTRFS_I(inode)->lock);
1597 BTRFS_I(inode)->outstanding_extents--;
1598 spin_unlock(&BTRFS_I(inode)->lock);
1599 return;
1600 }
1601
1602 /*
Josef Bacikba117212015-03-13 15:01:24 -04001603 * We have to add up either side to figure out how many extents were
1604 * accounted for before we merged into one big extent. If the number of
1605 * extents we accounted for is <= the amount we need for the new range
1606 * then we can return, otherwise drop. Think of it like this
1607 *
1608 * [ 4k][MAX_SIZE]
1609 *
1610 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1611 * need 2 outstanding extents, on one side we have 1 and the other side
1612 * we have 1 so they are == and we can return. But in this case
1613 *
1614 * [MAX_SIZE+4k][MAX_SIZE+4k]
1615 *
1616 * Each range on their own accounts for 2 extents, but merged together
1617 * they are only 3 extents worth of accounting, so we need to drop in
1618 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001619 */
Josef Bacikba117212015-03-13 15:01:24 -04001620 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001621 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1622 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001623 old_size = new->end - new->start + 1;
1624 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1625 BTRFS_MAX_EXTENT_SIZE);
1626
Josef Bacikdcab6a32015-02-11 15:08:59 -05001627 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001628 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001629 return;
1630
Josef Bacik9e0baf62011-07-15 15:16:44 +00001631 spin_lock(&BTRFS_I(inode)->lock);
1632 BTRFS_I(inode)->outstanding_extents--;
1633 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001634}
1635
Miao Xieeb73c1b2013-05-15 07:48:22 +00001636static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1637 struct inode *inode)
1638{
1639 spin_lock(&root->delalloc_lock);
1640 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1641 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1642 &root->delalloc_inodes);
1643 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1644 &BTRFS_I(inode)->runtime_flags);
1645 root->nr_delalloc_inodes++;
1646 if (root->nr_delalloc_inodes == 1) {
1647 spin_lock(&root->fs_info->delalloc_root_lock);
1648 BUG_ON(!list_empty(&root->delalloc_root));
1649 list_add_tail(&root->delalloc_root,
1650 &root->fs_info->delalloc_roots);
1651 spin_unlock(&root->fs_info->delalloc_root_lock);
1652 }
1653 }
1654 spin_unlock(&root->delalloc_lock);
1655}
1656
1657static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1658 struct inode *inode)
1659{
1660 spin_lock(&root->delalloc_lock);
1661 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1662 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1663 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1664 &BTRFS_I(inode)->runtime_flags);
1665 root->nr_delalloc_inodes--;
1666 if (!root->nr_delalloc_inodes) {
1667 spin_lock(&root->fs_info->delalloc_root_lock);
1668 BUG_ON(list_empty(&root->delalloc_root));
1669 list_del_init(&root->delalloc_root);
1670 spin_unlock(&root->fs_info->delalloc_root_lock);
1671 }
1672 }
1673 spin_unlock(&root->delalloc_lock);
1674}
1675
Chris Masond352ac62008-09-29 15:18:18 -04001676/*
1677 * extent_io.c set_bit_hook, used to track delayed allocation
1678 * bytes in this file, and to maintain the list of inodes that
1679 * have pending delalloc work to be done.
1680 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001681static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001682 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001683{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001684
Wang Shilong47059d92014-07-03 18:22:07 +08001685 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1686 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001687 /*
1688 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001689 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001690 * bit, which is only set or cleared with irqs on
1691 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001692 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001693 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001694 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001695 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001696
Josef Bacik9e0baf62011-07-15 15:16:44 +00001697 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001698 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001699 } else {
1700 spin_lock(&BTRFS_I(inode)->lock);
1701 BTRFS_I(inode)->outstanding_extents++;
1702 spin_unlock(&BTRFS_I(inode)->lock);
1703 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001704
Josef Bacik6a3891c2015-03-16 17:38:52 -04001705 /* For sanity tests */
1706 if (btrfs_test_is_dummy_root(root))
1707 return;
1708
Miao Xie963d6782013-01-29 10:10:51 +00001709 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1710 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001711 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001712 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001713 if (*bits & EXTENT_DEFRAG)
1714 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001715 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001716 &BTRFS_I(inode)->runtime_flags))
1717 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001718 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001719 }
Chris Mason291d6732008-01-29 15:55:23 -05001720}
1721
Chris Masond352ac62008-09-29 15:18:18 -04001722/*
1723 * extent_io.c clear_bit_hook, see set_bit_hook for why
1724 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001725static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001726 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001727 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001728{
Wang Shilong47059d92014-07-03 18:22:07 +08001729 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001730 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1731 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001732
1733 spin_lock(&BTRFS_I(inode)->lock);
1734 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1735 BTRFS_I(inode)->defrag_bytes -= len;
1736 spin_unlock(&BTRFS_I(inode)->lock);
1737
Chris Mason75eff682008-12-15 15:54:40 -05001738 /*
1739 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001740 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001741 * bit, which is only set or cleared with irqs on
1742 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001743 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001744 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001745 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001746
Josef Bacik9e0baf62011-07-15 15:16:44 +00001747 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001748 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001749 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1750 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001751 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001752 spin_unlock(&BTRFS_I(inode)->lock);
1753 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001754
Josef Bacikb6d08f02013-09-27 14:57:43 -04001755 /*
1756 * We don't reserve metadata space for space cache inodes so we
1757 * don't need to call dellalloc_release_metadata if there is an
1758 * error.
1759 */
1760 if (*bits & EXTENT_DO_ACCOUNTING &&
1761 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001762 btrfs_delalloc_release_metadata(inode, len);
1763
Josef Bacik6a3891c2015-03-16 17:38:52 -04001764 /* For sanity tests. */
1765 if (btrfs_test_is_dummy_root(root))
1766 return;
1767
Josef Bacik0cb59c92010-07-02 12:14:14 -04001768 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001769 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001770 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001771
Miao Xie963d6782013-01-29 10:10:51 +00001772 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1773 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001774 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001775 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001776 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001777 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001778 &BTRFS_I(inode)->runtime_flags))
1779 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001780 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001781 }
Chris Mason291d6732008-01-29 15:55:23 -05001782}
1783
Chris Masond352ac62008-09-29 15:18:18 -04001784/*
1785 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1786 * we don't create bios that span stripes or chunks
1787 */
David Woodhouse64a16702009-07-15 23:29:37 +01001788int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001789 size_t size, struct bio *bio,
1790 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001791{
1792 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001793 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001794 u64 length = 0;
1795 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001796 int ret;
1797
Chris Mason771ed682008-11-06 22:02:51 -05001798 if (bio_flags & EXTENT_BIO_COMPRESSED)
1799 return 0;
1800
Kent Overstreet4f024f32013-10-11 15:44:27 -07001801 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001802 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001803 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001804 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001805 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001806 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001807 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001808 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001809 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001810}
1811
Chris Masond352ac62008-09-29 15:18:18 -04001812/*
1813 * in order to insert checksums into the metadata in large chunks,
1814 * we wait until bio submission time. All the pages in the bio are
1815 * checksummed and sums are attached onto the ordered extent record.
1816 *
1817 * At IO completion time the cums attached on the ordered extent record
1818 * are inserted into the btree
1819 */
Chris Masond3977122009-01-05 21:25:51 -05001820static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1821 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001822 unsigned long bio_flags,
1823 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001824{
Chris Mason065631f2008-02-20 12:07:25 -05001825 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001826 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001827
Chris Masond20f7042008-12-08 16:58:54 -05001828 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001829 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001830 return 0;
1831}
Chris Masone0156402008-04-16 11:15:20 -04001832
Chris Mason4a69a412008-11-06 22:03:00 -05001833/*
1834 * in order to insert checksums into the metadata in large chunks,
1835 * we wait until bio submission time. All the pages in the bio are
1836 * checksummed and sums are attached onto the ordered extent record.
1837 *
1838 * At IO completion time the cums attached on the ordered extent record
1839 * are inserted into the btree
1840 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001841static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001842 int mirror_num, unsigned long bio_flags,
1843 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001844{
1845 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001846 int ret;
1847
1848 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1849 if (ret)
1850 bio_endio(bio, ret);
1851 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001852}
1853
Chris Masond352ac62008-09-29 15:18:18 -04001854/*
Chris Masoncad321a2008-12-17 14:51:42 -05001855 * extent_io.c submission hook. This does the right thing for csum calculation
1856 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001857 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001858static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001859 int mirror_num, unsigned long bio_flags,
1860 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001861{
1862 struct btrfs_root *root = BTRFS_I(inode)->root;
1863 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001864 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001865 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001866 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001867
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001868 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001869
Liu Bo83eea1f2012-07-10 05:28:39 -06001870 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001871 metadata = 2;
1872
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001873 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001874 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1875 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001876 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001877
Chris Masond20f7042008-12-08 16:58:54 -05001878 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001879 ret = btrfs_submit_compressed_read(inode, bio,
1880 mirror_num,
1881 bio_flags);
1882 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001883 } else if (!skip_sum) {
1884 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1885 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001886 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001887 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001888 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001889 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001890 /* csum items have already been cloned */
1891 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1892 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001893 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001894 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001895 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001896 bio_flags, bio_offset,
1897 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001898 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001899 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001900 } else if (!skip_sum) {
1901 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1902 if (ret)
1903 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001904 }
1905
Chris Mason0b86a832008-03-24 15:01:56 -04001906mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001907 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1908
1909out:
1910 if (ret < 0)
1911 bio_endio(bio, ret);
1912 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001913}
Chris Mason6885f302008-02-20 16:11:05 -05001914
Chris Masond352ac62008-09-29 15:18:18 -04001915/*
1916 * given a list of ordered sums record them in the inode. This happens
1917 * at IO completion time based on sums calculated at bio submission time.
1918 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001919static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001920 struct inode *inode, u64 file_offset,
1921 struct list_head *list)
1922{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001923 struct btrfs_ordered_sum *sum;
1924
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001925 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001926 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001927 btrfs_csum_file_blocks(trans,
1928 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001929 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001930 }
1931 return 0;
1932}
1933
Josef Bacik2ac55d42010-02-03 19:33:23 +00001934int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1935 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001936{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001937 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001938 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001939 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001940}
1941
Chris Masond352ac62008-09-29 15:18:18 -04001942/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001943struct btrfs_writepage_fixup {
1944 struct page *page;
1945 struct btrfs_work work;
1946};
1947
Christoph Hellwigb2950862008-12-02 09:54:17 -05001948static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001949{
1950 struct btrfs_writepage_fixup *fixup;
1951 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001952 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001953 struct page *page;
1954 struct inode *inode;
1955 u64 page_start;
1956 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001957 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001958
1959 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1960 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001961again:
Chris Mason247e7432008-07-17 12:53:51 -04001962 lock_page(page);
1963 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1964 ClearPageChecked(page);
1965 goto out_page;
1966 }
1967
1968 inode = page->mapping->host;
1969 page_start = page_offset(page);
1970 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1971
Josef Bacik2ac55d42010-02-03 19:33:23 +00001972 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001973 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001974
1975 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001976 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001977 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001978
1979 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1980 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001981 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1982 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001983 unlock_page(page);
1984 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001985 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001986 goto again;
1987 }
Chris Mason247e7432008-07-17 12:53:51 -04001988
Jeff Mahoney87826df2012-02-15 16:23:57 +01001989 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1990 if (ret) {
1991 mapping_set_error(page->mapping, ret);
1992 end_extent_writepage(page, ret, page_start, page_end);
1993 ClearPageChecked(page);
1994 goto out;
1995 }
1996
Josef Bacik2ac55d42010-02-03 19:33:23 +00001997 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001998 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001999 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002000out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002001 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2002 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002003out_page:
2004 unlock_page(page);
2005 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002006 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002007}
2008
2009/*
2010 * There are a few paths in the higher layers of the kernel that directly
2011 * set the page dirty bit without asking the filesystem if it is a
2012 * good idea. This causes problems because we want to make sure COW
2013 * properly happens and the data=ordered rules are followed.
2014 *
Chris Masonc8b97812008-10-29 14:49:59 -04002015 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002016 * hasn't been properly setup for IO. We kick off an async process
2017 * to fix it up. The async helper will wait for ordered extents, set
2018 * the delalloc bit and make it safe to write the page.
2019 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002020static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002021{
2022 struct inode *inode = page->mapping->host;
2023 struct btrfs_writepage_fixup *fixup;
2024 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04002025
Chris Mason8b62b722009-09-02 16:53:46 -04002026 /* this page is properly in the ordered list */
2027 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002028 return 0;
2029
2030 if (PageChecked(page))
2031 return -EAGAIN;
2032
2033 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2034 if (!fixup)
2035 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002036
Chris Mason247e7432008-07-17 12:53:51 -04002037 SetPageChecked(page);
2038 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002039 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2040 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002041 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08002042 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002043 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002044}
2045
Yan Zhengd899e052008-10-30 14:25:28 -04002046static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2047 struct inode *inode, u64 file_pos,
2048 u64 disk_bytenr, u64 disk_num_bytes,
2049 u64 num_bytes, u64 ram_bytes,
2050 u8 compression, u8 encryption,
2051 u16 other_encoding, int extent_type)
2052{
2053 struct btrfs_root *root = BTRFS_I(inode)->root;
2054 struct btrfs_file_extent_item *fi;
2055 struct btrfs_path *path;
2056 struct extent_buffer *leaf;
2057 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002058 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002059 int ret;
2060
2061 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002062 if (!path)
2063 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002064
Chris Masona1ed8352009-09-11 12:27:37 -04002065 /*
2066 * we may be replacing one extent in the tree with another.
2067 * The new extent is pinned in the extent map, and we don't want
2068 * to drop it from the cache until it is completely in the btree.
2069 *
2070 * So, tell btrfs_drop_extents to leave this extent in the cache.
2071 * the caller is expected to unpin it and allow it to be merged
2072 * with the others.
2073 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002074 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2075 file_pos + num_bytes, NULL, 0,
2076 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002077 if (ret)
2078 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002079
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002080 if (!extent_inserted) {
2081 ins.objectid = btrfs_ino(inode);
2082 ins.offset = file_pos;
2083 ins.type = BTRFS_EXTENT_DATA_KEY;
2084
2085 path->leave_spinning = 1;
2086 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2087 sizeof(*fi));
2088 if (ret)
2089 goto out;
2090 }
Yan Zhengd899e052008-10-30 14:25:28 -04002091 leaf = path->nodes[0];
2092 fi = btrfs_item_ptr(leaf, path->slots[0],
2093 struct btrfs_file_extent_item);
2094 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2095 btrfs_set_file_extent_type(leaf, fi, extent_type);
2096 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2097 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2098 btrfs_set_file_extent_offset(leaf, fi, 0);
2099 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2100 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2101 btrfs_set_file_extent_compression(leaf, fi, compression);
2102 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2103 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002104
Yan Zhengd899e052008-10-30 14:25:28 -04002105 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002106 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002107
2108 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002109
2110 ins.objectid = disk_bytenr;
2111 ins.offset = disk_num_bytes;
2112 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002113 ret = btrfs_alloc_reserved_file_extent(trans, root,
2114 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002115 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002116out:
Yan Zhengd899e052008-10-30 14:25:28 -04002117 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002118
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002119 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002120}
2121
Liu Bo38c227d2013-01-29 03:18:40 +00002122/* snapshot-aware defrag */
2123struct sa_defrag_extent_backref {
2124 struct rb_node node;
2125 struct old_sa_defrag_extent *old;
2126 u64 root_id;
2127 u64 inum;
2128 u64 file_pos;
2129 u64 extent_offset;
2130 u64 num_bytes;
2131 u64 generation;
2132};
2133
2134struct old_sa_defrag_extent {
2135 struct list_head list;
2136 struct new_sa_defrag_extent *new;
2137
2138 u64 extent_offset;
2139 u64 bytenr;
2140 u64 offset;
2141 u64 len;
2142 int count;
2143};
2144
2145struct new_sa_defrag_extent {
2146 struct rb_root root;
2147 struct list_head head;
2148 struct btrfs_path *path;
2149 struct inode *inode;
2150 u64 file_pos;
2151 u64 len;
2152 u64 bytenr;
2153 u64 disk_len;
2154 u8 compress_type;
2155};
2156
2157static int backref_comp(struct sa_defrag_extent_backref *b1,
2158 struct sa_defrag_extent_backref *b2)
2159{
2160 if (b1->root_id < b2->root_id)
2161 return -1;
2162 else if (b1->root_id > b2->root_id)
2163 return 1;
2164
2165 if (b1->inum < b2->inum)
2166 return -1;
2167 else if (b1->inum > b2->inum)
2168 return 1;
2169
2170 if (b1->file_pos < b2->file_pos)
2171 return -1;
2172 else if (b1->file_pos > b2->file_pos)
2173 return 1;
2174
2175 /*
2176 * [------------------------------] ===> (a range of space)
2177 * |<--->| |<---->| =============> (fs/file tree A)
2178 * |<---------------------------->| ===> (fs/file tree B)
2179 *
2180 * A range of space can refer to two file extents in one tree while
2181 * refer to only one file extent in another tree.
2182 *
2183 * So we may process a disk offset more than one time(two extents in A)
2184 * and locate at the same extent(one extent in B), then insert two same
2185 * backrefs(both refer to the extent in B).
2186 */
2187 return 0;
2188}
2189
2190static void backref_insert(struct rb_root *root,
2191 struct sa_defrag_extent_backref *backref)
2192{
2193 struct rb_node **p = &root->rb_node;
2194 struct rb_node *parent = NULL;
2195 struct sa_defrag_extent_backref *entry;
2196 int ret;
2197
2198 while (*p) {
2199 parent = *p;
2200 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2201
2202 ret = backref_comp(backref, entry);
2203 if (ret < 0)
2204 p = &(*p)->rb_left;
2205 else
2206 p = &(*p)->rb_right;
2207 }
2208
2209 rb_link_node(&backref->node, parent, p);
2210 rb_insert_color(&backref->node, root);
2211}
2212
2213/*
2214 * Note the backref might has changed, and in this case we just return 0.
2215 */
2216static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2217 void *ctx)
2218{
2219 struct btrfs_file_extent_item *extent;
2220 struct btrfs_fs_info *fs_info;
2221 struct old_sa_defrag_extent *old = ctx;
2222 struct new_sa_defrag_extent *new = old->new;
2223 struct btrfs_path *path = new->path;
2224 struct btrfs_key key;
2225 struct btrfs_root *root;
2226 struct sa_defrag_extent_backref *backref;
2227 struct extent_buffer *leaf;
2228 struct inode *inode = new->inode;
2229 int slot;
2230 int ret;
2231 u64 extent_offset;
2232 u64 num_bytes;
2233
2234 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2235 inum == btrfs_ino(inode))
2236 return 0;
2237
2238 key.objectid = root_id;
2239 key.type = BTRFS_ROOT_ITEM_KEY;
2240 key.offset = (u64)-1;
2241
2242 fs_info = BTRFS_I(inode)->root->fs_info;
2243 root = btrfs_read_fs_root_no_name(fs_info, &key);
2244 if (IS_ERR(root)) {
2245 if (PTR_ERR(root) == -ENOENT)
2246 return 0;
2247 WARN_ON(1);
2248 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2249 inum, offset, root_id);
2250 return PTR_ERR(root);
2251 }
2252
2253 key.objectid = inum;
2254 key.type = BTRFS_EXTENT_DATA_KEY;
2255 if (offset > (u64)-1 << 32)
2256 key.offset = 0;
2257 else
2258 key.offset = offset;
2259
2260 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302261 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002262 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002263 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002264
2265 while (1) {
2266 cond_resched();
2267
2268 leaf = path->nodes[0];
2269 slot = path->slots[0];
2270
2271 if (slot >= btrfs_header_nritems(leaf)) {
2272 ret = btrfs_next_leaf(root, path);
2273 if (ret < 0) {
2274 goto out;
2275 } else if (ret > 0) {
2276 ret = 0;
2277 goto out;
2278 }
2279 continue;
2280 }
2281
2282 path->slots[0]++;
2283
2284 btrfs_item_key_to_cpu(leaf, &key, slot);
2285
2286 if (key.objectid > inum)
2287 goto out;
2288
2289 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2290 continue;
2291
2292 extent = btrfs_item_ptr(leaf, slot,
2293 struct btrfs_file_extent_item);
2294
2295 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2296 continue;
2297
Liu Boe68afa42013-07-01 22:13:26 +08002298 /*
2299 * 'offset' refers to the exact key.offset,
2300 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2301 * (key.offset - extent_offset).
2302 */
2303 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002304 continue;
2305
Liu Boe68afa42013-07-01 22:13:26 +08002306 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002307 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002308
Liu Bo38c227d2013-01-29 03:18:40 +00002309 if (extent_offset >= old->extent_offset + old->offset +
2310 old->len || extent_offset + num_bytes <=
2311 old->extent_offset + old->offset)
2312 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002313 break;
2314 }
2315
2316 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2317 if (!backref) {
2318 ret = -ENOENT;
2319 goto out;
2320 }
2321
2322 backref->root_id = root_id;
2323 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002324 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002325 backref->num_bytes = num_bytes;
2326 backref->extent_offset = extent_offset;
2327 backref->generation = btrfs_file_extent_generation(leaf, extent);
2328 backref->old = old;
2329 backref_insert(&new->root, backref);
2330 old->count++;
2331out:
2332 btrfs_release_path(path);
2333 WARN_ON(ret);
2334 return ret;
2335}
2336
2337static noinline bool record_extent_backrefs(struct btrfs_path *path,
2338 struct new_sa_defrag_extent *new)
2339{
2340 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2341 struct old_sa_defrag_extent *old, *tmp;
2342 int ret;
2343
2344 new->path = path;
2345
2346 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002347 ret = iterate_inodes_from_logical(old->bytenr +
2348 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002349 path, record_one_backref,
2350 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002351 if (ret < 0 && ret != -ENOENT)
2352 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002353
2354 /* no backref to be processed for this extent */
2355 if (!old->count) {
2356 list_del(&old->list);
2357 kfree(old);
2358 }
2359 }
2360
2361 if (list_empty(&new->head))
2362 return false;
2363
2364 return true;
2365}
2366
2367static int relink_is_mergable(struct extent_buffer *leaf,
2368 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002369 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002370{
Liu Bo116e0022013-08-02 16:30:40 +08002371 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002372 return 0;
2373
2374 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2375 return 0;
2376
Liu Bo116e0022013-08-02 16:30:40 +08002377 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2378 return 0;
2379
2380 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002381 btrfs_file_extent_other_encoding(leaf, fi))
2382 return 0;
2383
2384 return 1;
2385}
2386
2387/*
2388 * Note the backref might has changed, and in this case we just return 0.
2389 */
2390static noinline int relink_extent_backref(struct btrfs_path *path,
2391 struct sa_defrag_extent_backref *prev,
2392 struct sa_defrag_extent_backref *backref)
2393{
2394 struct btrfs_file_extent_item *extent;
2395 struct btrfs_file_extent_item *item;
2396 struct btrfs_ordered_extent *ordered;
2397 struct btrfs_trans_handle *trans;
2398 struct btrfs_fs_info *fs_info;
2399 struct btrfs_root *root;
2400 struct btrfs_key key;
2401 struct extent_buffer *leaf;
2402 struct old_sa_defrag_extent *old = backref->old;
2403 struct new_sa_defrag_extent *new = old->new;
2404 struct inode *src_inode = new->inode;
2405 struct inode *inode;
2406 struct extent_state *cached = NULL;
2407 int ret = 0;
2408 u64 start;
2409 u64 len;
2410 u64 lock_start;
2411 u64 lock_end;
2412 bool merge = false;
2413 int index;
2414
2415 if (prev && prev->root_id == backref->root_id &&
2416 prev->inum == backref->inum &&
2417 prev->file_pos + prev->num_bytes == backref->file_pos)
2418 merge = true;
2419
2420 /* step 1: get root */
2421 key.objectid = backref->root_id;
2422 key.type = BTRFS_ROOT_ITEM_KEY;
2423 key.offset = (u64)-1;
2424
2425 fs_info = BTRFS_I(src_inode)->root->fs_info;
2426 index = srcu_read_lock(&fs_info->subvol_srcu);
2427
2428 root = btrfs_read_fs_root_no_name(fs_info, &key);
2429 if (IS_ERR(root)) {
2430 srcu_read_unlock(&fs_info->subvol_srcu, index);
2431 if (PTR_ERR(root) == -ENOENT)
2432 return 0;
2433 return PTR_ERR(root);
2434 }
Liu Bo38c227d2013-01-29 03:18:40 +00002435
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002436 if (btrfs_root_readonly(root)) {
2437 srcu_read_unlock(&fs_info->subvol_srcu, index);
2438 return 0;
2439 }
2440
Liu Bo38c227d2013-01-29 03:18:40 +00002441 /* step 2: get inode */
2442 key.objectid = backref->inum;
2443 key.type = BTRFS_INODE_ITEM_KEY;
2444 key.offset = 0;
2445
2446 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2447 if (IS_ERR(inode)) {
2448 srcu_read_unlock(&fs_info->subvol_srcu, index);
2449 return 0;
2450 }
2451
2452 srcu_read_unlock(&fs_info->subvol_srcu, index);
2453
2454 /* step 3: relink backref */
2455 lock_start = backref->file_pos;
2456 lock_end = backref->file_pos + backref->num_bytes - 1;
2457 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2458 0, &cached);
2459
2460 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2461 if (ordered) {
2462 btrfs_put_ordered_extent(ordered);
2463 goto out_unlock;
2464 }
2465
2466 trans = btrfs_join_transaction(root);
2467 if (IS_ERR(trans)) {
2468 ret = PTR_ERR(trans);
2469 goto out_unlock;
2470 }
2471
2472 key.objectid = backref->inum;
2473 key.type = BTRFS_EXTENT_DATA_KEY;
2474 key.offset = backref->file_pos;
2475
2476 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2477 if (ret < 0) {
2478 goto out_free_path;
2479 } else if (ret > 0) {
2480 ret = 0;
2481 goto out_free_path;
2482 }
2483
2484 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2485 struct btrfs_file_extent_item);
2486
2487 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2488 backref->generation)
2489 goto out_free_path;
2490
2491 btrfs_release_path(path);
2492
2493 start = backref->file_pos;
2494 if (backref->extent_offset < old->extent_offset + old->offset)
2495 start += old->extent_offset + old->offset -
2496 backref->extent_offset;
2497
2498 len = min(backref->extent_offset + backref->num_bytes,
2499 old->extent_offset + old->offset + old->len);
2500 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2501
2502 ret = btrfs_drop_extents(trans, root, inode, start,
2503 start + len, 1);
2504 if (ret)
2505 goto out_free_path;
2506again:
2507 key.objectid = btrfs_ino(inode);
2508 key.type = BTRFS_EXTENT_DATA_KEY;
2509 key.offset = start;
2510
Liu Boa09a0a72013-03-11 09:20:58 +00002511 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002512 if (merge) {
2513 struct btrfs_file_extent_item *fi;
2514 u64 extent_len;
2515 struct btrfs_key found_key;
2516
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002517 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002518 if (ret < 0)
2519 goto out_free_path;
2520
2521 path->slots[0]--;
2522 leaf = path->nodes[0];
2523 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2524
2525 fi = btrfs_item_ptr(leaf, path->slots[0],
2526 struct btrfs_file_extent_item);
2527 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2528
Liu Bo116e0022013-08-02 16:30:40 +08002529 if (extent_len + found_key.offset == start &&
2530 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002531 btrfs_set_file_extent_num_bytes(leaf, fi,
2532 extent_len + len);
2533 btrfs_mark_buffer_dirty(leaf);
2534 inode_add_bytes(inode, len);
2535
2536 ret = 1;
2537 goto out_free_path;
2538 } else {
2539 merge = false;
2540 btrfs_release_path(path);
2541 goto again;
2542 }
2543 }
2544
2545 ret = btrfs_insert_empty_item(trans, root, path, &key,
2546 sizeof(*extent));
2547 if (ret) {
2548 btrfs_abort_transaction(trans, root, ret);
2549 goto out_free_path;
2550 }
2551
2552 leaf = path->nodes[0];
2553 item = btrfs_item_ptr(leaf, path->slots[0],
2554 struct btrfs_file_extent_item);
2555 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2556 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2557 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2558 btrfs_set_file_extent_num_bytes(leaf, item, len);
2559 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2560 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2561 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2562 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2563 btrfs_set_file_extent_encryption(leaf, item, 0);
2564 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2565
2566 btrfs_mark_buffer_dirty(leaf);
2567 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002568 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002569
2570 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2571 new->disk_len, 0,
2572 backref->root_id, backref->inum,
2573 new->file_pos, 0); /* start - extent_offset */
2574 if (ret) {
2575 btrfs_abort_transaction(trans, root, ret);
2576 goto out_free_path;
2577 }
2578
2579 ret = 1;
2580out_free_path:
2581 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002582 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002583 btrfs_end_transaction(trans, root);
2584out_unlock:
2585 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2586 &cached, GFP_NOFS);
2587 iput(inode);
2588 return ret;
2589}
2590
Liu Bo6f519562013-10-29 10:45:05 +08002591static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2592{
2593 struct old_sa_defrag_extent *old, *tmp;
2594
2595 if (!new)
2596 return;
2597
2598 list_for_each_entry_safe(old, tmp, &new->head, list) {
2599 list_del(&old->list);
2600 kfree(old);
2601 }
2602 kfree(new);
2603}
2604
Liu Bo38c227d2013-01-29 03:18:40 +00002605static void relink_file_extents(struct new_sa_defrag_extent *new)
2606{
2607 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002608 struct sa_defrag_extent_backref *backref;
2609 struct sa_defrag_extent_backref *prev = NULL;
2610 struct inode *inode;
2611 struct btrfs_root *root;
2612 struct rb_node *node;
2613 int ret;
2614
2615 inode = new->inode;
2616 root = BTRFS_I(inode)->root;
2617
2618 path = btrfs_alloc_path();
2619 if (!path)
2620 return;
2621
2622 if (!record_extent_backrefs(path, new)) {
2623 btrfs_free_path(path);
2624 goto out;
2625 }
2626 btrfs_release_path(path);
2627
2628 while (1) {
2629 node = rb_first(&new->root);
2630 if (!node)
2631 break;
2632 rb_erase(node, &new->root);
2633
2634 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2635
2636 ret = relink_extent_backref(path, prev, backref);
2637 WARN_ON(ret < 0);
2638
2639 kfree(prev);
2640
2641 if (ret == 1)
2642 prev = backref;
2643 else
2644 prev = NULL;
2645 cond_resched();
2646 }
2647 kfree(prev);
2648
2649 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002650out:
Liu Bo6f519562013-10-29 10:45:05 +08002651 free_sa_defrag_extent(new);
2652
Liu Bo38c227d2013-01-29 03:18:40 +00002653 atomic_dec(&root->fs_info->defrag_running);
2654 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002655}
2656
2657static struct new_sa_defrag_extent *
2658record_old_file_extents(struct inode *inode,
2659 struct btrfs_ordered_extent *ordered)
2660{
2661 struct btrfs_root *root = BTRFS_I(inode)->root;
2662 struct btrfs_path *path;
2663 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002664 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002665 struct new_sa_defrag_extent *new;
2666 int ret;
2667
2668 new = kmalloc(sizeof(*new), GFP_NOFS);
2669 if (!new)
2670 return NULL;
2671
2672 new->inode = inode;
2673 new->file_pos = ordered->file_offset;
2674 new->len = ordered->len;
2675 new->bytenr = ordered->start;
2676 new->disk_len = ordered->disk_len;
2677 new->compress_type = ordered->compress_type;
2678 new->root = RB_ROOT;
2679 INIT_LIST_HEAD(&new->head);
2680
2681 path = btrfs_alloc_path();
2682 if (!path)
2683 goto out_kfree;
2684
2685 key.objectid = btrfs_ino(inode);
2686 key.type = BTRFS_EXTENT_DATA_KEY;
2687 key.offset = new->file_pos;
2688
2689 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2690 if (ret < 0)
2691 goto out_free_path;
2692 if (ret > 0 && path->slots[0] > 0)
2693 path->slots[0]--;
2694
2695 /* find out all the old extents for the file range */
2696 while (1) {
2697 struct btrfs_file_extent_item *extent;
2698 struct extent_buffer *l;
2699 int slot;
2700 u64 num_bytes;
2701 u64 offset;
2702 u64 end;
2703 u64 disk_bytenr;
2704 u64 extent_offset;
2705
2706 l = path->nodes[0];
2707 slot = path->slots[0];
2708
2709 if (slot >= btrfs_header_nritems(l)) {
2710 ret = btrfs_next_leaf(root, path);
2711 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002712 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002713 else if (ret > 0)
2714 break;
2715 continue;
2716 }
2717
2718 btrfs_item_key_to_cpu(l, &key, slot);
2719
2720 if (key.objectid != btrfs_ino(inode))
2721 break;
2722 if (key.type != BTRFS_EXTENT_DATA_KEY)
2723 break;
2724 if (key.offset >= new->file_pos + new->len)
2725 break;
2726
2727 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2728
2729 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2730 if (key.offset + num_bytes < new->file_pos)
2731 goto next;
2732
2733 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2734 if (!disk_bytenr)
2735 goto next;
2736
2737 extent_offset = btrfs_file_extent_offset(l, extent);
2738
2739 old = kmalloc(sizeof(*old), GFP_NOFS);
2740 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002741 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002742
2743 offset = max(new->file_pos, key.offset);
2744 end = min(new->file_pos + new->len, key.offset + num_bytes);
2745
2746 old->bytenr = disk_bytenr;
2747 old->extent_offset = extent_offset;
2748 old->offset = offset - key.offset;
2749 old->len = end - offset;
2750 old->new = new;
2751 old->count = 0;
2752 list_add_tail(&old->list, &new->head);
2753next:
2754 path->slots[0]++;
2755 cond_resched();
2756 }
2757
2758 btrfs_free_path(path);
2759 atomic_inc(&root->fs_info->defrag_running);
2760
2761 return new;
2762
Liu Bo38c227d2013-01-29 03:18:40 +00002763out_free_path:
2764 btrfs_free_path(path);
2765out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002766 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002767 return NULL;
2768}
2769
Miao Xiee570fd22014-06-19 10:42:50 +08002770static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2771 u64 start, u64 len)
2772{
2773 struct btrfs_block_group_cache *cache;
2774
2775 cache = btrfs_lookup_block_group(root->fs_info, start);
2776 ASSERT(cache);
2777
2778 spin_lock(&cache->lock);
2779 cache->delalloc_bytes -= len;
2780 spin_unlock(&cache->lock);
2781
2782 btrfs_put_block_group(cache);
2783}
2784
Chris Masond352ac62008-09-29 15:18:18 -04002785/* as ordered data IO finishes, this gets called so we can finish
2786 * an ordered extent if the range of bytes in the file it covers are
2787 * fully written.
2788 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002789static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002790{
Josef Bacik5fd02042012-05-02 14:00:54 -04002791 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002792 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002793 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002794 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002795 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002796 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002797 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002798 int ret = 0;
2799 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002800 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002801 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002802
Liu Bo83eea1f2012-07-10 05:28:39 -06002803 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002804
Josef Bacik5fd02042012-05-02 14:00:54 -04002805 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2806 ret = -EIO;
2807 goto out;
2808 }
2809
Miao Xief6124962014-09-12 18:44:04 +08002810 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2811 ordered_extent->file_offset +
2812 ordered_extent->len - 1);
2813
Josef Bacik77cef2e2013-08-29 13:57:21 -04002814 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2815 truncated = true;
2816 logical_len = ordered_extent->truncated_len;
2817 /* Truncated the entire extent, don't bother adding */
2818 if (!logical_len)
2819 goto out;
2820 }
2821
Yan, Zhengc2167752009-11-12 09:34:21 +00002822 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002823 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002824 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2825 if (nolock)
2826 trans = btrfs_join_transaction_nolock(root);
2827 else
2828 trans = btrfs_join_transaction(root);
2829 if (IS_ERR(trans)) {
2830 ret = PTR_ERR(trans);
2831 trans = NULL;
2832 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002833 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002834 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2835 ret = btrfs_update_inode_fallback(trans, root, inode);
2836 if (ret) /* -ENOMEM or corruption */
2837 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002838 goto out;
2839 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002840
Josef Bacik2ac55d42010-02-03 19:33:23 +00002841 lock_extent_bits(io_tree, ordered_extent->file_offset,
2842 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002843 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002844
Liu Bo38c227d2013-01-29 03:18:40 +00002845 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2846 ordered_extent->file_offset + ordered_extent->len - 1,
2847 EXTENT_DEFRAG, 1, cached_state);
2848 if (ret) {
2849 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002850 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002851 /* the inode is shared */
2852 new = record_old_file_extents(inode, ordered_extent);
2853
2854 clear_extent_bit(io_tree, ordered_extent->file_offset,
2855 ordered_extent->file_offset + ordered_extent->len - 1,
2856 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2857 }
2858
Josef Bacik0cb59c92010-07-02 12:14:14 -04002859 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002860 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002861 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002862 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002863 if (IS_ERR(trans)) {
2864 ret = PTR_ERR(trans);
2865 trans = NULL;
2866 goto out_unlock;
2867 }
Chris Masona79b7d42014-05-22 16:18:52 -07002868
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002869 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002870
Chris Masonc8b97812008-10-29 14:49:59 -04002871 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002872 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002873 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002874 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002875 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002876 ordered_extent->file_offset,
2877 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002878 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002879 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002880 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002881 ret = insert_reserved_file_extent(trans, inode,
2882 ordered_extent->file_offset,
2883 ordered_extent->start,
2884 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002885 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002886 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002887 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002888 if (!ret)
2889 btrfs_release_delalloc_bytes(root,
2890 ordered_extent->start,
2891 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002892 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002893 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2894 ordered_extent->file_offset, ordered_extent->len,
2895 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002896 if (ret < 0) {
2897 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002898 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002899 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002900
Chris Masone6dcd2d2008-07-17 12:53:50 -04002901 add_pending_csums(trans, inode, ordered_extent->file_offset,
2902 &ordered_extent->list);
2903
Josef Bacik6c760c02012-11-09 10:53:21 -05002904 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2905 ret = btrfs_update_inode_fallback(trans, root, inode);
2906 if (ret) { /* -ENOMEM or corruption */
2907 btrfs_abort_transaction(trans, root, ret);
2908 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002909 }
2910 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002911out_unlock:
2912 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2913 ordered_extent->file_offset +
2914 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002915out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002916 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002917 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002918 if (trans)
2919 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002920
Josef Bacik77cef2e2013-08-29 13:57:21 -04002921 if (ret || truncated) {
2922 u64 start, end;
2923
2924 if (truncated)
2925 start = ordered_extent->file_offset + logical_len;
2926 else
2927 start = ordered_extent->file_offset;
2928 end = ordered_extent->file_offset + ordered_extent->len - 1;
2929 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2930
2931 /* Drop the cache for the part of the extent we didn't write. */
2932 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002933
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002934 /*
2935 * If the ordered extent had an IOERR or something else went
2936 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002937 * back to the allocator. We only free the extent in the
2938 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002939 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002940 if ((ret || !logical_len) &&
2941 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002942 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2943 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002944 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002945 }
2946
2947
Josef Bacik5fd02042012-05-02 14:00:54 -04002948 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002949 * This needs to be done to make sure anybody waiting knows we are done
2950 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002951 */
2952 btrfs_remove_ordered_extent(inode, ordered_extent);
2953
Liu Bo38c227d2013-01-29 03:18:40 +00002954 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002955 if (new) {
2956 if (ret) {
2957 free_sa_defrag_extent(new);
2958 atomic_dec(&root->fs_info->defrag_running);
2959 } else {
2960 relink_file_extents(new);
2961 }
2962 }
Liu Bo38c227d2013-01-29 03:18:40 +00002963
Chris Masone6dcd2d2008-07-17 12:53:50 -04002964 /* once for us */
2965 btrfs_put_ordered_extent(ordered_extent);
2966 /* once for the tree */
2967 btrfs_put_ordered_extent(ordered_extent);
2968
Josef Bacik5fd02042012-05-02 14:00:54 -04002969 return ret;
2970}
2971
2972static void finish_ordered_fn(struct btrfs_work *work)
2973{
2974 struct btrfs_ordered_extent *ordered_extent;
2975 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2976 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002977}
2978
Christoph Hellwigb2950862008-12-02 09:54:17 -05002979static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002980 struct extent_state *state, int uptodate)
2981{
Josef Bacik5fd02042012-05-02 14:00:54 -04002982 struct inode *inode = page->mapping->host;
2983 struct btrfs_root *root = BTRFS_I(inode)->root;
2984 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002985 struct btrfs_workqueue *wq;
2986 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002987
liubo1abe9b82011-03-24 11:18:59 +00002988 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2989
Chris Mason8b62b722009-09-02 16:53:46 -04002990 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002991 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2992 end - start + 1, uptodate))
2993 return 0;
2994
Liu Bo9e0af232014-08-15 23:36:53 +08002995 if (btrfs_is_free_space_inode(inode)) {
2996 wq = root->fs_info->endio_freespace_worker;
2997 func = btrfs_freespace_write_helper;
2998 } else {
2999 wq = root->fs_info->endio_write_workers;
3000 func = btrfs_endio_write_helper;
3001 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003002
Liu Bo9e0af232014-08-15 23:36:53 +08003003 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3004 NULL);
3005 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003006
3007 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003008}
3009
Miao Xiedc380ae2014-09-12 18:43:55 +08003010static int __readpage_endio_check(struct inode *inode,
3011 struct btrfs_io_bio *io_bio,
3012 int icsum, struct page *page,
3013 int pgoff, u64 start, size_t len)
3014{
3015 char *kaddr;
3016 u32 csum_expected;
3017 u32 csum = ~(u32)0;
3018 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
3019 DEFAULT_RATELIMIT_BURST);
3020
3021 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3022
3023 kaddr = kmap_atomic(page);
3024 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
3025 btrfs_csum_final(csum, (char *)&csum);
3026 if (csum != csum_expected)
3027 goto zeroit;
3028
3029 kunmap_atomic(kaddr);
3030 return 0;
3031zeroit:
3032 if (__ratelimit(&_rs))
David Sterbaf0954c62014-12-19 18:38:44 +01003033 btrfs_warn(BTRFS_I(inode)->root->fs_info,
Miao Xiedc380ae2014-09-12 18:43:55 +08003034 "csum failed ino %llu off %llu csum %u expected csum %u",
3035 btrfs_ino(inode), start, csum, csum_expected);
3036 memset(kaddr + pgoff, 1, len);
3037 flush_dcache_page(page);
3038 kunmap_atomic(kaddr);
3039 if (csum_expected == 0)
3040 return 0;
3041 return -EIO;
3042}
3043
Chris Masond352ac62008-09-29 15:18:18 -04003044/*
Chris Masond352ac62008-09-29 15:18:18 -04003045 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003046 * if there's a match, we allow the bio to finish. If not, the code in
3047 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003048 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003049static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3050 u64 phy_offset, struct page *page,
3051 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003052{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003053 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003054 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003055 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003056 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003057
Chris Masond20f7042008-12-08 16:58:54 -05003058 if (PageChecked(page)) {
3059 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003060 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003061 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003062
3063 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003064 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003065
Yan Zheng17d217f2008-12-12 10:03:38 -05003066 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003067 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05003068 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
3069 GFP_NOFS);
3070 return 0;
3071 }
3072
Miao Xiefacc8a222013-07-25 19:22:34 +08003073 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003074 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3075 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003076}
Chris Masonb888db22007-08-27 16:49:44 -04003077
Yan, Zheng24bbcf042009-11-12 09:36:34 +00003078struct delayed_iput {
3079 struct list_head list;
3080 struct inode *inode;
3081};
3082
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003083/* JDM: If this is fs-wide, why can't we add a pointer to
3084 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf042009-11-12 09:36:34 +00003085void btrfs_add_delayed_iput(struct inode *inode)
3086{
3087 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
3088 struct delayed_iput *delayed;
3089
3090 if (atomic_add_unless(&inode->i_count, -1, 1))
3091 return;
3092
3093 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
3094 delayed->inode = inode;
3095
3096 spin_lock(&fs_info->delayed_iput_lock);
3097 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
3098 spin_unlock(&fs_info->delayed_iput_lock);
3099}
3100
3101void btrfs_run_delayed_iputs(struct btrfs_root *root)
3102{
3103 LIST_HEAD(list);
3104 struct btrfs_fs_info *fs_info = root->fs_info;
3105 struct delayed_iput *delayed;
3106 int empty;
3107
3108 spin_lock(&fs_info->delayed_iput_lock);
3109 empty = list_empty(&fs_info->delayed_iputs);
3110 spin_unlock(&fs_info->delayed_iput_lock);
3111 if (empty)
3112 return;
3113
Zhao Leid7c15172015-02-26 10:49:20 +08003114 down_read(&fs_info->delayed_iput_sem);
3115
Yan, Zheng24bbcf042009-11-12 09:36:34 +00003116 spin_lock(&fs_info->delayed_iput_lock);
3117 list_splice_init(&fs_info->delayed_iputs, &list);
3118 spin_unlock(&fs_info->delayed_iput_lock);
3119
3120 while (!list_empty(&list)) {
3121 delayed = list_entry(list.next, struct delayed_iput, list);
3122 list_del(&delayed->list);
3123 iput(delayed->inode);
3124 kfree(delayed);
3125 }
Zhao Leid7c15172015-02-26 10:49:20 +08003126
3127 up_read(&root->fs_info->delayed_iput_sem);
Yan, Zheng24bbcf042009-11-12 09:36:34 +00003128}
3129
Yan, Zhengd68fc572010-05-16 10:49:58 -04003130/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003131 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003132 * files in the subvolume, it removes orphan item and frees block_rsv
3133 * structure.
3134 */
3135void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3136 struct btrfs_root *root)
3137{
Josef Bacik90290e12011-12-02 15:44:12 -05003138 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003139 int ret;
3140
Josef Bacik8a35d952012-05-23 14:26:42 -04003141 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003142 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3143 return;
3144
Josef Bacik90290e12011-12-02 15:44:12 -05003145 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003146 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003147 spin_unlock(&root->orphan_lock);
3148 return;
3149 }
3150
3151 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3152 spin_unlock(&root->orphan_lock);
3153 return;
3154 }
3155
3156 block_rsv = root->orphan_block_rsv;
3157 root->orphan_block_rsv = NULL;
3158 spin_unlock(&root->orphan_lock);
3159
Miao Xie27cdeb72014-04-02 19:51:05 +08003160 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003161 btrfs_root_refs(&root->root_item) > 0) {
3162 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3163 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003164 if (ret)
3165 btrfs_abort_transaction(trans, root, ret);
3166 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003167 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3168 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003169 }
3170
Josef Bacik90290e12011-12-02 15:44:12 -05003171 if (block_rsv) {
3172 WARN_ON(block_rsv->size > 0);
3173 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003174 }
3175}
3176
3177/*
Josef Bacik7b128762008-07-24 12:17:14 -04003178 * This creates an orphan entry for the given inode in case something goes
3179 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003180 *
3181 * NOTE: caller of this function should reserve 5 units of metadata for
3182 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003183 */
3184int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3185{
3186 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003187 struct btrfs_block_rsv *block_rsv = NULL;
3188 int reserve = 0;
3189 int insert = 0;
3190 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003191
Yan, Zhengd68fc572010-05-16 10:49:58 -04003192 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003193 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003194 if (!block_rsv)
3195 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003196 }
3197
Yan, Zhengd68fc572010-05-16 10:49:58 -04003198 spin_lock(&root->orphan_lock);
3199 if (!root->orphan_block_rsv) {
3200 root->orphan_block_rsv = block_rsv;
3201 } else if (block_rsv) {
3202 btrfs_free_block_rsv(root, block_rsv);
3203 block_rsv = NULL;
3204 }
Josef Bacik7b128762008-07-24 12:17:14 -04003205
Josef Bacik8a35d952012-05-23 14:26:42 -04003206 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3207 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003208#if 0
3209 /*
3210 * For proper ENOSPC handling, we should do orphan
3211 * cleanup when mounting. But this introduces backward
3212 * compatibility issue.
3213 */
3214 if (!xchg(&root->orphan_item_inserted, 1))
3215 insert = 2;
3216 else
3217 insert = 1;
3218#endif
3219 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003220 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003221 }
Josef Bacik7b128762008-07-24 12:17:14 -04003222
Josef Bacik72ac3c02012-05-23 14:13:11 -04003223 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3224 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003225 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003226 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003227
Yan, Zhengd68fc572010-05-16 10:49:58 -04003228 /* grab metadata reservation from transaction handle */
3229 if (reserve) {
3230 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003231 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003232 }
3233
3234 /* insert an orphan item to track this unlinked/truncated file */
3235 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003236 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003237 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003238 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003239 if (reserve) {
3240 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3241 &BTRFS_I(inode)->runtime_flags);
3242 btrfs_orphan_release_metadata(inode);
3243 }
3244 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003245 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3246 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003247 btrfs_abort_transaction(trans, root, ret);
3248 return ret;
3249 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003250 }
3251 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003252 }
3253
3254 /* insert an orphan item to track subvolume contains orphan files */
3255 if (insert >= 2) {
3256 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3257 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003258 if (ret && ret != -EEXIST) {
3259 btrfs_abort_transaction(trans, root, ret);
3260 return ret;
3261 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003262 }
3263 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003264}
3265
3266/*
3267 * We have done the truncate/delete so we can go ahead and remove the orphan
3268 * item for this particular inode.
3269 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003270static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3271 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003272{
3273 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003274 int delete_item = 0;
3275 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003276 int ret = 0;
3277
Yan, Zhengd68fc572010-05-16 10:49:58 -04003278 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003279 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3280 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003281 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003282
Josef Bacik72ac3c02012-05-23 14:13:11 -04003283 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3284 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003285 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003286 spin_unlock(&root->orphan_lock);
3287
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003288 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003289 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003290 if (trans)
3291 ret = btrfs_del_orphan_item(trans, root,
3292 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003293 }
Josef Bacik7b128762008-07-24 12:17:14 -04003294
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003295 if (release_rsv)
3296 btrfs_orphan_release_metadata(inode);
3297
Josef Bacik4ef31a42013-08-13 14:10:08 -04003298 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003299}
3300
3301/*
3302 * this cleans up any orphans that may be left on the list from the last use
3303 * of this root.
3304 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003305int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003306{
3307 struct btrfs_path *path;
3308 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003309 struct btrfs_key key, found_key;
3310 struct btrfs_trans_handle *trans;
3311 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003312 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003313 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3314
Yan, Zhengd68fc572010-05-16 10:49:58 -04003315 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003316 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003317
3318 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003319 if (!path) {
3320 ret = -ENOMEM;
3321 goto out;
3322 }
Josef Bacik7b128762008-07-24 12:17:14 -04003323 path->reada = -1;
3324
3325 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003326 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003327 key.offset = (u64)-1;
3328
Josef Bacik7b128762008-07-24 12:17:14 -04003329 while (1) {
3330 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003331 if (ret < 0)
3332 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003333
3334 /*
3335 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003336 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003337 * find the key and see if we have stuff that matches
3338 */
3339 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003340 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003341 if (path->slots[0] == 0)
3342 break;
3343 path->slots[0]--;
3344 }
3345
3346 /* pull out the item */
3347 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003348 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3349
3350 /* make sure the item matches what we want */
3351 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3352 break;
David Sterba962a2982014-06-04 18:41:45 +02003353 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003354 break;
3355
3356 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003357 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003358
3359 /*
3360 * this is where we are basically btrfs_lookup, without the
3361 * crossing root thing. we store the inode number in the
3362 * offset of the orphan item.
3363 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003364
3365 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003366 btrfs_err(root->fs_info,
3367 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003368 ret = -EINVAL;
3369 goto out;
3370 }
3371
3372 last_objectid = found_key.offset;
3373
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003374 found_key.objectid = found_key.offset;
3375 found_key.type = BTRFS_INODE_ITEM_KEY;
3376 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003377 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303378 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003379 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003380 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003381
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003382 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3383 struct btrfs_root *dead_root;
3384 struct btrfs_fs_info *fs_info = root->fs_info;
3385 int is_dead_root = 0;
3386
3387 /*
3388 * this is an orphan in the tree root. Currently these
3389 * could come from 2 sources:
3390 * a) a snapshot deletion in progress
3391 * b) a free space cache inode
3392 * We need to distinguish those two, as the snapshot
3393 * orphan must not get deleted.
3394 * find_dead_roots already ran before us, so if this
3395 * is a snapshot deletion, we should find the root
3396 * in the dead_roots list
3397 */
3398 spin_lock(&fs_info->trans_lock);
3399 list_for_each_entry(dead_root, &fs_info->dead_roots,
3400 root_list) {
3401 if (dead_root->root_key.objectid ==
3402 found_key.objectid) {
3403 is_dead_root = 1;
3404 break;
3405 }
3406 }
3407 spin_unlock(&fs_info->trans_lock);
3408 if (is_dead_root) {
3409 /* prevent this orphan from being found again */
3410 key.offset = found_key.objectid - 1;
3411 continue;
3412 }
3413 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003414 /*
3415 * Inode is already gone but the orphan item is still there,
3416 * kill the orphan item.
3417 */
3418 if (ret == -ESTALE) {
3419 trans = btrfs_start_transaction(root, 1);
3420 if (IS_ERR(trans)) {
3421 ret = PTR_ERR(trans);
3422 goto out;
3423 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003424 btrfs_debug(root->fs_info, "auto deleting %Lu",
3425 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003426 ret = btrfs_del_orphan_item(trans, root,
3427 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003428 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003429 if (ret)
3430 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003431 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003432 }
Josef Bacik7b128762008-07-24 12:17:14 -04003433
Josef Bacik7b128762008-07-24 12:17:14 -04003434 /*
3435 * add this inode to the orphan list so btrfs_orphan_del does
3436 * the proper thing when we hit it
3437 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003438 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3439 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003440 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003441
Josef Bacik7b128762008-07-24 12:17:14 -04003442 /* if we have links, this was a truncate, lets do that */
3443 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303444 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003445 iput(inode);
3446 continue;
3447 }
Josef Bacik7b128762008-07-24 12:17:14 -04003448 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003449
3450 /* 1 for the orphan item deletion. */
3451 trans = btrfs_start_transaction(root, 1);
3452 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003453 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003454 ret = PTR_ERR(trans);
3455 goto out;
3456 }
3457 ret = btrfs_orphan_add(trans, inode);
3458 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003459 if (ret) {
3460 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003461 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003462 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003463
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003464 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003465 if (ret)
3466 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003467 } else {
3468 nr_unlink++;
3469 }
3470
3471 /* this will do delete_inode and everything for us */
3472 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003473 if (ret)
3474 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003475 }
Miao Xie3254c872011-11-10 20:45:05 -05003476 /* release the path since we're done with it */
3477 btrfs_release_path(path);
3478
Yan, Zhengd68fc572010-05-16 10:49:58 -04003479 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3480
3481 if (root->orphan_block_rsv)
3482 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3483 (u64)-1);
3484
Miao Xie27cdeb72014-04-02 19:51:05 +08003485 if (root->orphan_block_rsv ||
3486 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003487 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003488 if (!IS_ERR(trans))
3489 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003490 }
Josef Bacik7b128762008-07-24 12:17:14 -04003491
3492 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003493 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003494 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003495 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003496
3497out:
3498 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003499 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003500 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003501 btrfs_free_path(path);
3502 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003503}
3504
Chris Masond352ac62008-09-29 15:18:18 -04003505/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003506 * very simple check to peek ahead in the leaf looking for xattrs. If we
3507 * don't find any xattrs, we know there can't be any acls.
3508 *
3509 * slot is the slot the inode is in, objectid is the objectid of the inode
3510 */
3511static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003512 int slot, u64 objectid,
3513 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003514{
3515 u32 nritems = btrfs_header_nritems(leaf);
3516 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003517 static u64 xattr_access = 0;
3518 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003519 int scanned = 0;
3520
Josef Bacikf23b5a52013-06-19 10:16:26 -04003521 if (!xattr_access) {
3522 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3523 strlen(POSIX_ACL_XATTR_ACCESS));
3524 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3525 strlen(POSIX_ACL_XATTR_DEFAULT));
3526 }
3527
Chris Mason46a53cc2009-04-27 11:47:50 -04003528 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003529 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003530 while (slot < nritems) {
3531 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3532
3533 /* we found a different objectid, there must not be acls */
3534 if (found_key.objectid != objectid)
3535 return 0;
3536
3537 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003538 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003539 if (*first_xattr_slot == -1)
3540 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003541 if (found_key.offset == xattr_access ||
3542 found_key.offset == xattr_default)
3543 return 1;
3544 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003545
3546 /*
3547 * we found a key greater than an xattr key, there can't
3548 * be any acls later on
3549 */
3550 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3551 return 0;
3552
3553 slot++;
3554 scanned++;
3555
3556 /*
3557 * it goes inode, inode backrefs, xattrs, extents,
3558 * so if there are a ton of hard links to an inode there can
3559 * be a lot of backrefs. Don't waste time searching too hard,
3560 * this is just an optimization
3561 */
3562 if (scanned >= 8)
3563 break;
3564 }
3565 /* we hit the end of the leaf before we found an xattr or
3566 * something larger than an xattr. We have to assume the inode
3567 * has acls
3568 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003569 if (*first_xattr_slot == -1)
3570 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003571 return 1;
3572}
3573
3574/*
Chris Masond352ac62008-09-29 15:18:18 -04003575 * read an inode from the btree into the in-memory inode
3576 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003577static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003578{
3579 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003580 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003581 struct btrfs_inode_item *inode_item;
3582 struct btrfs_root *root = BTRFS_I(inode)->root;
3583 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003584 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003585 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003586 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003587 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003588 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003589 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003590
3591 ret = btrfs_fill_inode(inode, &rdev);
3592 if (!ret)
3593 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003594
3595 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003596 if (!path)
3597 goto make_bad;
3598
Chris Mason39279cc2007-06-12 06:35:45 -04003599 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003600
Chris Mason39279cc2007-06-12 06:35:45 -04003601 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003602 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003603 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003604
Chris Mason5f39d392007-10-15 16:14:19 -04003605 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003606
3607 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003608 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003609
Chris Mason5f39d392007-10-15 16:14:19 -04003610 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3611 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003612 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003613 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003614 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3615 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003616 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003617
David Sterbaa937b972014-12-12 17:39:12 +01003618 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3619 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003620
David Sterbaa937b972014-12-12 17:39:12 +01003621 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3622 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003623
David Sterbaa937b972014-12-12 17:39:12 +01003624 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3625 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003626
chandan r9cc97d62012-07-04 12:48:07 +05303627 BTRFS_I(inode)->i_otime.tv_sec =
3628 btrfs_timespec_sec(leaf, &inode_item->otime);
3629 BTRFS_I(inode)->i_otime.tv_nsec =
3630 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003631
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003632 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003633 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003634 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3635
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003636 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003637 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003638 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003639 rdev = btrfs_inode_rdev(leaf, inode_item);
3640
Josef Bacikaec74772008-07-24 12:12:38 -04003641 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003642 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003643
3644cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003645 /*
3646 * If we were modified in the current generation and evicted from memory
3647 * and then re-read we need to do a full sync since we don't have any
3648 * idea about which extents were modified before we were evicted from
3649 * cache.
3650 *
3651 * This is required for both inode re-read from disk and delayed inode
3652 * in delayed_nodes_tree.
3653 */
3654 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3655 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3656 &BTRFS_I(inode)->runtime_flags);
3657
Miao Xie67de1172013-12-26 13:07:06 +08003658 path->slots[0]++;
3659 if (inode->i_nlink != 1 ||
3660 path->slots[0] >= btrfs_header_nritems(leaf))
3661 goto cache_acl;
3662
3663 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3664 if (location.objectid != btrfs_ino(inode))
3665 goto cache_acl;
3666
3667 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3668 if (location.type == BTRFS_INODE_REF_KEY) {
3669 struct btrfs_inode_ref *ref;
3670
3671 ref = (struct btrfs_inode_ref *)ptr;
3672 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3673 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3674 struct btrfs_inode_extref *extref;
3675
3676 extref = (struct btrfs_inode_extref *)ptr;
3677 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3678 extref);
3679 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003680cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003681 /*
3682 * try to precache a NULL acl entry for files that don't have
3683 * any xattrs or acls
3684 */
Li Zefan33345d012011-04-20 10:31:50 +08003685 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003686 btrfs_ino(inode), &first_xattr_slot);
3687 if (first_xattr_slot != -1) {
3688 path->slots[0] = first_xattr_slot;
3689 ret = btrfs_load_inode_props(inode, path);
3690 if (ret)
3691 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003692 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003693 btrfs_ino(inode),
3694 root->root_key.objectid, ret);
3695 }
3696 btrfs_free_path(path);
3697
Al Viro72c04902009-06-24 16:58:48 -04003698 if (!maybe_acls)
3699 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003700
Chris Mason39279cc2007-06-12 06:35:45 -04003701 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003702 case S_IFREG:
3703 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003704 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003705 inode->i_fop = &btrfs_file_operations;
3706 inode->i_op = &btrfs_file_inode_operations;
3707 break;
3708 case S_IFDIR:
3709 inode->i_fop = &btrfs_dir_file_operations;
3710 if (root == root->fs_info->tree_root)
3711 inode->i_op = &btrfs_dir_ro_inode_operations;
3712 else
3713 inode->i_op = &btrfs_dir_inode_operations;
3714 break;
3715 case S_IFLNK:
3716 inode->i_op = &btrfs_symlink_inode_operations;
3717 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3718 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003719 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003720 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003721 init_special_inode(inode, inode->i_mode, rdev);
3722 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003723 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003724
3725 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003726 return;
3727
3728make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003729 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003730 make_bad_inode(inode);
3731}
3732
Chris Masond352ac62008-09-29 15:18:18 -04003733/*
3734 * given a leaf and an inode, copy the inode fields into the leaf
3735 */
Chris Masone02119d2008-09-05 16:13:11 -04003736static void fill_inode_item(struct btrfs_trans_handle *trans,
3737 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003738 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003739 struct inode *inode)
3740{
Liu Bo51fab692012-12-27 09:01:21 +00003741 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003742
Liu Bo51fab692012-12-27 09:01:21 +00003743 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003744
Liu Bo51fab692012-12-27 09:01:21 +00003745 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3746 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3747 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3748 &token);
3749 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3750 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003751
David Sterbaa937b972014-12-12 17:39:12 +01003752 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003753 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003754 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003755 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003756
David Sterbaa937b972014-12-12 17:39:12 +01003757 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003758 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003759 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003760 inode->i_mtime.tv_nsec, &token);
3761
David Sterbaa937b972014-12-12 17:39:12 +01003762 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003763 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003764 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003765 inode->i_ctime.tv_nsec, &token);
3766
chandan r9cc97d62012-07-04 12:48:07 +05303767 btrfs_set_token_timespec_sec(leaf, &item->otime,
3768 BTRFS_I(inode)->i_otime.tv_sec, &token);
3769 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3770 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3771
Liu Bo51fab692012-12-27 09:01:21 +00003772 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3773 &token);
3774 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3775 &token);
3776 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3777 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3778 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3779 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3780 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003781}
3782
Chris Masond352ac62008-09-29 15:18:18 -04003783/*
3784 * copy everything in the in-memory inode into the btree.
3785 */
Chris Mason21151332011-11-10 20:39:08 -05003786static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003787 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003788{
3789 struct btrfs_inode_item *inode_item;
3790 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003791 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003792 int ret;
3793
3794 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003795 if (!path)
3796 return -ENOMEM;
3797
Chris Masonb9473432009-03-13 11:00:37 -04003798 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003799 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3800 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003801 if (ret) {
3802 if (ret > 0)
3803 ret = -ENOENT;
3804 goto failed;
3805 }
3806
Chris Mason5f39d392007-10-15 16:14:19 -04003807 leaf = path->nodes[0];
3808 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003809 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003810
Chris Masone02119d2008-09-05 16:13:11 -04003811 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003812 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003813 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003814 ret = 0;
3815failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003816 btrfs_free_path(path);
3817 return ret;
3818}
3819
Chris Masond352ac62008-09-29 15:18:18 -04003820/*
Chris Mason21151332011-11-10 20:39:08 -05003821 * copy everything in the in-memory inode into the btree.
3822 */
3823noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3824 struct btrfs_root *root, struct inode *inode)
3825{
3826 int ret;
3827
3828 /*
3829 * If the inode is a free space inode, we can deadlock during commit
3830 * if we put it into the delayed code.
3831 *
3832 * The data relocation inode should also be directly updated
3833 * without delay
3834 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003835 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003836 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3837 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003838 btrfs_update_root_times(trans, root);
3839
Chris Mason21151332011-11-10 20:39:08 -05003840 ret = btrfs_delayed_update_inode(trans, root, inode);
3841 if (!ret)
3842 btrfs_set_inode_last_trans(trans, inode);
3843 return ret;
3844 }
3845
3846 return btrfs_update_inode_item(trans, root, inode);
3847}
3848
Josef Bacikbe6aef62012-10-22 15:43:12 -04003849noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3850 struct btrfs_root *root,
3851 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003852{
3853 int ret;
3854
3855 ret = btrfs_update_inode(trans, root, inode);
3856 if (ret == -ENOSPC)
3857 return btrfs_update_inode_item(trans, root, inode);
3858 return ret;
3859}
3860
3861/*
Chris Masond352ac62008-09-29 15:18:18 -04003862 * unlink helper that gets used here in inode.c and in the tree logging
3863 * recovery code. It remove a link in a directory with a given name, and
3864 * also drops the back refs in the inode to the directory
3865 */
Al Viro92986792011-03-04 17:14:37 +00003866static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3867 struct btrfs_root *root,
3868 struct inode *dir, struct inode *inode,
3869 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003870{
3871 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003872 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003873 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003874 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003875 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003876 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003877 u64 ino = btrfs_ino(inode);
3878 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003879
3880 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003881 if (!path) {
3882 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003883 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003884 }
3885
Chris Masonb9473432009-03-13 11:00:37 -04003886 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003887 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003888 name, name_len, -1);
3889 if (IS_ERR(di)) {
3890 ret = PTR_ERR(di);
3891 goto err;
3892 }
3893 if (!di) {
3894 ret = -ENOENT;
3895 goto err;
3896 }
Chris Mason5f39d392007-10-15 16:14:19 -04003897 leaf = path->nodes[0];
3898 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003899 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003900 if (ret)
3901 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003902 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003903
Miao Xie67de1172013-12-26 13:07:06 +08003904 /*
3905 * If we don't have dir index, we have to get it by looking up
3906 * the inode ref, since we get the inode ref, remove it directly,
3907 * it is unnecessary to do delayed deletion.
3908 *
3909 * But if we have dir index, needn't search inode ref to get it.
3910 * Since the inode ref is close to the inode item, it is better
3911 * that we delay to delete it, and just do this deletion when
3912 * we update the inode item.
3913 */
3914 if (BTRFS_I(inode)->dir_index) {
3915 ret = btrfs_delayed_delete_inode_ref(inode);
3916 if (!ret) {
3917 index = BTRFS_I(inode)->dir_index;
3918 goto skip_backref;
3919 }
3920 }
3921
Li Zefan33345d012011-04-20 10:31:50 +08003922 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3923 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003924 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003925 btrfs_info(root->fs_info,
3926 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003927 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003928 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003929 goto err;
3930 }
Miao Xie67de1172013-12-26 13:07:06 +08003931skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003932 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003933 if (ret) {
3934 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003935 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003936 }
Chris Mason39279cc2007-06-12 06:35:45 -04003937
Chris Masone02119d2008-09-05 16:13:11 -04003938 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003939 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003940 if (ret != 0 && ret != -ENOENT) {
3941 btrfs_abort_transaction(trans, root, ret);
3942 goto err;
3943 }
Chris Masone02119d2008-09-05 16:13:11 -04003944
3945 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3946 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003947 if (ret == -ENOENT)
3948 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003949 else if (ret)
3950 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003951err:
3952 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003953 if (ret)
3954 goto out;
3955
3956 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003957 inode_inc_iversion(inode);
3958 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003959 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003960 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003961out:
Chris Mason39279cc2007-06-12 06:35:45 -04003962 return ret;
3963}
3964
Al Viro92986792011-03-04 17:14:37 +00003965int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3966 struct btrfs_root *root,
3967 struct inode *dir, struct inode *inode,
3968 const char *name, int name_len)
3969{
3970 int ret;
3971 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3972 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003973 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003974 ret = btrfs_update_inode(trans, root, inode);
3975 }
3976 return ret;
3977}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003978
3979/*
3980 * helper to start transaction for unlink and rmdir.
3981 *
Josef Bacikd52be812013-05-29 14:54:47 -04003982 * unlink and rmdir are special in btrfs, they do not always free space, so
3983 * if we cannot make our reservations the normal way try and see if there is
3984 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3985 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003986 */
Josef Bacikd52be812013-05-29 14:54:47 -04003987static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003988{
3989 struct btrfs_trans_handle *trans;
3990 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003991 int ret;
3992
Josef Bacike70bea52011-10-11 14:18:24 -04003993 /*
3994 * 1 for the possible orphan item
3995 * 1 for the dir item
3996 * 1 for the dir index
3997 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003998 * 1 for the inode
3999 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04004000 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004001 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
4002 return trans;
4003
Josef Bacikd52be812013-05-29 14:54:47 -04004004 if (PTR_ERR(trans) == -ENOSPC) {
4005 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004006
Josef Bacikd52be812013-05-29 14:54:47 -04004007 trans = btrfs_start_transaction(root, 0);
4008 if (IS_ERR(trans))
4009 return trans;
4010 ret = btrfs_cond_migrate_bytes(root->fs_info,
4011 &root->fs_info->trans_block_rsv,
4012 num_bytes, 5);
4013 if (ret) {
4014 btrfs_end_transaction(trans, root);
4015 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004016 }
Josef Bacik5a77d762011-11-01 14:32:23 -04004017 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04004018 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004019 }
Josef Bacikd52be812013-05-29 14:54:47 -04004020 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004021}
4022
Chris Mason39279cc2007-06-12 06:35:45 -04004023static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4024{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004025 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004026 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04004027 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004028 int ret;
4029
Josef Bacikd52be812013-05-29 14:54:47 -04004030 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004031 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004032 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004033
Chris Mason12fcfd22009-03-24 10:24:20 -04004034 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
4035
Chris Masone02119d2008-09-05 16:13:11 -04004036 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4037 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004038 if (ret)
4039 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004040
Yan, Zhenga22285a2010-05-16 10:48:46 -04004041 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004042 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004043 if (ret)
4044 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004045 }
Josef Bacik7b128762008-07-24 12:17:14 -04004046
Tsutomu Itohb5324022011-07-19 07:27:20 +00004047out:
Josef Bacikd52be812013-05-29 14:54:47 -04004048 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004049 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004050 return ret;
4051}
4052
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004053int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4054 struct btrfs_root *root,
4055 struct inode *dir, u64 objectid,
4056 const char *name, int name_len)
4057{
4058 struct btrfs_path *path;
4059 struct extent_buffer *leaf;
4060 struct btrfs_dir_item *di;
4061 struct btrfs_key key;
4062 u64 index;
4063 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004064 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004065
4066 path = btrfs_alloc_path();
4067 if (!path)
4068 return -ENOMEM;
4069
Li Zefan33345d012011-04-20 10:31:50 +08004070 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004071 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004072 if (IS_ERR_OR_NULL(di)) {
4073 if (!di)
4074 ret = -ENOENT;
4075 else
4076 ret = PTR_ERR(di);
4077 goto out;
4078 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004079
4080 leaf = path->nodes[0];
4081 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4082 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4083 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004084 if (ret) {
4085 btrfs_abort_transaction(trans, root, ret);
4086 goto out;
4087 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004088 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004089
4090 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4091 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004092 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004093 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004094 if (ret != -ENOENT) {
4095 btrfs_abort_transaction(trans, root, ret);
4096 goto out;
4097 }
Li Zefan33345d012011-04-20 10:31:50 +08004098 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004099 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004100 if (IS_ERR_OR_NULL(di)) {
4101 if (!di)
4102 ret = -ENOENT;
4103 else
4104 ret = PTR_ERR(di);
4105 btrfs_abort_transaction(trans, root, ret);
4106 goto out;
4107 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004108
4109 leaf = path->nodes[0];
4110 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004111 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004112 index = key.offset;
4113 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004114 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004115
Miao Xie16cdcec2011-04-22 18:12:22 +08004116 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004117 if (ret) {
4118 btrfs_abort_transaction(trans, root, ret);
4119 goto out;
4120 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004121
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004122 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004123 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004124 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06004125 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004126 if (ret)
4127 btrfs_abort_transaction(trans, root, ret);
4128out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004129 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004130 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004131}
4132
Chris Mason39279cc2007-06-12 06:35:45 -04004133static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4134{
4135 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004136 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004137 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004138 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004139
David Sterbab3ae2442012-09-13 16:04:34 -06004140 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004141 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004142 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4143 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004144
Josef Bacikd52be812013-05-29 14:54:47 -04004145 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004146 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004147 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004148
Li Zefan33345d012011-04-20 10:31:50 +08004149 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004150 err = btrfs_unlink_subvol(trans, root, dir,
4151 BTRFS_I(inode)->location.objectid,
4152 dentry->d_name.name,
4153 dentry->d_name.len);
4154 goto out;
4155 }
4156
Josef Bacik7b128762008-07-24 12:17:14 -04004157 err = btrfs_orphan_add(trans, inode);
4158 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004159 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004160
Chris Mason39279cc2007-06-12 06:35:45 -04004161 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004162 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4163 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004164 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004165 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004166out:
Josef Bacikd52be812013-05-29 14:54:47 -04004167 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004168 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004169
Chris Mason39279cc2007-06-12 06:35:45 -04004170 return err;
4171}
4172
Chris Mason28f75a02015-02-04 06:59:29 -08004173static int truncate_space_check(struct btrfs_trans_handle *trans,
4174 struct btrfs_root *root,
4175 u64 bytes_deleted)
4176{
4177 int ret;
4178
4179 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted);
4180 ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv,
4181 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
4182 if (!ret)
4183 trans->bytes_reserved += bytes_deleted;
4184 return ret;
4185
4186}
4187
Chris Mason323ac952008-10-01 19:05:46 -04004188/*
Chris Mason39279cc2007-06-12 06:35:45 -04004189 * this can truncate away extent items, csum items and directory items.
4190 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004191 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004192 *
4193 * csum items that cross the new i_size are truncated to the new size
4194 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004195 *
4196 * min_type is the minimum key type to truncate down to. If set to 0, this
4197 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004198 */
Yan, Zheng80825102009-11-12 09:35:36 +00004199int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4200 struct btrfs_root *root,
4201 struct inode *inode,
4202 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004203{
Chris Mason39279cc2007-06-12 06:35:45 -04004204 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004205 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004206 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004207 struct btrfs_key key;
4208 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004209 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004210 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004211 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004212 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004213 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00004214 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004215 int found_extent;
4216 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004217 int pending_del_nr = 0;
4218 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004219 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004220 int ret;
4221 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004222 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004223 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004224 bool be_nice = 0;
4225 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004226 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004227
4228 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004229
Chris Mason28ed1342014-12-17 09:41:04 -08004230 /*
4231 * for non-free space inodes and ref cows, we want to back off from
4232 * time to time
4233 */
4234 if (!btrfs_is_free_space_inode(inode) &&
4235 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4236 be_nice = 1;
4237
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004238 path = btrfs_alloc_path();
4239 if (!path)
4240 return -ENOMEM;
4241 path->reada = -1;
4242
Josef Bacik5dc562c2012-08-17 13:14:17 -04004243 /*
4244 * We want to drop from the next block forward in case this new size is
4245 * not block aligned since we will be keeping the last block of the
4246 * extent just the way it is.
4247 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004248 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4249 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004250 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4251 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004252
Miao Xie16cdcec2011-04-22 18:12:22 +08004253 /*
4254 * This function is also used to drop the items in the log tree before
4255 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4256 * it is used to drop the loged items. So we shouldn't kill the delayed
4257 * items.
4258 */
4259 if (min_type == 0 && root == BTRFS_I(inode)->root)
4260 btrfs_kill_delayed_inode_items(inode);
4261
Li Zefan33345d012011-04-20 10:31:50 +08004262 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004263 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004264 key.type = (u8)-1;
4265
Chris Mason85e21ba2008-01-29 15:11:36 -05004266search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004267 /*
4268 * with a 16K leaf size and 128MB extents, you can actually queue
4269 * up a huge file in a single leaf. Most of the time that
4270 * bytes_deleted is > 0, it will be huge by the time we get here
4271 */
4272 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
4273 if (btrfs_should_end_transaction(trans, root)) {
4274 err = -EAGAIN;
4275 goto error;
4276 }
4277 }
4278
4279
Chris Masonb9473432009-03-13 11:00:37 -04004280 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004281 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004282 if (ret < 0) {
4283 err = ret;
4284 goto out;
4285 }
Chris Masond3977122009-01-05 21:25:51 -05004286
Chris Mason85e21ba2008-01-29 15:11:36 -05004287 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004288 /* there are no items in the tree for us to truncate, we're
4289 * done
4290 */
Yan, Zheng80825102009-11-12 09:35:36 +00004291 if (path->slots[0] == 0)
4292 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004293 path->slots[0]--;
4294 }
4295
Chris Masond3977122009-01-05 21:25:51 -05004296 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004297 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004298 leaf = path->nodes[0];
4299 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004300 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004301
Li Zefan33345d012011-04-20 10:31:50 +08004302 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004303 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004304
Chris Mason85e21ba2008-01-29 15:11:36 -05004305 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004306 break;
4307
Chris Mason5f39d392007-10-15 16:14:19 -04004308 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004309 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004310 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004311 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004312 extent_type = btrfs_file_extent_type(leaf, fi);
4313 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004314 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004315 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004316 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004317 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004318 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004319 }
Yan008630c2007-11-07 13:31:09 -05004320 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004321 }
Yan, Zheng80825102009-11-12 09:35:36 +00004322 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004323 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004324 } else {
4325 if (item_end < new_size)
4326 break;
4327 if (found_key.offset >= new_size)
4328 del_item = 1;
4329 else
4330 del_item = 0;
4331 }
Chris Mason39279cc2007-06-12 06:35:45 -04004332 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004333 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004334 if (found_type != BTRFS_EXTENT_DATA_KEY)
4335 goto delete;
4336
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004337 if (del_item)
4338 last_size = found_key.offset;
4339 else
4340 last_size = new_size;
4341
Chris Mason179e29e2007-11-01 11:28:41 -04004342 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004343 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004344 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004345 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004346 u64 orig_num_bytes =
4347 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004348 extent_num_bytes = ALIGN(new_size -
4349 found_key.offset,
4350 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004351 btrfs_set_file_extent_num_bytes(leaf, fi,
4352 extent_num_bytes);
4353 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004354 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004355 if (test_bit(BTRFS_ROOT_REF_COWS,
4356 &root->state) &&
4357 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004358 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004359 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004360 } else {
Chris Masondb945352007-10-15 16:15:53 -04004361 extent_num_bytes =
4362 btrfs_file_extent_disk_num_bytes(leaf,
4363 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004364 extent_offset = found_key.offset -
4365 btrfs_file_extent_offset(leaf, fi);
4366
Chris Mason39279cc2007-06-12 06:35:45 -04004367 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004368 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004369 if (extent_start != 0) {
4370 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004371 if (test_bit(BTRFS_ROOT_REF_COWS,
4372 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004373 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004374 }
4375 }
Chris Mason90692182008-02-08 13:49:28 -05004376 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004377 /*
4378 * we can't truncate inline items that have had
4379 * special encodings
4380 */
4381 if (!del_item &&
4382 btrfs_file_extent_compression(leaf, fi) == 0 &&
4383 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4384 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004385 u32 size = new_size - found_key.offset;
4386
Miao Xie27cdeb72014-04-02 19:51:05 +08004387 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004388 inode_sub_bytes(inode, item_end + 1 -
4389 new_size);
Chris Mason514ac8a2014-01-03 21:07:00 -08004390
4391 /*
4392 * update the ram bytes to properly reflect
4393 * the new size of our item
4394 */
4395 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004396 size =
4397 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004398 btrfs_truncate_item(root, path, size, 1);
Miao Xie27cdeb72014-04-02 19:51:05 +08004399 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4400 &root->state)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004401 inode_sub_bytes(inode, item_end + 1 -
4402 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004403 }
Chris Mason39279cc2007-06-12 06:35:45 -04004404 }
Chris Mason179e29e2007-11-01 11:28:41 -04004405delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004406 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004407 if (!pending_del_nr) {
4408 /* no pending yet, add ourselves */
4409 pending_del_slot = path->slots[0];
4410 pending_del_nr = 1;
4411 } else if (pending_del_nr &&
4412 path->slots[0] + 1 == pending_del_slot) {
4413 /* hop on the pending chunk */
4414 pending_del_nr++;
4415 pending_del_slot = path->slots[0];
4416 } else {
Chris Masond3977122009-01-05 21:25:51 -05004417 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004418 }
Chris Mason39279cc2007-06-12 06:35:45 -04004419 } else {
4420 break;
4421 }
Chris Mason28f75a02015-02-04 06:59:29 -08004422 should_throttle = 0;
4423
Miao Xie27cdeb72014-04-02 19:51:05 +08004424 if (found_extent &&
4425 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4426 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004427 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004428 bytes_deleted += extent_num_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04004429 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004430 extent_num_bytes, 0,
4431 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004432 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004433 BUG_ON(ret);
Josef Bacik12621332015-02-03 07:50:16 -08004434 if (btrfs_should_throttle_delayed_refs(trans, root))
Chris Mason28ed1342014-12-17 09:41:04 -08004435 btrfs_async_run_delayed_refs(root,
4436 trans->delayed_ref_updates * 2, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004437 if (be_nice) {
4438 if (truncate_space_check(trans, root,
4439 extent_num_bytes)) {
4440 should_end = 1;
4441 }
4442 if (btrfs_should_throttle_delayed_refs(trans,
4443 root)) {
4444 should_throttle = 1;
4445 }
4446 }
Chris Mason39279cc2007-06-12 06:35:45 -04004447 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004448
Yan, Zheng80825102009-11-12 09:35:36 +00004449 if (found_type == BTRFS_INODE_ITEM_KEY)
4450 break;
4451
4452 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004453 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004454 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004455 if (pending_del_nr) {
4456 ret = btrfs_del_items(trans, root, path,
4457 pending_del_slot,
4458 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004459 if (ret) {
4460 btrfs_abort_transaction(trans,
4461 root, ret);
4462 goto error;
4463 }
Yan, Zheng80825102009-11-12 09:35:36 +00004464 pending_del_nr = 0;
4465 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004466 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004467 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004468 unsigned long updates = trans->delayed_ref_updates;
4469 if (updates) {
4470 trans->delayed_ref_updates = 0;
4471 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4472 if (ret && !err)
4473 err = ret;
4474 }
4475 }
Chris Mason28f75a02015-02-04 06:59:29 -08004476 /*
4477 * if we failed to refill our space rsv, bail out
4478 * and let the transaction restart
4479 */
4480 if (should_end) {
4481 err = -EAGAIN;
4482 goto error;
4483 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004484 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004485 } else {
4486 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004487 }
Chris Mason39279cc2007-06-12 06:35:45 -04004488 }
Yan, Zheng80825102009-11-12 09:35:36 +00004489out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004490 if (pending_del_nr) {
4491 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4492 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004493 if (ret)
4494 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004495 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004496error:
Filipe Mananadac57052014-08-29 20:54:26 +01004497 if (last_size != (u64)-1 &&
4498 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004499 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004500
Chris Mason39279cc2007-06-12 06:35:45 -04004501 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004502
Chris Mason28f75a02015-02-04 06:59:29 -08004503 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
Chris Mason28ed1342014-12-17 09:41:04 -08004504 unsigned long updates = trans->delayed_ref_updates;
4505 if (updates) {
4506 trans->delayed_ref_updates = 0;
4507 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4508 if (ret && !err)
4509 err = ret;
4510 }
4511 }
Yan, Zheng80825102009-11-12 09:35:36 +00004512 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004513}
4514
Chris Masona52d9a82007-08-27 16:49:44 -04004515/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004516 * btrfs_truncate_page - read, zero a chunk and write a page
4517 * @inode - inode that we're zeroing
4518 * @from - the offset to start zeroing
4519 * @len - the length to zero, 0 to zero the entire range respective to the
4520 * offset
4521 * @front - zero up to the offset instead of from the offset on
4522 *
4523 * This will find the page for the "from" offset and cow the page and zero the
4524 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004525 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004526int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4527 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004528{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004529 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004530 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004531 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4532 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004533 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004534 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004535 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004536 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4537 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4538 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004539 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004540 int ret = 0;
4541 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004542 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004543
Josef Bacik2aaa6652012-08-29 14:27:18 -04004544 if ((offset & (blocksize - 1)) == 0 &&
4545 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004546 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004547 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004548 if (ret)
4549 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004550
Chris Mason211c17f2008-05-15 09:13:45 -04004551again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004552 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004553 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004554 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004555 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004556 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004557 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004558
4559 page_start = page_offset(page);
4560 page_end = page_start + PAGE_CACHE_SIZE - 1;
4561
Chris Masona52d9a82007-08-27 16:49:44 -04004562 if (!PageUptodate(page)) {
4563 ret = btrfs_readpage(NULL, page);
4564 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004565 if (page->mapping != mapping) {
4566 unlock_page(page);
4567 page_cache_release(page);
4568 goto again;
4569 }
Chris Masona52d9a82007-08-27 16:49:44 -04004570 if (!PageUptodate(page)) {
4571 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004572 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004573 }
4574 }
Chris Mason211c17f2008-05-15 09:13:45 -04004575 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004576
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004577 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004578 set_page_extent_mapped(page);
4579
4580 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4581 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004582 unlock_extent_cached(io_tree, page_start, page_end,
4583 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004584 unlock_page(page);
4585 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004586 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004587 btrfs_put_ordered_extent(ordered);
4588 goto again;
4589 }
4590
Josef Bacik2ac55d42010-02-03 19:33:23 +00004591 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004592 EXTENT_DIRTY | EXTENT_DELALLOC |
4593 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004594 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004595
Josef Bacik2ac55d42010-02-03 19:33:23 +00004596 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4597 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004598 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004599 unlock_extent_cached(io_tree, page_start, page_end,
4600 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004601 goto out_unlock;
4602 }
4603
Chris Masone6dcd2d2008-07-17 12:53:50 -04004604 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004605 if (!len)
4606 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004607 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004608 if (front)
4609 memset(kaddr, 0, offset);
4610 else
4611 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004612 flush_dcache_page(page);
4613 kunmap(page);
4614 }
Chris Mason247e7432008-07-17 12:53:51 -04004615 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004616 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004617 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4618 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004619
Chris Mason89642222008-07-24 09:41:53 -04004620out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004621 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004622 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004623 unlock_page(page);
4624 page_cache_release(page);
4625out:
4626 return ret;
4627}
4628
Josef Bacik16e75492013-10-22 12:18:51 -04004629static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4630 u64 offset, u64 len)
4631{
4632 struct btrfs_trans_handle *trans;
4633 int ret;
4634
4635 /*
4636 * Still need to make sure the inode looks like it's been updated so
4637 * that any holes get logged if we fsync.
4638 */
4639 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4640 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4641 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4642 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4643 return 0;
4644 }
4645
4646 /*
4647 * 1 - for the one we're dropping
4648 * 1 - for the one we're adding
4649 * 1 - for updating the inode.
4650 */
4651 trans = btrfs_start_transaction(root, 3);
4652 if (IS_ERR(trans))
4653 return PTR_ERR(trans);
4654
4655 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4656 if (ret) {
4657 btrfs_abort_transaction(trans, root, ret);
4658 btrfs_end_transaction(trans, root);
4659 return ret;
4660 }
4661
4662 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4663 0, 0, len, 0, len, 0, 0, 0);
4664 if (ret)
4665 btrfs_abort_transaction(trans, root, ret);
4666 else
4667 btrfs_update_inode(trans, root, inode);
4668 btrfs_end_transaction(trans, root);
4669 return ret;
4670}
4671
Josef Bacik695a0d02011-03-04 15:46:53 -05004672/*
4673 * This function puts in dummy file extents for the area we're creating a hole
4674 * for. So if we are truncating this file to a larger size we need to insert
4675 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4676 * the range between oldsize and size
4677 */
Josef Bacika41ad392011-01-31 15:30:16 -05004678int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004679{
Yan Zheng9036c102008-10-30 14:19:41 -04004680 struct btrfs_root *root = BTRFS_I(inode)->root;
4681 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004682 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004683 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004684 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004685 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4686 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004687 u64 last_byte;
4688 u64 cur_offset;
4689 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004690 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004691
Josef Bacika71754f2013-06-17 17:14:39 -04004692 /*
4693 * If our size started in the middle of a page we need to zero out the
4694 * rest of the page before we expand the i_size, otherwise we could
4695 * expose stale data.
4696 */
4697 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4698 if (err)
4699 return err;
4700
Yan Zheng9036c102008-10-30 14:19:41 -04004701 if (size <= hole_start)
4702 return 0;
4703
Yan Zheng9036c102008-10-30 14:19:41 -04004704 while (1) {
4705 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004706
Josef Bacik2ac55d42010-02-03 19:33:23 +00004707 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004708 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004709 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4710 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004711 if (!ordered)
4712 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004713 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4714 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004715 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004716 btrfs_put_ordered_extent(ordered);
4717 }
4718
Yan Zheng9036c102008-10-30 14:19:41 -04004719 cur_offset = hole_start;
4720 while (1) {
4721 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4722 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004723 if (IS_ERR(em)) {
4724 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004725 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004726 break;
4727 }
Yan Zheng9036c102008-10-30 14:19:41 -04004728 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004729 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004730 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004731 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004732 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004733
Josef Bacik16e75492013-10-22 12:18:51 -04004734 err = maybe_insert_hole(root, inode, cur_offset,
4735 hole_size);
4736 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004737 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004738 btrfs_drop_extent_cache(inode, cur_offset,
4739 cur_offset + hole_size - 1, 0);
4740 hole_em = alloc_extent_map();
4741 if (!hole_em) {
4742 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4743 &BTRFS_I(inode)->runtime_flags);
4744 goto next;
4745 }
4746 hole_em->start = cur_offset;
4747 hole_em->len = hole_size;
4748 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004749
Josef Bacik5dc562c2012-08-17 13:14:17 -04004750 hole_em->block_start = EXTENT_MAP_HOLE;
4751 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004752 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004753 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004754 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4755 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004756 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004757
4758 while (1) {
4759 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004760 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004761 write_unlock(&em_tree->lock);
4762 if (err != -EEXIST)
4763 break;
4764 btrfs_drop_extent_cache(inode, cur_offset,
4765 cur_offset +
4766 hole_size - 1, 0);
4767 }
4768 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004769 }
Josef Bacik16e75492013-10-22 12:18:51 -04004770next:
Yan Zheng9036c102008-10-30 14:19:41 -04004771 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004772 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004773 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004774 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004775 break;
4776 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004777 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004778 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4779 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004780 return err;
4781}
4782
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004783static int wait_snapshoting_atomic_t(atomic_t *a)
4784{
4785 schedule();
4786 return 0;
4787}
4788
4789static void wait_for_snapshot_creation(struct btrfs_root *root)
4790{
4791 while (true) {
4792 int ret;
4793
4794 ret = btrfs_start_write_no_snapshoting(root);
4795 if (ret)
4796 break;
4797 wait_on_atomic_t(&root->will_be_snapshoted,
4798 wait_snapshoting_atomic_t,
4799 TASK_UNINTERRUPTIBLE);
4800 }
4801}
4802
Eric Sandeen3972f262013-01-12 02:57:22 +00004803static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004804{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004805 struct btrfs_root *root = BTRFS_I(inode)->root;
4806 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004807 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004808 loff_t newsize = attr->ia_size;
4809 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004810 int ret;
4811
Eric Sandeen3972f262013-01-12 02:57:22 +00004812 /*
4813 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4814 * special case where we need to update the times despite not having
4815 * these flags set. For all other operations the VFS set these flags
4816 * explicitly if it wants a timestamp update.
4817 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004818 if (newsize != oldsize) {
4819 inode_inc_iversion(inode);
4820 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4821 inode->i_ctime = inode->i_mtime =
4822 current_fs_time(inode->i_sb);
4823 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004824
Josef Bacika41ad392011-01-31 15:30:16 -05004825 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004826 truncate_pagecache(inode, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004827 /*
4828 * Don't do an expanding truncate while snapshoting is ongoing.
4829 * This is to ensure the snapshot captures a fully consistent
4830 * state of this file - if the snapshot captures this expanding
4831 * truncation, it must capture all writes that happened before
4832 * this truncation.
4833 */
4834 wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004835 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004836 if (ret) {
4837 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004838 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004839 }
Yan, Zheng80825102009-11-12 09:35:36 +00004840
Miao Xief4a2f4c2011-12-14 20:12:01 -05004841 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004842 if (IS_ERR(trans)) {
4843 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004844 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004845 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004846
4847 i_size_write(inode, newsize);
4848 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4849 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004850 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004851 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004852 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004853
Josef Bacika41ad392011-01-31 15:30:16 -05004854 /*
4855 * We're truncating a file that used to have good data down to
4856 * zero. Make sure it gets into the ordered flush list so that
4857 * any new writes get down to disk quickly.
4858 */
4859 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004860 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4861 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004862
Josef Bacikf3fe8202013-01-07 17:03:21 -05004863 /*
4864 * 1 for the orphan item we're going to add
4865 * 1 for the orphan item deletion.
4866 */
4867 trans = btrfs_start_transaction(root, 2);
4868 if (IS_ERR(trans))
4869 return PTR_ERR(trans);
4870
4871 /*
4872 * We need to do this in case we fail at _any_ point during the
4873 * actual truncate. Once we do the truncate_setsize we could
4874 * invalidate pages which forces any outstanding ordered io to
4875 * be instantly completed which will give us extents that need
4876 * to be truncated. If we fail to get an orphan inode down we
4877 * could have left over extents that were never meant to live,
4878 * so we need to garuntee from this point on that everything
4879 * will be consistent.
4880 */
4881 ret = btrfs_orphan_add(trans, inode);
4882 btrfs_end_transaction(trans, root);
4883 if (ret)
4884 return ret;
4885
Josef Bacika41ad392011-01-31 15:30:16 -05004886 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4887 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004888
4889 /* Disable nonlocked read DIO to avoid the end less truncate */
4890 btrfs_inode_block_unlocked_dio(inode);
4891 inode_dio_wait(inode);
4892 btrfs_inode_resume_unlocked_dio(inode);
4893
Josef Bacika41ad392011-01-31 15:30:16 -05004894 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004895 if (ret && inode->i_nlink) {
4896 int err;
4897
4898 /*
4899 * failed to truncate, disk_i_size is only adjusted down
4900 * as we remove extents, so it should represent the true
4901 * size of the inode, so reset the in memory size and
4902 * delete our orphan entry.
4903 */
4904 trans = btrfs_join_transaction(root);
4905 if (IS_ERR(trans)) {
4906 btrfs_orphan_del(NULL, inode);
4907 return ret;
4908 }
4909 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4910 err = btrfs_orphan_del(trans, inode);
4911 if (err)
4912 btrfs_abort_transaction(trans, root, err);
4913 btrfs_end_transaction(trans, root);
4914 }
Yan, Zheng80825102009-11-12 09:35:36 +00004915 }
4916
Josef Bacika41ad392011-01-31 15:30:16 -05004917 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004918}
4919
Chris Mason39279cc2007-06-12 06:35:45 -04004920static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4921{
4922 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004923 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004924 int err;
4925
Li Zefanb83cc962010-12-20 16:04:08 +08004926 if (btrfs_root_readonly(root))
4927 return -EROFS;
4928
Chris Mason39279cc2007-06-12 06:35:45 -04004929 err = inode_change_ok(inode, attr);
4930 if (err)
4931 return err;
4932
Chris Mason5a3f23d2009-03-31 13:27:11 -04004933 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004934 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004935 if (err)
4936 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004937 }
Yan Zheng9036c102008-10-30 14:19:41 -04004938
Christoph Hellwig10257742010-06-04 11:30:02 +02004939 if (attr->ia_valid) {
4940 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004941 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004942 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004943
Josef Bacik22c44fe2011-11-30 10:45:38 -05004944 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004945 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004946 }
4947
Chris Mason39279cc2007-06-12 06:35:45 -04004948 return err;
4949}
Chris Mason61295eb2008-01-14 16:24:38 -05004950
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004951/*
4952 * While truncating the inode pages during eviction, we get the VFS calling
4953 * btrfs_invalidatepage() against each page of the inode. This is slow because
4954 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4955 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4956 * extent_state structures over and over, wasting lots of time.
4957 *
4958 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4959 * those expensive operations on a per page basis and do only the ordered io
4960 * finishing, while we release here the extent_map and extent_state structures,
4961 * without the excessive merging and splitting.
4962 */
4963static void evict_inode_truncate_pages(struct inode *inode)
4964{
4965 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4966 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4967 struct rb_node *node;
4968
4969 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004970 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004971
4972 write_lock(&map_tree->lock);
4973 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4974 struct extent_map *em;
4975
4976 node = rb_first(&map_tree->map);
4977 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004978 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4979 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004980 remove_extent_mapping(map_tree, em);
4981 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004982 if (need_resched()) {
4983 write_unlock(&map_tree->lock);
4984 cond_resched();
4985 write_lock(&map_tree->lock);
4986 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004987 }
4988 write_unlock(&map_tree->lock);
4989
4990 spin_lock(&io_tree->lock);
4991 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4992 struct extent_state *state;
4993 struct extent_state *cached_state = NULL;
4994
4995 node = rb_first(&io_tree->state);
4996 state = rb_entry(node, struct extent_state, rb_node);
4997 atomic_inc(&state->refs);
4998 spin_unlock(&io_tree->lock);
4999
5000 lock_extent_bits(io_tree, state->start, state->end,
5001 0, &cached_state);
5002 clear_extent_bit(io_tree, state->start, state->end,
5003 EXTENT_LOCKED | EXTENT_DIRTY |
5004 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5005 EXTENT_DEFRAG, 1, 1,
5006 &cached_state, GFP_NOFS);
5007 free_extent_state(state);
5008
Filipe Manana7064dd52014-08-08 02:47:05 +01005009 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005010 spin_lock(&io_tree->lock);
5011 }
5012 spin_unlock(&io_tree->lock);
5013}
5014
Al Virobd555972010-06-07 11:35:40 -04005015void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005016{
5017 struct btrfs_trans_handle *trans;
5018 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005019 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005020 int steal_from_global = 0;
Josef Bacik07127182011-08-19 10:29:59 -04005021 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04005022 int ret;
5023
liubo1abe9b82011-03-24 11:18:59 +00005024 trace_btrfs_inode_evict(inode);
5025
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005026 evict_inode_truncate_pages(inode);
5027
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005028 if (inode->i_nlink &&
5029 ((btrfs_root_refs(&root->root_item) != 0 &&
5030 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5031 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005032 goto no_delete;
5033
Chris Mason39279cc2007-06-12 06:35:45 -04005034 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005035 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005036 goto no_delete;
5037 }
Al Virobd555972010-06-07 11:35:40 -04005038 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04005039 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005040
Miao Xief6124962014-09-12 18:44:04 +08005041 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5042
Yan, Zhengc71bf092009-11-12 09:34:40 +00005043 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06005044 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005045 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005046 goto no_delete;
5047 }
5048
Yan, Zheng76dda932009-09-21 16:00:26 -04005049 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005050 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5051 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005052 goto no_delete;
5053 }
5054
Miao Xie0e8c36a2012-12-19 06:59:51 +00005055 ret = btrfs_commit_inode_delayed_inode(inode);
5056 if (ret) {
5057 btrfs_orphan_del(NULL, inode);
5058 goto no_delete;
5059 }
5060
Miao Xie66d8f3d2012-09-06 04:02:28 -06005061 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005062 if (!rsv) {
5063 btrfs_orphan_del(NULL, inode);
5064 goto no_delete;
5065 }
Josef Bacik4a338542011-08-29 11:01:31 -04005066 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005067 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04005068 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005069
Chris Masondbe674a2008-07-17 12:54:05 -04005070 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005071
Josef Bacik4289a662011-08-05 13:22:24 -04005072 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005073 * This is a bit simpler than btrfs_truncate since we've already
5074 * reserved our space for our orphan item in the unlink, so we just
5075 * need to reserve some slack space in case we add bytes and update
5076 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005077 */
Yan, Zheng80825102009-11-12 09:35:36 +00005078 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005079 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5080 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005081
Josef Bacik726c35f2011-09-26 15:46:06 -04005082 /*
5083 * Try and steal from the global reserve since we will
5084 * likely not use this space anyway, we want to try as
5085 * hard as possible to get this to work.
5086 */
5087 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005088 steal_from_global++;
5089 else
5090 steal_from_global = 0;
5091 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005092
Josef Bacik3bce8762015-02-24 12:35:51 -08005093 /*
5094 * steal_from_global == 0: we reserved stuff, hooray!
5095 * steal_from_global == 1: we didn't reserve stuff, boo!
5096 * steal_from_global == 2: we've committed, still not a lot of
5097 * room but maybe we'll have room in the global reserve this
5098 * time.
5099 * steal_from_global == 3: abandon all hope!
5100 */
5101 if (steal_from_global > 2) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005102 btrfs_warn(root->fs_info,
5103 "Could not get space for a delete, will truncate on mount %d",
5104 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005105 btrfs_orphan_del(NULL, inode);
5106 btrfs_free_block_rsv(root, rsv);
5107 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005108 }
5109
Miao Xie0e8c36a2012-12-19 06:59:51 +00005110 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005111 if (IS_ERR(trans)) {
5112 btrfs_orphan_del(NULL, inode);
5113 btrfs_free_block_rsv(root, rsv);
5114 goto no_delete;
5115 }
5116
Josef Bacik3bce8762015-02-24 12:35:51 -08005117 /*
5118 * We can't just steal from the global reserve, we need tomake
5119 * sure there is room to do it, if not we need to commit and try
5120 * again.
5121 */
5122 if (steal_from_global) {
5123 if (!btrfs_check_space_for_delayed_refs(trans, root))
5124 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
5125 min_size);
5126 else
5127 ret = -ENOSPC;
5128 }
5129
5130 /*
5131 * Couldn't steal from the global reserve, we have too much
5132 * pending stuff built up, commit the transaction and try it
5133 * again.
5134 */
5135 if (ret) {
5136 ret = btrfs_commit_transaction(trans, root);
5137 if (ret) {
5138 btrfs_orphan_del(NULL, inode);
5139 btrfs_free_block_rsv(root, rsv);
5140 goto no_delete;
5141 }
5142 continue;
5143 } else {
5144 steal_from_global = 0;
5145 }
5146
Josef Bacik4289a662011-08-05 13:22:24 -04005147 trans->block_rsv = rsv;
5148
Yan, Zhengd68fc572010-05-16 10:49:58 -04005149 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005150 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005151 break;
5152
Miao Xie8407aa42012-09-07 01:43:32 -06005153 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00005154 btrfs_end_transaction(trans, root);
5155 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00005156 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04005157 }
5158
Josef Bacik4289a662011-08-05 13:22:24 -04005159 btrfs_free_block_rsv(root, rsv);
5160
Josef Bacik4ef31a42013-08-13 14:10:08 -04005161 /*
5162 * Errors here aren't a big deal, it just means we leave orphan items
5163 * in the tree. They will be cleaned up on the next mount.
5164 */
Yan, Zheng80825102009-11-12 09:35:36 +00005165 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005166 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005167 btrfs_orphan_del(trans, inode);
5168 } else {
5169 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005170 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005171
Josef Bacik4289a662011-08-05 13:22:24 -04005172 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08005173 if (!(root == root->fs_info->tree_root ||
5174 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005175 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005176
Chris Mason54aa1f42007-06-22 14:16:25 -04005177 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005178 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005179no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005180 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005181 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005182 return;
Chris Mason39279cc2007-06-12 06:35:45 -04005183}
5184
5185/*
5186 * this returns the key found in the dir entry in the location pointer.
5187 * If no dir entries were found, location->objectid is 0.
5188 */
5189static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5190 struct btrfs_key *location)
5191{
5192 const char *name = dentry->d_name.name;
5193 int namelen = dentry->d_name.len;
5194 struct btrfs_dir_item *di;
5195 struct btrfs_path *path;
5196 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005197 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005198
5199 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005200 if (!path)
5201 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005202
Li Zefan33345d012011-04-20 10:31:50 +08005203 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005204 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005205 if (IS_ERR(di))
5206 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005207
David Sterbac7040052011-04-19 18:00:01 +02005208 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005209 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005210
Chris Mason5f39d392007-10-15 16:14:19 -04005211 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005212out:
Chris Mason39279cc2007-06-12 06:35:45 -04005213 btrfs_free_path(path);
5214 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005215out_err:
5216 location->objectid = 0;
5217 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005218}
5219
5220/*
5221 * when we hit a tree root in a directory, the btrfs part of the inode
5222 * needs to be changed to reflect the root directory of the tree root. This
5223 * is kind of like crossing a mount point.
5224 */
5225static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005226 struct inode *dir,
5227 struct dentry *dentry,
5228 struct btrfs_key *location,
5229 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005230{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005231 struct btrfs_path *path;
5232 struct btrfs_root *new_root;
5233 struct btrfs_root_ref *ref;
5234 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005235 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005236 int ret;
5237 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005238
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005239 path = btrfs_alloc_path();
5240 if (!path) {
5241 err = -ENOMEM;
5242 goto out;
5243 }
Chris Mason39279cc2007-06-12 06:35:45 -04005244
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005245 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005246 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5247 key.type = BTRFS_ROOT_REF_KEY;
5248 key.offset = location->objectid;
5249
5250 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5251 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005252 if (ret) {
5253 if (ret < 0)
5254 err = ret;
5255 goto out;
5256 }
Chris Mason39279cc2007-06-12 06:35:45 -04005257
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005258 leaf = path->nodes[0];
5259 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005260 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005261 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5262 goto out;
5263
5264 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5265 (unsigned long)(ref + 1),
5266 dentry->d_name.len);
5267 if (ret)
5268 goto out;
5269
David Sterbab3b4aa72011-04-21 01:20:15 +02005270 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005271
5272 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5273 if (IS_ERR(new_root)) {
5274 err = PTR_ERR(new_root);
5275 goto out;
5276 }
5277
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005278 *sub_root = new_root;
5279 location->objectid = btrfs_root_dirid(&new_root->root_item);
5280 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005281 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005282 err = 0;
5283out:
5284 btrfs_free_path(path);
5285 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005286}
5287
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005288static void inode_tree_add(struct inode *inode)
5289{
5290 struct btrfs_root *root = BTRFS_I(inode)->root;
5291 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005292 struct rb_node **p;
5293 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005294 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005295 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005296
Al Viro1d3382cb2010-10-23 15:19:20 -04005297 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005298 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005299 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005300 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005301 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005302 while (*p) {
5303 parent = *p;
5304 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5305
Li Zefan33345d012011-04-20 10:31:50 +08005306 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005307 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005308 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005309 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005310 else {
5311 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005312 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005313 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005314 RB_CLEAR_NODE(parent);
5315 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005316 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005317 }
5318 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005319 rb_link_node(new, parent, p);
5320 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005321 spin_unlock(&root->inode_lock);
5322}
5323
5324static void inode_tree_del(struct inode *inode)
5325{
5326 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005327 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005328
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005329 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005330 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005331 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005332 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005333 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005334 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005335 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005336
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005337 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005338 synchronize_srcu(&root->fs_info->subvol_srcu);
5339 spin_lock(&root->inode_lock);
5340 empty = RB_EMPTY_ROOT(&root->inode_tree);
5341 spin_unlock(&root->inode_lock);
5342 if (empty)
5343 btrfs_add_dead_root(root);
5344 }
5345}
5346
Jeff Mahoney143bede2012-03-01 14:56:26 +01005347void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005348{
5349 struct rb_node *node;
5350 struct rb_node *prev;
5351 struct btrfs_inode *entry;
5352 struct inode *inode;
5353 u64 objectid = 0;
5354
Liu Bo7813b3d2014-02-10 17:37:25 +08005355 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5356 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005357
5358 spin_lock(&root->inode_lock);
5359again:
5360 node = root->inode_tree.rb_node;
5361 prev = NULL;
5362 while (node) {
5363 prev = node;
5364 entry = rb_entry(node, struct btrfs_inode, rb_node);
5365
Li Zefan33345d012011-04-20 10:31:50 +08005366 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005367 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005368 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005369 node = node->rb_right;
5370 else
5371 break;
5372 }
5373 if (!node) {
5374 while (prev) {
5375 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005376 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005377 node = prev;
5378 break;
5379 }
5380 prev = rb_next(prev);
5381 }
5382 }
5383 while (node) {
5384 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005385 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005386 inode = igrab(&entry->vfs_inode);
5387 if (inode) {
5388 spin_unlock(&root->inode_lock);
5389 if (atomic_read(&inode->i_count) > 1)
5390 d_prune_aliases(inode);
5391 /*
Al Viro45321ac2010-06-07 13:43:19 -04005392 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005393 * the inode cache when its usage count
5394 * hits zero.
5395 */
5396 iput(inode);
5397 cond_resched();
5398 spin_lock(&root->inode_lock);
5399 goto again;
5400 }
5401
5402 if (cond_resched_lock(&root->inode_lock))
5403 goto again;
5404
5405 node = rb_next(node);
5406 }
5407 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005408}
5409
Chris Masone02119d2008-09-05 16:13:11 -04005410static int btrfs_init_locked_inode(struct inode *inode, void *p)
5411{
5412 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005413 inode->i_ino = args->location->objectid;
5414 memcpy(&BTRFS_I(inode)->location, args->location,
5415 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005416 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005417 return 0;
5418}
5419
5420static int btrfs_find_actor(struct inode *inode, void *opaque)
5421{
5422 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005423 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005424 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005425}
5426
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005427static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005428 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005429 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005430{
5431 struct inode *inode;
5432 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005433 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005434
Chris Mason90d3e592014-01-09 17:28:00 -08005435 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005436 args.root = root;
5437
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005438 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005439 btrfs_init_locked_inode,
5440 (void *)&args);
5441 return inode;
5442}
5443
Balaji Rao1a54ef82008-07-21 02:01:04 +05305444/* Get an inode object given its location and corresponding root.
5445 * Returns in *is_new if the inode was read from disk
5446 */
5447struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005448 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305449{
5450 struct inode *inode;
5451
Chris Mason90d3e592014-01-09 17:28:00 -08005452 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305453 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005454 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305455
5456 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305457 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005458 if (!is_bad_inode(inode)) {
5459 inode_tree_add(inode);
5460 unlock_new_inode(inode);
5461 if (new)
5462 *new = 1;
5463 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005464 unlock_new_inode(inode);
5465 iput(inode);
5466 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005467 }
5468 }
5469
Balaji Rao1a54ef82008-07-21 02:01:04 +05305470 return inode;
5471}
5472
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005473static struct inode *new_simple_dir(struct super_block *s,
5474 struct btrfs_key *key,
5475 struct btrfs_root *root)
5476{
5477 struct inode *inode = new_inode(s);
5478
5479 if (!inode)
5480 return ERR_PTR(-ENOMEM);
5481
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005482 BTRFS_I(inode)->root = root;
5483 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005484 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005485
5486 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005487 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005488 inode->i_fop = &simple_dir_operations;
5489 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
chandan r9cc97d62012-07-04 12:48:07 +05305490 inode->i_mtime = CURRENT_TIME;
5491 inode->i_atime = inode->i_mtime;
5492 inode->i_ctime = inode->i_mtime;
5493 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005494
5495 return inode;
5496}
5497
Chris Mason3de45862008-11-17 21:02:50 -05005498struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005499{
Chris Masond3977122009-01-05 21:25:51 -05005500 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005501 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005502 struct btrfs_root *sub_root = root;
5503 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005504 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005505 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005506
5507 if (dentry->d_name.len > BTRFS_NAME_LEN)
5508 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005509
Jeff Layton39e3c952012-11-28 11:30:53 -05005510 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005511 if (ret < 0)
5512 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005513
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005514 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005515 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005516
5517 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005518 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005519 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005520 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005521
5522 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5523
Yan, Zheng76dda932009-09-21 16:00:26 -04005524 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005525 ret = fixup_tree_root_location(root, dir, dentry,
5526 &location, &sub_root);
5527 if (ret < 0) {
5528 if (ret != -ENOENT)
5529 inode = ERR_PTR(ret);
5530 else
5531 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5532 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005533 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005534 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005535 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5536
Julia Lawall34d19ba2011-01-24 19:55:19 +00005537 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005538 down_read(&root->fs_info->cleanup_work_sem);
5539 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005540 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005541 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005542 if (ret) {
5543 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005544 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005545 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005546 }
5547
Chris Mason3de45862008-11-17 21:02:50 -05005548 return inode;
5549}
5550
Nick Pigginfe15ce42011-01-07 17:49:23 +11005551static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005552{
5553 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005554 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005555
Li Zefan848cce02012-02-21 17:04:28 +08005556 if (!inode && !IS_ROOT(dentry))
5557 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005558
Li Zefan848cce02012-02-21 17:04:28 +08005559 if (inode) {
5560 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005561 if (btrfs_root_refs(&root->root_item) == 0)
5562 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005563
5564 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5565 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005566 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005567 return 0;
5568}
5569
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005570static void btrfs_dentry_release(struct dentry *dentry)
5571{
Daeseok Youn944a4512014-04-14 15:37:02 +09005572 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005573}
5574
Chris Mason3de45862008-11-17 21:02:50 -05005575static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005576 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005577{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005578 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005579
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005580 inode = btrfs_lookup_dentry(dir, dentry);
5581 if (IS_ERR(inode)) {
5582 if (PTR_ERR(inode) == -ENOENT)
5583 inode = NULL;
5584 else
5585 return ERR_CAST(inode);
5586 }
5587
Al Viro41d28bc2014-10-12 22:24:21 -04005588 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005589}
5590
Miao Xie16cdcec2011-04-22 18:12:22 +08005591unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005592 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5593};
5594
Al Viro9cdda8d2013-05-22 16:48:09 -04005595static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005596{
Al Viro9cdda8d2013-05-22 16:48:09 -04005597 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005598 struct btrfs_root *root = BTRFS_I(inode)->root;
5599 struct btrfs_item *item;
5600 struct btrfs_dir_item *di;
5601 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005602 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005603 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005604 struct list_head ins_list;
5605 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005606 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005607 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005608 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005609 unsigned char d_type;
5610 int over = 0;
5611 u32 di_cur;
5612 u32 di_total;
5613 u32 di_len;
5614 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005615 char tmp_name[32];
5616 char *name_ptr;
5617 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005618 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005619
5620 /* FIXME, use a real flag for deciding about the key type */
5621 if (root->fs_info->tree_root == root)
5622 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005623
Al Viro9cdda8d2013-05-22 16:48:09 -04005624 if (!dir_emit_dots(file, ctx))
5625 return 0;
5626
David Woodhouse49593bf2008-08-17 17:08:36 +01005627 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005628 if (!path)
5629 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005630
Josef Bacik026fd312011-05-13 10:32:11 -04005631 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005632
Miao Xie16cdcec2011-04-22 18:12:22 +08005633 if (key_type == BTRFS_DIR_INDEX_KEY) {
5634 INIT_LIST_HEAD(&ins_list);
5635 INIT_LIST_HEAD(&del_list);
5636 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5637 }
5638
David Sterba962a2982014-06-04 18:41:45 +02005639 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005640 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005641 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005642
Chris Mason39279cc2007-06-12 06:35:45 -04005643 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5644 if (ret < 0)
5645 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005646
5647 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005648 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005649 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005650 if (slot >= btrfs_header_nritems(leaf)) {
5651 ret = btrfs_next_leaf(root, path);
5652 if (ret < 0)
5653 goto err;
5654 else if (ret > 0)
5655 break;
5656 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005657 }
Chris Mason3de45862008-11-17 21:02:50 -05005658
Ross Kirkdd3cc162013-09-16 15:58:09 +01005659 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005660 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5661
5662 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005663 break;
David Sterba962a2982014-06-04 18:41:45 +02005664 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005665 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005666 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005667 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005668 if (key_type == BTRFS_DIR_INDEX_KEY &&
5669 btrfs_should_delete_dir_index(&del_list,
5670 found_key.offset))
5671 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005672
Al Viro9cdda8d2013-05-22 16:48:09 -04005673 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005674 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005675
Chris Mason39279cc2007-06-12 06:35:45 -04005676 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5677 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005678 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005679
5680 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005681 struct btrfs_key location;
5682
Josef Bacik22a94d42011-03-16 16:47:17 -04005683 if (verify_dir_item(root, leaf, di))
5684 break;
5685
Chris Mason5f39d392007-10-15 16:14:19 -04005686 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005687 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005688 name_ptr = tmp_name;
5689 } else {
5690 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005691 if (!name_ptr) {
5692 ret = -ENOMEM;
5693 goto err;
5694 }
Chris Mason5f39d392007-10-15 16:14:19 -04005695 }
5696 read_extent_buffer(leaf, name_ptr,
5697 (unsigned long)(di + 1), name_len);
5698
5699 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5700 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005701
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005702
Chris Mason3de45862008-11-17 21:02:50 -05005703 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005704 * skip it.
5705 *
5706 * In contrast to old kernels, we insert the snapshot's
5707 * dir item and dir index after it has been created, so
5708 * we won't find a reference to our own snapshot. We
5709 * still keep the following code for backward
5710 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005711 */
5712 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5713 location.objectid == root->root_key.objectid) {
5714 over = 0;
5715 goto skip;
5716 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005717 over = !dir_emit(ctx, name_ptr, name_len,
5718 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005719
Chris Mason3de45862008-11-17 21:02:50 -05005720skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005721 if (name_ptr != tmp_name)
5722 kfree(name_ptr);
5723
Chris Mason39279cc2007-06-12 06:35:45 -04005724 if (over)
5725 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005726 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005727 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005728 di_cur += di_len;
5729 di = (struct btrfs_dir_item *)((char *)di + di_len);
5730 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005731next:
5732 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005733 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005734
Miao Xie16cdcec2011-04-22 18:12:22 +08005735 if (key_type == BTRFS_DIR_INDEX_KEY) {
5736 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005737 ctx->pos++;
5738 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005739 if (ret)
5740 goto nopos;
5741 }
5742
David Woodhouse49593bf2008-08-17 17:08:36 +01005743 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005744 ctx->pos++;
5745
5746 /*
5747 * Stop new entries from being returned after we return the last
5748 * entry.
5749 *
5750 * New directory entries are assigned a strictly increasing
5751 * offset. This means that new entries created during readdir
5752 * are *guaranteed* to be seen in the future by that readdir.
5753 * This has broken buggy programs which operate on names as
5754 * they're returned by readdir. Until we re-use freed offsets
5755 * we have this hack to stop new entries from being returned
5756 * under the assumption that they'll never reach this huge
5757 * offset.
5758 *
5759 * This is being careful not to overflow 32bit loff_t unless the
5760 * last entry requires it because doing so has broken 32bit apps
5761 * in the past.
5762 */
5763 if (key_type == BTRFS_DIR_INDEX_KEY) {
5764 if (ctx->pos >= INT_MAX)
5765 ctx->pos = LLONG_MAX;
5766 else
5767 ctx->pos = INT_MAX;
5768 }
Chris Mason39279cc2007-06-12 06:35:45 -04005769nopos:
5770 ret = 0;
5771err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005772 if (key_type == BTRFS_DIR_INDEX_KEY)
5773 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005774 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005775 return ret;
5776}
5777
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005778int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005779{
5780 struct btrfs_root *root = BTRFS_I(inode)->root;
5781 struct btrfs_trans_handle *trans;
5782 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005783 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005784
Josef Bacik72ac3c02012-05-23 14:13:11 -04005785 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005786 return 0;
5787
Liu Bo83eea1f2012-07-10 05:28:39 -06005788 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005789 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005790
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005791 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005792 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005793 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005794 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005795 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005796 if (IS_ERR(trans))
5797 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005798 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005799 }
5800 return ret;
5801}
5802
5803/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005804 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005805 * inode changes. But, it is most likely to find the inode in cache.
5806 * FIXME, needs more benchmarking...there are no reasons other than performance
5807 * to keep or drop this code.
5808 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005809static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005810{
5811 struct btrfs_root *root = BTRFS_I(inode)->root;
5812 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005813 int ret;
5814
Josef Bacik72ac3c02012-05-23 14:13:11 -04005815 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005816 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005817
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005818 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005819 if (IS_ERR(trans))
5820 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005821
5822 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005823 if (ret && ret == -ENOSPC) {
5824 /* whoops, lets try again with the full transaction */
5825 btrfs_end_transaction(trans, root);
5826 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005827 if (IS_ERR(trans))
5828 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005829
Chris Mason94b60442010-05-26 11:02:00 -04005830 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005831 }
Chris Mason39279cc2007-06-12 06:35:45 -04005832 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005833 if (BTRFS_I(inode)->delayed_node)
5834 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005835
5836 return ret;
5837}
5838
5839/*
5840 * This is a copy of file_update_time. We need this so we can return error on
5841 * ENOSPC for updating the inode in the case of file write and mmap writes.
5842 */
Josef Bacike41f9412012-03-26 09:46:47 -04005843static int btrfs_update_time(struct inode *inode, struct timespec *now,
5844 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005845{
Alexander Block2bc5565282012-06-15 09:49:33 +02005846 struct btrfs_root *root = BTRFS_I(inode)->root;
5847
5848 if (btrfs_root_readonly(root))
5849 return -EROFS;
5850
Josef Bacike41f9412012-03-26 09:46:47 -04005851 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005852 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005853 if (flags & S_CTIME)
5854 inode->i_ctime = *now;
5855 if (flags & S_MTIME)
5856 inode->i_mtime = *now;
5857 if (flags & S_ATIME)
5858 inode->i_atime = *now;
5859 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005860}
5861
Chris Masond352ac62008-09-29 15:18:18 -04005862/*
5863 * find the highest existing sequence number in a directory
5864 * and then set the in-memory index_cnt variable to reflect
5865 * free sequence numbers
5866 */
Josef Bacikaec74772008-07-24 12:12:38 -04005867static int btrfs_set_inode_index_count(struct inode *inode)
5868{
5869 struct btrfs_root *root = BTRFS_I(inode)->root;
5870 struct btrfs_key key, found_key;
5871 struct btrfs_path *path;
5872 struct extent_buffer *leaf;
5873 int ret;
5874
Li Zefan33345d012011-04-20 10:31:50 +08005875 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005876 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005877 key.offset = (u64)-1;
5878
5879 path = btrfs_alloc_path();
5880 if (!path)
5881 return -ENOMEM;
5882
5883 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5884 if (ret < 0)
5885 goto out;
5886 /* FIXME: we should be able to handle this */
5887 if (ret == 0)
5888 goto out;
5889 ret = 0;
5890
5891 /*
5892 * MAGIC NUMBER EXPLANATION:
5893 * since we search a directory based on f_pos we have to start at 2
5894 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5895 * else has to start at 2
5896 */
5897 if (path->slots[0] == 0) {
5898 BTRFS_I(inode)->index_cnt = 2;
5899 goto out;
5900 }
5901
5902 path->slots[0]--;
5903
5904 leaf = path->nodes[0];
5905 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5906
Li Zefan33345d012011-04-20 10:31:50 +08005907 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005908 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04005909 BTRFS_I(inode)->index_cnt = 2;
5910 goto out;
5911 }
5912
5913 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5914out:
5915 btrfs_free_path(path);
5916 return ret;
5917}
5918
Chris Masond352ac62008-09-29 15:18:18 -04005919/*
5920 * helper to find a free sequence number in a given directory. This current
5921 * code is very simple, later versions will do smarter things in the btree
5922 */
Chris Mason3de45862008-11-17 21:02:50 -05005923int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005924{
5925 int ret = 0;
5926
5927 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005928 ret = btrfs_inode_delayed_dir_index_count(dir);
5929 if (ret) {
5930 ret = btrfs_set_inode_index_count(dir);
5931 if (ret)
5932 return ret;
5933 }
Josef Bacikaec74772008-07-24 12:12:38 -04005934 }
5935
Chris Mason00e4e6b2008-08-05 11:18:09 -04005936 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005937 BTRFS_I(dir)->index_cnt++;
5938
5939 return ret;
5940}
5941
Chris Masonb0d5d102014-09-08 13:08:51 -07005942static int btrfs_insert_inode_locked(struct inode *inode)
5943{
5944 struct btrfs_iget_args args;
5945 args.location = &BTRFS_I(inode)->location;
5946 args.root = BTRFS_I(inode)->root;
5947
5948 return insert_inode_locked4(inode,
5949 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5950 btrfs_find_actor, &args);
5951}
5952
Chris Mason39279cc2007-06-12 06:35:45 -04005953static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5954 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005955 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005956 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005957 u64 ref_objectid, u64 objectid,
5958 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005959{
5960 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005961 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005962 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005963 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005964 struct btrfs_inode_ref *ref;
5965 struct btrfs_key key[2];
5966 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005967 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005968 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005969 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005970
Chris Mason5f39d392007-10-15 16:14:19 -04005971 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005972 if (!path)
5973 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005974
Chris Mason39279cc2007-06-12 06:35:45 -04005975 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005976 if (!inode) {
5977 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005978 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005979 }
Chris Mason39279cc2007-06-12 06:35:45 -04005980
Li Zefan581bb052011-04-20 10:06:11 +08005981 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005982 * O_TMPFILE, set link count to 0, so that after this point,
5983 * we fill in an inode item with the correct link count.
5984 */
5985 if (!name)
5986 set_nlink(inode, 0);
5987
5988 /*
Li Zefan581bb052011-04-20 10:06:11 +08005989 * we have to initialize this early, so we can reclaim the inode
5990 * number if we fail afterwards in this function.
5991 */
5992 inode->i_ino = objectid;
5993
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005994 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005995 trace_btrfs_inode_request(dir);
5996
Chris Mason3de45862008-11-17 21:02:50 -05005997 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005998 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005999 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006000 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006001 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006002 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006003 } else if (dir) {
6004 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006005 }
6006 /*
6007 * index_cnt is ignored for everything but a dir,
6008 * btrfs_get_inode_index_count has an explanation for the magic
6009 * number
6010 */
6011 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006012 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006013 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006014 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006015 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006016
Josef Bacik5dc562c2012-08-17 13:14:17 -04006017 /*
6018 * We could have gotten an inode number from somebody who was fsynced
6019 * and then removed in this same transaction, so let's just set full
6020 * sync since it will be a full sync anyway and this will blow away the
6021 * old info in the log.
6022 */
6023 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6024
Chris Mason9c583092008-01-29 15:15:18 -05006025 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006026 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006027 key[0].offset = 0;
6028
Chris Mason9c583092008-01-29 15:15:18 -05006029 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006030
6031 if (name) {
6032 /*
6033 * Start new inodes with an inode_ref. This is slightly more
6034 * efficient for small numbers of hard links since they will
6035 * be packed into one item. Extended refs will kick in if we
6036 * add more hard links than can fit in the ref item.
6037 */
6038 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006039 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006040 key[1].offset = ref_objectid;
6041
6042 sizes[1] = name_len + sizeof(*ref);
6043 }
Chris Mason9c583092008-01-29 15:15:18 -05006044
Chris Masonb0d5d102014-09-08 13:08:51 -07006045 location = &BTRFS_I(inode)->location;
6046 location->objectid = objectid;
6047 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006048 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006049
6050 ret = btrfs_insert_inode_locked(inode);
6051 if (ret < 0)
6052 goto fail;
6053
Chris Masonb9473432009-03-13 11:00:37 -04006054 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006055 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006056 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006057 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006058
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006059 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006060 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306061
6062 inode->i_mtime = CURRENT_TIME;
6063 inode->i_atime = inode->i_mtime;
6064 inode->i_ctime = inode->i_mtime;
6065 BTRFS_I(inode)->i_otime = inode->i_mtime;
6066
Chris Mason5f39d392007-10-15 16:14:19 -04006067 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6068 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06006069 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
6070 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006071 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006072
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006073 if (name) {
6074 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6075 struct btrfs_inode_ref);
6076 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6077 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6078 ptr = (unsigned long)(ref + 1);
6079 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6080 }
Chris Mason9c583092008-01-29 15:15:18 -05006081
Chris Mason5f39d392007-10-15 16:14:19 -04006082 btrfs_mark_buffer_dirty(path->nodes[0]);
6083 btrfs_free_path(path);
6084
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006085 btrfs_inherit_iflags(inode, dir);
6086
Al Viro569254b2011-07-24 17:08:40 -04006087 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04006088 if (btrfs_test_opt(root, NODATASUM))
6089 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06006090 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006091 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6092 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006093 }
6094
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006095 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006096
6097 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006098 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006099
Alexander Block8ea05e32012-07-25 17:35:53 +02006100 btrfs_update_root_times(trans, root);
6101
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006102 ret = btrfs_inode_inherit_props(trans, inode, dir);
6103 if (ret)
6104 btrfs_err(root->fs_info,
6105 "error inheriting props for ino %llu (root %llu): %d",
6106 btrfs_ino(inode), root->root_key.objectid, ret);
6107
Chris Mason39279cc2007-06-12 06:35:45 -04006108 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006109
6110fail_unlock:
6111 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006112fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006113 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006114 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006115 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006116 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006117 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006118}
6119
6120static inline u8 btrfs_inode_type(struct inode *inode)
6121{
6122 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6123}
6124
Chris Masond352ac62008-09-29 15:18:18 -04006125/*
6126 * utility function to add 'inode' into 'parent_inode' with
6127 * a give name and a given sequence number.
6128 * if 'add_backref' is true, also insert a backref from the
6129 * inode to the parent directory.
6130 */
Chris Masone02119d2008-09-05 16:13:11 -04006131int btrfs_add_link(struct btrfs_trans_handle *trans,
6132 struct inode *parent_inode, struct inode *inode,
6133 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006134{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006135 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006136 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006137 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006138 u64 ino = btrfs_ino(inode);
6139 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006140
Li Zefan33345d012011-04-20 10:31:50 +08006141 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006142 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6143 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006144 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006145 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006146 key.offset = 0;
6147 }
Chris Mason39279cc2007-06-12 06:35:45 -04006148
Li Zefan33345d012011-04-20 10:31:50 +08006149 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006150 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
6151 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08006152 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006153 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006154 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6155 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006156 }
6157
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006158 /* Nothing to clean up yet */
6159 if (ret)
6160 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006161
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006162 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6163 parent_inode, &key,
6164 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006165 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006166 goto fail_dir_item;
6167 else if (ret) {
6168 btrfs_abort_transaction(trans, root, ret);
6169 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006170 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006171
6172 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6173 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006174 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006175 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
6176 ret = btrfs_update_inode(trans, root, parent_inode);
6177 if (ret)
6178 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006179 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006180
6181fail_dir_item:
6182 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6183 u64 local_index;
6184 int err;
6185 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6186 key.objectid, root->root_key.objectid,
6187 parent_ino, &local_index, name, name_len);
6188
6189 } else if (add_backref) {
6190 u64 local_index;
6191 int err;
6192
6193 err = btrfs_del_inode_ref(trans, root, name, name_len,
6194 ino, parent_ino, &local_index);
6195 }
6196 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006197}
6198
6199static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006200 struct inode *dir, struct dentry *dentry,
6201 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006202{
Josef Bacika1b075d2010-11-19 20:36:11 +00006203 int err = btrfs_add_link(trans, dir, inode,
6204 dentry->d_name.name, dentry->d_name.len,
6205 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006206 if (err > 0)
6207 err = -EEXIST;
6208 return err;
6209}
6210
Josef Bacik618e21d2007-07-11 10:18:17 -04006211static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006212 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006213{
6214 struct btrfs_trans_handle *trans;
6215 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006216 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006217 int err;
6218 int drop_inode = 0;
6219 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006220 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006221
6222 if (!new_valid_dev(rdev))
6223 return -EINVAL;
6224
Josef Bacik9ed74f22009-09-11 16:12:44 -04006225 /*
6226 * 2 for inode item and ref
6227 * 2 for dir items
6228 * 1 for xattr if selinux is on
6229 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006230 trans = btrfs_start_transaction(root, 5);
6231 if (IS_ERR(trans))
6232 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006233
Li Zefan581bb052011-04-20 10:06:11 +08006234 err = btrfs_find_free_ino(root, &objectid);
6235 if (err)
6236 goto out_unlock;
6237
Josef Bacikaec74772008-07-24 12:12:38 -04006238 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006239 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006240 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006241 if (IS_ERR(inode)) {
6242 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006243 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006244 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006245
Casey Schauflerad19db72011-12-15 10:09:07 -05006246 /*
6247 * If the active LSM wants to access the inode during
6248 * d_instantiate it needs these. Smack checks to see
6249 * if the filesystem supports xattrs by looking at the
6250 * ops vector.
6251 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006252 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006253 init_special_inode(inode, inode->i_mode, rdev);
6254
6255 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006256 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006257 goto out_unlock_inode;
6258
6259 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6260 if (err) {
6261 goto out_unlock_inode;
6262 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006263 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006264 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006265 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006266 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006267
Josef Bacik618e21d2007-07-11 10:18:17 -04006268out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006269 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006270 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006271 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006272 if (drop_inode) {
6273 inode_dec_link_count(inode);
6274 iput(inode);
6275 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006276 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006277
6278out_unlock_inode:
6279 drop_inode = 1;
6280 unlock_new_inode(inode);
6281 goto out_unlock;
6282
Josef Bacik618e21d2007-07-11 10:18:17 -04006283}
6284
Chris Mason39279cc2007-06-12 06:35:45 -04006285static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006286 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006287{
6288 struct btrfs_trans_handle *trans;
6289 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006290 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006291 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006292 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006293 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006294 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006295
Josef Bacik9ed74f22009-09-11 16:12:44 -04006296 /*
6297 * 2 for inode item and ref
6298 * 2 for dir items
6299 * 1 for xattr if selinux is on
6300 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006301 trans = btrfs_start_transaction(root, 5);
6302 if (IS_ERR(trans))
6303 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006304
Li Zefan581bb052011-04-20 10:06:11 +08006305 err = btrfs_find_free_ino(root, &objectid);
6306 if (err)
6307 goto out_unlock;
6308
Josef Bacikaec74772008-07-24 12:12:38 -04006309 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006310 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006311 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006312 if (IS_ERR(inode)) {
6313 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006314 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006315 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006316 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006317 /*
6318 * If the active LSM wants to access the inode during
6319 * d_instantiate it needs these. Smack checks to see
6320 * if the filesystem supports xattrs by looking at the
6321 * ops vector.
6322 */
6323 inode->i_fop = &btrfs_file_operations;
6324 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006325 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006326
6327 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6328 if (err)
6329 goto out_unlock_inode;
6330
6331 err = btrfs_update_inode(trans, root, inode);
6332 if (err)
6333 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006334
Josef Bacika1b075d2010-11-19 20:36:11 +00006335 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006336 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006337 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006338
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006339 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006340 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006341 d_instantiate(dentry, inode);
6342
Chris Mason39279cc2007-06-12 06:35:45 -04006343out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006344 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006345 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006346 inode_dec_link_count(inode);
6347 iput(inode);
6348 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006349 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006350 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006351 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006352
6353out_unlock_inode:
6354 unlock_new_inode(inode);
6355 goto out_unlock;
6356
Chris Mason39279cc2007-06-12 06:35:45 -04006357}
6358
6359static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6360 struct dentry *dentry)
6361{
6362 struct btrfs_trans_handle *trans;
6363 struct btrfs_root *root = BTRFS_I(dir)->root;
6364 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006365 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006366 int err;
6367 int drop_inode = 0;
6368
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006369 /* do not allow sys_link's with other subvols of the same device */
6370 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006371 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006372
Mark Fashehf1863732012-08-08 11:32:27 -07006373 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006374 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006375
Chris Mason3de45862008-11-17 21:02:50 -05006376 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006377 if (err)
6378 goto fail;
6379
Yan, Zhenga22285a2010-05-16 10:48:46 -04006380 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006381 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006382 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006383 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006384 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006385 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006386 if (IS_ERR(trans)) {
6387 err = PTR_ERR(trans);
6388 goto fail;
6389 }
Chris Mason5f39d392007-10-15 16:14:19 -04006390
Miao Xie67de1172013-12-26 13:07:06 +08006391 /* There are several dir indexes for this inode, clear the cache. */
6392 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006393 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006394 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006395 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006396 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006397 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006398
Josef Bacika1b075d2010-11-19 20:36:11 +00006399 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006400
Yan, Zhenga5719522009-09-24 09:17:31 -04006401 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006402 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006403 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006404 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006405 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006406 if (err)
6407 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006408 if (inode->i_nlink == 1) {
6409 /*
6410 * If new hard link count is 1, it's a file created
6411 * with open(2) O_TMPFILE flag.
6412 */
6413 err = btrfs_orphan_del(trans, inode);
6414 if (err)
6415 goto fail;
6416 }
Al Viro08c422c2011-12-23 07:58:13 -05006417 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006418 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006419 }
Chris Mason39279cc2007-06-12 06:35:45 -04006420
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006421 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006422 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006423fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006424 if (drop_inode) {
6425 inode_dec_link_count(inode);
6426 iput(inode);
6427 }
Liu Bob53d3f52012-11-14 14:34:34 +00006428 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006429 return err;
6430}
6431
Al Viro18bb1db2011-07-26 01:41:39 -04006432static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006433{
Chris Masonb9d86662008-05-02 16:13:49 -04006434 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006435 struct btrfs_trans_handle *trans;
6436 struct btrfs_root *root = BTRFS_I(dir)->root;
6437 int err = 0;
6438 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006439 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006440 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006441
Josef Bacik9ed74f22009-09-11 16:12:44 -04006442 /*
6443 * 2 items for inode and ref
6444 * 2 items for dir items
6445 * 1 for xattr if selinux is on
6446 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006447 trans = btrfs_start_transaction(root, 5);
6448 if (IS_ERR(trans))
6449 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006450
Li Zefan581bb052011-04-20 10:06:11 +08006451 err = btrfs_find_free_ino(root, &objectid);
6452 if (err)
6453 goto out_fail;
6454
Josef Bacikaec74772008-07-24 12:12:38 -04006455 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006456 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006457 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006458 if (IS_ERR(inode)) {
6459 err = PTR_ERR(inode);
6460 goto out_fail;
6461 }
Chris Mason5f39d392007-10-15 16:14:19 -04006462
Chris Mason39279cc2007-06-12 06:35:45 -04006463 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006464 /* these must be set before we unlock the inode */
6465 inode->i_op = &btrfs_dir_inode_operations;
6466 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006467
Eric Paris2a7dba32011-02-01 11:05:39 -05006468 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006469 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006470 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006471
Chris Masondbe674a2008-07-17 12:54:05 -04006472 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006473 err = btrfs_update_inode(trans, root, inode);
6474 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006475 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006476
Josef Bacika1b075d2010-11-19 20:36:11 +00006477 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6478 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006479 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006480 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006481
Chris Mason39279cc2007-06-12 06:35:45 -04006482 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006483 /*
6484 * mkdir is special. We're unlocking after we call d_instantiate
6485 * to avoid a race with nfsd calling d_instantiate.
6486 */
6487 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006488 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006489
6490out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006491 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006492 if (drop_on_err) {
6493 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006494 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006495 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006496 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006497 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006498 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006499
6500out_fail_inode:
6501 unlock_new_inode(inode);
6502 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006503}
6504
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006505/* Find next extent map of a given extent map, caller needs to ensure locks */
6506static struct extent_map *next_extent_map(struct extent_map *em)
6507{
6508 struct rb_node *next;
6509
6510 next = rb_next(&em->rb_node);
6511 if (!next)
6512 return NULL;
6513 return container_of(next, struct extent_map, rb_node);
6514}
6515
6516static struct extent_map *prev_extent_map(struct extent_map *em)
6517{
6518 struct rb_node *prev;
6519
6520 prev = rb_prev(&em->rb_node);
6521 if (!prev)
6522 return NULL;
6523 return container_of(prev, struct extent_map, rb_node);
6524}
6525
Chris Masond352ac62008-09-29 15:18:18 -04006526/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006527 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006528 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006529 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006530 */
Chris Mason3b951512008-04-17 11:29:12 -04006531static int merge_extent_mapping(struct extent_map_tree *em_tree,
6532 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006533 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006534 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006535{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006536 struct extent_map *prev;
6537 struct extent_map *next;
6538 u64 start;
6539 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006540 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006541
Chris Masone6dcd2d2008-07-17 12:53:50 -04006542 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006543
6544 if (existing->start > map_start) {
6545 next = existing;
6546 prev = prev_extent_map(next);
6547 } else {
6548 prev = existing;
6549 next = next_extent_map(prev);
6550 }
6551
6552 start = prev ? extent_map_end(prev) : em->start;
6553 start = max_t(u64, start, em->start);
6554 end = next ? next->start : extent_map_end(em);
6555 end = min_t(u64, end, extent_map_end(em));
6556 start_diff = start - em->start;
6557 em->start = start;
6558 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006559 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6560 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006561 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006562 em->block_len -= start_diff;
6563 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006564 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006565}
6566
Chris Masonc8b97812008-10-29 14:49:59 -04006567static noinline int uncompress_inline(struct btrfs_path *path,
6568 struct inode *inode, struct page *page,
6569 size_t pg_offset, u64 extent_offset,
6570 struct btrfs_file_extent_item *item)
6571{
6572 int ret;
6573 struct extent_buffer *leaf = path->nodes[0];
6574 char *tmp;
6575 size_t max_size;
6576 unsigned long inline_size;
6577 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006578 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006579
6580 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006581 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006582 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6583 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006584 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006585 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006586 if (!tmp)
6587 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006588 ptr = btrfs_file_extent_inline_start(item);
6589
6590 read_extent_buffer(leaf, tmp, ptr, inline_size);
6591
Chris Mason5b050f02008-11-11 09:34:41 -05006592 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006593 ret = btrfs_decompress(compress_type, tmp, page,
6594 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006595 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006596 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006597}
6598
Chris Masond352ac62008-09-29 15:18:18 -04006599/*
6600 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006601 * the ugly parts come from merging extents from the disk with the in-ram
6602 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006603 * where the in-ram extents might be locked pending data=ordered completion.
6604 *
6605 * This also copies inline extents directly into the page.
6606 */
Chris Masond3977122009-01-05 21:25:51 -05006607
Chris Masona52d9a82007-08-27 16:49:44 -04006608struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006609 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006610 int create)
6611{
6612 int ret;
6613 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006614 u64 extent_start = 0;
6615 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006616 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006617 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006618 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006619 struct btrfs_root *root = BTRFS_I(inode)->root;
6620 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006621 struct extent_buffer *leaf;
6622 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006623 struct extent_map *em = NULL;
6624 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006625 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006626 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006627 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006628
Chris Masona52d9a82007-08-27 16:49:44 -04006629again:
Chris Mason890871b2009-09-02 16:24:52 -04006630 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006631 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006632 if (em)
6633 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006634 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006635
Chris Masona52d9a82007-08-27 16:49:44 -04006636 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006637 if (em->start > start || em->start + em->len <= start)
6638 free_extent_map(em);
6639 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006640 free_extent_map(em);
6641 else
6642 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006643 }
David Sterba172ddd62011-04-21 00:48:27 +02006644 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006645 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006646 err = -ENOMEM;
6647 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006648 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006649 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006650 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006651 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006652 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006653 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006654
6655 if (!path) {
6656 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006657 if (!path) {
6658 err = -ENOMEM;
6659 goto out;
6660 }
6661 /*
6662 * Chances are we'll be called again, so go ahead and do
6663 * readahead
6664 */
6665 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006666 }
6667
Chris Mason179e29e2007-11-01 11:28:41 -04006668 ret = btrfs_lookup_file_extent(trans, root, path,
6669 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006670 if (ret < 0) {
6671 err = ret;
6672 goto out;
6673 }
6674
6675 if (ret != 0) {
6676 if (path->slots[0] == 0)
6677 goto not_found;
6678 path->slots[0]--;
6679 }
6680
Chris Mason5f39d392007-10-15 16:14:19 -04006681 leaf = path->nodes[0];
6682 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006683 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006684 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006685 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006686 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006687 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006688 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006689 /*
6690 * If we backup past the first extent we want to move forward
6691 * and see if there is an extent in front of us, otherwise we'll
6692 * say there is a hole for our whole search range which can
6693 * cause problems.
6694 */
6695 extent_end = start;
6696 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006697 }
6698
Chris Mason5f39d392007-10-15 16:14:19 -04006699 found_type = btrfs_file_extent_type(leaf, item);
6700 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006701 if (found_type == BTRFS_FILE_EXTENT_REG ||
6702 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006703 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006704 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006705 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6706 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006707 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006708 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006709 }
Josef Bacik25a50342013-10-14 12:08:38 -04006710next:
Yan Zheng9036c102008-10-30 14:19:41 -04006711 if (start >= extent_end) {
6712 path->slots[0]++;
6713 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6714 ret = btrfs_next_leaf(root, path);
6715 if (ret < 0) {
6716 err = ret;
6717 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006718 }
Yan Zheng9036c102008-10-30 14:19:41 -04006719 if (ret > 0)
6720 goto not_found;
6721 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006722 }
Yan Zheng9036c102008-10-30 14:19:41 -04006723 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6724 if (found_key.objectid != objectid ||
6725 found_key.type != BTRFS_EXTENT_DATA_KEY)
6726 goto not_found;
6727 if (start + len <= found_key.offset)
6728 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006729 if (start > found_key.offset)
6730 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006731 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006732 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006733 em->len = found_key.offset - start;
6734 goto not_found_em;
6735 }
6736
Filipe Manana7ffbb592014-06-09 03:48:05 +01006737 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6738
Yan Zhengd899e052008-10-30 14:25:28 -04006739 if (found_type == BTRFS_FILE_EXTENT_REG ||
6740 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006741 goto insert;
6742 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006743 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006744 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006745 size_t size;
6746 size_t extent_offset;
6747 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006748
Filipe Manana7ffbb592014-06-09 03:48:05 +01006749 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006750 goto out;
Yan689f9342007-10-29 11:41:07 -04006751
Chris Mason514ac8a2014-01-03 21:07:00 -08006752 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006753 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006754 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006755 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006756 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006757 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006758 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006759 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006760 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006761 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006762 if (btrfs_file_extent_compression(leaf, item) !=
6763 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006764 ret = uncompress_inline(path, inode, page,
6765 pg_offset,
6766 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006767 if (ret) {
6768 err = ret;
6769 goto out;
6770 }
Chris Masonc8b97812008-10-29 14:49:59 -04006771 } else {
6772 map = kmap(page);
6773 read_extent_buffer(leaf, map + pg_offset, ptr,
6774 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006775 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6776 memset(map + pg_offset + copy_size, 0,
6777 PAGE_CACHE_SIZE - pg_offset -
6778 copy_size);
6779 }
Chris Masonc8b97812008-10-29 14:49:59 -04006780 kunmap(page);
6781 }
Chris Mason179e29e2007-11-01 11:28:41 -04006782 flush_dcache_page(page);
6783 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006784 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006785 if (!trans) {
6786 kunmap(page);
6787 free_extent_map(em);
6788 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006789
David Sterbab3b4aa72011-04-21 01:20:15 +02006790 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006791 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006792
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006793 if (IS_ERR(trans))
6794 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006795 goto again;
6796 }
Chris Masonc8b97812008-10-29 14:49:59 -04006797 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006798 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006799 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006800 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006801 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006802 }
Chris Masond1310b22008-01-24 16:13:08 -05006803 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006804 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006805 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006806 }
6807not_found:
6808 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006809 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006810 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006811not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006812 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006813 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006814insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006815 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006816 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006817 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006818 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006819 err = -EIO;
6820 goto out;
6821 }
Chris Masond1310b22008-01-24 16:13:08 -05006822
6823 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006824 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006825 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006826 /* it is possible that someone inserted the extent into the tree
6827 * while we had the lock dropped. It is also possible that
6828 * an overlapping map exists in the tree
6829 */
Chris Masona52d9a82007-08-27 16:49:44 -04006830 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006831 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006832
6833 ret = 0;
6834
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006835 existing = search_extent_mapping(em_tree, start, len);
6836 /*
6837 * existing will always be non-NULL, since there must be
6838 * extent causing the -EEXIST.
6839 */
6840 if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006841 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006842 /*
6843 * The existing extent map is the one nearest to
6844 * the [start, start + len) range which overlaps
6845 */
6846 err = merge_extent_mapping(em_tree, existing,
6847 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006848 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006849 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006850 free_extent_map(em);
6851 em = NULL;
6852 }
6853 } else {
6854 free_extent_map(em);
6855 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006856 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006857 }
Chris Masona52d9a82007-08-27 16:49:44 -04006858 }
Chris Mason890871b2009-09-02 16:24:52 -04006859 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006860out:
liubo1abe9b82011-03-24 11:18:59 +00006861
Steven Rostedt4cd8587c2013-11-14 22:57:29 -05006862 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006863
Chris Masonf4219502008-07-22 11:18:09 -04006864 if (path)
6865 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006866 if (trans) {
6867 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006868 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006869 err = ret;
6870 }
Chris Masona52d9a82007-08-27 16:49:44 -04006871 if (err) {
6872 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006873 return ERR_PTR(err);
6874 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006875 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006876 return em;
6877}
6878
Chris Masonec29ed52011-02-23 16:23:20 -05006879struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6880 size_t pg_offset, u64 start, u64 len,
6881 int create)
6882{
6883 struct extent_map *em;
6884 struct extent_map *hole_em = NULL;
6885 u64 range_start = start;
6886 u64 end;
6887 u64 found;
6888 u64 found_end;
6889 int err = 0;
6890
6891 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6892 if (IS_ERR(em))
6893 return em;
6894 if (em) {
6895 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006896 * if our em maps to
6897 * - a hole or
6898 * - a pre-alloc extent,
6899 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006900 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006901 if (em->block_start != EXTENT_MAP_HOLE &&
6902 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006903 return em;
6904 else
6905 hole_em = em;
6906 }
6907
6908 /* check to see if we've wrapped (len == -1 or similar) */
6909 end = start + len;
6910 if (end < start)
6911 end = (u64)-1;
6912 else
6913 end -= 1;
6914
6915 em = NULL;
6916
6917 /* ok, we didn't find anything, lets look for delalloc */
6918 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6919 end, len, EXTENT_DELALLOC, 1);
6920 found_end = range_start + found;
6921 if (found_end < range_start)
6922 found_end = (u64)-1;
6923
6924 /*
6925 * we didn't find anything useful, return
6926 * the original results from get_extent()
6927 */
6928 if (range_start > end || found_end <= start) {
6929 em = hole_em;
6930 hole_em = NULL;
6931 goto out;
6932 }
6933
6934 /* adjust the range_start to make sure it doesn't
6935 * go backwards from the start they passed in
6936 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306937 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006938 found = found_end - range_start;
6939
6940 if (found > 0) {
6941 u64 hole_start = start;
6942 u64 hole_len = len;
6943
David Sterba172ddd62011-04-21 00:48:27 +02006944 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006945 if (!em) {
6946 err = -ENOMEM;
6947 goto out;
6948 }
6949 /*
6950 * when btrfs_get_extent can't find anything it
6951 * returns one huge hole
6952 *
6953 * make sure what it found really fits our range, and
6954 * adjust to make sure it is based on the start from
6955 * the caller
6956 */
6957 if (hole_em) {
6958 u64 calc_end = extent_map_end(hole_em);
6959
6960 if (calc_end <= start || (hole_em->start > end)) {
6961 free_extent_map(hole_em);
6962 hole_em = NULL;
6963 } else {
6964 hole_start = max(hole_em->start, start);
6965 hole_len = calc_end - hole_start;
6966 }
6967 }
6968 em->bdev = NULL;
6969 if (hole_em && range_start > hole_start) {
6970 /* our hole starts before our delalloc, so we
6971 * have to return just the parts of the hole
6972 * that go until the delalloc starts
6973 */
6974 em->len = min(hole_len,
6975 range_start - hole_start);
6976 em->start = hole_start;
6977 em->orig_start = hole_start;
6978 /*
6979 * don't adjust block start at all,
6980 * it is fixed at EXTENT_MAP_HOLE
6981 */
6982 em->block_start = hole_em->block_start;
6983 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006984 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6985 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006986 } else {
6987 em->start = range_start;
6988 em->len = found;
6989 em->orig_start = range_start;
6990 em->block_start = EXTENT_MAP_DELALLOC;
6991 em->block_len = found;
6992 }
6993 } else if (hole_em) {
6994 return hole_em;
6995 }
6996out:
6997
6998 free_extent_map(hole_em);
6999 if (err) {
7000 free_extent_map(em);
7001 return ERR_PTR(err);
7002 }
7003 return em;
7004}
7005
Josef Bacik4b46fce2010-05-23 11:00:55 -04007006static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7007 u64 start, u64 len)
7008{
7009 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007010 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007011 struct btrfs_key ins;
7012 u64 alloc_hint;
7013 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007014
Josef Bacik4b46fce2010-05-23 11:00:55 -04007015 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04007016 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08007017 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007018 if (ret)
7019 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007020
Josef Bacik70c8a912012-10-11 16:54:30 -04007021 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007022 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04007023 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08007024 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007025 return em;
7026 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007027
7028 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
7029 ins.offset, ins.offset, 0);
7030 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08007031 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007032 free_extent_map(em);
7033 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007034 }
Josef Bacik00361582013-08-14 14:02:47 -04007035
Josef Bacik4b46fce2010-05-23 11:00:55 -04007036 return em;
7037}
7038
Chris Mason46bfbb52010-05-26 11:04:10 -04007039/*
7040 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7041 * block must be cow'd
7042 */
Josef Bacik00361582013-08-14 14:02:47 -04007043noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007044 u64 *orig_start, u64 *orig_block_len,
7045 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007046{
Josef Bacik00361582013-08-14 14:02:47 -04007047 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007048 struct btrfs_path *path;
7049 int ret;
7050 struct extent_buffer *leaf;
7051 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007052 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007053 struct btrfs_file_extent_item *fi;
7054 struct btrfs_key key;
7055 u64 disk_bytenr;
7056 u64 backref_offset;
7057 u64 extent_end;
7058 u64 num_bytes;
7059 int slot;
7060 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007061 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007062
Chris Mason46bfbb52010-05-26 11:04:10 -04007063 path = btrfs_alloc_path();
7064 if (!path)
7065 return -ENOMEM;
7066
Josef Bacik00361582013-08-14 14:02:47 -04007067 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007068 offset, 0);
7069 if (ret < 0)
7070 goto out;
7071
7072 slot = path->slots[0];
7073 if (ret == 1) {
7074 if (slot == 0) {
7075 /* can't find the item, must cow */
7076 ret = 0;
7077 goto out;
7078 }
7079 slot--;
7080 }
7081 ret = 0;
7082 leaf = path->nodes[0];
7083 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007084 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007085 key.type != BTRFS_EXTENT_DATA_KEY) {
7086 /* not our file or wrong item type, must cow */
7087 goto out;
7088 }
7089
7090 if (key.offset > offset) {
7091 /* Wrong offset, must cow */
7092 goto out;
7093 }
7094
7095 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7096 found_type = btrfs_file_extent_type(leaf, fi);
7097 if (found_type != BTRFS_FILE_EXTENT_REG &&
7098 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7099 /* not a regular extent, must cow */
7100 goto out;
7101 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007102
7103 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7104 goto out;
7105
Miao Xiee77751a2013-12-27 21:11:50 +08007106 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7107 if (extent_end <= offset)
7108 goto out;
7109
Chris Mason46bfbb52010-05-26 11:04:10 -04007110 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007111 if (disk_bytenr == 0)
7112 goto out;
7113
7114 if (btrfs_file_extent_compression(leaf, fi) ||
7115 btrfs_file_extent_encryption(leaf, fi) ||
7116 btrfs_file_extent_other_encoding(leaf, fi))
7117 goto out;
7118
Chris Mason46bfbb52010-05-26 11:04:10 -04007119 backref_offset = btrfs_file_extent_offset(leaf, fi);
7120
Josef Bacik7ee9e442013-06-21 16:37:03 -04007121 if (orig_start) {
7122 *orig_start = key.offset - backref_offset;
7123 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7124 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7125 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007126
Chris Mason46bfbb52010-05-26 11:04:10 -04007127 if (btrfs_extent_readonly(root, disk_bytenr))
7128 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007129
7130 num_bytes = min(offset + *len, extent_end) - offset;
7131 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7132 u64 range_end;
7133
7134 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
7135 ret = test_range_bit(io_tree, offset, range_end,
7136 EXTENT_DELALLOC, 0, NULL);
7137 if (ret) {
7138 ret = -EAGAIN;
7139 goto out;
7140 }
7141 }
7142
Josef Bacik1bda19e2013-10-18 12:10:36 -04007143 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007144
7145 /*
7146 * look for other files referencing this extent, if we
7147 * find any we must cow
7148 */
Josef Bacik00361582013-08-14 14:02:47 -04007149 trans = btrfs_join_transaction(root);
7150 if (IS_ERR(trans)) {
7151 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007152 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007153 }
7154
7155 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7156 key.offset - backref_offset, disk_bytenr);
7157 btrfs_end_transaction(trans, root);
7158 if (ret) {
7159 ret = 0;
7160 goto out;
7161 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007162
7163 /*
7164 * adjust disk_bytenr and num_bytes to cover just the bytes
7165 * in this extent we are about to write. If there
7166 * are any csums in that range we have to cow in order
7167 * to keep the csums correct
7168 */
7169 disk_bytenr += backref_offset;
7170 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04007171 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7172 goto out;
7173 /*
7174 * all of the above have passed, it is safe to overwrite this extent
7175 * without cow
7176 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007177 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007178 ret = 1;
7179out:
7180 btrfs_free_path(path);
7181 return ret;
7182}
7183
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007184bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7185{
7186 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7187 int found = false;
7188 void **pagep = NULL;
7189 struct page *page = NULL;
7190 int start_idx;
7191 int end_idx;
7192
7193 start_idx = start >> PAGE_CACHE_SHIFT;
7194
7195 /*
7196 * end is the last byte in the last page. end == start is legal
7197 */
7198 end_idx = end >> PAGE_CACHE_SHIFT;
7199
7200 rcu_read_lock();
7201
7202 /* Most of the code in this while loop is lifted from
7203 * find_get_page. It's been modified to begin searching from a
7204 * page and return just the first page found in that range. If the
7205 * found idx is less than or equal to the end idx then we know that
7206 * a page exists. If no pages are found or if those pages are
7207 * outside of the range then we're fine (yay!) */
7208 while (page == NULL &&
7209 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7210 page = radix_tree_deref_slot(pagep);
7211 if (unlikely(!page))
7212 break;
7213
7214 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007215 if (radix_tree_deref_retry(page)) {
7216 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007217 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007218 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007219 /*
7220 * Otherwise, shmem/tmpfs must be storing a swap entry
7221 * here as an exceptional entry: so return it without
7222 * attempting to raise page count.
7223 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007224 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007225 break; /* TODO: Is this relevant for this use case? */
7226 }
7227
Filipe Manana91405152014-06-05 13:22:24 +01007228 if (!page_cache_get_speculative(page)) {
7229 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007230 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007231 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007232
7233 /*
7234 * Has the page moved?
7235 * This is part of the lockless pagecache protocol. See
7236 * include/linux/pagemap.h for details.
7237 */
7238 if (unlikely(page != *pagep)) {
7239 page_cache_release(page);
7240 page = NULL;
7241 }
7242 }
7243
7244 if (page) {
7245 if (page->index <= end_idx)
7246 found = true;
7247 page_cache_release(page);
7248 }
7249
7250 rcu_read_unlock();
7251 return found;
7252}
7253
Josef Bacikeb838e72012-07-31 16:28:48 -04007254static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7255 struct extent_state **cached_state, int writing)
7256{
7257 struct btrfs_ordered_extent *ordered;
7258 int ret = 0;
7259
7260 while (1) {
7261 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7262 0, cached_state);
7263 /*
7264 * We're concerned with the entire range that we're going to be
7265 * doing DIO to, so we need to make sure theres no ordered
7266 * extents in this range.
7267 */
7268 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7269 lockend - lockstart + 1);
7270
7271 /*
7272 * We need to make sure there are no buffered pages in this
7273 * range either, we could have raced between the invalidate in
7274 * generic_file_direct_write and locking the extent. The
7275 * invalidate needs to happen so that reads after a write do not
7276 * get stale data.
7277 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007278 if (!ordered &&
7279 (!writing ||
7280 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007281 break;
7282
7283 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7284 cached_state, GFP_NOFS);
7285
7286 if (ordered) {
7287 btrfs_start_ordered_extent(inode, ordered, 1);
7288 btrfs_put_ordered_extent(ordered);
7289 } else {
7290 /* Screw you mmap */
Filipe Manana728404d2014-10-10 09:43:11 +01007291 ret = btrfs_fdatawrite_range(inode, lockstart, lockend);
Filipe Manana075bdbd2014-10-09 21:18:55 +01007292 if (ret)
7293 break;
7294 ret = filemap_fdatawait_range(inode->i_mapping,
7295 lockstart,
7296 lockend);
Josef Bacikeb838e72012-07-31 16:28:48 -04007297 if (ret)
7298 break;
7299
7300 /*
7301 * If we found a page that couldn't be invalidated just
7302 * fall back to buffered.
7303 */
7304 ret = invalidate_inode_pages2_range(inode->i_mapping,
7305 lockstart >> PAGE_CACHE_SHIFT,
7306 lockend >> PAGE_CACHE_SHIFT);
7307 if (ret)
7308 break;
7309 }
7310
7311 cond_resched();
7312 }
7313
7314 return ret;
7315}
7316
Josef Bacik69ffb542012-09-11 15:40:07 -04007317static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7318 u64 len, u64 orig_start,
7319 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007320 u64 orig_block_len, u64 ram_bytes,
7321 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007322{
7323 struct extent_map_tree *em_tree;
7324 struct extent_map *em;
7325 struct btrfs_root *root = BTRFS_I(inode)->root;
7326 int ret;
7327
7328 em_tree = &BTRFS_I(inode)->extent_tree;
7329 em = alloc_extent_map();
7330 if (!em)
7331 return ERR_PTR(-ENOMEM);
7332
7333 em->start = start;
7334 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007335 em->mod_start = start;
7336 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007337 em->len = len;
7338 em->block_len = block_len;
7339 em->block_start = block_start;
7340 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007341 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007342 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007343 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007344 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7345 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007346 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007347
7348 do {
7349 btrfs_drop_extent_cache(inode, em->start,
7350 em->start + em->len - 1, 0);
7351 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007352 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007353 write_unlock(&em_tree->lock);
7354 } while (ret == -EEXIST);
7355
7356 if (ret) {
7357 free_extent_map(em);
7358 return ERR_PTR(ret);
7359 }
7360
7361 return em;
7362}
7363
7364
Josef Bacik4b46fce2010-05-23 11:00:55 -04007365static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7366 struct buffer_head *bh_result, int create)
7367{
7368 struct extent_map *em;
7369 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007370 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007371 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007372 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007373 u64 len = bh_result->b_size;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007374 u64 *outstanding_extents = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007375 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007376 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007377
Miao Xie172a5042013-02-21 02:48:22 -07007378 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007379 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007380 else
Josef Bacikc3298612012-08-03 16:49:19 -04007381 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007382
Josef Bacikc3298612012-08-03 16:49:19 -04007383 lockstart = start;
7384 lockend = start + len - 1;
7385
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007386 if (current->journal_info) {
7387 /*
7388 * Need to pull our outstanding extents and set journal_info to NULL so
7389 * that anything that needs to check if there's a transction doesn't get
7390 * confused.
7391 */
7392 outstanding_extents = current->journal_info;
7393 current->journal_info = NULL;
7394 }
7395
Josef Bacikeb838e72012-07-31 16:28:48 -04007396 /*
7397 * If this errors out it's because we couldn't invalidate pagecache for
7398 * this range and we need to fallback to buffered.
7399 */
7400 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7401 return -ENOTBLK;
7402
Josef Bacik4b46fce2010-05-23 11:00:55 -04007403 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007404 if (IS_ERR(em)) {
7405 ret = PTR_ERR(em);
7406 goto unlock_err;
7407 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007408
7409 /*
7410 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7411 * io. INLINE is special, and we could probably kludge it in here, but
7412 * it's still buffered so for safety lets just fall back to the generic
7413 * buffered path.
7414 *
7415 * For COMPRESSED we _have_ to read the entire extent in so we can
7416 * decompress it, so there will be buffering required no matter what we
7417 * do, so go ahead and fallback to buffered.
7418 *
7419 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7420 * to buffered IO. Don't blame me, this is the price we pay for using
7421 * the generic code.
7422 */
7423 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7424 em->block_start == EXTENT_MAP_INLINE) {
7425 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007426 ret = -ENOTBLK;
7427 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007428 }
7429
7430 /* Just a good old fashioned hole, return */
7431 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7432 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7433 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007434 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007435 }
7436
7437 /*
7438 * We don't allocate a new extent in the following cases
7439 *
7440 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7441 * existing extent.
7442 * 2) The extent is marked as PREALLOC. We're good to go here and can
7443 * just use the extent.
7444 *
7445 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007446 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007447 len = min(len, em->len - (start - em->start));
7448 lockstart = start + len;
7449 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007450 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007451
7452 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7453 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7454 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007455 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007456 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007457
7458 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7459 type = BTRFS_ORDERED_PREALLOC;
7460 else
7461 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007462 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007463 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007464
Josef Bacik00361582013-08-14 14:02:47 -04007465 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007466 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007467 if (type == BTRFS_ORDERED_PREALLOC) {
7468 free_extent_map(em);
7469 em = create_pinned_em(inode, start, len,
7470 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007471 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007472 orig_block_len,
7473 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007474 if (IS_ERR(em)) {
7475 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007476 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007477 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007478 }
7479
Chris Mason46bfbb52010-05-26 11:04:10 -04007480 ret = btrfs_add_ordered_extent_dio(inode, start,
7481 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007482 if (ret) {
7483 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007484 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007485 }
7486 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007487 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007488 }
Josef Bacik00361582013-08-14 14:02:47 -04007489
Chris Mason46bfbb52010-05-26 11:04:10 -04007490 /*
7491 * this will cow the extent, reset the len in case we changed
7492 * it above
7493 */
7494 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007495 free_extent_map(em);
7496 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007497 if (IS_ERR(em)) {
7498 ret = PTR_ERR(em);
7499 goto unlock_err;
7500 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007501 len = min(len, em->len - (start - em->start));
7502unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007503 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7504 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007505 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007506 bh_result->b_bdev = em->bdev;
7507 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007508 if (create) {
7509 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7510 set_buffer_new(bh_result);
7511
7512 /*
7513 * Need to update the i_size under the extent lock so buffered
7514 * readers will get the updated i_size when we unlock.
7515 */
7516 if (start + len > i_size_read(inode))
7517 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007518
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007519 /*
7520 * If we have an outstanding_extents count still set then we're
7521 * within our reservation, otherwise we need to adjust our inode
7522 * counter appropriately.
7523 */
7524 if (*outstanding_extents) {
7525 (*outstanding_extents)--;
7526 } else {
Josef Bacik3e05bde2015-02-11 15:08:57 -05007527 spin_lock(&BTRFS_I(inode)->lock);
7528 BTRFS_I(inode)->outstanding_extents++;
7529 spin_unlock(&BTRFS_I(inode)->lock);
7530 }
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007531
7532 current->journal_info = outstanding_extents;
Josef Bacik32667892015-02-11 15:08:58 -05007533 btrfs_free_reserved_data_space(inode, len);
Josef Bacikc3473e82012-06-19 10:59:00 -04007534 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007535
Josef Bacikeb838e72012-07-31 16:28:48 -04007536 /*
7537 * In the case of write we need to clear and unlock the entire range,
7538 * in the case of read we need to unlock only the end area that we
7539 * aren't using if there is any left over space.
7540 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007541 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007542 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7543 lockend, unlock_bits, 1, 0,
7544 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007545 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007546 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007547 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007548
Josef Bacik4b46fce2010-05-23 11:00:55 -04007549 free_extent_map(em);
7550
7551 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007552
7553unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007554 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7555 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007556 if (outstanding_extents)
7557 current->journal_info = outstanding_extents;
Josef Bacikeb838e72012-07-31 16:28:48 -04007558 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007559}
7560
Miao Xie8b110e32014-09-12 18:44:03 +08007561static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7562 int rw, int mirror_num)
7563{
7564 struct btrfs_root *root = BTRFS_I(inode)->root;
7565 int ret;
7566
7567 BUG_ON(rw & REQ_WRITE);
7568
7569 bio_get(bio);
7570
7571 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7572 BTRFS_WQ_ENDIO_DIO_REPAIR);
7573 if (ret)
7574 goto err;
7575
7576 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7577err:
7578 bio_put(bio);
7579 return ret;
7580}
7581
7582static int btrfs_check_dio_repairable(struct inode *inode,
7583 struct bio *failed_bio,
7584 struct io_failure_record *failrec,
7585 int failed_mirror)
7586{
7587 int num_copies;
7588
7589 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7590 failrec->logical, failrec->len);
7591 if (num_copies == 1) {
7592 /*
7593 * we only have a single copy of the data, so don't bother with
7594 * all the retry and error correction code that follows. no
7595 * matter what the error is, it is very likely to persist.
7596 */
7597 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7598 num_copies, failrec->this_mirror, failed_mirror);
7599 return 0;
7600 }
7601
7602 failrec->failed_mirror = failed_mirror;
7603 failrec->this_mirror++;
7604 if (failrec->this_mirror == failed_mirror)
7605 failrec->this_mirror++;
7606
7607 if (failrec->this_mirror > num_copies) {
7608 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7609 num_copies, failrec->this_mirror, failed_mirror);
7610 return 0;
7611 }
7612
7613 return 1;
7614}
7615
7616static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7617 struct page *page, u64 start, u64 end,
7618 int failed_mirror, bio_end_io_t *repair_endio,
7619 void *repair_arg)
7620{
7621 struct io_failure_record *failrec;
7622 struct bio *bio;
7623 int isector;
7624 int read_mode;
7625 int ret;
7626
7627 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7628
7629 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7630 if (ret)
7631 return ret;
7632
7633 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7634 failed_mirror);
7635 if (!ret) {
7636 free_io_failure(inode, failrec);
7637 return -EIO;
7638 }
7639
7640 if (failed_bio->bi_vcnt > 1)
7641 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7642 else
7643 read_mode = READ_SYNC;
7644
7645 isector = start - btrfs_io_bio(failed_bio)->logical;
7646 isector >>= inode->i_sb->s_blocksize_bits;
7647 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7648 0, isector, repair_endio, repair_arg);
7649 if (!bio) {
7650 free_io_failure(inode, failrec);
7651 return -EIO;
7652 }
7653
7654 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7655 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7656 read_mode, failrec->this_mirror, failrec->in_validation);
7657
7658 ret = submit_dio_repair_bio(inode, bio, read_mode,
7659 failrec->this_mirror);
7660 if (ret) {
7661 free_io_failure(inode, failrec);
7662 bio_put(bio);
7663 }
7664
7665 return ret;
7666}
7667
7668struct btrfs_retry_complete {
7669 struct completion done;
7670 struct inode *inode;
7671 u64 start;
7672 int uptodate;
7673};
7674
7675static void btrfs_retry_endio_nocsum(struct bio *bio, int err)
7676{
7677 struct btrfs_retry_complete *done = bio->bi_private;
7678 struct bio_vec *bvec;
7679 int i;
7680
7681 if (err)
7682 goto end;
7683
7684 done->uptodate = 1;
7685 bio_for_each_segment_all(bvec, bio, i)
7686 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7687end:
7688 complete(&done->done);
7689 bio_put(bio);
7690}
7691
7692static int __btrfs_correct_data_nocsum(struct inode *inode,
7693 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007694{
Kent Overstreet2c30c712013-11-07 12:20:26 -08007695 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007696 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007697 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007698 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007699 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007700
Miao Xiec1dc0892014-09-12 18:43:56 +08007701 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007702 done.inode = inode;
7703
7704 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7705try_again:
7706 done.uptodate = 0;
7707 done.start = start;
7708 init_completion(&done.done);
7709
7710 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7711 start + bvec->bv_len - 1,
7712 io_bio->mirror_num,
7713 btrfs_retry_endio_nocsum, &done);
7714 if (ret)
7715 return ret;
7716
7717 wait_for_completion(&done.done);
7718
7719 if (!done.uptodate) {
7720 /* We might have another mirror, so try again */
7721 goto try_again;
7722 }
7723
7724 start += bvec->bv_len;
7725 }
7726
7727 return 0;
7728}
7729
7730static void btrfs_retry_endio(struct bio *bio, int err)
7731{
7732 struct btrfs_retry_complete *done = bio->bi_private;
7733 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7734 struct bio_vec *bvec;
7735 int uptodate;
7736 int ret;
7737 int i;
7738
7739 if (err)
7740 goto end;
7741
7742 uptodate = 1;
7743 bio_for_each_segment_all(bvec, bio, i) {
7744 ret = __readpage_endio_check(done->inode, io_bio, i,
7745 bvec->bv_page, 0,
7746 done->start, bvec->bv_len);
7747 if (!ret)
7748 clean_io_failure(done->inode, done->start,
7749 bvec->bv_page, 0);
7750 else
7751 uptodate = 0;
7752 }
7753
7754 done->uptodate = uptodate;
7755end:
7756 complete(&done->done);
7757 bio_put(bio);
7758}
7759
7760static int __btrfs_subio_endio_read(struct inode *inode,
7761 struct btrfs_io_bio *io_bio, int err)
7762{
7763 struct bio_vec *bvec;
7764 struct btrfs_retry_complete done;
7765 u64 start;
7766 u64 offset = 0;
7767 int i;
7768 int ret;
7769
7770 err = 0;
7771 start = io_bio->logical;
7772 done.inode = inode;
7773
Miao Xiec1dc0892014-09-12 18:43:56 +08007774 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Miao Xiedc380ae2014-09-12 18:43:55 +08007775 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7776 0, start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007777 if (likely(!ret))
7778 goto next;
7779try_again:
7780 done.uptodate = 0;
7781 done.start = start;
7782 init_completion(&done.done);
7783
7784 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7785 start + bvec->bv_len - 1,
7786 io_bio->mirror_num,
7787 btrfs_retry_endio, &done);
7788 if (ret) {
7789 err = ret;
7790 goto next;
7791 }
7792
7793 wait_for_completion(&done.done);
7794
7795 if (!done.uptodate) {
7796 /* We might have another mirror, so try again */
7797 goto try_again;
7798 }
7799next:
7800 offset += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007801 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007802 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007803
7804 return err;
7805}
7806
Miao Xie8b110e32014-09-12 18:44:03 +08007807static int btrfs_subio_endio_read(struct inode *inode,
7808 struct btrfs_io_bio *io_bio, int err)
7809{
7810 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7811
7812 if (skip_csum) {
7813 if (unlikely(err))
7814 return __btrfs_correct_data_nocsum(inode, io_bio);
7815 else
7816 return 0;
7817 } else {
7818 return __btrfs_subio_endio_read(inode, io_bio, err);
7819 }
7820}
7821
Miao Xiec1dc0892014-09-12 18:43:56 +08007822static void btrfs_endio_direct_read(struct bio *bio, int err)
7823{
7824 struct btrfs_dio_private *dip = bio->bi_private;
7825 struct inode *inode = dip->inode;
7826 struct bio *dio_bio;
7827 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7828
Miao Xie8b110e32014-09-12 18:44:03 +08007829 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
7830 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007831
Josef Bacik4b46fce2010-05-23 11:00:55 -04007832 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007833 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007834 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007835
Josef Bacik4b46fce2010-05-23 11:00:55 -04007836 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007837
7838 /* If we had a csum failure make sure to clear the uptodate flag */
7839 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007840 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7841 dio_end_io(dio_bio, err);
Miao Xie23ea8e52014-09-12 18:43:54 +08007842
7843 if (io_bio->end_io)
7844 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04007845 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007846}
7847
7848static void btrfs_endio_direct_write(struct bio *bio, int err)
7849{
7850 struct btrfs_dio_private *dip = bio->bi_private;
7851 struct inode *inode = dip->inode;
7852 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007853 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007854 u64 ordered_offset = dip->logical_offset;
7855 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007856 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007857 int ret;
7858
7859 if (err)
7860 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007861again:
7862 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7863 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007864 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007865 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007866 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007867
Liu Bo9e0af232014-08-15 23:36:53 +08007868 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7869 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007870 btrfs_queue_work(root->fs_info->endio_write_workers,
7871 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007872out_test:
7873 /*
7874 * our bio might span multiple ordered extents. If we haven't
7875 * completed the accounting for the whole dio, go back and try again
7876 */
7877 if (ordered_offset < dip->logical_offset + dip->bytes) {
7878 ordered_bytes = dip->logical_offset + dip->bytes -
7879 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007880 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007881 goto again;
7882 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007883out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007884 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007885
Josef Bacik4b46fce2010-05-23 11:00:55 -04007886 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007887
7888 /* If we had an error make sure to clear the uptodate flag */
7889 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007890 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7891 dio_end_io(dio_bio, err);
7892 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007893}
7894
Chris Masoneaf25d92010-05-25 09:48:28 -04007895static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7896 struct bio *bio, int mirror_num,
7897 unsigned long bio_flags, u64 offset)
7898{
7899 int ret;
7900 struct btrfs_root *root = BTRFS_I(inode)->root;
7901 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007902 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007903 return 0;
7904}
7905
Miao Xiee65e1532010-11-22 03:04:43 +00007906static void btrfs_end_dio_bio(struct bio *bio, int err)
7907{
7908 struct btrfs_dio_private *dip = bio->bi_private;
7909
Miao Xie8b110e32014-09-12 18:44:03 +08007910 if (err)
7911 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
7912 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
7913 btrfs_ino(dip->inode), bio->bi_rw,
7914 (unsigned long long)bio->bi_iter.bi_sector,
7915 bio->bi_iter.bi_size, err);
7916
7917 if (dip->subio_endio)
7918 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007919
7920 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00007921 dip->errors = 1;
7922
7923 /*
7924 * before atomic variable goto zero, we must make sure
7925 * dip->errors is perceived to be set.
7926 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007927 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007928 }
7929
7930 /* if there are more bios still pending for this dio, just exit */
7931 if (!atomic_dec_and_test(&dip->pending_bios))
7932 goto out;
7933
Chris Mason9be33952013-05-17 18:30:14 -04007934 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007935 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007936 } else {
7937 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007938 bio_endio(dip->orig_bio, 0);
7939 }
7940out:
7941 bio_put(bio);
7942}
7943
7944static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7945 u64 first_sector, gfp_t gfp_flags)
7946{
7947 int nr_vecs = bio_get_nr_vecs(bdev);
7948 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7949}
7950
Miao Xiec1dc0892014-09-12 18:43:56 +08007951static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
7952 struct inode *inode,
7953 struct btrfs_dio_private *dip,
7954 struct bio *bio,
7955 u64 file_offset)
7956{
7957 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7958 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
7959 int ret;
7960
7961 /*
7962 * We load all the csum data we need when we submit
7963 * the first bio to reduce the csum tree search and
7964 * contention.
7965 */
7966 if (dip->logical_offset == file_offset) {
7967 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
7968 file_offset);
7969 if (ret)
7970 return ret;
7971 }
7972
7973 if (bio == dip->orig_bio)
7974 return 0;
7975
7976 file_offset -= dip->logical_offset;
7977 file_offset >>= inode->i_sb->s_blocksize_bits;
7978 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
7979
7980 return 0;
7981}
7982
Miao Xiee65e1532010-11-22 03:04:43 +00007983static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7984 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007985 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007986{
Miao Xiefacc8a222013-07-25 19:22:34 +08007987 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007988 int write = rw & REQ_WRITE;
7989 struct btrfs_root *root = BTRFS_I(inode)->root;
7990 int ret;
7991
Josef Bacikb812ce22012-11-16 13:56:32 -05007992 if (async_submit)
7993 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7994
Miao Xiee65e1532010-11-22 03:04:43 +00007995 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007996
7997 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02007998 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7999 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008000 if (ret)
8001 goto err;
8002 }
Miao Xiee65e1532010-11-22 03:04:43 +00008003
Josef Bacik1ae39932011-04-06 14:41:34 -04008004 if (skip_sum)
8005 goto map;
8006
8007 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00008008 ret = btrfs_wq_submit_bio(root->fs_info,
8009 inode, rw, bio, 0, 0,
8010 file_offset,
8011 __btrfs_submit_bio_start_direct_io,
8012 __btrfs_submit_bio_done);
8013 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008014 } else if (write) {
8015 /*
8016 * If we aren't doing async submit, calculate the csum of the
8017 * bio now.
8018 */
8019 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
8020 if (ret)
8021 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008022 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08008023 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
8024 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008025 if (ret)
8026 goto err;
8027 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008028map:
8029 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008030err:
8031 bio_put(bio);
8032 return ret;
8033}
8034
8035static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
8036 int skip_sum)
8037{
8038 struct inode *inode = dip->inode;
8039 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008040 struct bio *bio;
8041 struct bio *orig_bio = dip->orig_bio;
8042 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008043 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008044 u64 file_offset = dip->logical_offset;
8045 u64 submit_len = 0;
8046 u64 map_length;
8047 int nr_pages = 0;
Miao Xie23ea8e52014-09-12 18:43:54 +08008048 int ret;
Josef Bacik1ae39932011-04-06 14:41:34 -04008049 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008050
Kent Overstreet4f024f32013-10-11 15:44:27 -07008051 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008052 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008053 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008054 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008055 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008056
Kent Overstreet4f024f32013-10-11 15:44:27 -07008057 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008058 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008059 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008060 goto submit;
8061 }
8062
David Woodhouse53b381b2013-01-29 18:40:14 -05008063 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008064 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008065 async_submit = 0;
8066 else
8067 async_submit = 1;
8068
Josef Bacik02f57c72011-04-06 14:25:44 -04008069 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8070 if (!bio)
8071 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008072
Josef Bacik02f57c72011-04-06 14:25:44 -04008073 bio->bi_private = dip;
8074 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008075 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008076 atomic_inc(&dip->pending_bios);
8077
Miao Xiee65e1532010-11-22 03:04:43 +00008078 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
David Sterbaee39b432014-09-30 01:33:33 +02008079 if (map_length < submit_len + bvec->bv_len ||
Miao Xiee65e1532010-11-22 03:04:43 +00008080 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
David Sterbaee39b432014-09-30 01:33:33 +02008081 bvec->bv_offset) < bvec->bv_len) {
Miao Xiee65e1532010-11-22 03:04:43 +00008082 /*
8083 * inc the count before we submit the bio so
8084 * we know the end IO handler won't happen before
8085 * we inc the count. Otherwise, the dip might get freed
8086 * before we're done setting it up
8087 */
8088 atomic_inc(&dip->pending_bios);
8089 ret = __btrfs_submit_dio_bio(bio, inode, rw,
8090 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008091 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008092 if (ret) {
8093 bio_put(bio);
8094 atomic_dec(&dip->pending_bios);
8095 goto out_err;
8096 }
8097
Miao Xiee65e1532010-11-22 03:04:43 +00008098 start_sector += submit_len >> 9;
8099 file_offset += submit_len;
8100
8101 submit_len = 0;
8102 nr_pages = 0;
8103
8104 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8105 start_sector, GFP_NOFS);
8106 if (!bio)
8107 goto out_err;
8108 bio->bi_private = dip;
8109 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008110 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008111
Kent Overstreet4f024f32013-10-11 15:44:27 -07008112 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008113 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008114 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008115 &map_length, NULL, 0);
8116 if (ret) {
8117 bio_put(bio);
8118 goto out_err;
8119 }
8120 } else {
8121 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308122 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00008123 bvec++;
8124 }
8125 }
8126
Josef Bacik02f57c72011-04-06 14:25:44 -04008127submit:
Miao Xiee65e1532010-11-22 03:04:43 +00008128 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008129 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008130 if (!ret)
8131 return 0;
8132
8133 bio_put(bio);
8134out_err:
8135 dip->errors = 1;
8136 /*
8137 * before atomic variable goto zero, we must
8138 * make sure dip->errors is perceived to be set.
8139 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008140 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008141 if (atomic_dec_and_test(&dip->pending_bios))
8142 bio_io_error(dip->orig_bio);
8143
8144 /* bio_end_io() will handle error, so we needn't return it */
8145 return 0;
8146}
8147
Chris Mason9be33952013-05-17 18:30:14 -04008148static void btrfs_submit_direct(int rw, struct bio *dio_bio,
8149 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008150{
8151 struct btrfs_root *root = BTRFS_I(inode)->root;
8152 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04008153 struct bio *io_bio;
Miao Xie23ea8e52014-09-12 18:43:54 +08008154 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008155 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02008156 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008157 int ret = 0;
8158
8159 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8160
Chris Mason9be33952013-05-17 18:30:14 -04008161 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008162 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008163 ret = -ENOMEM;
8164 goto free_ordered;
8165 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008166
Miao Xiec1dc0892014-09-12 18:43:56 +08008167 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008168 if (!dip) {
8169 ret = -ENOMEM;
8170 goto free_io_bio;
8171 }
8172
8173 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008174 dip->inode = inode;
8175 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008176 dip->bytes = dio_bio->bi_iter.bi_size;
8177 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008178 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008179 dip->orig_bio = io_bio;
8180 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008181 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008182 btrfs_bio = btrfs_io_bio(io_bio);
8183 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008184
Miao Xiec1dc0892014-09-12 18:43:56 +08008185 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008186 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008187 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008188 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008189 dip->subio_endio = btrfs_subio_endio_read;
8190 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008191
Miao Xiee65e1532010-11-22 03:04:43 +00008192 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
8193 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008194 return;
Chris Mason9be33952013-05-17 18:30:14 -04008195
Miao Xie23ea8e52014-09-12 18:43:54 +08008196 if (btrfs_bio->end_io)
8197 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008198free_io_bio:
8199 bio_put(io_bio);
8200
Josef Bacik4b46fce2010-05-23 11:00:55 -04008201free_ordered:
8202 /*
8203 * If this is a write, we need to clean up the reserved space and kill
8204 * the ordered extent.
8205 */
8206 if (write) {
8207 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05008208 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008209 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
8210 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
8211 btrfs_free_reserved_extent(root, ordered->start,
Miao Xiee570fd22014-06-19 10:42:50 +08008212 ordered->disk_len, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008213 btrfs_put_ordered_extent(ordered);
8214 btrfs_put_ordered_extent(ordered);
8215 }
Chris Mason9be33952013-05-17 18:30:14 -04008216 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008217}
8218
Chris Mason5a5f79b2010-05-26 21:33:37 -04008219static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04008220 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008221{
8222 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008223 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008224 unsigned blocksize_mask = root->sectorsize - 1;
8225 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008226
8227 if (offset & blocksize_mask)
8228 goto out;
8229
Al Viro28060d52014-03-22 05:15:17 -04008230 if (iov_iter_alignment(iter) & blocksize_mask)
8231 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008232
Al Viro28060d52014-03-22 05:15:17 -04008233 /* If this is a write we don't need to check anymore */
8234 if (rw & WRITE)
8235 return 0;
8236 /*
8237 * Check to make sure we don't have duplicate iov_base's in this
8238 * iovec, if so return EINVAL, otherwise we'll get csum errors
8239 * when reading back.
8240 */
8241 for (seg = 0; seg < iter->nr_segs; seg++) {
8242 for (i = seg + 1; i < iter->nr_segs; i++) {
8243 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008244 goto out;
8245 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008246 }
8247 retval = 0;
8248out:
8249 return retval;
8250}
Josef Bacikeb838e72012-07-31 16:28:48 -04008251
Chris Mason16432982008-04-10 10:23:21 -04008252static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05008253 struct iov_iter *iter, loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04008254{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008255 struct file *file = iocb->ki_filp;
8256 struct inode *inode = file->f_mapping->host;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008257 u64 outstanding_extents = 0;
Miao Xie09348562013-02-07 10:12:07 +00008258 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008259 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008260 bool wakeup = true;
8261 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008262 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008263
Al Viro28060d52014-03-22 05:15:17 -04008264 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008265 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008266
Miao Xie38851cc2013-02-08 07:04:11 +00008267 atomic_inc(&inode->i_dio_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008268 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008269
Josef Bacik0e267c42013-07-02 10:38:02 -04008270 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008271 * The generic stuff only does filemap_write_and_wait_range, which
8272 * isn't enough if we've written compressed pages to this area, so
8273 * we need to flush the dirty pages again to make absolutely sure
8274 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008275 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008276 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008277 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8278 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008279 filemap_fdatawrite_range(inode->i_mapping, offset,
8280 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008281
Miao Xie09348562013-02-07 10:12:07 +00008282 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008283 /*
8284 * If the write DIO is beyond the EOF, we need update
8285 * the isize, but it is protected by i_mutex. So we can
8286 * not unlock the i_mutex at this case.
8287 */
8288 if (offset + count <= inode->i_size) {
8289 mutex_unlock(&inode->i_mutex);
8290 relock = true;
8291 }
Miao Xie09348562013-02-07 10:12:07 +00008292 ret = btrfs_delalloc_reserve_space(inode, count);
8293 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008294 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008295 outstanding_extents = div64_u64(count +
8296 BTRFS_MAX_EXTENT_SIZE - 1,
8297 BTRFS_MAX_EXTENT_SIZE);
8298
8299 /*
8300 * We need to know how many extents we reserved so that we can
8301 * do the accounting properly if we go over the number we
8302 * originally calculated. Abuse current->journal_info for this.
8303 */
8304 current->journal_info = &outstanding_extents;
David Sterbaee39b432014-09-30 01:33:33 +02008305 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8306 &BTRFS_I(inode)->runtime_flags)) {
Miao Xie38851cc2013-02-08 07:04:11 +00008307 inode_dio_done(inode);
8308 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8309 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008310 }
8311
8312 ret = __blockdev_direct_IO(rw, iocb, inode,
8313 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Al Viro31b14032014-03-05 01:33:16 -05008314 iter, offset, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00008315 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00008316 if (rw & WRITE) {
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008317 current->journal_info = NULL;
Miao Xie09348562013-02-07 10:12:07 +00008318 if (ret < 0 && ret != -EIOCBQUEUED)
8319 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07008320 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00008321 btrfs_delalloc_release_space(inode,
8322 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008323 }
Miao Xie38851cc2013-02-08 07:04:11 +00008324out:
Miao Xie2e60a512013-02-08 07:01:08 +00008325 if (wakeup)
8326 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008327 if (relock)
8328 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00008329
8330 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008331}
8332
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008333#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8334
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008335static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8336 __u64 start, __u64 len)
8337{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008338 int ret;
8339
8340 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8341 if (ret)
8342 return ret;
8343
Chris Masonec29ed52011-02-23 16:23:20 -05008344 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008345}
8346
Chris Mason9ebefb182007-06-15 13:50:00 -04008347int btrfs_readpage(struct file *file, struct page *page)
8348{
Chris Masond1310b22008-01-24 16:13:08 -05008349 struct extent_io_tree *tree;
8350 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008351 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008352}
Chris Mason1832a6d2007-12-21 16:27:21 -05008353
Chris Mason39279cc2007-06-12 06:35:45 -04008354static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8355{
Chris Masond1310b22008-01-24 16:13:08 -05008356 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04008357
8358
8359 if (current->flags & PF_MEMALLOC) {
8360 redirty_page_for_writepage(wbc, page);
8361 unlock_page(page);
8362 return 0;
8363 }
Chris Masond1310b22008-01-24 16:13:08 -05008364 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04008365 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8366}
Chris Mason39279cc2007-06-12 06:35:45 -04008367
Eric Sandeen48a3b632013-04-25 20:41:01 +00008368static int btrfs_writepages(struct address_space *mapping,
8369 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008370{
Chris Masond1310b22008-01-24 16:13:08 -05008371 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008372
Chris Masond1310b22008-01-24 16:13:08 -05008373 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008374 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8375}
8376
Chris Mason3ab2fb52007-11-08 10:59:22 -05008377static int
8378btrfs_readpages(struct file *file, struct address_space *mapping,
8379 struct list_head *pages, unsigned nr_pages)
8380{
Chris Masond1310b22008-01-24 16:13:08 -05008381 struct extent_io_tree *tree;
8382 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008383 return extent_readpages(tree, mapping, pages, nr_pages,
8384 btrfs_get_extent);
8385}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008386static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008387{
Chris Masond1310b22008-01-24 16:13:08 -05008388 struct extent_io_tree *tree;
8389 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008390 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008391
Chris Masond1310b22008-01-24 16:13:08 -05008392 tree = &BTRFS_I(page->mapping->host)->io_tree;
8393 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008394 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008395 if (ret == 1) {
8396 ClearPagePrivate(page);
8397 set_page_private(page, 0);
8398 page_cache_release(page);
8399 }
8400 return ret;
8401}
Chris Mason39279cc2007-06-12 06:35:45 -04008402
Chris Masone6dcd2d2008-07-17 12:53:50 -04008403static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8404{
Chris Mason98509cfc2008-09-11 15:51:43 -04008405 if (PageWriteback(page) || PageDirty(page))
8406 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008407 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008408}
8409
Lukas Czernerd47992f2013-05-21 23:17:23 -04008410static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8411 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008412{
Josef Bacik5fd02042012-05-02 14:00:54 -04008413 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008414 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008415 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008416 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008417 u64 page_start = page_offset(page);
8418 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008419 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008420
Chris Mason8b62b722009-09-02 16:53:46 -04008421 /*
8422 * we have the page locked, so new writeback can't start,
8423 * and the dirty bit won't be cleared while we are here.
8424 *
8425 * Wait for IO on this page so that we can safely clear
8426 * the PagePrivate2 bit and do ordered accounting
8427 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008428 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008429
Josef Bacik5fd02042012-05-02 14:00:54 -04008430 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008431 if (offset) {
8432 btrfs_releasepage(page, GFP_NOFS);
8433 return;
8434 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008435
8436 if (!inode_evicting)
8437 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
8438 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008439 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04008440 /*
8441 * IO on this page will never be started, so we need
8442 * to account for any ordered extents now
8443 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008444 if (!inode_evicting)
8445 clear_extent_bit(tree, page_start, page_end,
8446 EXTENT_DIRTY | EXTENT_DELALLOC |
8447 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8448 EXTENT_DEFRAG, 1, 0, &cached_state,
8449 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008450 /*
8451 * whoever cleared the private bit is responsible
8452 * for the finish_ordered_io
8453 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008454 if (TestClearPagePrivate2(page)) {
8455 struct btrfs_ordered_inode_tree *tree;
8456 u64 new_len;
8457
8458 tree = &BTRFS_I(inode)->ordered_tree;
8459
8460 spin_lock_irq(&tree->lock);
8461 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8462 new_len = page_start - ordered->file_offset;
8463 if (new_len < ordered->truncated_len)
8464 ordered->truncated_len = new_len;
8465 spin_unlock_irq(&tree->lock);
8466
8467 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8468 page_start,
8469 PAGE_CACHE_SIZE, 1))
8470 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008471 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008472 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008473 if (!inode_evicting) {
8474 cached_state = NULL;
8475 lock_extent_bits(tree, page_start, page_end, 0,
8476 &cached_state);
8477 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008478 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008479
8480 if (!inode_evicting) {
8481 clear_extent_bit(tree, page_start, page_end,
8482 EXTENT_LOCKED | EXTENT_DIRTY |
8483 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8484 EXTENT_DEFRAG, 1, 1,
8485 &cached_state, GFP_NOFS);
8486
8487 __btrfs_releasepage(page, GFP_NOFS);
8488 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008489
Chris Mason4a096752008-07-21 10:29:44 -04008490 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008491 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008492 ClearPagePrivate(page);
8493 set_page_private(page, 0);
8494 page_cache_release(page);
8495 }
Chris Mason39279cc2007-06-12 06:35:45 -04008496}
8497
Chris Mason9ebefb182007-06-15 13:50:00 -04008498/*
8499 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8500 * called from a page fault handler when a page is first dirtied. Hence we must
8501 * be careful to check for EOF conditions here. We set the page up correctly
8502 * for a written page which means we get ENOSPC checking when writing into
8503 * holes and correct delalloc and unwritten extent mapping on filesystems that
8504 * support these features.
8505 *
8506 * We are not allowed to take the i_mutex here so we have to play games to
8507 * protect against truncate races as the page could now be beyond EOF. Because
8508 * vmtruncate() writes the inode size before removing pages, once we have the
8509 * page lock we can determine safely if the page is beyond EOF. If it is not
8510 * beyond EOF, then the page is guaranteed safe against truncation until we
8511 * unlock the page.
8512 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008513int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008514{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008515 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008516 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008517 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008518 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8519 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008520 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008521 char *kaddr;
8522 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008523 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008524 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008525 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04008526 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008527 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04008528
Jan Karab2b5ef52012-06-12 16:20:45 +02008529 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04008530 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008531 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008532 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008533 reserved = 1;
8534 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008535 if (ret) {
8536 if (ret == -ENOMEM)
8537 ret = VM_FAULT_OOM;
8538 else /* -ENOSPC, -EIO, etc */
8539 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008540 if (reserved)
8541 goto out;
8542 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008543 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008544
Nick Piggin56a76f82009-03-31 15:23:23 -07008545 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008546again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008547 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008548 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008549 page_start = page_offset(page);
8550 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04008551
Chris Mason9ebefb182007-06-15 13:50:00 -04008552 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008553 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008554 /* page got truncated out from underneath us */
8555 goto out_unlock;
8556 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008557 wait_on_page_writeback(page);
8558
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008559 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008560 set_page_extent_mapped(page);
8561
Chris Masoneb84ae02008-07-17 13:53:27 -04008562 /*
8563 * we can't set the delalloc bits if there are pending ordered
8564 * extents. Drop our locks and wait for them to finish
8565 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008566 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8567 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008568 unlock_extent_cached(io_tree, page_start, page_end,
8569 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008570 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008571 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008572 btrfs_put_ordered_extent(ordered);
8573 goto again;
8574 }
8575
Josef Bacikfbf19082009-10-01 17:10:23 -04008576 /*
8577 * XXX - page_mkwrite gets called every time the page is dirtied, even
8578 * if it was already dirty, so for space accounting reasons we need to
8579 * clear any delalloc bits for the range we are fixing to save. There
8580 * is probably a better way to do this, but for now keep consistent with
8581 * prepare_pages in the normal write path.
8582 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00008583 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008584 EXTENT_DIRTY | EXTENT_DELALLOC |
8585 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008586 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008587
Josef Bacik2ac55d42010-02-03 19:33:23 +00008588 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
8589 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008590 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008591 unlock_extent_cached(io_tree, page_start, page_end,
8592 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008593 ret = VM_FAULT_SIGBUS;
8594 goto out_unlock;
8595 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008596 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008597
8598 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04008599 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008600 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008601 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008602 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008603
Chris Masone6dcd2d2008-07-17 12:53:50 -04008604 if (zero_start != PAGE_CACHE_SIZE) {
8605 kaddr = kmap(page);
8606 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8607 flush_dcache_page(page);
8608 kunmap(page);
8609 }
Chris Mason247e7432008-07-17 12:53:51 -04008610 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008611 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008612 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008613
Chris Mason257c62e2009-10-13 13:21:08 -04008614 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8615 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008616 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008617
Josef Bacik2ac55d42010-02-03 19:33:23 +00008618 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008619
8620out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008621 if (!ret) {
8622 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008623 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008624 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008625 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008626out:
Josef Bacikec39e182012-01-12 19:10:12 -05008627 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008628out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008629 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008630 return ret;
8631}
8632
Josef Bacika41ad392011-01-31 15:30:16 -05008633static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008634{
8635 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008636 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008637 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008638 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008639 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008640 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008641 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008642
Josef Bacik0ef8b722013-10-25 16:13:35 -04008643 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8644 (u64)-1);
8645 if (ret)
8646 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008647
Josef Bacikfcb80c22011-05-03 10:40:22 -04008648 /*
8649 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8650 * 3 things going on here
8651 *
8652 * 1) We need to reserve space for our orphan item and the space to
8653 * delete our orphan item. Lord knows we don't want to have a dangling
8654 * orphan item because we didn't reserve space to remove it.
8655 *
8656 * 2) We need to reserve space to update our inode.
8657 *
8658 * 3) We need to have something to cache all the space that is going to
8659 * be free'd up by the truncate operation, but also have some slack
8660 * space reserved in case it uses space during the truncate (thank you
8661 * very much snapshotting).
8662 *
8663 * And we need these to all be seperate. The fact is we can use alot of
8664 * space doing the truncate, and we have no earthly idea how much space
8665 * we will use, so we need the truncate reservation to be seperate so it
8666 * doesn't end up using space reserved for updating the inode or
8667 * removing the orphan item. We also need to be able to stop the
8668 * transaction and start a new one, which means we need to be able to
8669 * update the inode several times, and we have no idea of knowing how
8670 * many times that will be, so we can't just reserve 1 item for the
8671 * entirety of the opration, so that has to be done seperately as well.
8672 * Then there is the orphan item, which does indeed need to be held on
8673 * to for the whole operation, and we need nobody to touch this reserved
8674 * space except the orphan code.
8675 *
8676 * So that leaves us with
8677 *
8678 * 1) root->orphan_block_rsv - for the orphan deletion.
8679 * 2) rsv - for the truncate reservation, which we will steal from the
8680 * transaction reservation.
8681 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8682 * updating the inode.
8683 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008684 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008685 if (!rsv)
8686 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008687 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008688 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008689
Josef Bacik907cbce2011-08-08 13:46:15 -04008690 /*
Josef Bacik07127182011-08-19 10:29:59 -04008691 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008692 * 1 for updating the inode.
8693 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008694 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008695 if (IS_ERR(trans)) {
8696 err = PTR_ERR(trans);
8697 goto out;
8698 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008699
Josef Bacik907cbce2011-08-08 13:46:15 -04008700 /* Migrate the slack space for the truncate to our reserve */
8701 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8702 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008703 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008704
Chris Mason5a3f23d2009-03-31 13:27:11 -04008705 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008706 * So if we truncate and then write and fsync we normally would just
8707 * write the extents that changed, which is a problem if we need to
8708 * first truncate that entire inode. So set this flag so we write out
8709 * all of the extents in the inode to the sync log so we're completely
8710 * safe.
8711 */
8712 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008713 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008714
Yan, Zheng80825102009-11-12 09:35:36 +00008715 while (1) {
8716 ret = btrfs_truncate_inode_items(trans, root, inode,
8717 inode->i_size,
8718 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08008719 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05008720 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008721 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008722 }
Chris Mason39279cc2007-06-12 06:35:45 -04008723
Josef Bacikfcb80c22011-05-03 10:40:22 -04008724 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008725 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008726 if (ret) {
8727 err = ret;
8728 break;
8729 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008730
Yan, Zheng80825102009-11-12 09:35:36 +00008731 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008732 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008733
8734 trans = btrfs_start_transaction(root, 2);
8735 if (IS_ERR(trans)) {
8736 ret = err = PTR_ERR(trans);
8737 trans = NULL;
8738 break;
8739 }
8740
8741 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8742 rsv, min_size);
8743 BUG_ON(ret); /* shouldn't happen */
8744 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008745 }
8746
8747 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008748 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008749 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008750 if (ret)
8751 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008752 }
8753
Chris Mason917c16b2011-11-08 14:49:59 -05008754 if (trans) {
8755 trans->block_rsv = &root->fs_info->trans_block_rsv;
8756 ret = btrfs_update_inode(trans, root, inode);
8757 if (ret && !err)
8758 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008759
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008760 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008761 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008762 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008763
8764out:
8765 btrfs_free_block_rsv(root, rsv);
8766
Josef Bacik3893e332011-01-31 16:03:11 -05008767 if (ret && !err)
8768 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008769
Josef Bacik3893e332011-01-31 16:03:11 -05008770 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008771}
8772
Sven Wegener3b963622008-06-09 21:57:42 -04008773/*
Chris Masond352ac62008-09-29 15:18:18 -04008774 * create a new subvolume directory/inode (helper for the ioctl).
8775 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008776int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008777 struct btrfs_root *new_root,
8778 struct btrfs_root *parent_root,
8779 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008780{
Chris Mason39279cc2007-06-12 06:35:45 -04008781 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008782 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008783 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008784
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008785 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8786 new_dirid, new_dirid,
8787 S_IFDIR | (~current_umask() & S_IRWXUGO),
8788 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008789 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008790 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008791 inode->i_op = &btrfs_dir_inode_operations;
8792 inode->i_fop = &btrfs_dir_file_operations;
8793
Miklos Szeredibfe86842011-10-28 14:13:29 +02008794 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008795 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008796 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008797
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008798 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8799 if (err)
8800 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008801 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008802 new_root->root_key.objectid, err);
8803
Yan, Zheng76dda932009-09-21 16:00:26 -04008804 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008805
Yan, Zheng76dda932009-09-21 16:00:26 -04008806 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008807 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008808}
8809
Chris Mason39279cc2007-06-12 06:35:45 -04008810struct inode *btrfs_alloc_inode(struct super_block *sb)
8811{
8812 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008813 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008814
8815 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8816 if (!ei)
8817 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008818
8819 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008820 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008821 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008822 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008823 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008824 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008825 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008826 ei->disk_i_size = 0;
8827 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008828 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008829 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008830 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008831 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008832 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008833
Josef Bacik9e0baf62011-07-15 15:16:44 +00008834 spin_lock_init(&ei->lock);
8835 ei->outstanding_extents = 0;
8836 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008837
Josef Bacik72ac3c02012-05-23 14:13:11 -04008838 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008839 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008840
Miao Xie16cdcec2011-04-22 18:12:22 +08008841 ei->delayed_node = NULL;
8842
chandan r9cc97d62012-07-04 12:48:07 +05308843 ei->i_otime.tv_sec = 0;
8844 ei->i_otime.tv_nsec = 0;
8845
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008846 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008847 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008848 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8849 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008850 ei->io_tree.track_uptodate = 1;
8851 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008852 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008853 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008854 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008855 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008856 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008857 RB_CLEAR_NODE(&ei->rb_node);
8858
8859 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008860}
8861
Josef Bacikaaedb552013-10-11 14:44:09 -04008862#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8863void btrfs_test_destroy_inode(struct inode *inode)
8864{
8865 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8866 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8867}
8868#endif
8869
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008870static void btrfs_i_callback(struct rcu_head *head)
8871{
8872 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008873 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8874}
8875
Chris Mason39279cc2007-06-12 06:35:45 -04008876void btrfs_destroy_inode(struct inode *inode)
8877{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008878 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008879 struct btrfs_root *root = BTRFS_I(inode)->root;
8880
Al Virob3d9b7a2012-06-09 13:51:19 -04008881 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008882 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008883 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8884 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008885 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8886 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008887 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008888
Chris Mason5a3f23d2009-03-31 13:27:11 -04008889 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008890 * This can happen where we create an inode, but somebody else also
8891 * created the same inode and we need to destroy the one we already
8892 * created.
8893 */
8894 if (!root)
8895 goto free;
8896
Josef Bacik8a35d952012-05-23 14:26:42 -04008897 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8898 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008899 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008900 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008901 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008902 }
Josef Bacik7b128762008-07-24 12:17:14 -04008903
Chris Masond3977122009-01-05 21:25:51 -05008904 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008905 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8906 if (!ordered)
8907 break;
8908 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008909 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008910 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008911 btrfs_remove_ordered_extent(inode, ordered);
8912 btrfs_put_ordered_extent(ordered);
8913 btrfs_put_ordered_extent(ordered);
8914 }
8915 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008916 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008917 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008918free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008919 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008920}
8921
Al Viro45321ac2010-06-07 13:43:19 -04008922int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008923{
8924 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008925
Naohiro Aota6379ef92013-06-06 09:56:34 +00008926 if (root == NULL)
8927 return 1;
8928
Liu Bofa6ac872013-02-20 14:10:23 +00008929 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008930 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008931 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008932 else
Al Viro45321ac2010-06-07 13:43:19 -04008933 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008934}
8935
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008936static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008937{
8938 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8939
8940 inode_init_once(&ei->vfs_inode);
8941}
8942
8943void btrfs_destroy_cachep(void)
8944{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008945 /*
8946 * Make sure all delayed rcu free inodes are flushed before we
8947 * destroy cache.
8948 */
8949 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008950 if (btrfs_inode_cachep)
8951 kmem_cache_destroy(btrfs_inode_cachep);
8952 if (btrfs_trans_handle_cachep)
8953 kmem_cache_destroy(btrfs_trans_handle_cachep);
8954 if (btrfs_transaction_cachep)
8955 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008956 if (btrfs_path_cachep)
8957 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008958 if (btrfs_free_space_cachep)
8959 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008960 if (btrfs_delalloc_work_cachep)
8961 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008962}
8963
8964int btrfs_init_cachep(void)
8965{
David Sterba837e1972012-09-07 03:00:48 -06008966 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008967 sizeof(struct btrfs_inode), 0,
8968 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008969 if (!btrfs_inode_cachep)
8970 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008971
David Sterba837e1972012-09-07 03:00:48 -06008972 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008973 sizeof(struct btrfs_trans_handle), 0,
8974 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008975 if (!btrfs_trans_handle_cachep)
8976 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008977
David Sterba837e1972012-09-07 03:00:48 -06008978 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008979 sizeof(struct btrfs_transaction), 0,
8980 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008981 if (!btrfs_transaction_cachep)
8982 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008983
David Sterba837e1972012-09-07 03:00:48 -06008984 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008985 sizeof(struct btrfs_path), 0,
8986 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008987 if (!btrfs_path_cachep)
8988 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008989
David Sterba837e1972012-09-07 03:00:48 -06008990 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008991 sizeof(struct btrfs_free_space), 0,
8992 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8993 if (!btrfs_free_space_cachep)
8994 goto fail;
8995
Miao Xie8ccf6f192012-10-25 09:28:04 +00008996 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8997 sizeof(struct btrfs_delalloc_work), 0,
8998 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8999 NULL);
9000 if (!btrfs_delalloc_work_cachep)
9001 goto fail;
9002
Chris Mason39279cc2007-06-12 06:35:45 -04009003 return 0;
9004fail:
9005 btrfs_destroy_cachep();
9006 return -ENOMEM;
9007}
9008
9009static int btrfs_getattr(struct vfsmount *mnt,
9010 struct dentry *dentry, struct kstat *stat)
9011{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009012 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04009013 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05009014 u32 blocksize = inode->i_sb->s_blocksize;
9015
Chris Mason39279cc2007-06-12 06:35:45 -04009016 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009017 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05009018 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009019
9020 spin_lock(&BTRFS_I(inode)->lock);
9021 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9022 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009023 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009024 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009025 return 0;
9026}
9027
Chris Masond3977122009-01-05 21:25:51 -05009028static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9029 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009030{
9031 struct btrfs_trans_handle *trans;
9032 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009033 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04009034 struct inode *new_inode = new_dentry->d_inode;
9035 struct inode *old_inode = old_dentry->d_inode;
9036 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009037 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009038 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009039 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009040 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009041
Li Zefan33345d012011-04-20 10:31:50 +08009042 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009043 return -EPERM;
9044
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009045 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009046 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009047 return -EXDEV;
9048
Li Zefan33345d012011-04-20 10:31:50 +08009049 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9050 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009051 return -ENOTEMPTY;
9052
Chris Mason39279cc2007-06-12 06:35:45 -04009053 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009054 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009055 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009056
9057
9058 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009059 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009060 new_dentry->d_name.name,
9061 new_dentry->d_name.len);
9062
9063 if (ret) {
9064 if (ret == -EEXIST) {
9065 /* we shouldn't get
9066 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309067 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009068 return ret;
9069 }
9070 } else {
9071 /* maybe -EOVERFLOW */
9072 return ret;
9073 }
9074 }
9075 ret = 0;
9076
Chris Mason5a3f23d2009-03-31 13:27:11 -04009077 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009078 * we're using rename to replace one file with another. Start IO on it
9079 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009080 */
Chris Mason8d875f92014-08-12 10:47:42 -07009081 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009082 filemap_flush(old_inode->i_mapping);
9083
Yan, Zheng76dda932009-09-21 16:00:26 -04009084 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009085 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009086 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009087 /*
9088 * We want to reserve the absolute worst case amount of items. So if
9089 * both inodes are subvols and we need to unlink them then that would
9090 * require 4 item modifications, but if they are both normal inodes it
9091 * would require 5 item modifications, so we'll assume their normal
9092 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9093 * should cover the worst case number of items we'll modify.
9094 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04009095 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009096 if (IS_ERR(trans)) {
9097 ret = PTR_ERR(trans);
9098 goto out_notrans;
9099 }
Chris Mason5f39d392007-10-15 16:14:19 -04009100
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009101 if (dest != root)
9102 btrfs_record_root_in_trans(trans, dest);
9103
Yan, Zhenga5719522009-09-24 09:17:31 -04009104 ret = btrfs_set_inode_index(new_dir, &index);
9105 if (ret)
9106 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009107
Miao Xie67de1172013-12-26 13:07:06 +08009108 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009109 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009110 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08009111 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009112 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04009113 ret = btrfs_insert_inode_ref(trans, dest,
9114 new_dentry->d_name.name,
9115 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009116 old_ino,
9117 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009118 if (ret)
9119 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009120 /*
9121 * this is an ugly little race, but the rename is required
9122 * to make sure that if we crash, the inode is either at the
9123 * old name or the new one. pinning the log transaction lets
9124 * us make sure we don't allow a log commit to come in after
9125 * we unlink the name but before we add the new name back in.
9126 */
9127 btrfs_pin_log_trans(root);
9128 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009129
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009130 inode_inc_iversion(old_dir);
9131 inode_inc_iversion(new_dir);
9132 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009133 old_dir->i_ctime = old_dir->i_mtime = ctime;
9134 new_dir->i_ctime = new_dir->i_mtime = ctime;
9135 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04009136
Chris Mason12fcfd22009-03-24 10:24:20 -04009137 if (old_dentry->d_parent != new_dentry->d_parent)
9138 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9139
Li Zefan33345d012011-04-20 10:31:50 +08009140 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009141 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9142 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9143 old_dentry->d_name.name,
9144 old_dentry->d_name.len);
9145 } else {
Al Viro92986792011-03-04 17:14:37 +00009146 ret = __btrfs_unlink_inode(trans, root, old_dir,
9147 old_dentry->d_inode,
9148 old_dentry->d_name.name,
9149 old_dentry->d_name.len);
9150 if (!ret)
9151 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009152 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009153 if (ret) {
9154 btrfs_abort_transaction(trans, root, ret);
9155 goto out_fail;
9156 }
Chris Mason39279cc2007-06-12 06:35:45 -04009157
9158 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009159 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009160 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08009161 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009162 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9163 root_objectid = BTRFS_I(new_inode)->location.objectid;
9164 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9165 root_objectid,
9166 new_dentry->d_name.name,
9167 new_dentry->d_name.len);
9168 BUG_ON(new_inode->i_nlink == 0);
9169 } else {
9170 ret = btrfs_unlink_inode(trans, dest, new_dir,
9171 new_dentry->d_inode,
9172 new_dentry->d_name.name,
9173 new_dentry->d_name.len);
9174 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009175 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04009176 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009177 if (ret) {
9178 btrfs_abort_transaction(trans, root, ret);
9179 goto out_fail;
9180 }
Chris Mason39279cc2007-06-12 06:35:45 -04009181 }
Josef Bacikaec74772008-07-24 12:12:38 -04009182
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009183 ret = btrfs_add_link(trans, new_dir, old_inode,
9184 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009185 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009186 if (ret) {
9187 btrfs_abort_transaction(trans, root, ret);
9188 goto out_fail;
9189 }
Chris Mason39279cc2007-06-12 06:35:45 -04009190
Miao Xie67de1172013-12-26 13:07:06 +08009191 if (old_inode->i_nlink == 1)
9192 BTRFS_I(old_inode)->dir_index = index;
9193
Li Zefan33345d012011-04-20 10:31:50 +08009194 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04009195 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00009196 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009197 btrfs_end_log_trans(root);
9198 }
Chris Mason39279cc2007-06-12 06:35:45 -04009199out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009200 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009201out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009202 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009203 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009204
Chris Mason39279cc2007-06-12 06:35:45 -04009205 return ret;
9206}
9207
Miklos Szeredi80ace852014-07-23 15:15:32 +02009208static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9209 struct inode *new_dir, struct dentry *new_dentry,
9210 unsigned int flags)
9211{
9212 if (flags & ~RENAME_NOREPLACE)
9213 return -EINVAL;
9214
9215 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
9216}
9217
Miao Xie8ccf6f192012-10-25 09:28:04 +00009218static void btrfs_run_delalloc_work(struct btrfs_work *work)
9219{
9220 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009221 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009222
9223 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9224 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009225 inode = delalloc_work->inode;
9226 if (delalloc_work->wait) {
9227 btrfs_wait_ordered_range(inode, 0, (u64)-1);
9228 } else {
9229 filemap_flush(inode->i_mapping);
9230 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9231 &BTRFS_I(inode)->runtime_flags))
9232 filemap_flush(inode->i_mapping);
9233 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00009234
9235 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009236 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009237 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009238 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009239 complete(&delalloc_work->completion);
9240}
9241
9242struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
9243 int wait, int delay_iput)
9244{
9245 struct btrfs_delalloc_work *work;
9246
9247 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
9248 if (!work)
9249 return NULL;
9250
9251 init_completion(&work->completion);
9252 INIT_LIST_HEAD(&work->list);
9253 work->inode = inode;
9254 work->wait = wait;
9255 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009256 WARN_ON_ONCE(!inode);
9257 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9258 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009259
9260 return work;
9261}
9262
9263void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9264{
9265 wait_for_completion(&work->completion);
9266 kmem_cache_free(btrfs_delalloc_work_cachep, work);
9267}
9268
Chris Masond352ac62008-09-29 15:18:18 -04009269/*
9270 * some fairly slow code that needs optimization. This walks the list
9271 * of all the inodes with pending delalloc and forces them to disk.
9272 */
Miao Xie6c255e62014-03-06 13:55:01 +08009273static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9274 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009275{
Chris Masonea8c2812008-08-04 23:17:27 -04009276 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009277 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009278 struct btrfs_delalloc_work *work, *next;
9279 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009280 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009281 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009282
Miao Xie8ccf6f192012-10-25 09:28:04 +00009283 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009284 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009285
Miao Xie573bfb72014-03-06 13:55:03 +08009286 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009287 spin_lock(&root->delalloc_lock);
9288 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009289 while (!list_empty(&splice)) {
9290 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009291 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009292
Miao Xieeb73c1b2013-05-15 07:48:22 +00009293 list_move_tail(&binode->delalloc_inodes,
9294 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009295 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009296 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009297 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009298 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009299 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009300 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009301
9302 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +02009303 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04009304 if (delay_iput)
9305 btrfs_add_delayed_iput(inode);
9306 else
9307 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009308 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009309 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009310 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009311 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009312 btrfs_queue_work(root->fs_info->flush_workers,
9313 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009314 ret++;
9315 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009316 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009317 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009318 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009319 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009320 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009321
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009322out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009323 list_for_each_entry_safe(work, next, &works, list) {
9324 list_del_init(&work->list);
9325 btrfs_wait_and_free_delalloc_work(work);
9326 }
9327
Miao Xieeb73c1b2013-05-15 07:48:22 +00009328 if (!list_empty_careful(&splice)) {
9329 spin_lock(&root->delalloc_lock);
9330 list_splice_tail(&splice, &root->delalloc_inodes);
9331 spin_unlock(&root->delalloc_lock);
9332 }
Miao Xie573bfb72014-03-06 13:55:03 +08009333 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009334 return ret;
9335}
9336
9337int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
9338{
9339 int ret;
9340
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009341 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009342 return -EROFS;
9343
Miao Xie6c255e62014-03-06 13:55:01 +08009344 ret = __start_delalloc_inodes(root, delay_iput, -1);
9345 if (ret > 0)
9346 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009347 /*
9348 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04009349 * we have to make sure the IO is actually started and that
9350 * ordered extents get created before we return
9351 */
9352 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05009353 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05009354 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04009355 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05009356 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
9357 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04009358 }
9359 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009360 return ret;
9361}
9362
Miao Xie6c255e62014-03-06 13:55:01 +08009363int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
9364 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009365{
9366 struct btrfs_root *root;
9367 struct list_head splice;
9368 int ret;
9369
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009370 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009371 return -EROFS;
9372
9373 INIT_LIST_HEAD(&splice);
9374
Miao Xie573bfb72014-03-06 13:55:03 +08009375 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009376 spin_lock(&fs_info->delalloc_root_lock);
9377 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009378 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009379 root = list_first_entry(&splice, struct btrfs_root,
9380 delalloc_root);
9381 root = btrfs_grab_fs_root(root);
9382 BUG_ON(!root);
9383 list_move_tail(&root->delalloc_root,
9384 &fs_info->delalloc_roots);
9385 spin_unlock(&fs_info->delalloc_root_lock);
9386
Miao Xie6c255e62014-03-06 13:55:01 +08009387 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009388 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009389 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009390 goto out;
9391
Miao Xie6c255e62014-03-06 13:55:01 +08009392 if (nr != -1) {
9393 nr -= ret;
9394 WARN_ON(nr < 0);
9395 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009396 spin_lock(&fs_info->delalloc_root_lock);
9397 }
9398 spin_unlock(&fs_info->delalloc_root_lock);
9399
Miao Xie6c255e62014-03-06 13:55:01 +08009400 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009401 atomic_inc(&fs_info->async_submit_draining);
9402 while (atomic_read(&fs_info->nr_async_submits) ||
9403 atomic_read(&fs_info->async_delalloc_pages)) {
9404 wait_event(fs_info->async_submit_wait,
9405 (atomic_read(&fs_info->nr_async_submits) == 0 &&
9406 atomic_read(&fs_info->async_delalloc_pages) == 0));
9407 }
9408 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009409out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009410 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009411 spin_lock(&fs_info->delalloc_root_lock);
9412 list_splice_tail(&splice, &fs_info->delalloc_roots);
9413 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009414 }
Miao Xie573bfb72014-03-06 13:55:03 +08009415 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009416 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009417}
9418
Chris Mason39279cc2007-06-12 06:35:45 -04009419static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9420 const char *symname)
9421{
9422 struct btrfs_trans_handle *trans;
9423 struct btrfs_root *root = BTRFS_I(dir)->root;
9424 struct btrfs_path *path;
9425 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009426 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009427 int err;
9428 int drop_inode = 0;
9429 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309430 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009431 int name_len;
9432 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009433 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009434 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009435 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009436
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009437 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04009438 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
9439 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009440
Josef Bacik9ed74f22009-09-11 16:12:44 -04009441 /*
9442 * 2 items for inode item and ref
9443 * 2 items for dir items
9444 * 1 item for xattr if selinux is on
9445 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04009446 trans = btrfs_start_transaction(root, 5);
9447 if (IS_ERR(trans))
9448 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009449
Li Zefan581bb052011-04-20 10:06:11 +08009450 err = btrfs_find_free_ino(root, &objectid);
9451 if (err)
9452 goto out_unlock;
9453
Josef Bacikaec74772008-07-24 12:12:38 -04009454 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08009455 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04009456 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009457 if (IS_ERR(inode)) {
9458 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009459 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009460 }
Chris Mason39279cc2007-06-12 06:35:45 -04009461
Casey Schauflerad19db72011-12-15 10:09:07 -05009462 /*
9463 * If the active LSM wants to access the inode during
9464 * d_instantiate it needs these. Smack checks to see
9465 * if the filesystem supports xattrs by looking at the
9466 * ops vector.
9467 */
9468 inode->i_fop = &btrfs_file_operations;
9469 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009470 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009471 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9472
9473 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9474 if (err)
9475 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05009476
Josef Bacika1b075d2010-11-19 20:36:11 +00009477 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04009478 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07009479 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009480
9481 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009482 if (!path) {
9483 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07009484 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009485 }
Li Zefan33345d012011-04-20 10:31:50 +08009486 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009487 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009488 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009489 datasize = btrfs_file_extent_calc_inline_size(name_len);
9490 err = btrfs_insert_empty_item(trans, root, path, &key,
9491 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009492 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009493 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07009494 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04009495 }
Chris Mason5f39d392007-10-15 16:14:19 -04009496 leaf = path->nodes[0];
9497 ei = btrfs_item_ptr(leaf, path->slots[0],
9498 struct btrfs_file_extent_item);
9499 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9500 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009501 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009502 btrfs_set_file_extent_encryption(leaf, ei, 0);
9503 btrfs_set_file_extent_compression(leaf, ei, 0);
9504 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9505 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9506
Chris Mason39279cc2007-06-12 06:35:45 -04009507 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009508 write_extent_buffer(leaf, symname, ptr, name_len);
9509 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009510 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009511
Chris Mason39279cc2007-06-12 06:35:45 -04009512 inode->i_op = &btrfs_symlink_inode_operations;
9513 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -04009514 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009515 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009516 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07009517 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04009518 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07009519 goto out_unlock_inode;
9520 }
9521
9522 unlock_new_inode(inode);
9523 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009524
9525out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009526 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04009527 if (drop_inode) {
9528 inode_dec_link_count(inode);
9529 iput(inode);
9530 }
Liu Bob53d3f52012-11-14 14:34:34 +00009531 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04009532 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07009533
9534out_unlock_inode:
9535 drop_inode = 1;
9536 unlock_new_inode(inode);
9537 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04009538}
Chris Mason16432982008-04-10 10:23:21 -04009539
Josef Bacik0af3d002010-06-21 14:48:16 -04009540static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9541 u64 start, u64 num_bytes, u64 min_size,
9542 loff_t actual_len, u64 *alloc_hint,
9543 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009544{
Josef Bacik5dc562c2012-08-17 13:14:17 -04009545 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9546 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009547 struct btrfs_root *root = BTRFS_I(inode)->root;
9548 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009549 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009550 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009551 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04009552 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009553 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04009554
Josef Bacik0af3d002010-06-21 14:48:16 -04009555 if (trans)
9556 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009557 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009558 if (own_trans) {
9559 trans = btrfs_start_transaction(root, 3);
9560 if (IS_ERR(trans)) {
9561 ret = PTR_ERR(trans);
9562 break;
9563 }
Yan Zhengd899e052008-10-30 14:25:28 -04009564 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009565
Chris Mason154ea282013-03-05 11:11:26 -05009566 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
9567 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04009568 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08009569 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009570 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009571 if (own_trans)
9572 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009573 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009574 }
9575
Yan Zhengd899e052008-10-30 14:25:28 -04009576 ret = insert_reserved_file_extent(trans, inode,
9577 cur_offset, ins.objectid,
9578 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009579 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009580 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009581 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04009582 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009583 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009584 btrfs_abort_transaction(trans, root, ret);
9585 if (own_trans)
9586 btrfs_end_transaction(trans, root);
9587 break;
9588 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009589
Chris Masona1ed8352009-09-11 12:27:37 -04009590 btrfs_drop_extent_cache(inode, cur_offset,
9591 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009592
Josef Bacik5dc562c2012-08-17 13:14:17 -04009593 em = alloc_extent_map();
9594 if (!em) {
9595 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9596 &BTRFS_I(inode)->runtime_flags);
9597 goto next;
9598 }
9599
9600 em->start = cur_offset;
9601 em->orig_start = cur_offset;
9602 em->len = ins.offset;
9603 em->block_start = ins.objectid;
9604 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009605 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009606 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009607 em->bdev = root->fs_info->fs_devices->latest_bdev;
9608 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9609 em->generation = trans->transid;
9610
9611 while (1) {
9612 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009613 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009614 write_unlock(&em_tree->lock);
9615 if (ret != -EEXIST)
9616 break;
9617 btrfs_drop_extent_cache(inode, cur_offset,
9618 cur_offset + ins.offset - 1,
9619 0);
9620 }
9621 free_extent_map(em);
9622next:
Yan Zhengd899e052008-10-30 14:25:28 -04009623 num_bytes -= ins.offset;
9624 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009625 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009626
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009627 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009628 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009629 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009630 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009631 (actual_len > inode->i_size) &&
9632 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009633 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009634 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009635 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009636 i_size = cur_offset;
9637 i_size_write(inode, i_size);
9638 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009639 }
9640
Yan Zhengd899e052008-10-30 14:25:28 -04009641 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009642
9643 if (ret) {
9644 btrfs_abort_transaction(trans, root, ret);
9645 if (own_trans)
9646 btrfs_end_transaction(trans, root);
9647 break;
9648 }
Yan Zhengd899e052008-10-30 14:25:28 -04009649
Josef Bacik0af3d002010-06-21 14:48:16 -04009650 if (own_trans)
9651 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009652 }
Yan Zhengd899e052008-10-30 14:25:28 -04009653 return ret;
9654}
9655
Josef Bacik0af3d002010-06-21 14:48:16 -04009656int btrfs_prealloc_file_range(struct inode *inode, int mode,
9657 u64 start, u64 num_bytes, u64 min_size,
9658 loff_t actual_len, u64 *alloc_hint)
9659{
9660 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9661 min_size, actual_len, alloc_hint,
9662 NULL);
9663}
9664
9665int btrfs_prealloc_file_range_trans(struct inode *inode,
9666 struct btrfs_trans_handle *trans, int mode,
9667 u64 start, u64 num_bytes, u64 min_size,
9668 loff_t actual_len, u64 *alloc_hint)
9669{
9670 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9671 min_size, actual_len, alloc_hint, trans);
9672}
9673
Chris Masone6dcd2d2008-07-17 12:53:50 -04009674static int btrfs_set_page_dirty(struct page *page)
9675{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009676 return __set_page_dirty_nobuffers(page);
9677}
9678
Al Viro10556cb2011-06-20 19:28:19 -04009679static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009680{
Li Zefanb83cc962010-12-20 16:04:08 +08009681 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009682 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009683
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009684 if (mask & MAY_WRITE &&
9685 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9686 if (btrfs_root_readonly(root))
9687 return -EROFS;
9688 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9689 return -EACCES;
9690 }
Al Viro2830ba72011-06-20 19:16:29 -04009691 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009692}
Chris Mason39279cc2007-06-12 06:35:45 -04009693
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009694static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9695{
9696 struct btrfs_trans_handle *trans;
9697 struct btrfs_root *root = BTRFS_I(dir)->root;
9698 struct inode *inode = NULL;
9699 u64 objectid;
9700 u64 index;
9701 int ret = 0;
9702
9703 /*
9704 * 5 units required for adding orphan entry
9705 */
9706 trans = btrfs_start_transaction(root, 5);
9707 if (IS_ERR(trans))
9708 return PTR_ERR(trans);
9709
9710 ret = btrfs_find_free_ino(root, &objectid);
9711 if (ret)
9712 goto out;
9713
9714 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9715 btrfs_ino(dir), objectid, mode, &index);
9716 if (IS_ERR(inode)) {
9717 ret = PTR_ERR(inode);
9718 inode = NULL;
9719 goto out;
9720 }
9721
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009722 inode->i_fop = &btrfs_file_operations;
9723 inode->i_op = &btrfs_file_inode_operations;
9724
9725 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009726 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9727
Chris Masonb0d5d102014-09-08 13:08:51 -07009728 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9729 if (ret)
9730 goto out_inode;
9731
9732 ret = btrfs_update_inode(trans, root, inode);
9733 if (ret)
9734 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009735 ret = btrfs_orphan_add(trans, inode);
9736 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009737 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009738
Filipe Manana5762b5c2014-08-01 00:10:32 +01009739 /*
9740 * We set number of links to 0 in btrfs_new_inode(), and here we set
9741 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9742 * through:
9743 *
9744 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9745 */
9746 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009747 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009748 d_tmpfile(dentry, inode);
9749 mark_inode_dirty(inode);
9750
9751out:
9752 btrfs_end_transaction(trans, root);
9753 if (ret)
9754 iput(inode);
9755 btrfs_balance_delayed_items(root);
9756 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009757 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009758
9759out_inode:
9760 unlock_new_inode(inode);
9761 goto out;
9762
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009763}
9764
Filipe Mananab38ef712014-11-13 17:01:45 +00009765/* Inspired by filemap_check_errors() */
9766int btrfs_inode_check_errors(struct inode *inode)
9767{
9768 int ret = 0;
9769
9770 if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) &&
9771 test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags))
9772 ret = -ENOSPC;
9773 if (test_bit(AS_EIO, &inode->i_mapping->flags) &&
9774 test_and_clear_bit(AS_EIO, &inode->i_mapping->flags))
9775 ret = -EIO;
9776
9777 return ret;
9778}
9779
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009780static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009781 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009782 .lookup = btrfs_lookup,
9783 .create = btrfs_create,
9784 .unlink = btrfs_unlink,
9785 .link = btrfs_link,
9786 .mkdir = btrfs_mkdir,
9787 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +02009788 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -04009789 .symlink = btrfs_symlink,
9790 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009791 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009792 .setxattr = btrfs_setxattr,
9793 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009794 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009795 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009796 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009797 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009798 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009799 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009800 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009801};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009802static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009803 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009804 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009805 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009806 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009807 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009808};
Yan, Zheng76dda932009-09-21 16:00:26 -04009809
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009810static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009811 .llseek = generic_file_llseek,
9812 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009813 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009814 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009815#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009816 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009817#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009818 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009819 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009820};
9821
Chris Masond1310b22008-01-24 16:13:08 -05009822static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009823 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009824 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009825 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009826 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009827 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009828 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009829 .set_bit_hook = btrfs_set_bit_hook,
9830 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009831 .merge_extent_hook = btrfs_merge_extent_hook,
9832 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009833};
9834
Chris Mason35054392009-01-21 13:11:13 -05009835/*
9836 * btrfs doesn't support the bmap operation because swapfiles
9837 * use bmap to make a mapping of extents in the file. They assume
9838 * these extents won't change over the life of the file and they
9839 * use the bmap result to do IO directly to the drive.
9840 *
9841 * the btrfs bmap call would return logical addresses that aren't
9842 * suitable for IO and they also will change frequently as COW
9843 * operations happen. So, swapfile + btrfs == corruption.
9844 *
9845 * For now we're avoiding this by dropping bmap.
9846 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009847static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009848 .readpage = btrfs_readpage,
9849 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04009850 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009851 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009852 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009853 .invalidatepage = btrfs_invalidatepage,
9854 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009855 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009856 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009857};
9858
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009859static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009860 .readpage = btrfs_readpage,
9861 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009862 .invalidatepage = btrfs_invalidatepage,
9863 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009864};
9865
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009866static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009867 .getattr = btrfs_getattr,
9868 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009869 .setxattr = btrfs_setxattr,
9870 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009871 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009872 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009873 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009874 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009875 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009876 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009877 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009878};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009879static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009880 .getattr = btrfs_getattr,
9881 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009882 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009883 .setxattr = btrfs_setxattr,
9884 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009885 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009886 .removexattr = btrfs_removexattr,
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,
Josef Bacik618e21d2007-07-11 10:18:17 -04009890};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009891static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009892 .readlink = generic_readlink,
9893 .follow_link = page_follow_link_light,
9894 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009895 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009896 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009897 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009898 .setxattr = btrfs_setxattr,
9899 .getxattr = btrfs_getxattr,
9900 .listxattr = btrfs_listxattr,
9901 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009902 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009903};
Yan, Zheng76dda932009-09-21 16:00:26 -04009904
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009905const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009906 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009907 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009908};