blob: bb74a4181075b17eb47d4fb8c8a6a08ca316694c [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070035#include <linux/aio.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050037#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040038#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040039#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020041#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050042#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000043#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050044#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040045#include <linux/posix_acl_xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040060#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061#include "props.h"
Chris Mason39279cc2007-06-12 06:35:45 -040062
63struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080064 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040065 struct btrfs_root *root;
66};
67
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070068static const struct inode_operations btrfs_dir_inode_operations;
69static const struct inode_operations btrfs_symlink_inode_operations;
70static const struct inode_operations btrfs_dir_ro_inode_operations;
71static const struct inode_operations btrfs_special_inode_operations;
72static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070073static const struct address_space_operations btrfs_aops;
74static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070075static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050076static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040077
78static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000079static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040080struct kmem_cache *btrfs_trans_handle_cachep;
81struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040082struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050083struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040084
85#define S_SHIFT 12
86static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
87 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
88 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
89 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
90 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
91 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
92 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
93 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
94};
95
Eric Sandeen3972f262013-01-12 02:57:22 +000096static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050097static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040098static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050099static noinline int cow_file_range(struct inode *inode,
100 struct page *locked_page,
101 u64 start, u64 end, int *page_started,
102 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400103static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
104 u64 len, u64 orig_start,
105 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400106 u64 orig_block_len, u64 ram_bytes,
107 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400108
Eric Sandeen48a3b632013-04-25 20:41:01 +0000109static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400110
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000111static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500112 struct inode *inode, struct inode *dir,
113 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500114{
115 int err;
116
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000117 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500118 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500119 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500120 return err;
121}
122
Chris Masond352ac62008-09-29 15:18:18 -0400123/*
Chris Masonc8b97812008-10-29 14:49:59 -0400124 * this does all the hard work for inserting an inline extent into
125 * the btree. The caller should have done a btrfs_drop_extents so that
126 * no overlapping inline items exist in the btree
127 */
Chris Mason40f76582014-05-21 13:35:51 -0700128static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000129 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400130 struct btrfs_root *root, struct inode *inode,
131 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000132 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400133 struct page **compressed_pages)
134{
Chris Masonc8b97812008-10-29 14:49:59 -0400135 struct extent_buffer *leaf;
136 struct page *page = NULL;
137 char *kaddr;
138 unsigned long ptr;
139 struct btrfs_file_extent_item *ei;
140 int err = 0;
141 int ret;
142 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400143 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400144
Li Zefanfe3f5662011-03-28 08:30:38 +0000145 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400146 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400147
Chris Masonc8b97812008-10-29 14:49:59 -0400148 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000149
150 if (!extent_inserted) {
151 struct btrfs_key key;
152 size_t datasize;
153
154 key.objectid = btrfs_ino(inode);
155 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200156 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000157
158 datasize = btrfs_file_extent_calc_inline_size(cur_size);
159 path->leave_spinning = 1;
160 ret = btrfs_insert_empty_item(trans, root, path, &key,
161 datasize);
162 if (ret) {
163 err = ret;
164 goto fail;
165 }
Chris Masonc8b97812008-10-29 14:49:59 -0400166 }
167 leaf = path->nodes[0];
168 ei = btrfs_item_ptr(leaf, path->slots[0],
169 struct btrfs_file_extent_item);
170 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
171 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
172 btrfs_set_file_extent_encryption(leaf, ei, 0);
173 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
174 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
175 ptr = btrfs_file_extent_inline_start(ei);
176
Li Zefan261507a02010-12-17 14:21:50 +0800177 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400178 struct page *cpage;
179 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500180 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400181 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500182 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400183 PAGE_CACHE_SIZE);
184
Cong Wang7ac687d2011-11-25 23:14:28 +0800185 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400186 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800187 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400188
189 i++;
190 ptr += cur_size;
191 compressed_size -= cur_size;
192 }
193 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800194 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400195 } else {
196 page = find_get_page(inode->i_mapping,
197 start >> PAGE_CACHE_SHIFT);
198 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800199 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400200 offset = start & (PAGE_CACHE_SIZE - 1);
201 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800202 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400203 page_cache_release(page);
204 }
205 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000206 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400207
Yan, Zhengc2167752009-11-12 09:34:21 +0000208 /*
209 * we're an inline extent, so nobody can
210 * extend the file past i_size without locking
211 * a page we already have locked.
212 *
213 * We must do any isize and inode updates
214 * before we unlock the pages. Otherwise we
215 * could end up racing with unlink.
216 */
Chris Masonc8b97812008-10-29 14:49:59 -0400217 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100218 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000219
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100220 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400221fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400222 return err;
223}
224
225
226/*
227 * conditionally insert an inline extent into the file. This
228 * does the checks required to make sure the data is small enough
229 * to fit as an inline extent.
230 */
Josef Bacik00361582013-08-14 14:02:47 -0400231static noinline int cow_file_range_inline(struct btrfs_root *root,
232 struct inode *inode, u64 start,
233 u64 end, size_t compressed_size,
234 int compress_type,
235 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400236{
Josef Bacik00361582013-08-14 14:02:47 -0400237 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400238 u64 isize = i_size_read(inode);
239 u64 actual_end = min(end + 1, isize);
240 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000241 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400242 u64 data_len = inline_len;
243 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000244 struct btrfs_path *path;
245 int extent_inserted = 0;
246 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400247
248 if (compressed_size)
249 data_len = compressed_size;
250
251 if (start > 0 ||
Wang Shilong354877b2014-07-17 11:44:11 +0800252 actual_end > PAGE_CACHE_SIZE ||
253 data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400254 (!compressed_size &&
255 (actual_end & (root->sectorsize - 1)) == 0) ||
256 end + 1 < isize ||
257 data_len > root->fs_info->max_inline) {
258 return 1;
259 }
260
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000261 path = btrfs_alloc_path();
262 if (!path)
263 return -ENOMEM;
264
Josef Bacik00361582013-08-14 14:02:47 -0400265 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000266 if (IS_ERR(trans)) {
267 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400268 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000269 }
Josef Bacik00361582013-08-14 14:02:47 -0400270 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
271
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000272 if (compressed_size && compressed_pages)
273 extent_item_size = btrfs_file_extent_calc_inline_size(
274 compressed_size);
275 else
276 extent_item_size = btrfs_file_extent_calc_inline_size(
277 inline_len);
278
279 ret = __btrfs_drop_extents(trans, root, inode, path,
280 start, aligned_end, NULL,
281 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400282 if (ret) {
283 btrfs_abort_transaction(trans, root, ret);
284 goto out;
285 }
Chris Masonc8b97812008-10-29 14:49:59 -0400286
287 if (isize > actual_end)
288 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000289 ret = insert_inline_extent(trans, path, extent_inserted,
290 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400291 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000292 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400293 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100294 btrfs_abort_transaction(trans, root, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400295 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400296 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400297 ret = 1;
298 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100299 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400300
Josef Bacikbdc20e62013-02-28 13:23:38 -0500301 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400302 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400303 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400304out:
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000305 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400306 btrfs_end_transaction(trans, root);
307 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400308}
309
Chris Mason771ed682008-11-06 22:02:51 -0500310struct async_extent {
311 u64 start;
312 u64 ram_size;
313 u64 compressed_size;
314 struct page **pages;
315 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800316 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500317 struct list_head list;
318};
319
320struct async_cow {
321 struct inode *inode;
322 struct btrfs_root *root;
323 struct page *locked_page;
324 u64 start;
325 u64 end;
326 struct list_head extents;
327 struct btrfs_work work;
328};
329
330static noinline int add_async_extent(struct async_cow *cow,
331 u64 start, u64 ram_size,
332 u64 compressed_size,
333 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800334 unsigned long nr_pages,
335 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500336{
337 struct async_extent *async_extent;
338
339 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100340 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500341 async_extent->start = start;
342 async_extent->ram_size = ram_size;
343 async_extent->compressed_size = compressed_size;
344 async_extent->pages = pages;
345 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800346 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500347 list_add_tail(&async_extent->list, &cow->extents);
348 return 0;
349}
350
Wang Shilongf79707b2014-07-17 11:44:09 +0800351static inline int inode_need_compress(struct inode *inode)
352{
353 struct btrfs_root *root = BTRFS_I(inode)->root;
354
355 /* force compress */
356 if (btrfs_test_opt(root, FORCE_COMPRESS))
357 return 1;
358 /* bad compression ratios */
359 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
360 return 0;
361 if (btrfs_test_opt(root, COMPRESS) ||
362 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
363 BTRFS_I(inode)->force_compress)
364 return 1;
365 return 0;
366}
367
Chris Masonc8b97812008-10-29 14:49:59 -0400368/*
Chris Mason771ed682008-11-06 22:02:51 -0500369 * we create compressed extents in two phases. The first
370 * phase compresses a range of pages that have already been
371 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400372 *
Chris Mason771ed682008-11-06 22:02:51 -0500373 * This is done inside an ordered work queue, and the compression
374 * is spread across many cpus. The actual IO submission is step
375 * two, and the ordered work queue takes care of making sure that
376 * happens in the same order things were put onto the queue by
377 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400378 *
Chris Mason771ed682008-11-06 22:02:51 -0500379 * If this code finds it can't get good compression, it puts an
380 * entry onto the work queue to write the uncompressed bytes. This
381 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300382 * are written in the same order that the flusher thread sent them
383 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400384 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100385static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500386 struct page *locked_page,
387 u64 start, u64 end,
388 struct async_cow *async_cow,
389 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400390{
391 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400392 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400393 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400394 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500395 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400396 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400397 struct page **pages = NULL;
398 unsigned long nr_pages;
399 unsigned long nr_pages_ret = 0;
400 unsigned long total_compressed = 0;
401 unsigned long total_in = 0;
402 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500403 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400404 int i;
405 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800406 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400407 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400408
Liu Bo4cb13e52012-03-29 09:57:45 -0400409 /* if this is a small write inside eof, kick off a defrag */
410 if ((end - start + 1) < 16 * 1024 &&
411 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400412 btrfs_add_inode_defrag(NULL, inode);
413
Chris Mason42dc7ba2008-12-15 11:44:56 -0500414 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400415again:
416 will_compress = 0;
417 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
418 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
419
Chris Masonf03d9301f2009-02-04 09:31:06 -0500420 /*
421 * we don't want to send crud past the end of i_size through
422 * compression, that's just a waste of CPU time. So, if the
423 * end of the file is before the start of our current
424 * requested range of bytes, we bail out to the uncompressed
425 * cleanup code that can deal with all of this.
426 *
427 * It isn't really the fastest way to fix things, but this is a
428 * very uncommon corner.
429 */
430 if (actual_end <= start)
431 goto cleanup_and_bail_uncompressed;
432
Chris Masonc8b97812008-10-29 14:49:59 -0400433 total_compressed = actual_end - start;
434
Shilong Wang4bcbb332014-10-07 18:44:35 -0400435 /*
436 * skip compression for a small file range(<=blocksize) that
437 * isn't an inline extent, since it dosen't save disk space at all.
438 */
439 if (total_compressed <= blocksize &&
440 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
441 goto cleanup_and_bail_uncompressed;
442
Chris Masonc8b97812008-10-29 14:49:59 -0400443 /* we want to make sure that amount of ram required to uncompress
444 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500445 * of a compressed extent to 128k. This is a crucial number
446 * because it also controls how easily we can spread reads across
447 * cpus for decompression.
448 *
449 * We also want to make sure the amount of IO required to do
450 * a random read is reasonably small, so we limit the size of
451 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400452 */
453 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000454 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500455 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400456 total_in = 0;
457 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400458
Chris Mason771ed682008-11-06 22:02:51 -0500459 /*
460 * we do compression for mount -o compress and when the
461 * inode has not been flagged as nocompress. This flag can
462 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400463 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800464 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400465 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400466 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800467 if (!pages) {
468 /* just bail out to the uncompressed code */
469 goto cont;
470 }
Chris Mason179e29e2007-11-01 11:28:41 -0400471
Li Zefan261507a02010-12-17 14:21:50 +0800472 if (BTRFS_I(inode)->force_compress)
473 compress_type = BTRFS_I(inode)->force_compress;
474
Chris Mason4adaa612013-03-26 13:07:00 -0400475 /*
476 * we need to call clear_page_dirty_for_io on each
477 * page in the range. Otherwise applications with the file
478 * mmap'd can wander in and change the page contents while
479 * we are compressing them.
480 *
481 * If the compression fails for any reason, we set the pages
482 * dirty again later on.
483 */
484 extent_range_clear_dirty_for_io(inode, start, end);
485 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800486 ret = btrfs_compress_pages(compress_type,
487 inode->i_mapping, start,
488 total_compressed, pages,
489 nr_pages, &nr_pages_ret,
490 &total_in,
491 &total_compressed,
492 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400493
494 if (!ret) {
495 unsigned long offset = total_compressed &
496 (PAGE_CACHE_SIZE - 1);
497 struct page *page = pages[nr_pages_ret - 1];
498 char *kaddr;
499
500 /* zero the tail end of the last page, we might be
501 * sending it down to disk
502 */
503 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800504 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400505 memset(kaddr + offset, 0,
506 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800507 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400508 }
509 will_compress = 1;
510 }
511 }
Li Zefan560f7d72011-09-08 10:22:01 +0800512cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400513 if (start == 0) {
514 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500515 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400516 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500517 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400518 */
Josef Bacik00361582013-08-14 14:02:47 -0400519 ret = cow_file_range_inline(root, inode, start, end,
520 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400521 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500522 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400523 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000524 total_compressed,
525 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400526 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100527 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400528 unsigned long clear_flags = EXTENT_DELALLOC |
529 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100530 unsigned long page_error_op;
531
Josef Bacik151a41b2013-07-29 13:22:24 -0400532 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100533 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400534
Chris Mason771ed682008-11-06 22:02:51 -0500535 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100536 * inline extent creation worked or returned error,
537 * we don't need to create any more async work items.
538 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500539 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400540 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400541 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400542 PAGE_CLEAR_DIRTY |
543 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100544 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400545 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400546 goto free_pages_out;
547 }
548 }
549
550 if (will_compress) {
551 /*
552 * we aren't doing an inline extent round the compressed size
553 * up to a block size boundary so the allocator does sane
554 * things
555 */
Qu Wenruofda28322013-02-26 08:10:22 +0000556 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400557
558 /*
559 * one last check to make sure the compression is really a
560 * win, compare the page count read with the blocks on disk
561 */
Qu Wenruofda28322013-02-26 08:10:22 +0000562 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400563 if (total_compressed >= total_in) {
564 will_compress = 0;
565 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400566 num_bytes = total_in;
567 }
568 }
569 if (!will_compress && pages) {
570 /*
571 * the compression code ran but failed to make things smaller,
572 * free any pages it allocated and our page pointer array
573 */
574 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400575 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400576 page_cache_release(pages[i]);
577 }
578 kfree(pages);
579 pages = NULL;
580 total_compressed = 0;
581 nr_pages_ret = 0;
582
583 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500584 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
585 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500586 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500587 }
Chris Masonc8b97812008-10-29 14:49:59 -0400588 }
Chris Mason771ed682008-11-06 22:02:51 -0500589 if (will_compress) {
590 *num_added += 1;
591
592 /* the async work queues will take care of doing actual
593 * allocation on disk for these compressed pages,
594 * and will submit them to the elevator.
595 */
596 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800597 total_compressed, pages, nr_pages_ret,
598 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500599
Yan, Zheng24ae6362010-12-06 07:02:36 +0000600 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500601 start += num_bytes;
602 pages = NULL;
603 cond_resched();
604 goto again;
605 }
606 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500607cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500608 /*
609 * No compression, but we still need to write the pages in
610 * the file we've been given so far. redirty the locked
611 * page if it corresponds to our extent and set things up
612 * for the async work queue to run cow_file_range to do
613 * the normal delalloc dance
614 */
615 if (page_offset(locked_page) >= start &&
616 page_offset(locked_page) <= end) {
617 __set_page_dirty_nobuffers(locked_page);
618 /* unlocked later on in the async handlers */
619 }
Chris Mason4adaa612013-03-26 13:07:00 -0400620 if (redirty)
621 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800622 add_async_extent(async_cow, start, end - start + 1,
623 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500624 *num_added += 1;
625 }
626
Filipe Mananac44f6492014-10-09 21:15:44 +0100627 return;
Chris Mason771ed682008-11-06 22:02:51 -0500628
629free_pages_out:
630 for (i = 0; i < nr_pages_ret; i++) {
631 WARN_ON(pages[i]->mapping);
632 page_cache_release(pages[i]);
633 }
Chris Masond3977122009-01-05 21:25:51 -0500634 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500635}
Chris Mason771ed682008-11-06 22:02:51 -0500636
Filipe Manana40ae8372014-10-06 22:14:24 +0100637static void free_async_extent_pages(struct async_extent *async_extent)
638{
639 int i;
640
641 if (!async_extent->pages)
642 return;
643
644 for (i = 0; i < async_extent->nr_pages; i++) {
645 WARN_ON(async_extent->pages[i]->mapping);
646 page_cache_release(async_extent->pages[i]);
647 }
648 kfree(async_extent->pages);
649 async_extent->nr_pages = 0;
650 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500651}
652
653/*
654 * phase two of compressed writeback. This is the ordered portion
655 * of the code, which only gets called in the order the work was
656 * queued. We walk all the async extents created by compress_file_range
657 * and send them down to the disk.
658 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100659static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500660 struct async_cow *async_cow)
661{
662 struct async_extent *async_extent;
663 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500664 struct btrfs_key ins;
665 struct extent_map *em;
666 struct btrfs_root *root = BTRFS_I(inode)->root;
667 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
668 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500669 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500670
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500671again:
Chris Masond3977122009-01-05 21:25:51 -0500672 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500673 async_extent = list_entry(async_cow->extents.next,
674 struct async_extent, list);
675 list_del(&async_extent->list);
676
677 io_tree = &BTRFS_I(inode)->io_tree;
678
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500679retry:
Chris Mason771ed682008-11-06 22:02:51 -0500680 /* did the compression code fall back to uncompressed IO? */
681 if (!async_extent->pages) {
682 int page_started = 0;
683 unsigned long nr_written = 0;
684
685 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000686 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100687 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500688
689 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500690 ret = cow_file_range(inode, async_cow->locked_page,
691 async_extent->start,
692 async_extent->start +
693 async_extent->ram_size - 1,
694 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500695
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100696 /* JDM XXX */
697
Chris Mason771ed682008-11-06 22:02:51 -0500698 /*
699 * if page_started, cow_file_range inserted an
700 * inline extent and took care of all the unlocking
701 * and IO for us. Otherwise, we need to submit
702 * all those pages down to the drive.
703 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500704 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500705 extent_write_locked_range(io_tree,
706 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500707 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500708 async_extent->ram_size - 1,
709 btrfs_get_extent,
710 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500711 else if (ret)
712 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500713 kfree(async_extent);
714 cond_resched();
715 continue;
716 }
717
718 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100719 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500720
Josef Bacik00361582013-08-14 14:02:47 -0400721 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500722 async_extent->compressed_size,
723 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800724 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500725 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100726 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500727
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400728 if (ret == -ENOSPC) {
729 unlock_extent(io_tree, async_extent->start,
730 async_extent->start +
731 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800732
733 /*
734 * we need to redirty the pages if we decide to
735 * fallback to uncompressed IO, otherwise we
736 * will not submit these pages down to lower
737 * layers.
738 */
739 extent_range_redirty_for_io(inode,
740 async_extent->start,
741 async_extent->start +
742 async_extent->ram_size - 1);
743
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100744 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400745 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500746 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500747 }
748
Yan, Zhengc2167752009-11-12 09:34:21 +0000749 /*
750 * here we're doing allocation and writeback of the
751 * compressed pages
752 */
753 btrfs_drop_extent_cache(inode, async_extent->start,
754 async_extent->start +
755 async_extent->ram_size - 1, 0);
756
David Sterba172ddd62011-04-21 00:48:27 +0200757 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000758 if (!em) {
759 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500760 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000761 }
Chris Mason771ed682008-11-06 22:02:51 -0500762 em->start = async_extent->start;
763 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500764 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400765 em->mod_start = em->start;
766 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500767
768 em->block_start = ins.objectid;
769 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500770 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400771 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500772 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800773 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500774 set_bit(EXTENT_FLAG_PINNED, &em->flags);
775 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400776 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500777
Chris Masond3977122009-01-05 21:25:51 -0500778 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400779 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400780 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400781 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500782 if (ret != -EEXIST) {
783 free_extent_map(em);
784 break;
785 }
786 btrfs_drop_extent_cache(inode, async_extent->start,
787 async_extent->start +
788 async_extent->ram_size - 1, 0);
789 }
790
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500791 if (ret)
792 goto out_free_reserve;
793
Li Zefan261507a02010-12-17 14:21:50 +0800794 ret = btrfs_add_ordered_extent_compress(inode,
795 async_extent->start,
796 ins.objectid,
797 async_extent->ram_size,
798 ins.offset,
799 BTRFS_ORDERED_COMPRESSED,
800 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100801 if (ret) {
802 btrfs_drop_extent_cache(inode, async_extent->start,
803 async_extent->start +
804 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500805 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100806 }
Chris Mason771ed682008-11-06 22:02:51 -0500807
Chris Mason771ed682008-11-06 22:02:51 -0500808 /*
809 * clear dirty, set writeback and unlock the pages.
810 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400811 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400812 async_extent->start +
813 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400814 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
815 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400816 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500817 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500818 async_extent->start,
819 async_extent->ram_size,
820 ins.objectid,
821 ins.offset, async_extent->pages,
822 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100823 if (ret) {
824 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
825 struct page *p = async_extent->pages[0];
826 const u64 start = async_extent->start;
827 const u64 end = start + async_extent->ram_size - 1;
828
829 p->mapping = inode->i_mapping;
830 tree->ops->writepage_end_io_hook(p, start, end,
831 NULL, 0);
832 p->mapping = NULL;
833 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
834 PAGE_END_WRITEBACK |
835 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100836 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100837 }
Chris Mason771ed682008-11-06 22:02:51 -0500838 alloc_hint = ins.objectid + ins.offset;
839 kfree(async_extent);
840 cond_resched();
841 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100842 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500843out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800844 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100845out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400846 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500847 async_extent->start +
848 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400849 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400850 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
851 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100852 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
853 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100854 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100855 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500856 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500857}
858
Josef Bacik4b46fce2010-05-23 11:00:55 -0400859static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
860 u64 num_bytes)
861{
862 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
863 struct extent_map *em;
864 u64 alloc_hint = 0;
865
866 read_lock(&em_tree->lock);
867 em = search_extent_mapping(em_tree, start, num_bytes);
868 if (em) {
869 /*
870 * if block start isn't an actual block number then find the
871 * first block in this inode and use that as a hint. If that
872 * block is also bogus then just don't worry about it.
873 */
874 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
875 free_extent_map(em);
876 em = search_extent_mapping(em_tree, 0, 0);
877 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
878 alloc_hint = em->block_start;
879 if (em)
880 free_extent_map(em);
881 } else {
882 alloc_hint = em->block_start;
883 free_extent_map(em);
884 }
885 }
886 read_unlock(&em_tree->lock);
887
888 return alloc_hint;
889}
890
Chris Mason771ed682008-11-06 22:02:51 -0500891/*
892 * when extent_io.c finds a delayed allocation range in the file,
893 * the call backs end up in this code. The basic idea is to
894 * allocate extents on disk for the range, and create ordered data structs
895 * in ram to track those extents.
896 *
897 * locked_page is the page that writepage had locked already. We use
898 * it to make sure we don't do extra locks or unlocks.
899 *
900 * *page_started is set to one if we unlock locked_page and do everything
901 * required to start IO on it. It may be clean and already done with
902 * IO when we return.
903 */
Josef Bacik00361582013-08-14 14:02:47 -0400904static noinline int cow_file_range(struct inode *inode,
905 struct page *locked_page,
906 u64 start, u64 end, int *page_started,
907 unsigned long *nr_written,
908 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500909{
Josef Bacik00361582013-08-14 14:02:47 -0400910 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500911 u64 alloc_hint = 0;
912 u64 num_bytes;
913 unsigned long ram_size;
914 u64 disk_num_bytes;
915 u64 cur_alloc_size;
916 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500917 struct btrfs_key ins;
918 struct extent_map *em;
919 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
920 int ret = 0;
921
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400922 if (btrfs_is_free_space_inode(inode)) {
923 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500924 ret = -EINVAL;
925 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400926 }
Chris Mason771ed682008-11-06 22:02:51 -0500927
Qu Wenruofda28322013-02-26 08:10:22 +0000928 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500929 num_bytes = max(blocksize, num_bytes);
930 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500931
Chris Mason4cb53002011-05-24 15:35:30 -0400932 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400933 if (num_bytes < 64 * 1024 &&
934 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400935 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400936
Chris Mason771ed682008-11-06 22:02:51 -0500937 if (start == 0) {
938 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400939 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
940 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500941 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400942 extent_clear_unlock_delalloc(inode, start, end, NULL,
943 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400944 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400945 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
946 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000947
Chris Mason771ed682008-11-06 22:02:51 -0500948 *nr_written = *nr_written +
949 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
950 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500951 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100952 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100953 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500954 }
955 }
Chris Masonc8b97812008-10-29 14:49:59 -0400956
957 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200958 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400959
Josef Bacik4b46fce2010-05-23 11:00:55 -0400960 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400961 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400962
Chris Masond3977122009-01-05 21:25:51 -0500963 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400964 unsigned long op;
965
Josef Bacik287a0ab2010-03-19 18:07:23 +0000966 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400967 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500968 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800969 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400970 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100971 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500972
David Sterba172ddd62011-04-21 00:48:27 +0200973 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000974 if (!em) {
975 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000976 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000977 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400978 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500979 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500980 ram_size = ins.offset;
981 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400982 em->mod_start = em->start;
983 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400984
Chris Masone6dcd2d2008-07-17 12:53:50 -0400985 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400986 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500987 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400988 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400989 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400990 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400991 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400992
Chris Masond3977122009-01-05 21:25:51 -0500993 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400994 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400995 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400996 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400997 if (ret != -EEXIST) {
998 free_extent_map(em);
999 break;
1000 }
1001 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001002 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001003 }
Liu Boace68ba2013-04-22 10:53:47 +00001004 if (ret)
1005 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001006
Chris Mason98d20f62008-04-14 09:46:10 -04001007 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001008 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001009 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001010 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001011 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001012
Yan Zheng17d217f2008-12-12 10:03:38 -05001013 if (root->root_key.objectid ==
1014 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1015 ret = btrfs_reloc_clone_csums(inode, start,
1016 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001017 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001018 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001019 }
1020
Chris Masond3977122009-01-05 21:25:51 -05001021 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001022 break;
Chris Masond3977122009-01-05 21:25:51 -05001023
Chris Masonc8b97812008-10-29 14:49:59 -04001024 /* we're not doing compressed IO, don't unlock the first
1025 * page (which the caller expects to stay locked), don't
1026 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001027 *
1028 * Do set the Private2 bit so we know this page was properly
1029 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001030 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001031 op = unlock ? PAGE_UNLOCK : 0;
1032 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001033
Josef Bacikc2790a22013-07-29 11:20:47 -04001034 extent_clear_unlock_delalloc(inode, start,
1035 start + ram_size - 1, locked_page,
1036 EXTENT_LOCKED | EXTENT_DELALLOC,
1037 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001038 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001039 num_bytes -= cur_alloc_size;
1040 alloc_hint = ins.objectid + ins.offset;
1041 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001042 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001043out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001044 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001045
Filipe Mananad9f85962014-08-25 10:43:00 +01001046out_drop_extent_cache:
1047 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001048out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001049 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001050out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001051 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001052 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1053 EXTENT_DELALLOC | EXTENT_DEFRAG,
1054 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1055 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001056 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001057}
Chris Masonc8b97812008-10-29 14:49:59 -04001058
Chris Mason771ed682008-11-06 22:02:51 -05001059/*
1060 * work queue call back to started compression on a file and pages
1061 */
1062static noinline void async_cow_start(struct btrfs_work *work)
1063{
1064 struct async_cow *async_cow;
1065 int num_added = 0;
1066 async_cow = container_of(work, struct async_cow, work);
1067
1068 compress_file_range(async_cow->inode, async_cow->locked_page,
1069 async_cow->start, async_cow->end, async_cow,
1070 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001071 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001072 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001073 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001074 }
Chris Mason771ed682008-11-06 22:02:51 -05001075}
1076
1077/*
1078 * work queue call back to submit previously compressed pages
1079 */
1080static noinline void async_cow_submit(struct btrfs_work *work)
1081{
1082 struct async_cow *async_cow;
1083 struct btrfs_root *root;
1084 unsigned long nr_pages;
1085
1086 async_cow = container_of(work, struct async_cow, work);
1087
1088 root = async_cow->root;
1089 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1090 PAGE_CACHE_SHIFT;
1091
Josef Bacik66657b32012-08-01 15:36:24 -04001092 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001093 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001094 waitqueue_active(&root->fs_info->async_submit_wait))
1095 wake_up(&root->fs_info->async_submit_wait);
1096
Chris Masond3977122009-01-05 21:25:51 -05001097 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001098 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001099}
Chris Masonc8b97812008-10-29 14:49:59 -04001100
Chris Mason771ed682008-11-06 22:02:51 -05001101static noinline void async_cow_free(struct btrfs_work *work)
1102{
1103 struct async_cow *async_cow;
1104 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001105 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001106 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001107 kfree(async_cow);
1108}
1109
1110static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1111 u64 start, u64 end, int *page_started,
1112 unsigned long *nr_written)
1113{
1114 struct async_cow *async_cow;
1115 struct btrfs_root *root = BTRFS_I(inode)->root;
1116 unsigned long nr_pages;
1117 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001118 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001119
Chris Masona3429ab2009-10-08 12:30:20 -04001120 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1121 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001122 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001123 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001124 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001125 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001126 async_cow->root = root;
1127 async_cow->locked_page = locked_page;
1128 async_cow->start = start;
1129
Wang Shilongf79707b2014-07-17 11:44:09 +08001130 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1131 !btrfs_test_opt(root, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001132 cur_end = end;
1133 else
1134 cur_end = min(end, start + 512 * 1024 - 1);
1135
1136 async_cow->end = cur_end;
1137 INIT_LIST_HEAD(&async_cow->extents);
1138
Liu Bo9e0af232014-08-15 23:36:53 +08001139 btrfs_init_work(&async_cow->work,
1140 btrfs_delalloc_helper,
1141 async_cow_start, async_cow_submit,
1142 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001143
Chris Mason771ed682008-11-06 22:02:51 -05001144 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1145 PAGE_CACHE_SHIFT;
1146 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1147
Qu Wenruoafe3d242014-02-28 10:46:07 +08001148 btrfs_queue_work(root->fs_info->delalloc_workers,
1149 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001150
1151 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1152 wait_event(root->fs_info->async_submit_wait,
1153 (atomic_read(&root->fs_info->async_delalloc_pages) <
1154 limit));
1155 }
1156
Chris Masond3977122009-01-05 21:25:51 -05001157 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001158 atomic_read(&root->fs_info->async_delalloc_pages)) {
1159 wait_event(root->fs_info->async_submit_wait,
1160 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1161 0));
1162 }
1163
1164 *nr_written += nr_pages;
1165 start = cur_end + 1;
1166 }
1167 *page_started = 1;
1168 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001169}
1170
Chris Masond3977122009-01-05 21:25:51 -05001171static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001172 u64 bytenr, u64 num_bytes)
1173{
1174 int ret;
1175 struct btrfs_ordered_sum *sums;
1176 LIST_HEAD(list);
1177
Yan Zheng07d400a2009-01-06 11:42:00 -05001178 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001179 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001180 if (ret == 0 && list_empty(&list))
1181 return 0;
1182
1183 while (!list_empty(&list)) {
1184 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1185 list_del(&sums->list);
1186 kfree(sums);
1187 }
1188 return 1;
1189}
1190
Chris Masond352ac62008-09-29 15:18:18 -04001191/*
1192 * when nowcow writeback call back. This checks for snapshots or COW copies
1193 * of the extents that exist in the file, and COWs the file as required.
1194 *
1195 * If no cow copies or snapshots exist, we write directly to the existing
1196 * blocks on disk
1197 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001198static noinline int run_delalloc_nocow(struct inode *inode,
1199 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001200 u64 start, u64 end, int *page_started, int force,
1201 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001202{
Chris Masonbe20aa92007-12-17 20:14:01 -05001203 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001204 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001205 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001206 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001207 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001208 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001209 u64 cow_start;
1210 u64 cur_offset;
1211 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001212 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001213 u64 disk_bytenr;
1214 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001215 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001216 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001217 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001218 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001219 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001220 int nocow;
1221 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001222 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001223 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001224
1225 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001226 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001227 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1228 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001229 EXTENT_DO_ACCOUNTING |
1230 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001231 PAGE_CLEAR_DIRTY |
1232 PAGE_SET_WRITEBACK |
1233 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001234 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001235 }
Li Zefan82d59022011-04-20 10:33:24 +08001236
Liu Bo83eea1f2012-07-10 05:28:39 -06001237 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001238
1239 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001240 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001241 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001242 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001243
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001244 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001245 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1246 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001247 EXTENT_DO_ACCOUNTING |
1248 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001249 PAGE_CLEAR_DIRTY |
1250 PAGE_SET_WRITEBACK |
1251 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001252 btrfs_free_path(path);
1253 return PTR_ERR(trans);
1254 }
1255
Josef Bacik74b21072011-04-13 12:02:53 -04001256 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001257
Yan Zheng80ff3852008-10-30 14:20:02 -04001258 cow_start = (u64)-1;
1259 cur_offset = start;
1260 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001261 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001263 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001264 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1266 leaf = path->nodes[0];
1267 btrfs_item_key_to_cpu(leaf, &found_key,
1268 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001269 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001270 found_key.type == BTRFS_EXTENT_DATA_KEY)
1271 path->slots[0]--;
1272 }
1273 check_prev = 0;
1274next_slot:
1275 leaf = path->nodes[0];
1276 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1277 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001278 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001279 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001280 if (ret > 0)
1281 break;
1282 leaf = path->nodes[0];
1283 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001284
Yan Zheng80ff3852008-10-30 14:20:02 -04001285 nocow = 0;
1286 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001287 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001288 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001289
Li Zefan33345d012011-04-20 10:31:50 +08001290 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001291 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1292 found_key.offset > end)
1293 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001294
Yan Zheng80ff3852008-10-30 14:20:02 -04001295 if (found_key.offset > cur_offset) {
1296 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001297 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001298 goto out_check;
1299 }
Chris Masonc31f8832008-01-08 15:46:31 -05001300
Yan Zheng80ff3852008-10-30 14:20:02 -04001301 fi = btrfs_item_ptr(leaf, path->slots[0],
1302 struct btrfs_file_extent_item);
1303 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001304
Josef Bacikcc95bef2013-04-04 14:31:27 -04001305 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001306 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1307 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001309 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001310 extent_end = found_key.offset +
1311 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001312 disk_num_bytes =
1313 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001314 if (extent_end <= start) {
1315 path->slots[0]++;
1316 goto next_slot;
1317 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001318 if (disk_bytenr == 0)
1319 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001320 if (btrfs_file_extent_compression(leaf, fi) ||
1321 btrfs_file_extent_encryption(leaf, fi) ||
1322 btrfs_file_extent_other_encoding(leaf, fi))
1323 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001324 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1325 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001326 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001327 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001328 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001329 found_key.offset -
1330 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001331 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001332 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001333 disk_bytenr += cur_offset - found_key.offset;
1334 num_bytes = min(end + 1, extent_end) - cur_offset;
1335 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001336 * if there are pending snapshots for this root,
1337 * we fall into common COW way.
1338 */
1339 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001340 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001341 if (!err)
1342 goto out_check;
1343 }
1344 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001345 * force cow if csum exists in the range.
1346 * this ensure that csum for a given extent are
1347 * either valid or do not exist.
1348 */
1349 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1350 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001351 nocow = 1;
1352 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1353 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001354 btrfs_file_extent_inline_len(leaf,
1355 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001356 extent_end = ALIGN(extent_end, root->sectorsize);
1357 } else {
1358 BUG_ON(1);
1359 }
1360out_check:
1361 if (extent_end <= start) {
1362 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001363 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001364 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001365 goto next_slot;
1366 }
1367 if (!nocow) {
1368 if (cow_start == (u64)-1)
1369 cow_start = cur_offset;
1370 cur_offset = extent_end;
1371 if (cur_offset > end)
1372 break;
1373 path->slots[0]++;
1374 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001375 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001376
David Sterbab3b4aa72011-04-21 01:20:15 +02001377 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001378 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001379 ret = cow_file_range(inode, locked_page,
1380 cow_start, found_key.offset - 1,
1381 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001382 if (ret) {
1383 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001384 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001385 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001386 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001387 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001388 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001389
Yan Zhengd899e052008-10-30 14:25:28 -04001390 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1391 struct extent_map *em;
1392 struct extent_map_tree *em_tree;
1393 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001394 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001395 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001396 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001397 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001398 em->len = num_bytes;
1399 em->block_len = num_bytes;
1400 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001401 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001402 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001403 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001404 em->mod_start = em->start;
1405 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001406 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001407 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001408 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001409 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001410 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001411 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001412 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001413 if (ret != -EEXIST) {
1414 free_extent_map(em);
1415 break;
1416 }
1417 btrfs_drop_extent_cache(inode, em->start,
1418 em->start + em->len - 1, 0);
1419 }
1420 type = BTRFS_ORDERED_PREALLOC;
1421 } else {
1422 type = BTRFS_ORDERED_NOCOW;
1423 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001424
1425 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001426 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001427 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001428
Yan, Zhengefa56462010-05-16 10:49:59 -04001429 if (root->root_key.objectid ==
1430 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1431 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1432 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001433 if (ret) {
1434 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001435 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001436 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001437 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001438 }
1439
Josef Bacikc2790a22013-07-29 11:20:47 -04001440 extent_clear_unlock_delalloc(inode, cur_offset,
1441 cur_offset + num_bytes - 1,
1442 locked_page, EXTENT_LOCKED |
1443 EXTENT_DELALLOC, PAGE_UNLOCK |
1444 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001445 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001446 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001447 cur_offset = extent_end;
1448 if (cur_offset > end)
1449 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001450 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001451 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001452
Josef Bacik17ca04a2012-05-31 15:58:55 -04001453 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001454 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001455 cur_offset = end;
1456 }
1457
Yan Zheng80ff3852008-10-30 14:20:02 -04001458 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001459 ret = cow_file_range(inode, locked_page, cow_start, end,
1460 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001461 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001462 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001463 }
1464
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001465error:
Miao Xiea698d0752012-09-20 01:51:59 -06001466 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001467 if (!ret)
1468 ret = err;
1469
Josef Bacik17ca04a2012-05-31 15:58:55 -04001470 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001471 extent_clear_unlock_delalloc(inode, cur_offset, end,
1472 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001473 EXTENT_DELALLOC | EXTENT_DEFRAG |
1474 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1475 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001476 PAGE_SET_WRITEBACK |
1477 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001478 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001479 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001480}
1481
Wang Shilong47059d92014-07-03 18:22:07 +08001482static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1483{
1484
1485 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1486 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1487 return 0;
1488
1489 /*
1490 * @defrag_bytes is a hint value, no spinlock held here,
1491 * if is not zero, it means the file is defragging.
1492 * Force cow if given extent needs to be defragged.
1493 */
1494 if (BTRFS_I(inode)->defrag_bytes &&
1495 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1496 EXTENT_DEFRAG, 0, NULL))
1497 return 1;
1498
1499 return 0;
1500}
1501
Chris Masond352ac62008-09-29 15:18:18 -04001502/*
1503 * extent_io.c call back to do delayed allocation processing
1504 */
Chris Masonc8b97812008-10-29 14:49:59 -04001505static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001506 u64 start, u64 end, int *page_started,
1507 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001508{
Chris Masonbe20aa92007-12-17 20:14:01 -05001509 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001510 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001511
Wang Shilong47059d92014-07-03 18:22:07 +08001512 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001513 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001514 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001515 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001516 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001517 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001518 } else if (!inode_need_compress(inode)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001519 ret = cow_file_range(inode, locked_page, start, end,
1520 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001521 } else {
1522 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1523 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001524 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001525 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001526 }
Chris Masonb888db22007-08-27 16:49:44 -04001527 return ret;
1528}
1529
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001530static void btrfs_split_extent_hook(struct inode *inode,
1531 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001532{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001533 u64 size;
1534
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001535 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001536 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001537 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001538
Josef Bacikdcab6a32015-02-11 15:08:59 -05001539 size = orig->end - orig->start + 1;
1540 if (size > BTRFS_MAX_EXTENT_SIZE) {
1541 u64 num_extents;
1542 u64 new_size;
1543
1544 /*
Josef Bacikba117212015-03-13 15:01:24 -04001545 * See the explanation in btrfs_merge_extent_hook, the same
1546 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001547 */
1548 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001549 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001550 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001551 new_size = split - orig->start;
1552 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1553 BTRFS_MAX_EXTENT_SIZE);
1554 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1555 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001556 return;
1557 }
1558
Josef Bacik9e0baf62011-07-15 15:16:44 +00001559 spin_lock(&BTRFS_I(inode)->lock);
1560 BTRFS_I(inode)->outstanding_extents++;
1561 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001562}
1563
1564/*
1565 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1566 * extents so we can keep track of new extents that are just merged onto old
1567 * extents, such as when we are doing sequential writes, so we can properly
1568 * account for the metadata space we'll need.
1569 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001570static void btrfs_merge_extent_hook(struct inode *inode,
1571 struct extent_state *new,
1572 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001573{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001574 u64 new_size, old_size;
1575 u64 num_extents;
1576
Josef Bacik9ed74f22009-09-11 16:12:44 -04001577 /* not delalloc, ignore it */
1578 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001579 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001580
Josef Bacik8461a3d2015-03-13 15:12:08 -04001581 if (new->start > other->start)
1582 new_size = new->end - other->start + 1;
1583 else
1584 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001585
1586 /* we're not bigger than the max, unreserve the space and go */
1587 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1588 spin_lock(&BTRFS_I(inode)->lock);
1589 BTRFS_I(inode)->outstanding_extents--;
1590 spin_unlock(&BTRFS_I(inode)->lock);
1591 return;
1592 }
1593
1594 /*
Josef Bacikba117212015-03-13 15:01:24 -04001595 * We have to add up either side to figure out how many extents were
1596 * accounted for before we merged into one big extent. If the number of
1597 * extents we accounted for is <= the amount we need for the new range
1598 * then we can return, otherwise drop. Think of it like this
1599 *
1600 * [ 4k][MAX_SIZE]
1601 *
1602 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1603 * need 2 outstanding extents, on one side we have 1 and the other side
1604 * we have 1 so they are == and we can return. But in this case
1605 *
1606 * [MAX_SIZE+4k][MAX_SIZE+4k]
1607 *
1608 * Each range on their own accounts for 2 extents, but merged together
1609 * they are only 3 extents worth of accounting, so we need to drop in
1610 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001611 */
Josef Bacikba117212015-03-13 15:01:24 -04001612 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001613 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1614 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001615 old_size = new->end - new->start + 1;
1616 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1617 BTRFS_MAX_EXTENT_SIZE);
1618
Josef Bacikdcab6a32015-02-11 15:08:59 -05001619 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001620 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001621 return;
1622
Josef Bacik9e0baf62011-07-15 15:16:44 +00001623 spin_lock(&BTRFS_I(inode)->lock);
1624 BTRFS_I(inode)->outstanding_extents--;
1625 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001626}
1627
Miao Xieeb73c1b2013-05-15 07:48:22 +00001628static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1629 struct inode *inode)
1630{
1631 spin_lock(&root->delalloc_lock);
1632 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1633 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1634 &root->delalloc_inodes);
1635 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1636 &BTRFS_I(inode)->runtime_flags);
1637 root->nr_delalloc_inodes++;
1638 if (root->nr_delalloc_inodes == 1) {
1639 spin_lock(&root->fs_info->delalloc_root_lock);
1640 BUG_ON(!list_empty(&root->delalloc_root));
1641 list_add_tail(&root->delalloc_root,
1642 &root->fs_info->delalloc_roots);
1643 spin_unlock(&root->fs_info->delalloc_root_lock);
1644 }
1645 }
1646 spin_unlock(&root->delalloc_lock);
1647}
1648
1649static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1650 struct inode *inode)
1651{
1652 spin_lock(&root->delalloc_lock);
1653 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1654 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1655 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1656 &BTRFS_I(inode)->runtime_flags);
1657 root->nr_delalloc_inodes--;
1658 if (!root->nr_delalloc_inodes) {
1659 spin_lock(&root->fs_info->delalloc_root_lock);
1660 BUG_ON(list_empty(&root->delalloc_root));
1661 list_del_init(&root->delalloc_root);
1662 spin_unlock(&root->fs_info->delalloc_root_lock);
1663 }
1664 }
1665 spin_unlock(&root->delalloc_lock);
1666}
1667
Chris Masond352ac62008-09-29 15:18:18 -04001668/*
1669 * extent_io.c set_bit_hook, used to track delayed allocation
1670 * bytes in this file, and to maintain the list of inodes that
1671 * have pending delalloc work to be done.
1672 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001673static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001674 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001675{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001676
Wang Shilong47059d92014-07-03 18:22:07 +08001677 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1678 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001679 /*
1680 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001681 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001682 * bit, which is only set or cleared with irqs on
1683 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001684 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001685 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001686 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001687 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001688
Josef Bacik9e0baf62011-07-15 15:16:44 +00001689 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001690 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001691 } else {
1692 spin_lock(&BTRFS_I(inode)->lock);
1693 BTRFS_I(inode)->outstanding_extents++;
1694 spin_unlock(&BTRFS_I(inode)->lock);
1695 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001696
Miao Xie963d6782013-01-29 10:10:51 +00001697 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1698 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001699 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001700 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001701 if (*bits & EXTENT_DEFRAG)
1702 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001703 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001704 &BTRFS_I(inode)->runtime_flags))
1705 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001706 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001707 }
Chris Mason291d6732008-01-29 15:55:23 -05001708}
1709
Chris Masond352ac62008-09-29 15:18:18 -04001710/*
1711 * extent_io.c clear_bit_hook, see set_bit_hook for why
1712 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001713static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001714 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001715 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001716{
Wang Shilong47059d92014-07-03 18:22:07 +08001717 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001718 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1719 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001720
1721 spin_lock(&BTRFS_I(inode)->lock);
1722 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1723 BTRFS_I(inode)->defrag_bytes -= len;
1724 spin_unlock(&BTRFS_I(inode)->lock);
1725
Chris Mason75eff682008-12-15 15:54:40 -05001726 /*
1727 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001728 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001729 * bit, which is only set or cleared with irqs on
1730 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001731 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001732 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001733 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001734
Josef Bacik9e0baf62011-07-15 15:16:44 +00001735 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001736 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001737 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1738 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001739 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001740 spin_unlock(&BTRFS_I(inode)->lock);
1741 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001742
Josef Bacikb6d08f02013-09-27 14:57:43 -04001743 /*
1744 * We don't reserve metadata space for space cache inodes so we
1745 * don't need to call dellalloc_release_metadata if there is an
1746 * error.
1747 */
1748 if (*bits & EXTENT_DO_ACCOUNTING &&
1749 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001750 btrfs_delalloc_release_metadata(inode, len);
1751
Josef Bacik0cb59c92010-07-02 12:14:14 -04001752 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001753 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001754 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001755
Miao Xie963d6782013-01-29 10:10:51 +00001756 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1757 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001758 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001759 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001760 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001761 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001762 &BTRFS_I(inode)->runtime_flags))
1763 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001764 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001765 }
Chris Mason291d6732008-01-29 15:55:23 -05001766}
1767
Chris Masond352ac62008-09-29 15:18:18 -04001768/*
1769 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1770 * we don't create bios that span stripes or chunks
1771 */
David Woodhouse64a16702009-07-15 23:29:37 +01001772int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001773 size_t size, struct bio *bio,
1774 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001775{
1776 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001777 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001778 u64 length = 0;
1779 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001780 int ret;
1781
Chris Mason771ed682008-11-06 22:02:51 -05001782 if (bio_flags & EXTENT_BIO_COMPRESSED)
1783 return 0;
1784
Kent Overstreet4f024f32013-10-11 15:44:27 -07001785 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001786 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001787 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001788 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001789 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001790 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001791 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001792 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001793 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001794}
1795
Chris Masond352ac62008-09-29 15:18:18 -04001796/*
1797 * in order to insert checksums into the metadata in large chunks,
1798 * we wait until bio submission time. All the pages in the bio are
1799 * checksummed and sums are attached onto the ordered extent record.
1800 *
1801 * At IO completion time the cums attached on the ordered extent record
1802 * are inserted into the btree
1803 */
Chris Masond3977122009-01-05 21:25:51 -05001804static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1805 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001806 unsigned long bio_flags,
1807 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001808{
Chris Mason065631f2008-02-20 12:07:25 -05001809 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001810 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001811
Chris Masond20f7042008-12-08 16:58:54 -05001812 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001813 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001814 return 0;
1815}
Chris Masone0156402008-04-16 11:15:20 -04001816
Chris Mason4a69a412008-11-06 22:03:00 -05001817/*
1818 * in order to insert checksums into the metadata in large chunks,
1819 * we wait until bio submission time. All the pages in the bio are
1820 * checksummed and sums are attached onto the ordered extent record.
1821 *
1822 * At IO completion time the cums attached on the ordered extent record
1823 * are inserted into the btree
1824 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001825static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001826 int mirror_num, unsigned long bio_flags,
1827 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001828{
1829 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001830 int ret;
1831
1832 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1833 if (ret)
1834 bio_endio(bio, ret);
1835 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001836}
1837
Chris Masond352ac62008-09-29 15:18:18 -04001838/*
Chris Masoncad321a2008-12-17 14:51:42 -05001839 * extent_io.c submission hook. This does the right thing for csum calculation
1840 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001841 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001842static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001843 int mirror_num, unsigned long bio_flags,
1844 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001845{
1846 struct btrfs_root *root = BTRFS_I(inode)->root;
1847 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001848 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001849 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001850 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001851
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001852 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001853
Liu Bo83eea1f2012-07-10 05:28:39 -06001854 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001855 metadata = 2;
1856
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001857 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001858 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1859 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001860 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001861
Chris Masond20f7042008-12-08 16:58:54 -05001862 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001863 ret = btrfs_submit_compressed_read(inode, bio,
1864 mirror_num,
1865 bio_flags);
1866 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001867 } else if (!skip_sum) {
1868 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1869 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001870 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001871 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001872 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001873 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001874 /* csum items have already been cloned */
1875 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1876 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001877 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001878 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001879 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001880 bio_flags, bio_offset,
1881 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001882 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001883 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001884 } else if (!skip_sum) {
1885 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1886 if (ret)
1887 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001888 }
1889
Chris Mason0b86a832008-03-24 15:01:56 -04001890mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001891 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1892
1893out:
1894 if (ret < 0)
1895 bio_endio(bio, ret);
1896 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001897}
Chris Mason6885f302008-02-20 16:11:05 -05001898
Chris Masond352ac62008-09-29 15:18:18 -04001899/*
1900 * given a list of ordered sums record them in the inode. This happens
1901 * at IO completion time based on sums calculated at bio submission time.
1902 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001903static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001904 struct inode *inode, u64 file_offset,
1905 struct list_head *list)
1906{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001907 struct btrfs_ordered_sum *sum;
1908
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001909 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001910 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001911 btrfs_csum_file_blocks(trans,
1912 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001913 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001914 }
1915 return 0;
1916}
1917
Josef Bacik2ac55d42010-02-03 19:33:23 +00001918int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1919 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001920{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001921 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001922 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001923 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001924}
1925
Chris Masond352ac62008-09-29 15:18:18 -04001926/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001927struct btrfs_writepage_fixup {
1928 struct page *page;
1929 struct btrfs_work work;
1930};
1931
Christoph Hellwigb2950862008-12-02 09:54:17 -05001932static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001933{
1934 struct btrfs_writepage_fixup *fixup;
1935 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001936 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001937 struct page *page;
1938 struct inode *inode;
1939 u64 page_start;
1940 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001941 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001942
1943 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1944 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001945again:
Chris Mason247e7432008-07-17 12:53:51 -04001946 lock_page(page);
1947 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1948 ClearPageChecked(page);
1949 goto out_page;
1950 }
1951
1952 inode = page->mapping->host;
1953 page_start = page_offset(page);
1954 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1955
Josef Bacik2ac55d42010-02-03 19:33:23 +00001956 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001957 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001958
1959 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001960 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001961 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001962
1963 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1964 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001965 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1966 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001967 unlock_page(page);
1968 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001969 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001970 goto again;
1971 }
Chris Mason247e7432008-07-17 12:53:51 -04001972
Jeff Mahoney87826df2012-02-15 16:23:57 +01001973 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1974 if (ret) {
1975 mapping_set_error(page->mapping, ret);
1976 end_extent_writepage(page, ret, page_start, page_end);
1977 ClearPageChecked(page);
1978 goto out;
1979 }
1980
Josef Bacik2ac55d42010-02-03 19:33:23 +00001981 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001982 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001983 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001984out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001985 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1986 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001987out_page:
1988 unlock_page(page);
1989 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001990 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001991}
1992
1993/*
1994 * There are a few paths in the higher layers of the kernel that directly
1995 * set the page dirty bit without asking the filesystem if it is a
1996 * good idea. This causes problems because we want to make sure COW
1997 * properly happens and the data=ordered rules are followed.
1998 *
Chris Masonc8b97812008-10-29 14:49:59 -04001999 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002000 * hasn't been properly setup for IO. We kick off an async process
2001 * to fix it up. The async helper will wait for ordered extents, set
2002 * the delalloc bit and make it safe to write the page.
2003 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002004static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002005{
2006 struct inode *inode = page->mapping->host;
2007 struct btrfs_writepage_fixup *fixup;
2008 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04002009
Chris Mason8b62b722009-09-02 16:53:46 -04002010 /* this page is properly in the ordered list */
2011 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002012 return 0;
2013
2014 if (PageChecked(page))
2015 return -EAGAIN;
2016
2017 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2018 if (!fixup)
2019 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002020
Chris Mason247e7432008-07-17 12:53:51 -04002021 SetPageChecked(page);
2022 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002023 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2024 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002025 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08002026 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002027 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002028}
2029
Yan Zhengd899e052008-10-30 14:25:28 -04002030static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2031 struct inode *inode, u64 file_pos,
2032 u64 disk_bytenr, u64 disk_num_bytes,
2033 u64 num_bytes, u64 ram_bytes,
2034 u8 compression, u8 encryption,
2035 u16 other_encoding, int extent_type)
2036{
2037 struct btrfs_root *root = BTRFS_I(inode)->root;
2038 struct btrfs_file_extent_item *fi;
2039 struct btrfs_path *path;
2040 struct extent_buffer *leaf;
2041 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002042 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002043 int ret;
2044
2045 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002046 if (!path)
2047 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002048
Chris Masona1ed8352009-09-11 12:27:37 -04002049 /*
2050 * we may be replacing one extent in the tree with another.
2051 * The new extent is pinned in the extent map, and we don't want
2052 * to drop it from the cache until it is completely in the btree.
2053 *
2054 * So, tell btrfs_drop_extents to leave this extent in the cache.
2055 * the caller is expected to unpin it and allow it to be merged
2056 * with the others.
2057 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002058 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2059 file_pos + num_bytes, NULL, 0,
2060 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002061 if (ret)
2062 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002063
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002064 if (!extent_inserted) {
2065 ins.objectid = btrfs_ino(inode);
2066 ins.offset = file_pos;
2067 ins.type = BTRFS_EXTENT_DATA_KEY;
2068
2069 path->leave_spinning = 1;
2070 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2071 sizeof(*fi));
2072 if (ret)
2073 goto out;
2074 }
Yan Zhengd899e052008-10-30 14:25:28 -04002075 leaf = path->nodes[0];
2076 fi = btrfs_item_ptr(leaf, path->slots[0],
2077 struct btrfs_file_extent_item);
2078 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2079 btrfs_set_file_extent_type(leaf, fi, extent_type);
2080 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2081 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2082 btrfs_set_file_extent_offset(leaf, fi, 0);
2083 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2084 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2085 btrfs_set_file_extent_compression(leaf, fi, compression);
2086 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2087 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002088
Yan Zhengd899e052008-10-30 14:25:28 -04002089 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002090 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002091
2092 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002093
2094 ins.objectid = disk_bytenr;
2095 ins.offset = disk_num_bytes;
2096 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002097 ret = btrfs_alloc_reserved_file_extent(trans, root,
2098 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002099 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002100out:
Yan Zhengd899e052008-10-30 14:25:28 -04002101 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002102
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002103 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002104}
2105
Liu Bo38c227d2013-01-29 03:18:40 +00002106/* snapshot-aware defrag */
2107struct sa_defrag_extent_backref {
2108 struct rb_node node;
2109 struct old_sa_defrag_extent *old;
2110 u64 root_id;
2111 u64 inum;
2112 u64 file_pos;
2113 u64 extent_offset;
2114 u64 num_bytes;
2115 u64 generation;
2116};
2117
2118struct old_sa_defrag_extent {
2119 struct list_head list;
2120 struct new_sa_defrag_extent *new;
2121
2122 u64 extent_offset;
2123 u64 bytenr;
2124 u64 offset;
2125 u64 len;
2126 int count;
2127};
2128
2129struct new_sa_defrag_extent {
2130 struct rb_root root;
2131 struct list_head head;
2132 struct btrfs_path *path;
2133 struct inode *inode;
2134 u64 file_pos;
2135 u64 len;
2136 u64 bytenr;
2137 u64 disk_len;
2138 u8 compress_type;
2139};
2140
2141static int backref_comp(struct sa_defrag_extent_backref *b1,
2142 struct sa_defrag_extent_backref *b2)
2143{
2144 if (b1->root_id < b2->root_id)
2145 return -1;
2146 else if (b1->root_id > b2->root_id)
2147 return 1;
2148
2149 if (b1->inum < b2->inum)
2150 return -1;
2151 else if (b1->inum > b2->inum)
2152 return 1;
2153
2154 if (b1->file_pos < b2->file_pos)
2155 return -1;
2156 else if (b1->file_pos > b2->file_pos)
2157 return 1;
2158
2159 /*
2160 * [------------------------------] ===> (a range of space)
2161 * |<--->| |<---->| =============> (fs/file tree A)
2162 * |<---------------------------->| ===> (fs/file tree B)
2163 *
2164 * A range of space can refer to two file extents in one tree while
2165 * refer to only one file extent in another tree.
2166 *
2167 * So we may process a disk offset more than one time(two extents in A)
2168 * and locate at the same extent(one extent in B), then insert two same
2169 * backrefs(both refer to the extent in B).
2170 */
2171 return 0;
2172}
2173
2174static void backref_insert(struct rb_root *root,
2175 struct sa_defrag_extent_backref *backref)
2176{
2177 struct rb_node **p = &root->rb_node;
2178 struct rb_node *parent = NULL;
2179 struct sa_defrag_extent_backref *entry;
2180 int ret;
2181
2182 while (*p) {
2183 parent = *p;
2184 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2185
2186 ret = backref_comp(backref, entry);
2187 if (ret < 0)
2188 p = &(*p)->rb_left;
2189 else
2190 p = &(*p)->rb_right;
2191 }
2192
2193 rb_link_node(&backref->node, parent, p);
2194 rb_insert_color(&backref->node, root);
2195}
2196
2197/*
2198 * Note the backref might has changed, and in this case we just return 0.
2199 */
2200static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2201 void *ctx)
2202{
2203 struct btrfs_file_extent_item *extent;
2204 struct btrfs_fs_info *fs_info;
2205 struct old_sa_defrag_extent *old = ctx;
2206 struct new_sa_defrag_extent *new = old->new;
2207 struct btrfs_path *path = new->path;
2208 struct btrfs_key key;
2209 struct btrfs_root *root;
2210 struct sa_defrag_extent_backref *backref;
2211 struct extent_buffer *leaf;
2212 struct inode *inode = new->inode;
2213 int slot;
2214 int ret;
2215 u64 extent_offset;
2216 u64 num_bytes;
2217
2218 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2219 inum == btrfs_ino(inode))
2220 return 0;
2221
2222 key.objectid = root_id;
2223 key.type = BTRFS_ROOT_ITEM_KEY;
2224 key.offset = (u64)-1;
2225
2226 fs_info = BTRFS_I(inode)->root->fs_info;
2227 root = btrfs_read_fs_root_no_name(fs_info, &key);
2228 if (IS_ERR(root)) {
2229 if (PTR_ERR(root) == -ENOENT)
2230 return 0;
2231 WARN_ON(1);
2232 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2233 inum, offset, root_id);
2234 return PTR_ERR(root);
2235 }
2236
2237 key.objectid = inum;
2238 key.type = BTRFS_EXTENT_DATA_KEY;
2239 if (offset > (u64)-1 << 32)
2240 key.offset = 0;
2241 else
2242 key.offset = offset;
2243
2244 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302245 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002246 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002247 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002248
2249 while (1) {
2250 cond_resched();
2251
2252 leaf = path->nodes[0];
2253 slot = path->slots[0];
2254
2255 if (slot >= btrfs_header_nritems(leaf)) {
2256 ret = btrfs_next_leaf(root, path);
2257 if (ret < 0) {
2258 goto out;
2259 } else if (ret > 0) {
2260 ret = 0;
2261 goto out;
2262 }
2263 continue;
2264 }
2265
2266 path->slots[0]++;
2267
2268 btrfs_item_key_to_cpu(leaf, &key, slot);
2269
2270 if (key.objectid > inum)
2271 goto out;
2272
2273 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2274 continue;
2275
2276 extent = btrfs_item_ptr(leaf, slot,
2277 struct btrfs_file_extent_item);
2278
2279 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2280 continue;
2281
Liu Boe68afa42013-07-01 22:13:26 +08002282 /*
2283 * 'offset' refers to the exact key.offset,
2284 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2285 * (key.offset - extent_offset).
2286 */
2287 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002288 continue;
2289
Liu Boe68afa42013-07-01 22:13:26 +08002290 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002291 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002292
Liu Bo38c227d2013-01-29 03:18:40 +00002293 if (extent_offset >= old->extent_offset + old->offset +
2294 old->len || extent_offset + num_bytes <=
2295 old->extent_offset + old->offset)
2296 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002297 break;
2298 }
2299
2300 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2301 if (!backref) {
2302 ret = -ENOENT;
2303 goto out;
2304 }
2305
2306 backref->root_id = root_id;
2307 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002308 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002309 backref->num_bytes = num_bytes;
2310 backref->extent_offset = extent_offset;
2311 backref->generation = btrfs_file_extent_generation(leaf, extent);
2312 backref->old = old;
2313 backref_insert(&new->root, backref);
2314 old->count++;
2315out:
2316 btrfs_release_path(path);
2317 WARN_ON(ret);
2318 return ret;
2319}
2320
2321static noinline bool record_extent_backrefs(struct btrfs_path *path,
2322 struct new_sa_defrag_extent *new)
2323{
2324 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2325 struct old_sa_defrag_extent *old, *tmp;
2326 int ret;
2327
2328 new->path = path;
2329
2330 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002331 ret = iterate_inodes_from_logical(old->bytenr +
2332 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002333 path, record_one_backref,
2334 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002335 if (ret < 0 && ret != -ENOENT)
2336 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002337
2338 /* no backref to be processed for this extent */
2339 if (!old->count) {
2340 list_del(&old->list);
2341 kfree(old);
2342 }
2343 }
2344
2345 if (list_empty(&new->head))
2346 return false;
2347
2348 return true;
2349}
2350
2351static int relink_is_mergable(struct extent_buffer *leaf,
2352 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002353 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002354{
Liu Bo116e0022013-08-02 16:30:40 +08002355 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002356 return 0;
2357
2358 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2359 return 0;
2360
Liu Bo116e0022013-08-02 16:30:40 +08002361 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2362 return 0;
2363
2364 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002365 btrfs_file_extent_other_encoding(leaf, fi))
2366 return 0;
2367
2368 return 1;
2369}
2370
2371/*
2372 * Note the backref might has changed, and in this case we just return 0.
2373 */
2374static noinline int relink_extent_backref(struct btrfs_path *path,
2375 struct sa_defrag_extent_backref *prev,
2376 struct sa_defrag_extent_backref *backref)
2377{
2378 struct btrfs_file_extent_item *extent;
2379 struct btrfs_file_extent_item *item;
2380 struct btrfs_ordered_extent *ordered;
2381 struct btrfs_trans_handle *trans;
2382 struct btrfs_fs_info *fs_info;
2383 struct btrfs_root *root;
2384 struct btrfs_key key;
2385 struct extent_buffer *leaf;
2386 struct old_sa_defrag_extent *old = backref->old;
2387 struct new_sa_defrag_extent *new = old->new;
2388 struct inode *src_inode = new->inode;
2389 struct inode *inode;
2390 struct extent_state *cached = NULL;
2391 int ret = 0;
2392 u64 start;
2393 u64 len;
2394 u64 lock_start;
2395 u64 lock_end;
2396 bool merge = false;
2397 int index;
2398
2399 if (prev && prev->root_id == backref->root_id &&
2400 prev->inum == backref->inum &&
2401 prev->file_pos + prev->num_bytes == backref->file_pos)
2402 merge = true;
2403
2404 /* step 1: get root */
2405 key.objectid = backref->root_id;
2406 key.type = BTRFS_ROOT_ITEM_KEY;
2407 key.offset = (u64)-1;
2408
2409 fs_info = BTRFS_I(src_inode)->root->fs_info;
2410 index = srcu_read_lock(&fs_info->subvol_srcu);
2411
2412 root = btrfs_read_fs_root_no_name(fs_info, &key);
2413 if (IS_ERR(root)) {
2414 srcu_read_unlock(&fs_info->subvol_srcu, index);
2415 if (PTR_ERR(root) == -ENOENT)
2416 return 0;
2417 return PTR_ERR(root);
2418 }
Liu Bo38c227d2013-01-29 03:18:40 +00002419
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002420 if (btrfs_root_readonly(root)) {
2421 srcu_read_unlock(&fs_info->subvol_srcu, index);
2422 return 0;
2423 }
2424
Liu Bo38c227d2013-01-29 03:18:40 +00002425 /* step 2: get inode */
2426 key.objectid = backref->inum;
2427 key.type = BTRFS_INODE_ITEM_KEY;
2428 key.offset = 0;
2429
2430 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2431 if (IS_ERR(inode)) {
2432 srcu_read_unlock(&fs_info->subvol_srcu, index);
2433 return 0;
2434 }
2435
2436 srcu_read_unlock(&fs_info->subvol_srcu, index);
2437
2438 /* step 3: relink backref */
2439 lock_start = backref->file_pos;
2440 lock_end = backref->file_pos + backref->num_bytes - 1;
2441 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2442 0, &cached);
2443
2444 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2445 if (ordered) {
2446 btrfs_put_ordered_extent(ordered);
2447 goto out_unlock;
2448 }
2449
2450 trans = btrfs_join_transaction(root);
2451 if (IS_ERR(trans)) {
2452 ret = PTR_ERR(trans);
2453 goto out_unlock;
2454 }
2455
2456 key.objectid = backref->inum;
2457 key.type = BTRFS_EXTENT_DATA_KEY;
2458 key.offset = backref->file_pos;
2459
2460 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2461 if (ret < 0) {
2462 goto out_free_path;
2463 } else if (ret > 0) {
2464 ret = 0;
2465 goto out_free_path;
2466 }
2467
2468 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2469 struct btrfs_file_extent_item);
2470
2471 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2472 backref->generation)
2473 goto out_free_path;
2474
2475 btrfs_release_path(path);
2476
2477 start = backref->file_pos;
2478 if (backref->extent_offset < old->extent_offset + old->offset)
2479 start += old->extent_offset + old->offset -
2480 backref->extent_offset;
2481
2482 len = min(backref->extent_offset + backref->num_bytes,
2483 old->extent_offset + old->offset + old->len);
2484 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2485
2486 ret = btrfs_drop_extents(trans, root, inode, start,
2487 start + len, 1);
2488 if (ret)
2489 goto out_free_path;
2490again:
2491 key.objectid = btrfs_ino(inode);
2492 key.type = BTRFS_EXTENT_DATA_KEY;
2493 key.offset = start;
2494
Liu Boa09a0a72013-03-11 09:20:58 +00002495 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002496 if (merge) {
2497 struct btrfs_file_extent_item *fi;
2498 u64 extent_len;
2499 struct btrfs_key found_key;
2500
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002501 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002502 if (ret < 0)
2503 goto out_free_path;
2504
2505 path->slots[0]--;
2506 leaf = path->nodes[0];
2507 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2508
2509 fi = btrfs_item_ptr(leaf, path->slots[0],
2510 struct btrfs_file_extent_item);
2511 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2512
Liu Bo116e0022013-08-02 16:30:40 +08002513 if (extent_len + found_key.offset == start &&
2514 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002515 btrfs_set_file_extent_num_bytes(leaf, fi,
2516 extent_len + len);
2517 btrfs_mark_buffer_dirty(leaf);
2518 inode_add_bytes(inode, len);
2519
2520 ret = 1;
2521 goto out_free_path;
2522 } else {
2523 merge = false;
2524 btrfs_release_path(path);
2525 goto again;
2526 }
2527 }
2528
2529 ret = btrfs_insert_empty_item(trans, root, path, &key,
2530 sizeof(*extent));
2531 if (ret) {
2532 btrfs_abort_transaction(trans, root, ret);
2533 goto out_free_path;
2534 }
2535
2536 leaf = path->nodes[0];
2537 item = btrfs_item_ptr(leaf, path->slots[0],
2538 struct btrfs_file_extent_item);
2539 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2540 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2541 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2542 btrfs_set_file_extent_num_bytes(leaf, item, len);
2543 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2544 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2545 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2546 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2547 btrfs_set_file_extent_encryption(leaf, item, 0);
2548 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2549
2550 btrfs_mark_buffer_dirty(leaf);
2551 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002552 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002553
2554 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2555 new->disk_len, 0,
2556 backref->root_id, backref->inum,
2557 new->file_pos, 0); /* start - extent_offset */
2558 if (ret) {
2559 btrfs_abort_transaction(trans, root, ret);
2560 goto out_free_path;
2561 }
2562
2563 ret = 1;
2564out_free_path:
2565 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002566 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002567 btrfs_end_transaction(trans, root);
2568out_unlock:
2569 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2570 &cached, GFP_NOFS);
2571 iput(inode);
2572 return ret;
2573}
2574
Liu Bo6f519562013-10-29 10:45:05 +08002575static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2576{
2577 struct old_sa_defrag_extent *old, *tmp;
2578
2579 if (!new)
2580 return;
2581
2582 list_for_each_entry_safe(old, tmp, &new->head, list) {
2583 list_del(&old->list);
2584 kfree(old);
2585 }
2586 kfree(new);
2587}
2588
Liu Bo38c227d2013-01-29 03:18:40 +00002589static void relink_file_extents(struct new_sa_defrag_extent *new)
2590{
2591 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002592 struct sa_defrag_extent_backref *backref;
2593 struct sa_defrag_extent_backref *prev = NULL;
2594 struct inode *inode;
2595 struct btrfs_root *root;
2596 struct rb_node *node;
2597 int ret;
2598
2599 inode = new->inode;
2600 root = BTRFS_I(inode)->root;
2601
2602 path = btrfs_alloc_path();
2603 if (!path)
2604 return;
2605
2606 if (!record_extent_backrefs(path, new)) {
2607 btrfs_free_path(path);
2608 goto out;
2609 }
2610 btrfs_release_path(path);
2611
2612 while (1) {
2613 node = rb_first(&new->root);
2614 if (!node)
2615 break;
2616 rb_erase(node, &new->root);
2617
2618 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2619
2620 ret = relink_extent_backref(path, prev, backref);
2621 WARN_ON(ret < 0);
2622
2623 kfree(prev);
2624
2625 if (ret == 1)
2626 prev = backref;
2627 else
2628 prev = NULL;
2629 cond_resched();
2630 }
2631 kfree(prev);
2632
2633 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002634out:
Liu Bo6f519562013-10-29 10:45:05 +08002635 free_sa_defrag_extent(new);
2636
Liu Bo38c227d2013-01-29 03:18:40 +00002637 atomic_dec(&root->fs_info->defrag_running);
2638 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002639}
2640
2641static struct new_sa_defrag_extent *
2642record_old_file_extents(struct inode *inode,
2643 struct btrfs_ordered_extent *ordered)
2644{
2645 struct btrfs_root *root = BTRFS_I(inode)->root;
2646 struct btrfs_path *path;
2647 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002648 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002649 struct new_sa_defrag_extent *new;
2650 int ret;
2651
2652 new = kmalloc(sizeof(*new), GFP_NOFS);
2653 if (!new)
2654 return NULL;
2655
2656 new->inode = inode;
2657 new->file_pos = ordered->file_offset;
2658 new->len = ordered->len;
2659 new->bytenr = ordered->start;
2660 new->disk_len = ordered->disk_len;
2661 new->compress_type = ordered->compress_type;
2662 new->root = RB_ROOT;
2663 INIT_LIST_HEAD(&new->head);
2664
2665 path = btrfs_alloc_path();
2666 if (!path)
2667 goto out_kfree;
2668
2669 key.objectid = btrfs_ino(inode);
2670 key.type = BTRFS_EXTENT_DATA_KEY;
2671 key.offset = new->file_pos;
2672
2673 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2674 if (ret < 0)
2675 goto out_free_path;
2676 if (ret > 0 && path->slots[0] > 0)
2677 path->slots[0]--;
2678
2679 /* find out all the old extents for the file range */
2680 while (1) {
2681 struct btrfs_file_extent_item *extent;
2682 struct extent_buffer *l;
2683 int slot;
2684 u64 num_bytes;
2685 u64 offset;
2686 u64 end;
2687 u64 disk_bytenr;
2688 u64 extent_offset;
2689
2690 l = path->nodes[0];
2691 slot = path->slots[0];
2692
2693 if (slot >= btrfs_header_nritems(l)) {
2694 ret = btrfs_next_leaf(root, path);
2695 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002696 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002697 else if (ret > 0)
2698 break;
2699 continue;
2700 }
2701
2702 btrfs_item_key_to_cpu(l, &key, slot);
2703
2704 if (key.objectid != btrfs_ino(inode))
2705 break;
2706 if (key.type != BTRFS_EXTENT_DATA_KEY)
2707 break;
2708 if (key.offset >= new->file_pos + new->len)
2709 break;
2710
2711 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2712
2713 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2714 if (key.offset + num_bytes < new->file_pos)
2715 goto next;
2716
2717 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2718 if (!disk_bytenr)
2719 goto next;
2720
2721 extent_offset = btrfs_file_extent_offset(l, extent);
2722
2723 old = kmalloc(sizeof(*old), GFP_NOFS);
2724 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002725 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002726
2727 offset = max(new->file_pos, key.offset);
2728 end = min(new->file_pos + new->len, key.offset + num_bytes);
2729
2730 old->bytenr = disk_bytenr;
2731 old->extent_offset = extent_offset;
2732 old->offset = offset - key.offset;
2733 old->len = end - offset;
2734 old->new = new;
2735 old->count = 0;
2736 list_add_tail(&old->list, &new->head);
2737next:
2738 path->slots[0]++;
2739 cond_resched();
2740 }
2741
2742 btrfs_free_path(path);
2743 atomic_inc(&root->fs_info->defrag_running);
2744
2745 return new;
2746
Liu Bo38c227d2013-01-29 03:18:40 +00002747out_free_path:
2748 btrfs_free_path(path);
2749out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002750 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002751 return NULL;
2752}
2753
Miao Xiee570fd22014-06-19 10:42:50 +08002754static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2755 u64 start, u64 len)
2756{
2757 struct btrfs_block_group_cache *cache;
2758
2759 cache = btrfs_lookup_block_group(root->fs_info, start);
2760 ASSERT(cache);
2761
2762 spin_lock(&cache->lock);
2763 cache->delalloc_bytes -= len;
2764 spin_unlock(&cache->lock);
2765
2766 btrfs_put_block_group(cache);
2767}
2768
Chris Masond352ac62008-09-29 15:18:18 -04002769/* as ordered data IO finishes, this gets called so we can finish
2770 * an ordered extent if the range of bytes in the file it covers are
2771 * fully written.
2772 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002773static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002774{
Josef Bacik5fd02042012-05-02 14:00:54 -04002775 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002776 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002777 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002778 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002779 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002780 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002781 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002782 int ret = 0;
2783 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002784 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002785 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002786
Liu Bo83eea1f2012-07-10 05:28:39 -06002787 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002788
Josef Bacik5fd02042012-05-02 14:00:54 -04002789 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2790 ret = -EIO;
2791 goto out;
2792 }
2793
Miao Xief6124962014-09-12 18:44:04 +08002794 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2795 ordered_extent->file_offset +
2796 ordered_extent->len - 1);
2797
Josef Bacik77cef2e2013-08-29 13:57:21 -04002798 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2799 truncated = true;
2800 logical_len = ordered_extent->truncated_len;
2801 /* Truncated the entire extent, don't bother adding */
2802 if (!logical_len)
2803 goto out;
2804 }
2805
Yan, Zhengc2167752009-11-12 09:34:21 +00002806 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002807 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002808 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2809 if (nolock)
2810 trans = btrfs_join_transaction_nolock(root);
2811 else
2812 trans = btrfs_join_transaction(root);
2813 if (IS_ERR(trans)) {
2814 ret = PTR_ERR(trans);
2815 trans = NULL;
2816 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002817 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002818 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2819 ret = btrfs_update_inode_fallback(trans, root, inode);
2820 if (ret) /* -ENOMEM or corruption */
2821 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002822 goto out;
2823 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002824
Josef Bacik2ac55d42010-02-03 19:33:23 +00002825 lock_extent_bits(io_tree, ordered_extent->file_offset,
2826 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002827 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002828
Liu Bo38c227d2013-01-29 03:18:40 +00002829 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2830 ordered_extent->file_offset + ordered_extent->len - 1,
2831 EXTENT_DEFRAG, 1, cached_state);
2832 if (ret) {
2833 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002834 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002835 /* the inode is shared */
2836 new = record_old_file_extents(inode, ordered_extent);
2837
2838 clear_extent_bit(io_tree, ordered_extent->file_offset,
2839 ordered_extent->file_offset + ordered_extent->len - 1,
2840 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2841 }
2842
Josef Bacik0cb59c92010-07-02 12:14:14 -04002843 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002844 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002845 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002846 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002847 if (IS_ERR(trans)) {
2848 ret = PTR_ERR(trans);
2849 trans = NULL;
2850 goto out_unlock;
2851 }
Chris Masona79b7d42014-05-22 16:18:52 -07002852
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002853 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002854
Chris Masonc8b97812008-10-29 14:49:59 -04002855 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002856 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002857 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002858 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002859 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002860 ordered_extent->file_offset,
2861 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002862 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002863 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002864 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002865 ret = insert_reserved_file_extent(trans, inode,
2866 ordered_extent->file_offset,
2867 ordered_extent->start,
2868 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002869 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002870 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002871 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002872 if (!ret)
2873 btrfs_release_delalloc_bytes(root,
2874 ordered_extent->start,
2875 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002876 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002877 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2878 ordered_extent->file_offset, ordered_extent->len,
2879 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002880 if (ret < 0) {
2881 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002882 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002883 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002884
Chris Masone6dcd2d2008-07-17 12:53:50 -04002885 add_pending_csums(trans, inode, ordered_extent->file_offset,
2886 &ordered_extent->list);
2887
Josef Bacik6c760c02012-11-09 10:53:21 -05002888 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2889 ret = btrfs_update_inode_fallback(trans, root, inode);
2890 if (ret) { /* -ENOMEM or corruption */
2891 btrfs_abort_transaction(trans, root, ret);
2892 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002893 }
2894 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002895out_unlock:
2896 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2897 ordered_extent->file_offset +
2898 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002899out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002900 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002901 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002902 if (trans)
2903 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002904
Josef Bacik77cef2e2013-08-29 13:57:21 -04002905 if (ret || truncated) {
2906 u64 start, end;
2907
2908 if (truncated)
2909 start = ordered_extent->file_offset + logical_len;
2910 else
2911 start = ordered_extent->file_offset;
2912 end = ordered_extent->file_offset + ordered_extent->len - 1;
2913 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2914
2915 /* Drop the cache for the part of the extent we didn't write. */
2916 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002917
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002918 /*
2919 * If the ordered extent had an IOERR or something else went
2920 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002921 * back to the allocator. We only free the extent in the
2922 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002923 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002924 if ((ret || !logical_len) &&
2925 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002926 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2927 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002928 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002929 }
2930
2931
Josef Bacik5fd02042012-05-02 14:00:54 -04002932 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002933 * This needs to be done to make sure anybody waiting knows we are done
2934 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002935 */
2936 btrfs_remove_ordered_extent(inode, ordered_extent);
2937
Liu Bo38c227d2013-01-29 03:18:40 +00002938 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002939 if (new) {
2940 if (ret) {
2941 free_sa_defrag_extent(new);
2942 atomic_dec(&root->fs_info->defrag_running);
2943 } else {
2944 relink_file_extents(new);
2945 }
2946 }
Liu Bo38c227d2013-01-29 03:18:40 +00002947
Chris Masone6dcd2d2008-07-17 12:53:50 -04002948 /* once for us */
2949 btrfs_put_ordered_extent(ordered_extent);
2950 /* once for the tree */
2951 btrfs_put_ordered_extent(ordered_extent);
2952
Josef Bacik5fd02042012-05-02 14:00:54 -04002953 return ret;
2954}
2955
2956static void finish_ordered_fn(struct btrfs_work *work)
2957{
2958 struct btrfs_ordered_extent *ordered_extent;
2959 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2960 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002961}
2962
Christoph Hellwigb2950862008-12-02 09:54:17 -05002963static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002964 struct extent_state *state, int uptodate)
2965{
Josef Bacik5fd02042012-05-02 14:00:54 -04002966 struct inode *inode = page->mapping->host;
2967 struct btrfs_root *root = BTRFS_I(inode)->root;
2968 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002969 struct btrfs_workqueue *wq;
2970 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002971
liubo1abe9b82011-03-24 11:18:59 +00002972 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2973
Chris Mason8b62b722009-09-02 16:53:46 -04002974 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002975 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2976 end - start + 1, uptodate))
2977 return 0;
2978
Liu Bo9e0af232014-08-15 23:36:53 +08002979 if (btrfs_is_free_space_inode(inode)) {
2980 wq = root->fs_info->endio_freespace_worker;
2981 func = btrfs_freespace_write_helper;
2982 } else {
2983 wq = root->fs_info->endio_write_workers;
2984 func = btrfs_endio_write_helper;
2985 }
Josef Bacik5fd02042012-05-02 14:00:54 -04002986
Liu Bo9e0af232014-08-15 23:36:53 +08002987 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
2988 NULL);
2989 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04002990
2991 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002992}
2993
Miao Xiedc380ae2014-09-12 18:43:55 +08002994static int __readpage_endio_check(struct inode *inode,
2995 struct btrfs_io_bio *io_bio,
2996 int icsum, struct page *page,
2997 int pgoff, u64 start, size_t len)
2998{
2999 char *kaddr;
3000 u32 csum_expected;
3001 u32 csum = ~(u32)0;
3002 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
3003 DEFAULT_RATELIMIT_BURST);
3004
3005 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3006
3007 kaddr = kmap_atomic(page);
3008 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
3009 btrfs_csum_final(csum, (char *)&csum);
3010 if (csum != csum_expected)
3011 goto zeroit;
3012
3013 kunmap_atomic(kaddr);
3014 return 0;
3015zeroit:
3016 if (__ratelimit(&_rs))
David Sterbaf0954c62014-12-19 18:38:44 +01003017 btrfs_warn(BTRFS_I(inode)->root->fs_info,
Miao Xiedc380ae2014-09-12 18:43:55 +08003018 "csum failed ino %llu off %llu csum %u expected csum %u",
3019 btrfs_ino(inode), start, csum, csum_expected);
3020 memset(kaddr + pgoff, 1, len);
3021 flush_dcache_page(page);
3022 kunmap_atomic(kaddr);
3023 if (csum_expected == 0)
3024 return 0;
3025 return -EIO;
3026}
3027
Chris Masond352ac62008-09-29 15:18:18 -04003028/*
Chris Masond352ac62008-09-29 15:18:18 -04003029 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003030 * if there's a match, we allow the bio to finish. If not, the code in
3031 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003032 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003033static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3034 u64 phy_offset, struct page *page,
3035 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003036{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003037 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003038 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003039 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003040 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003041
Chris Masond20f7042008-12-08 16:58:54 -05003042 if (PageChecked(page)) {
3043 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003044 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003045 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003046
3047 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003048 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003049
Yan Zheng17d217f2008-12-12 10:03:38 -05003050 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003051 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05003052 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
3053 GFP_NOFS);
3054 return 0;
3055 }
3056
Miao Xiefacc8a222013-07-25 19:22:34 +08003057 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003058 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3059 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003060}
Chris Masonb888db22007-08-27 16:49:44 -04003061
Yan, Zheng24bbcf042009-11-12 09:36:34 +00003062struct delayed_iput {
3063 struct list_head list;
3064 struct inode *inode;
3065};
3066
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003067/* JDM: If this is fs-wide, why can't we add a pointer to
3068 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf042009-11-12 09:36:34 +00003069void btrfs_add_delayed_iput(struct inode *inode)
3070{
3071 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
3072 struct delayed_iput *delayed;
3073
3074 if (atomic_add_unless(&inode->i_count, -1, 1))
3075 return;
3076
3077 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
3078 delayed->inode = inode;
3079
3080 spin_lock(&fs_info->delayed_iput_lock);
3081 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
3082 spin_unlock(&fs_info->delayed_iput_lock);
3083}
3084
3085void btrfs_run_delayed_iputs(struct btrfs_root *root)
3086{
3087 LIST_HEAD(list);
3088 struct btrfs_fs_info *fs_info = root->fs_info;
3089 struct delayed_iput *delayed;
3090 int empty;
3091
3092 spin_lock(&fs_info->delayed_iput_lock);
3093 empty = list_empty(&fs_info->delayed_iputs);
3094 spin_unlock(&fs_info->delayed_iput_lock);
3095 if (empty)
3096 return;
3097
Yan, Zheng24bbcf042009-11-12 09:36:34 +00003098 spin_lock(&fs_info->delayed_iput_lock);
3099 list_splice_init(&fs_info->delayed_iputs, &list);
3100 spin_unlock(&fs_info->delayed_iput_lock);
3101
3102 while (!list_empty(&list)) {
3103 delayed = list_entry(list.next, struct delayed_iput, list);
3104 list_del(&delayed->list);
3105 iput(delayed->inode);
3106 kfree(delayed);
3107 }
Yan, Zheng24bbcf042009-11-12 09:36:34 +00003108}
3109
Yan, Zhengd68fc572010-05-16 10:49:58 -04003110/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003111 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003112 * files in the subvolume, it removes orphan item and frees block_rsv
3113 * structure.
3114 */
3115void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3116 struct btrfs_root *root)
3117{
Josef Bacik90290e12011-12-02 15:44:12 -05003118 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003119 int ret;
3120
Josef Bacik8a35d952012-05-23 14:26:42 -04003121 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003122 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3123 return;
3124
Josef Bacik90290e12011-12-02 15:44:12 -05003125 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003126 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003127 spin_unlock(&root->orphan_lock);
3128 return;
3129 }
3130
3131 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3132 spin_unlock(&root->orphan_lock);
3133 return;
3134 }
3135
3136 block_rsv = root->orphan_block_rsv;
3137 root->orphan_block_rsv = NULL;
3138 spin_unlock(&root->orphan_lock);
3139
Miao Xie27cdeb72014-04-02 19:51:05 +08003140 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003141 btrfs_root_refs(&root->root_item) > 0) {
3142 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3143 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003144 if (ret)
3145 btrfs_abort_transaction(trans, root, ret);
3146 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003147 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3148 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003149 }
3150
Josef Bacik90290e12011-12-02 15:44:12 -05003151 if (block_rsv) {
3152 WARN_ON(block_rsv->size > 0);
3153 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003154 }
3155}
3156
3157/*
Josef Bacik7b128762008-07-24 12:17:14 -04003158 * This creates an orphan entry for the given inode in case something goes
3159 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003160 *
3161 * NOTE: caller of this function should reserve 5 units of metadata for
3162 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003163 */
3164int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3165{
3166 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003167 struct btrfs_block_rsv *block_rsv = NULL;
3168 int reserve = 0;
3169 int insert = 0;
3170 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003171
Yan, Zhengd68fc572010-05-16 10:49:58 -04003172 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003173 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003174 if (!block_rsv)
3175 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003176 }
3177
Yan, Zhengd68fc572010-05-16 10:49:58 -04003178 spin_lock(&root->orphan_lock);
3179 if (!root->orphan_block_rsv) {
3180 root->orphan_block_rsv = block_rsv;
3181 } else if (block_rsv) {
3182 btrfs_free_block_rsv(root, block_rsv);
3183 block_rsv = NULL;
3184 }
Josef Bacik7b128762008-07-24 12:17:14 -04003185
Josef Bacik8a35d952012-05-23 14:26:42 -04003186 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3187 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003188#if 0
3189 /*
3190 * For proper ENOSPC handling, we should do orphan
3191 * cleanup when mounting. But this introduces backward
3192 * compatibility issue.
3193 */
3194 if (!xchg(&root->orphan_item_inserted, 1))
3195 insert = 2;
3196 else
3197 insert = 1;
3198#endif
3199 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003200 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003201 }
Josef Bacik7b128762008-07-24 12:17:14 -04003202
Josef Bacik72ac3c02012-05-23 14:13:11 -04003203 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3204 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003205 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003206 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003207
Yan, Zhengd68fc572010-05-16 10:49:58 -04003208 /* grab metadata reservation from transaction handle */
3209 if (reserve) {
3210 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003211 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003212 }
3213
3214 /* insert an orphan item to track this unlinked/truncated file */
3215 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003216 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003217 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003218 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003219 if (reserve) {
3220 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3221 &BTRFS_I(inode)->runtime_flags);
3222 btrfs_orphan_release_metadata(inode);
3223 }
3224 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003225 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3226 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003227 btrfs_abort_transaction(trans, root, ret);
3228 return ret;
3229 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003230 }
3231 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003232 }
3233
3234 /* insert an orphan item to track subvolume contains orphan files */
3235 if (insert >= 2) {
3236 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3237 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003238 if (ret && ret != -EEXIST) {
3239 btrfs_abort_transaction(trans, root, ret);
3240 return ret;
3241 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003242 }
3243 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003244}
3245
3246/*
3247 * We have done the truncate/delete so we can go ahead and remove the orphan
3248 * item for this particular inode.
3249 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003250static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3251 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003252{
3253 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003254 int delete_item = 0;
3255 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003256 int ret = 0;
3257
Yan, Zhengd68fc572010-05-16 10:49:58 -04003258 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003259 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3260 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003261 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003262
Josef Bacik72ac3c02012-05-23 14:13:11 -04003263 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3264 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003265 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003266 spin_unlock(&root->orphan_lock);
3267
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003268 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003269 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003270 if (trans)
3271 ret = btrfs_del_orphan_item(trans, root,
3272 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003273 }
Josef Bacik7b128762008-07-24 12:17:14 -04003274
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003275 if (release_rsv)
3276 btrfs_orphan_release_metadata(inode);
3277
Josef Bacik4ef31a42013-08-13 14:10:08 -04003278 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003279}
3280
3281/*
3282 * this cleans up any orphans that may be left on the list from the last use
3283 * of this root.
3284 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003285int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003286{
3287 struct btrfs_path *path;
3288 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003289 struct btrfs_key key, found_key;
3290 struct btrfs_trans_handle *trans;
3291 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003292 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003293 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3294
Yan, Zhengd68fc572010-05-16 10:49:58 -04003295 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003296 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003297
3298 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003299 if (!path) {
3300 ret = -ENOMEM;
3301 goto out;
3302 }
Josef Bacik7b128762008-07-24 12:17:14 -04003303 path->reada = -1;
3304
3305 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003306 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003307 key.offset = (u64)-1;
3308
Josef Bacik7b128762008-07-24 12:17:14 -04003309 while (1) {
3310 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003311 if (ret < 0)
3312 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003313
3314 /*
3315 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003316 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003317 * find the key and see if we have stuff that matches
3318 */
3319 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003320 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003321 if (path->slots[0] == 0)
3322 break;
3323 path->slots[0]--;
3324 }
3325
3326 /* pull out the item */
3327 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003328 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3329
3330 /* make sure the item matches what we want */
3331 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3332 break;
David Sterba962a2982014-06-04 18:41:45 +02003333 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003334 break;
3335
3336 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003337 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003338
3339 /*
3340 * this is where we are basically btrfs_lookup, without the
3341 * crossing root thing. we store the inode number in the
3342 * offset of the orphan item.
3343 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003344
3345 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003346 btrfs_err(root->fs_info,
3347 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003348 ret = -EINVAL;
3349 goto out;
3350 }
3351
3352 last_objectid = found_key.offset;
3353
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003354 found_key.objectid = found_key.offset;
3355 found_key.type = BTRFS_INODE_ITEM_KEY;
3356 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003357 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303358 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003359 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003360 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003361
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003362 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3363 struct btrfs_root *dead_root;
3364 struct btrfs_fs_info *fs_info = root->fs_info;
3365 int is_dead_root = 0;
3366
3367 /*
3368 * this is an orphan in the tree root. Currently these
3369 * could come from 2 sources:
3370 * a) a snapshot deletion in progress
3371 * b) a free space cache inode
3372 * We need to distinguish those two, as the snapshot
3373 * orphan must not get deleted.
3374 * find_dead_roots already ran before us, so if this
3375 * is a snapshot deletion, we should find the root
3376 * in the dead_roots list
3377 */
3378 spin_lock(&fs_info->trans_lock);
3379 list_for_each_entry(dead_root, &fs_info->dead_roots,
3380 root_list) {
3381 if (dead_root->root_key.objectid ==
3382 found_key.objectid) {
3383 is_dead_root = 1;
3384 break;
3385 }
3386 }
3387 spin_unlock(&fs_info->trans_lock);
3388 if (is_dead_root) {
3389 /* prevent this orphan from being found again */
3390 key.offset = found_key.objectid - 1;
3391 continue;
3392 }
3393 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003394 /*
3395 * Inode is already gone but the orphan item is still there,
3396 * kill the orphan item.
3397 */
3398 if (ret == -ESTALE) {
3399 trans = btrfs_start_transaction(root, 1);
3400 if (IS_ERR(trans)) {
3401 ret = PTR_ERR(trans);
3402 goto out;
3403 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003404 btrfs_debug(root->fs_info, "auto deleting %Lu",
3405 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003406 ret = btrfs_del_orphan_item(trans, root,
3407 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003408 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003409 if (ret)
3410 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003411 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003412 }
Josef Bacik7b128762008-07-24 12:17:14 -04003413
Josef Bacik7b128762008-07-24 12:17:14 -04003414 /*
3415 * add this inode to the orphan list so btrfs_orphan_del does
3416 * the proper thing when we hit it
3417 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003418 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3419 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003420 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003421
Josef Bacik7b128762008-07-24 12:17:14 -04003422 /* if we have links, this was a truncate, lets do that */
3423 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303424 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003425 iput(inode);
3426 continue;
3427 }
Josef Bacik7b128762008-07-24 12:17:14 -04003428 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003429
3430 /* 1 for the orphan item deletion. */
3431 trans = btrfs_start_transaction(root, 1);
3432 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003433 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003434 ret = PTR_ERR(trans);
3435 goto out;
3436 }
3437 ret = btrfs_orphan_add(trans, inode);
3438 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003439 if (ret) {
3440 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003441 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003442 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003443
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003444 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003445 if (ret)
3446 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003447 } else {
3448 nr_unlink++;
3449 }
3450
3451 /* this will do delete_inode and everything for us */
3452 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003453 if (ret)
3454 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003455 }
Miao Xie3254c872011-11-10 20:45:05 -05003456 /* release the path since we're done with it */
3457 btrfs_release_path(path);
3458
Yan, Zhengd68fc572010-05-16 10:49:58 -04003459 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3460
3461 if (root->orphan_block_rsv)
3462 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3463 (u64)-1);
3464
Miao Xie27cdeb72014-04-02 19:51:05 +08003465 if (root->orphan_block_rsv ||
3466 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003467 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003468 if (!IS_ERR(trans))
3469 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003470 }
Josef Bacik7b128762008-07-24 12:17:14 -04003471
3472 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003473 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003474 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003475 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003476
3477out:
3478 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003479 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003480 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003481 btrfs_free_path(path);
3482 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003483}
3484
Chris Masond352ac62008-09-29 15:18:18 -04003485/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003486 * very simple check to peek ahead in the leaf looking for xattrs. If we
3487 * don't find any xattrs, we know there can't be any acls.
3488 *
3489 * slot is the slot the inode is in, objectid is the objectid of the inode
3490 */
3491static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003492 int slot, u64 objectid,
3493 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003494{
3495 u32 nritems = btrfs_header_nritems(leaf);
3496 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003497 static u64 xattr_access = 0;
3498 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003499 int scanned = 0;
3500
Josef Bacikf23b5a52013-06-19 10:16:26 -04003501 if (!xattr_access) {
3502 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3503 strlen(POSIX_ACL_XATTR_ACCESS));
3504 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3505 strlen(POSIX_ACL_XATTR_DEFAULT));
3506 }
3507
Chris Mason46a53cc2009-04-27 11:47:50 -04003508 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003509 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003510 while (slot < nritems) {
3511 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3512
3513 /* we found a different objectid, there must not be acls */
3514 if (found_key.objectid != objectid)
3515 return 0;
3516
3517 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003518 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003519 if (*first_xattr_slot == -1)
3520 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003521 if (found_key.offset == xattr_access ||
3522 found_key.offset == xattr_default)
3523 return 1;
3524 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003525
3526 /*
3527 * we found a key greater than an xattr key, there can't
3528 * be any acls later on
3529 */
3530 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3531 return 0;
3532
3533 slot++;
3534 scanned++;
3535
3536 /*
3537 * it goes inode, inode backrefs, xattrs, extents,
3538 * so if there are a ton of hard links to an inode there can
3539 * be a lot of backrefs. Don't waste time searching too hard,
3540 * this is just an optimization
3541 */
3542 if (scanned >= 8)
3543 break;
3544 }
3545 /* we hit the end of the leaf before we found an xattr or
3546 * something larger than an xattr. We have to assume the inode
3547 * has acls
3548 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003549 if (*first_xattr_slot == -1)
3550 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003551 return 1;
3552}
3553
3554/*
Chris Masond352ac62008-09-29 15:18:18 -04003555 * read an inode from the btree into the in-memory inode
3556 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003557static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003558{
3559 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003560 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003561 struct btrfs_inode_item *inode_item;
3562 struct btrfs_root *root = BTRFS_I(inode)->root;
3563 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003564 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003565 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003566 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003567 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003568 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003569 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003570
3571 ret = btrfs_fill_inode(inode, &rdev);
3572 if (!ret)
3573 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003574
3575 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003576 if (!path)
3577 goto make_bad;
3578
Chris Mason39279cc2007-06-12 06:35:45 -04003579 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003580
Chris Mason39279cc2007-06-12 06:35:45 -04003581 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003582 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003583 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003584
Chris Mason5f39d392007-10-15 16:14:19 -04003585 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003586
3587 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003588 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003589
Chris Mason5f39d392007-10-15 16:14:19 -04003590 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3591 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003592 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003593 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003594 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3595 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003596 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003597
David Sterbaa937b972014-12-12 17:39:12 +01003598 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3599 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003600
David Sterbaa937b972014-12-12 17:39:12 +01003601 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3602 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003603
David Sterbaa937b972014-12-12 17:39:12 +01003604 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3605 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003606
chandan r9cc97d62012-07-04 12:48:07 +05303607 BTRFS_I(inode)->i_otime.tv_sec =
3608 btrfs_timespec_sec(leaf, &inode_item->otime);
3609 BTRFS_I(inode)->i_otime.tv_nsec =
3610 btrfs_timespec_nsec(leaf, &inode_item->otime);
3611
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003612 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003613 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003614 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3615
3616 /*
3617 * If we were modified in the current generation and evicted from memory
3618 * and then re-read we need to do a full sync since we don't have any
3619 * idea about which extents were modified before we were evicted from
3620 * cache.
3621 */
3622 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3623 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3624 &BTRFS_I(inode)->runtime_flags);
3625
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003626 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003627 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003628 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003629 rdev = btrfs_inode_rdev(leaf, inode_item);
3630
Josef Bacikaec74772008-07-24 12:12:38 -04003631 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003632 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003633
3634cache_index:
3635 path->slots[0]++;
3636 if (inode->i_nlink != 1 ||
3637 path->slots[0] >= btrfs_header_nritems(leaf))
3638 goto cache_acl;
3639
3640 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3641 if (location.objectid != btrfs_ino(inode))
3642 goto cache_acl;
3643
3644 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3645 if (location.type == BTRFS_INODE_REF_KEY) {
3646 struct btrfs_inode_ref *ref;
3647
3648 ref = (struct btrfs_inode_ref *)ptr;
3649 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3650 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3651 struct btrfs_inode_extref *extref;
3652
3653 extref = (struct btrfs_inode_extref *)ptr;
3654 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3655 extref);
3656 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003657cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003658 /*
3659 * try to precache a NULL acl entry for files that don't have
3660 * any xattrs or acls
3661 */
Li Zefan33345d012011-04-20 10:31:50 +08003662 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003663 btrfs_ino(inode), &first_xattr_slot);
3664 if (first_xattr_slot != -1) {
3665 path->slots[0] = first_xattr_slot;
3666 ret = btrfs_load_inode_props(inode, path);
3667 if (ret)
3668 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003669 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003670 btrfs_ino(inode),
3671 root->root_key.objectid, ret);
3672 }
3673 btrfs_free_path(path);
3674
Al Viro72c04902009-06-24 16:58:48 -04003675 if (!maybe_acls)
3676 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003677
Chris Mason39279cc2007-06-12 06:35:45 -04003678 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003679 case S_IFREG:
3680 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003681 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003682 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003683 inode->i_fop = &btrfs_file_operations;
3684 inode->i_op = &btrfs_file_inode_operations;
3685 break;
3686 case S_IFDIR:
3687 inode->i_fop = &btrfs_dir_file_operations;
3688 if (root == root->fs_info->tree_root)
3689 inode->i_op = &btrfs_dir_ro_inode_operations;
3690 else
3691 inode->i_op = &btrfs_dir_inode_operations;
3692 break;
3693 case S_IFLNK:
3694 inode->i_op = &btrfs_symlink_inode_operations;
3695 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003696 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003697 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003698 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003699 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003700 init_special_inode(inode, inode->i_mode, rdev);
3701 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003702 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003703
3704 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003705 return;
3706
3707make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003708 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003709 make_bad_inode(inode);
3710}
3711
Chris Masond352ac62008-09-29 15:18:18 -04003712/*
3713 * given a leaf and an inode, copy the inode fields into the leaf
3714 */
Chris Masone02119d2008-09-05 16:13:11 -04003715static void fill_inode_item(struct btrfs_trans_handle *trans,
3716 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003717 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003718 struct inode *inode)
3719{
Liu Bo51fab692012-12-27 09:01:21 +00003720 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003721
Liu Bo51fab692012-12-27 09:01:21 +00003722 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003723
Liu Bo51fab692012-12-27 09:01:21 +00003724 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3725 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3726 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3727 &token);
3728 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3729 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003730
David Sterbaa937b972014-12-12 17:39:12 +01003731 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003732 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003733 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003734 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003735
David Sterbaa937b972014-12-12 17:39:12 +01003736 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003737 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003738 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003739 inode->i_mtime.tv_nsec, &token);
3740
David Sterbaa937b972014-12-12 17:39:12 +01003741 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003742 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003743 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003744 inode->i_ctime.tv_nsec, &token);
3745
chandan r9cc97d62012-07-04 12:48:07 +05303746 btrfs_set_token_timespec_sec(leaf, &item->otime,
3747 BTRFS_I(inode)->i_otime.tv_sec, &token);
3748 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3749 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3750
Liu Bo51fab692012-12-27 09:01:21 +00003751 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3752 &token);
3753 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3754 &token);
3755 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3756 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3757 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3758 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3759 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003760}
3761
Chris Masond352ac62008-09-29 15:18:18 -04003762/*
3763 * copy everything in the in-memory inode into the btree.
3764 */
Chris Mason21151332011-11-10 20:39:08 -05003765static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003766 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003767{
3768 struct btrfs_inode_item *inode_item;
3769 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003770 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003771 int ret;
3772
3773 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003774 if (!path)
3775 return -ENOMEM;
3776
Chris Masonb9473432009-03-13 11:00:37 -04003777 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003778 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3779 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003780 if (ret) {
3781 if (ret > 0)
3782 ret = -ENOENT;
3783 goto failed;
3784 }
3785
Chris Mason5f39d392007-10-15 16:14:19 -04003786 leaf = path->nodes[0];
3787 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003788 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003789
Chris Masone02119d2008-09-05 16:13:11 -04003790 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003791 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003792 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003793 ret = 0;
3794failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003795 btrfs_free_path(path);
3796 return ret;
3797}
3798
Chris Masond352ac62008-09-29 15:18:18 -04003799/*
Chris Mason21151332011-11-10 20:39:08 -05003800 * copy everything in the in-memory inode into the btree.
3801 */
3802noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3803 struct btrfs_root *root, struct inode *inode)
3804{
3805 int ret;
3806
3807 /*
3808 * If the inode is a free space inode, we can deadlock during commit
3809 * if we put it into the delayed code.
3810 *
3811 * The data relocation inode should also be directly updated
3812 * without delay
3813 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003814 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003815 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3816 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003817 btrfs_update_root_times(trans, root);
3818
Chris Mason21151332011-11-10 20:39:08 -05003819 ret = btrfs_delayed_update_inode(trans, root, inode);
3820 if (!ret)
3821 btrfs_set_inode_last_trans(trans, inode);
3822 return ret;
3823 }
3824
3825 return btrfs_update_inode_item(trans, root, inode);
3826}
3827
Josef Bacikbe6aef62012-10-22 15:43:12 -04003828noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3829 struct btrfs_root *root,
3830 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003831{
3832 int ret;
3833
3834 ret = btrfs_update_inode(trans, root, inode);
3835 if (ret == -ENOSPC)
3836 return btrfs_update_inode_item(trans, root, inode);
3837 return ret;
3838}
3839
3840/*
Chris Masond352ac62008-09-29 15:18:18 -04003841 * unlink helper that gets used here in inode.c and in the tree logging
3842 * recovery code. It remove a link in a directory with a given name, and
3843 * also drops the back refs in the inode to the directory
3844 */
Al Viro92986792011-03-04 17:14:37 +00003845static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3846 struct btrfs_root *root,
3847 struct inode *dir, struct inode *inode,
3848 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003849{
3850 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003851 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003852 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003853 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003854 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003855 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003856 u64 ino = btrfs_ino(inode);
3857 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003858
3859 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003860 if (!path) {
3861 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003862 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003863 }
3864
Chris Masonb9473432009-03-13 11:00:37 -04003865 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003866 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003867 name, name_len, -1);
3868 if (IS_ERR(di)) {
3869 ret = PTR_ERR(di);
3870 goto err;
3871 }
3872 if (!di) {
3873 ret = -ENOENT;
3874 goto err;
3875 }
Chris Mason5f39d392007-10-15 16:14:19 -04003876 leaf = path->nodes[0];
3877 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003878 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003879 if (ret)
3880 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003881 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003882
Miao Xie67de1172013-12-26 13:07:06 +08003883 /*
3884 * If we don't have dir index, we have to get it by looking up
3885 * the inode ref, since we get the inode ref, remove it directly,
3886 * it is unnecessary to do delayed deletion.
3887 *
3888 * But if we have dir index, needn't search inode ref to get it.
3889 * Since the inode ref is close to the inode item, it is better
3890 * that we delay to delete it, and just do this deletion when
3891 * we update the inode item.
3892 */
3893 if (BTRFS_I(inode)->dir_index) {
3894 ret = btrfs_delayed_delete_inode_ref(inode);
3895 if (!ret) {
3896 index = BTRFS_I(inode)->dir_index;
3897 goto skip_backref;
3898 }
3899 }
3900
Li Zefan33345d012011-04-20 10:31:50 +08003901 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3902 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003903 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003904 btrfs_info(root->fs_info,
3905 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003906 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003907 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003908 goto err;
3909 }
Miao Xie67de1172013-12-26 13:07:06 +08003910skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003911 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003912 if (ret) {
3913 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003914 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003915 }
Chris Mason39279cc2007-06-12 06:35:45 -04003916
Chris Masone02119d2008-09-05 16:13:11 -04003917 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003918 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003919 if (ret != 0 && ret != -ENOENT) {
3920 btrfs_abort_transaction(trans, root, ret);
3921 goto err;
3922 }
Chris Masone02119d2008-09-05 16:13:11 -04003923
3924 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3925 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003926 if (ret == -ENOENT)
3927 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003928 else if (ret)
3929 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003930err:
3931 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003932 if (ret)
3933 goto out;
3934
3935 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003936 inode_inc_iversion(inode);
3937 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003938 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003939 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003940out:
Chris Mason39279cc2007-06-12 06:35:45 -04003941 return ret;
3942}
3943
Al Viro92986792011-03-04 17:14:37 +00003944int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3945 struct btrfs_root *root,
3946 struct inode *dir, struct inode *inode,
3947 const char *name, int name_len)
3948{
3949 int ret;
3950 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3951 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003952 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003953 ret = btrfs_update_inode(trans, root, inode);
3954 }
3955 return ret;
3956}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003957
3958/*
3959 * helper to start transaction for unlink and rmdir.
3960 *
Josef Bacikd52be812013-05-29 14:54:47 -04003961 * unlink and rmdir are special in btrfs, they do not always free space, so
3962 * if we cannot make our reservations the normal way try and see if there is
3963 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3964 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003965 */
Josef Bacikd52be812013-05-29 14:54:47 -04003966static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003967{
3968 struct btrfs_trans_handle *trans;
3969 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003970 int ret;
3971
Josef Bacike70bea52011-10-11 14:18:24 -04003972 /*
3973 * 1 for the possible orphan item
3974 * 1 for the dir item
3975 * 1 for the dir index
3976 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003977 * 1 for the inode
3978 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003979 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003980 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3981 return trans;
3982
Josef Bacikd52be812013-05-29 14:54:47 -04003983 if (PTR_ERR(trans) == -ENOSPC) {
3984 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003985
Josef Bacikd52be812013-05-29 14:54:47 -04003986 trans = btrfs_start_transaction(root, 0);
3987 if (IS_ERR(trans))
3988 return trans;
3989 ret = btrfs_cond_migrate_bytes(root->fs_info,
3990 &root->fs_info->trans_block_rsv,
3991 num_bytes, 5);
3992 if (ret) {
3993 btrfs_end_transaction(trans, root);
3994 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003995 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003996 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003997 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003998 }
Josef Bacikd52be812013-05-29 14:54:47 -04003999 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004000}
4001
Chris Mason39279cc2007-06-12 06:35:45 -04004002static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4003{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004004 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004005 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04004006 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004007 int ret;
4008
Josef Bacikd52be812013-05-29 14:54:47 -04004009 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004010 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004011 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004012
Chris Mason12fcfd22009-03-24 10:24:20 -04004013 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
4014
Chris Masone02119d2008-09-05 16:13:11 -04004015 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4016 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004017 if (ret)
4018 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004019
Yan, Zhenga22285a2010-05-16 10:48:46 -04004020 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004021 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004022 if (ret)
4023 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004024 }
Josef Bacik7b128762008-07-24 12:17:14 -04004025
Tsutomu Itohb5324022011-07-19 07:27:20 +00004026out:
Josef Bacikd52be812013-05-29 14:54:47 -04004027 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004028 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004029 return ret;
4030}
4031
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004032int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4033 struct btrfs_root *root,
4034 struct inode *dir, u64 objectid,
4035 const char *name, int name_len)
4036{
4037 struct btrfs_path *path;
4038 struct extent_buffer *leaf;
4039 struct btrfs_dir_item *di;
4040 struct btrfs_key key;
4041 u64 index;
4042 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004043 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004044
4045 path = btrfs_alloc_path();
4046 if (!path)
4047 return -ENOMEM;
4048
Li Zefan33345d012011-04-20 10:31:50 +08004049 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004050 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004051 if (IS_ERR_OR_NULL(di)) {
4052 if (!di)
4053 ret = -ENOENT;
4054 else
4055 ret = PTR_ERR(di);
4056 goto out;
4057 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004058
4059 leaf = path->nodes[0];
4060 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4061 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4062 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004063 if (ret) {
4064 btrfs_abort_transaction(trans, root, ret);
4065 goto out;
4066 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004067 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004068
4069 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4070 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004071 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004072 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004073 if (ret != -ENOENT) {
4074 btrfs_abort_transaction(trans, root, ret);
4075 goto out;
4076 }
Li Zefan33345d012011-04-20 10:31:50 +08004077 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004078 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004079 if (IS_ERR_OR_NULL(di)) {
4080 if (!di)
4081 ret = -ENOENT;
4082 else
4083 ret = PTR_ERR(di);
4084 btrfs_abort_transaction(trans, root, ret);
4085 goto out;
4086 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004087
4088 leaf = path->nodes[0];
4089 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004090 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004091 index = key.offset;
4092 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004093 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004094
Miao Xie16cdcec2011-04-22 18:12:22 +08004095 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004096 if (ret) {
4097 btrfs_abort_transaction(trans, root, ret);
4098 goto out;
4099 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004100
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004101 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004102 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004103 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06004104 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004105 if (ret)
4106 btrfs_abort_transaction(trans, root, ret);
4107out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004108 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004109 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004110}
4111
Chris Mason39279cc2007-06-12 06:35:45 -04004112static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4113{
4114 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004115 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004116 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004117 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004118
David Sterbab3ae2442012-09-13 16:04:34 -06004119 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004120 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004121 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4122 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004123
Josef Bacikd52be812013-05-29 14:54:47 -04004124 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004125 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004126 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004127
Li Zefan33345d012011-04-20 10:31:50 +08004128 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004129 err = btrfs_unlink_subvol(trans, root, dir,
4130 BTRFS_I(inode)->location.objectid,
4131 dentry->d_name.name,
4132 dentry->d_name.len);
4133 goto out;
4134 }
4135
Josef Bacik7b128762008-07-24 12:17:14 -04004136 err = btrfs_orphan_add(trans, inode);
4137 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004138 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004139
Chris Mason39279cc2007-06-12 06:35:45 -04004140 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004141 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4142 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004143 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004144 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004145out:
Josef Bacikd52be812013-05-29 14:54:47 -04004146 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004147 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004148
Chris Mason39279cc2007-06-12 06:35:45 -04004149 return err;
4150}
4151
Chris Mason323ac952008-10-01 19:05:46 -04004152/*
Chris Mason39279cc2007-06-12 06:35:45 -04004153 * this can truncate away extent items, csum items and directory items.
4154 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004155 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004156 *
4157 * csum items that cross the new i_size are truncated to the new size
4158 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004159 *
4160 * min_type is the minimum key type to truncate down to. If set to 0, this
4161 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004162 */
Yan, Zheng80825102009-11-12 09:35:36 +00004163int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4164 struct btrfs_root *root,
4165 struct inode *inode,
4166 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004167{
Chris Mason39279cc2007-06-12 06:35:45 -04004168 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004169 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004170 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004171 struct btrfs_key key;
4172 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004173 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004174 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004175 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004176 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004177 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00004178 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004179 int found_extent;
4180 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004181 int pending_del_nr = 0;
4182 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004183 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004184 int ret;
4185 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004186 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004187
4188 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004189
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004190 path = btrfs_alloc_path();
4191 if (!path)
4192 return -ENOMEM;
4193 path->reada = -1;
4194
Josef Bacik5dc562c2012-08-17 13:14:17 -04004195 /*
4196 * We want to drop from the next block forward in case this new size is
4197 * not block aligned since we will be keeping the last block of the
4198 * extent just the way it is.
4199 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004200 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4201 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004202 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4203 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004204
Miao Xie16cdcec2011-04-22 18:12:22 +08004205 /*
4206 * This function is also used to drop the items in the log tree before
4207 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4208 * it is used to drop the loged items. So we shouldn't kill the delayed
4209 * items.
4210 */
4211 if (min_type == 0 && root == BTRFS_I(inode)->root)
4212 btrfs_kill_delayed_inode_items(inode);
4213
Li Zefan33345d012011-04-20 10:31:50 +08004214 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004215 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004216 key.type = (u8)-1;
4217
Chris Mason85e21ba2008-01-29 15:11:36 -05004218search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004219 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004220 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004221 if (ret < 0) {
4222 err = ret;
4223 goto out;
4224 }
Chris Masond3977122009-01-05 21:25:51 -05004225
Chris Mason85e21ba2008-01-29 15:11:36 -05004226 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004227 /* there are no items in the tree for us to truncate, we're
4228 * done
4229 */
Yan, Zheng80825102009-11-12 09:35:36 +00004230 if (path->slots[0] == 0)
4231 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004232 path->slots[0]--;
4233 }
4234
Chris Masond3977122009-01-05 21:25:51 -05004235 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004236 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004237 leaf = path->nodes[0];
4238 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004239 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004240
Li Zefan33345d012011-04-20 10:31:50 +08004241 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004242 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004243
Chris Mason85e21ba2008-01-29 15:11:36 -05004244 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004245 break;
4246
Chris Mason5f39d392007-10-15 16:14:19 -04004247 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004248 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004249 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004250 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004251 extent_type = btrfs_file_extent_type(leaf, fi);
4252 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004253 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004254 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004255 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004256 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004257 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004258 }
Yan008630c2007-11-07 13:31:09 -05004259 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004260 }
Yan, Zheng80825102009-11-12 09:35:36 +00004261 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004262 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004263 } else {
4264 if (item_end < new_size)
4265 break;
4266 if (found_key.offset >= new_size)
4267 del_item = 1;
4268 else
4269 del_item = 0;
4270 }
Chris Mason39279cc2007-06-12 06:35:45 -04004271 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004272 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004273 if (found_type != BTRFS_EXTENT_DATA_KEY)
4274 goto delete;
4275
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004276 if (del_item)
4277 last_size = found_key.offset;
4278 else
4279 last_size = new_size;
4280
Chris Mason179e29e2007-11-01 11:28:41 -04004281 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004282 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004283 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004284 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004285 u64 orig_num_bytes =
4286 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004287 extent_num_bytes = ALIGN(new_size -
4288 found_key.offset,
4289 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004290 btrfs_set_file_extent_num_bytes(leaf, fi,
4291 extent_num_bytes);
4292 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004293 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004294 if (test_bit(BTRFS_ROOT_REF_COWS,
4295 &root->state) &&
4296 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004297 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004298 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004299 } else {
Chris Masondb945352007-10-15 16:15:53 -04004300 extent_num_bytes =
4301 btrfs_file_extent_disk_num_bytes(leaf,
4302 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004303 extent_offset = found_key.offset -
4304 btrfs_file_extent_offset(leaf, fi);
4305
Chris Mason39279cc2007-06-12 06:35:45 -04004306 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004307 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004308 if (extent_start != 0) {
4309 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004310 if (test_bit(BTRFS_ROOT_REF_COWS,
4311 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004312 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004313 }
4314 }
Chris Mason90692182008-02-08 13:49:28 -05004315 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004316 /*
4317 * we can't truncate inline items that have had
4318 * special encodings
4319 */
4320 if (!del_item &&
4321 btrfs_file_extent_compression(leaf, fi) == 0 &&
4322 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4323 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004324 u32 size = new_size - found_key.offset;
4325
Miao Xie27cdeb72014-04-02 19:51:05 +08004326 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004327 inode_sub_bytes(inode, item_end + 1 -
4328 new_size);
Chris Mason514ac8a2014-01-03 21:07:00 -08004329
4330 /*
4331 * update the ram bytes to properly reflect
4332 * the new size of our item
4333 */
4334 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004335 size =
4336 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004337 btrfs_truncate_item(root, path, size, 1);
Miao Xie27cdeb72014-04-02 19:51:05 +08004338 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4339 &root->state)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004340 inode_sub_bytes(inode, item_end + 1 -
4341 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004342 }
Chris Mason39279cc2007-06-12 06:35:45 -04004343 }
Chris Mason179e29e2007-11-01 11:28:41 -04004344delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004345 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004346 if (!pending_del_nr) {
4347 /* no pending yet, add ourselves */
4348 pending_del_slot = path->slots[0];
4349 pending_del_nr = 1;
4350 } else if (pending_del_nr &&
4351 path->slots[0] + 1 == pending_del_slot) {
4352 /* hop on the pending chunk */
4353 pending_del_nr++;
4354 pending_del_slot = path->slots[0];
4355 } else {
Chris Masond3977122009-01-05 21:25:51 -05004356 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004357 }
Chris Mason39279cc2007-06-12 06:35:45 -04004358 } else {
4359 break;
4360 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004361 if (found_extent &&
4362 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4363 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004364 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004365 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004366 extent_num_bytes, 0,
4367 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004368 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004369 BUG_ON(ret);
4370 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004371
Yan, Zheng80825102009-11-12 09:35:36 +00004372 if (found_type == BTRFS_INODE_ITEM_KEY)
4373 break;
4374
4375 if (path->slots[0] == 0 ||
4376 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004377 if (pending_del_nr) {
4378 ret = btrfs_del_items(trans, root, path,
4379 pending_del_slot,
4380 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004381 if (ret) {
4382 btrfs_abort_transaction(trans,
4383 root, ret);
4384 goto error;
4385 }
Yan, Zheng80825102009-11-12 09:35:36 +00004386 pending_del_nr = 0;
4387 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004388 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004389 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004390 } else {
4391 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004392 }
Chris Mason39279cc2007-06-12 06:35:45 -04004393 }
Yan, Zheng80825102009-11-12 09:35:36 +00004394out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004395 if (pending_del_nr) {
4396 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4397 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004398 if (ret)
4399 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004400 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004401error:
Filipe Mananadac57052014-08-29 20:54:26 +01004402 if (last_size != (u64)-1 &&
4403 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004404 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004405 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004406 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004407}
4408
Chris Masona52d9a82007-08-27 16:49:44 -04004409/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004410 * btrfs_truncate_page - read, zero a chunk and write a page
4411 * @inode - inode that we're zeroing
4412 * @from - the offset to start zeroing
4413 * @len - the length to zero, 0 to zero the entire range respective to the
4414 * offset
4415 * @front - zero up to the offset instead of from the offset on
4416 *
4417 * This will find the page for the "from" offset and cow the page and zero the
4418 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004419 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004420int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4421 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004422{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004423 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004424 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004425 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4426 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004427 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004428 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004429 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004430 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4431 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4432 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004433 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004434 int ret = 0;
4435 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004436 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004437
Josef Bacik2aaa6652012-08-29 14:27:18 -04004438 if ((offset & (blocksize - 1)) == 0 &&
4439 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004440 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004441 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004442 if (ret)
4443 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004444
Chris Mason211c17f2008-05-15 09:13:45 -04004445again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004446 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004447 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004448 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004449 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004450 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004451 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004452
4453 page_start = page_offset(page);
4454 page_end = page_start + PAGE_CACHE_SIZE - 1;
4455
Chris Masona52d9a82007-08-27 16:49:44 -04004456 if (!PageUptodate(page)) {
4457 ret = btrfs_readpage(NULL, page);
4458 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004459 if (page->mapping != mapping) {
4460 unlock_page(page);
4461 page_cache_release(page);
4462 goto again;
4463 }
Chris Masona52d9a82007-08-27 16:49:44 -04004464 if (!PageUptodate(page)) {
4465 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004466 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004467 }
4468 }
Chris Mason211c17f2008-05-15 09:13:45 -04004469 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004470
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004471 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004472 set_page_extent_mapped(page);
4473
4474 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4475 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004476 unlock_extent_cached(io_tree, page_start, page_end,
4477 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004478 unlock_page(page);
4479 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004480 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004481 btrfs_put_ordered_extent(ordered);
4482 goto again;
4483 }
4484
Josef Bacik2ac55d42010-02-03 19:33:23 +00004485 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004486 EXTENT_DIRTY | EXTENT_DELALLOC |
4487 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004488 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004489
Josef Bacik2ac55d42010-02-03 19:33:23 +00004490 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4491 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004492 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004493 unlock_extent_cached(io_tree, page_start, page_end,
4494 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004495 goto out_unlock;
4496 }
4497
Chris Masone6dcd2d2008-07-17 12:53:50 -04004498 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004499 if (!len)
4500 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004501 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004502 if (front)
4503 memset(kaddr, 0, offset);
4504 else
4505 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004506 flush_dcache_page(page);
4507 kunmap(page);
4508 }
Chris Mason247e7432008-07-17 12:53:51 -04004509 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004510 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004511 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4512 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004513
Chris Mason89642222008-07-24 09:41:53 -04004514out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004515 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004516 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004517 unlock_page(page);
4518 page_cache_release(page);
4519out:
4520 return ret;
4521}
4522
Josef Bacik16e75492013-10-22 12:18:51 -04004523static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4524 u64 offset, u64 len)
4525{
4526 struct btrfs_trans_handle *trans;
4527 int ret;
4528
4529 /*
4530 * Still need to make sure the inode looks like it's been updated so
4531 * that any holes get logged if we fsync.
4532 */
4533 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4534 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4535 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4536 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4537 return 0;
4538 }
4539
4540 /*
4541 * 1 - for the one we're dropping
4542 * 1 - for the one we're adding
4543 * 1 - for updating the inode.
4544 */
4545 trans = btrfs_start_transaction(root, 3);
4546 if (IS_ERR(trans))
4547 return PTR_ERR(trans);
4548
4549 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4550 if (ret) {
4551 btrfs_abort_transaction(trans, root, ret);
4552 btrfs_end_transaction(trans, root);
4553 return ret;
4554 }
4555
4556 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4557 0, 0, len, 0, len, 0, 0, 0);
4558 if (ret)
4559 btrfs_abort_transaction(trans, root, ret);
4560 else
4561 btrfs_update_inode(trans, root, inode);
4562 btrfs_end_transaction(trans, root);
4563 return ret;
4564}
4565
Josef Bacik695a0d02011-03-04 15:46:53 -05004566/*
4567 * This function puts in dummy file extents for the area we're creating a hole
4568 * for. So if we are truncating this file to a larger size we need to insert
4569 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4570 * the range between oldsize and size
4571 */
Josef Bacika41ad392011-01-31 15:30:16 -05004572int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004573{
Yan Zheng9036c102008-10-30 14:19:41 -04004574 struct btrfs_root *root = BTRFS_I(inode)->root;
4575 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004576 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004577 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004578 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004579 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4580 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004581 u64 last_byte;
4582 u64 cur_offset;
4583 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004584 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004585
Josef Bacika71754f2013-06-17 17:14:39 -04004586 /*
4587 * If our size started in the middle of a page we need to zero out the
4588 * rest of the page before we expand the i_size, otherwise we could
4589 * expose stale data.
4590 */
4591 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4592 if (err)
4593 return err;
4594
Yan Zheng9036c102008-10-30 14:19:41 -04004595 if (size <= hole_start)
4596 return 0;
4597
Yan Zheng9036c102008-10-30 14:19:41 -04004598 while (1) {
4599 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004600
Josef Bacik2ac55d42010-02-03 19:33:23 +00004601 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004602 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004603 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4604 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004605 if (!ordered)
4606 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004607 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4608 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004609 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004610 btrfs_put_ordered_extent(ordered);
4611 }
4612
Yan Zheng9036c102008-10-30 14:19:41 -04004613 cur_offset = hole_start;
4614 while (1) {
4615 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4616 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004617 if (IS_ERR(em)) {
4618 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004619 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004620 break;
4621 }
Yan Zheng9036c102008-10-30 14:19:41 -04004622 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004623 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004624 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004625 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004626 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004627
Josef Bacik16e75492013-10-22 12:18:51 -04004628 err = maybe_insert_hole(root, inode, cur_offset,
4629 hole_size);
4630 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004631 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004632 btrfs_drop_extent_cache(inode, cur_offset,
4633 cur_offset + hole_size - 1, 0);
4634 hole_em = alloc_extent_map();
4635 if (!hole_em) {
4636 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4637 &BTRFS_I(inode)->runtime_flags);
4638 goto next;
4639 }
4640 hole_em->start = cur_offset;
4641 hole_em->len = hole_size;
4642 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004643
Josef Bacik5dc562c2012-08-17 13:14:17 -04004644 hole_em->block_start = EXTENT_MAP_HOLE;
4645 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004646 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004647 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004648 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4649 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004650 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004651
4652 while (1) {
4653 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004654 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004655 write_unlock(&em_tree->lock);
4656 if (err != -EEXIST)
4657 break;
4658 btrfs_drop_extent_cache(inode, cur_offset,
4659 cur_offset +
4660 hole_size - 1, 0);
4661 }
4662 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004663 }
Josef Bacik16e75492013-10-22 12:18:51 -04004664next:
Yan Zheng9036c102008-10-30 14:19:41 -04004665 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004666 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004667 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004668 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004669 break;
4670 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004671 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004672 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4673 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004674 return err;
4675}
4676
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004677static int wait_snapshoting_atomic_t(atomic_t *a)
4678{
4679 schedule();
4680 return 0;
4681}
4682
4683static void wait_for_snapshot_creation(struct btrfs_root *root)
4684{
4685 while (true) {
4686 int ret;
4687
4688 ret = btrfs_start_write_no_snapshoting(root);
4689 if (ret)
4690 break;
4691 wait_on_atomic_t(&root->will_be_snapshoted,
4692 wait_snapshoting_atomic_t,
4693 TASK_UNINTERRUPTIBLE);
4694 }
4695}
4696
Eric Sandeen3972f262013-01-12 02:57:22 +00004697static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004698{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004699 struct btrfs_root *root = BTRFS_I(inode)->root;
4700 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004701 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004702 loff_t newsize = attr->ia_size;
4703 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004704 int ret;
4705
Eric Sandeen3972f262013-01-12 02:57:22 +00004706 /*
4707 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4708 * special case where we need to update the times despite not having
4709 * these flags set. For all other operations the VFS set these flags
4710 * explicitly if it wants a timestamp update.
4711 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004712 if (newsize != oldsize) {
4713 inode_inc_iversion(inode);
4714 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4715 inode->i_ctime = inode->i_mtime =
4716 current_fs_time(inode->i_sb);
4717 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004718
Josef Bacika41ad392011-01-31 15:30:16 -05004719 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004720 truncate_pagecache(inode, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004721 /*
4722 * Don't do an expanding truncate while snapshoting is ongoing.
4723 * This is to ensure the snapshot captures a fully consistent
4724 * state of this file - if the snapshot captures this expanding
4725 * truncation, it must capture all writes that happened before
4726 * this truncation.
4727 */
4728 wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004729 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004730 if (ret) {
4731 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004732 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004733 }
Yan, Zheng80825102009-11-12 09:35:36 +00004734
Miao Xief4a2f4c2011-12-14 20:12:01 -05004735 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004736 if (IS_ERR(trans)) {
4737 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004738 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004739 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004740
4741 i_size_write(inode, newsize);
4742 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4743 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004744 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004745 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004746 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004747
Josef Bacika41ad392011-01-31 15:30:16 -05004748 /*
4749 * We're truncating a file that used to have good data down to
4750 * zero. Make sure it gets into the ordered flush list so that
4751 * any new writes get down to disk quickly.
4752 */
4753 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004754 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4755 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004756
Josef Bacikf3fe8202013-01-07 17:03:21 -05004757 /*
4758 * 1 for the orphan item we're going to add
4759 * 1 for the orphan item deletion.
4760 */
4761 trans = btrfs_start_transaction(root, 2);
4762 if (IS_ERR(trans))
4763 return PTR_ERR(trans);
4764
4765 /*
4766 * We need to do this in case we fail at _any_ point during the
4767 * actual truncate. Once we do the truncate_setsize we could
4768 * invalidate pages which forces any outstanding ordered io to
4769 * be instantly completed which will give us extents that need
4770 * to be truncated. If we fail to get an orphan inode down we
4771 * could have left over extents that were never meant to live,
4772 * so we need to garuntee from this point on that everything
4773 * will be consistent.
4774 */
4775 ret = btrfs_orphan_add(trans, inode);
4776 btrfs_end_transaction(trans, root);
4777 if (ret)
4778 return ret;
4779
Josef Bacika41ad392011-01-31 15:30:16 -05004780 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4781 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004782
4783 /* Disable nonlocked read DIO to avoid the end less truncate */
4784 btrfs_inode_block_unlocked_dio(inode);
4785 inode_dio_wait(inode);
4786 btrfs_inode_resume_unlocked_dio(inode);
4787
Josef Bacika41ad392011-01-31 15:30:16 -05004788 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004789 if (ret && inode->i_nlink) {
4790 int err;
4791
4792 /*
4793 * failed to truncate, disk_i_size is only adjusted down
4794 * as we remove extents, so it should represent the true
4795 * size of the inode, so reset the in memory size and
4796 * delete our orphan entry.
4797 */
4798 trans = btrfs_join_transaction(root);
4799 if (IS_ERR(trans)) {
4800 btrfs_orphan_del(NULL, inode);
4801 return ret;
4802 }
4803 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4804 err = btrfs_orphan_del(trans, inode);
4805 if (err)
4806 btrfs_abort_transaction(trans, root, err);
4807 btrfs_end_transaction(trans, root);
4808 }
Yan, Zheng80825102009-11-12 09:35:36 +00004809 }
4810
Josef Bacika41ad392011-01-31 15:30:16 -05004811 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004812}
4813
Chris Mason39279cc2007-06-12 06:35:45 -04004814static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4815{
4816 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004817 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004818 int err;
4819
Li Zefanb83cc962010-12-20 16:04:08 +08004820 if (btrfs_root_readonly(root))
4821 return -EROFS;
4822
Chris Mason39279cc2007-06-12 06:35:45 -04004823 err = inode_change_ok(inode, attr);
4824 if (err)
4825 return err;
4826
Chris Mason5a3f23d2009-03-31 13:27:11 -04004827 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004828 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004829 if (err)
4830 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004831 }
Yan Zheng9036c102008-10-30 14:19:41 -04004832
Christoph Hellwig10257742010-06-04 11:30:02 +02004833 if (attr->ia_valid) {
4834 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004835 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004836 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004837
Josef Bacik22c44fe2011-11-30 10:45:38 -05004838 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004839 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004840 }
4841
Chris Mason39279cc2007-06-12 06:35:45 -04004842 return err;
4843}
Chris Mason61295eb2008-01-14 16:24:38 -05004844
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004845/*
4846 * While truncating the inode pages during eviction, we get the VFS calling
4847 * btrfs_invalidatepage() against each page of the inode. This is slow because
4848 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4849 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4850 * extent_state structures over and over, wasting lots of time.
4851 *
4852 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4853 * those expensive operations on a per page basis and do only the ordered io
4854 * finishing, while we release here the extent_map and extent_state structures,
4855 * without the excessive merging and splitting.
4856 */
4857static void evict_inode_truncate_pages(struct inode *inode)
4858{
4859 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4860 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4861 struct rb_node *node;
4862
4863 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004864 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004865
4866 write_lock(&map_tree->lock);
4867 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4868 struct extent_map *em;
4869
4870 node = rb_first(&map_tree->map);
4871 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004872 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4873 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004874 remove_extent_mapping(map_tree, em);
4875 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004876 if (need_resched()) {
4877 write_unlock(&map_tree->lock);
4878 cond_resched();
4879 write_lock(&map_tree->lock);
4880 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004881 }
4882 write_unlock(&map_tree->lock);
4883
4884 spin_lock(&io_tree->lock);
4885 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4886 struct extent_state *state;
4887 struct extent_state *cached_state = NULL;
4888
4889 node = rb_first(&io_tree->state);
4890 state = rb_entry(node, struct extent_state, rb_node);
4891 atomic_inc(&state->refs);
4892 spin_unlock(&io_tree->lock);
4893
4894 lock_extent_bits(io_tree, state->start, state->end,
4895 0, &cached_state);
4896 clear_extent_bit(io_tree, state->start, state->end,
4897 EXTENT_LOCKED | EXTENT_DIRTY |
4898 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4899 EXTENT_DEFRAG, 1, 1,
4900 &cached_state, GFP_NOFS);
4901 free_extent_state(state);
4902
Filipe Manana7064dd52014-08-08 02:47:05 +01004903 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004904 spin_lock(&io_tree->lock);
4905 }
4906 spin_unlock(&io_tree->lock);
4907}
4908
Al Virobd555972010-06-07 11:35:40 -04004909void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004910{
4911 struct btrfs_trans_handle *trans;
4912 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004913 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004914 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004915 int ret;
4916
liubo1abe9b82011-03-24 11:18:59 +00004917 trace_btrfs_inode_evict(inode);
4918
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004919 evict_inode_truncate_pages(inode);
4920
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004921 if (inode->i_nlink &&
4922 ((btrfs_root_refs(&root->root_item) != 0 &&
4923 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4924 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004925 goto no_delete;
4926
Chris Mason39279cc2007-06-12 06:35:45 -04004927 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004928 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004929 goto no_delete;
4930 }
Al Virobd555972010-06-07 11:35:40 -04004931 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004932 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004933
Miao Xief6124962014-09-12 18:44:04 +08004934 btrfs_free_io_failure_record(inode, 0, (u64)-1);
4935
Yan, Zhengc71bf092009-11-12 09:34:40 +00004936 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004937 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004938 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004939 goto no_delete;
4940 }
4941
Yan, Zheng76dda932009-09-21 16:00:26 -04004942 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004943 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4944 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004945 goto no_delete;
4946 }
4947
Miao Xie0e8c36a2012-12-19 06:59:51 +00004948 ret = btrfs_commit_inode_delayed_inode(inode);
4949 if (ret) {
4950 btrfs_orphan_del(NULL, inode);
4951 goto no_delete;
4952 }
4953
Miao Xie66d8f3d2012-09-06 04:02:28 -06004954 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004955 if (!rsv) {
4956 btrfs_orphan_del(NULL, inode);
4957 goto no_delete;
4958 }
Josef Bacik4a338542011-08-29 11:01:31 -04004959 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004960 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004961 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004962
Chris Masondbe674a2008-07-17 12:54:05 -04004963 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004964
Josef Bacik4289a662011-08-05 13:22:24 -04004965 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004966 * This is a bit simpler than btrfs_truncate since we've already
4967 * reserved our space for our orphan item in the unlink, so we just
4968 * need to reserve some slack space in case we add bytes and update
4969 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004970 */
Yan, Zheng80825102009-11-12 09:35:36 +00004971 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004972 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4973 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004974
Josef Bacik726c35f2011-09-26 15:46:06 -04004975 /*
4976 * Try and steal from the global reserve since we will
4977 * likely not use this space anyway, we want to try as
4978 * hard as possible to get this to work.
4979 */
4980 if (ret)
4981 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4982
Yan, Zhengd68fc572010-05-16 10:49:58 -04004983 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004984 btrfs_warn(root->fs_info,
4985 "Could not get space for a delete, will truncate on mount %d",
4986 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004987 btrfs_orphan_del(NULL, inode);
4988 btrfs_free_block_rsv(root, rsv);
4989 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004990 }
4991
Miao Xie0e8c36a2012-12-19 06:59:51 +00004992 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004993 if (IS_ERR(trans)) {
4994 btrfs_orphan_del(NULL, inode);
4995 btrfs_free_block_rsv(root, rsv);
4996 goto no_delete;
4997 }
4998
4999 trans->block_rsv = rsv;
5000
Yan, Zhengd68fc572010-05-16 10:49:58 -04005001 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005002 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00005003 break;
5004
Miao Xie8407aa42012-09-07 01:43:32 -06005005 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00005006 btrfs_end_transaction(trans, root);
5007 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00005008 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04005009 }
5010
Josef Bacik4289a662011-08-05 13:22:24 -04005011 btrfs_free_block_rsv(root, rsv);
5012
Josef Bacik4ef31a42013-08-13 14:10:08 -04005013 /*
5014 * Errors here aren't a big deal, it just means we leave orphan items
5015 * in the tree. They will be cleaned up on the next mount.
5016 */
Yan, Zheng80825102009-11-12 09:35:36 +00005017 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005018 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005019 btrfs_orphan_del(trans, inode);
5020 } else {
5021 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005022 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005023
Josef Bacik4289a662011-08-05 13:22:24 -04005024 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08005025 if (!(root == root->fs_info->tree_root ||
5026 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005027 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005028
Chris Mason54aa1f42007-06-22 14:16:25 -04005029 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005030 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005031no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005032 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005033 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005034 return;
Chris Mason39279cc2007-06-12 06:35:45 -04005035}
5036
5037/*
5038 * this returns the key found in the dir entry in the location pointer.
5039 * If no dir entries were found, location->objectid is 0.
5040 */
5041static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5042 struct btrfs_key *location)
5043{
5044 const char *name = dentry->d_name.name;
5045 int namelen = dentry->d_name.len;
5046 struct btrfs_dir_item *di;
5047 struct btrfs_path *path;
5048 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005049 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005050
5051 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005052 if (!path)
5053 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005054
Li Zefan33345d012011-04-20 10:31:50 +08005055 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005056 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005057 if (IS_ERR(di))
5058 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005059
David Sterbac7040052011-04-19 18:00:01 +02005060 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005061 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005062
Chris Mason5f39d392007-10-15 16:14:19 -04005063 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005064out:
Chris Mason39279cc2007-06-12 06:35:45 -04005065 btrfs_free_path(path);
5066 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005067out_err:
5068 location->objectid = 0;
5069 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005070}
5071
5072/*
5073 * when we hit a tree root in a directory, the btrfs part of the inode
5074 * needs to be changed to reflect the root directory of the tree root. This
5075 * is kind of like crossing a mount point.
5076 */
5077static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005078 struct inode *dir,
5079 struct dentry *dentry,
5080 struct btrfs_key *location,
5081 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005082{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005083 struct btrfs_path *path;
5084 struct btrfs_root *new_root;
5085 struct btrfs_root_ref *ref;
5086 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005087 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005088 int ret;
5089 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005090
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005091 path = btrfs_alloc_path();
5092 if (!path) {
5093 err = -ENOMEM;
5094 goto out;
5095 }
Chris Mason39279cc2007-06-12 06:35:45 -04005096
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005097 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005098 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5099 key.type = BTRFS_ROOT_REF_KEY;
5100 key.offset = location->objectid;
5101
5102 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5103 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005104 if (ret) {
5105 if (ret < 0)
5106 err = ret;
5107 goto out;
5108 }
Chris Mason39279cc2007-06-12 06:35:45 -04005109
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005110 leaf = path->nodes[0];
5111 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005112 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005113 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5114 goto out;
5115
5116 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5117 (unsigned long)(ref + 1),
5118 dentry->d_name.len);
5119 if (ret)
5120 goto out;
5121
David Sterbab3b4aa72011-04-21 01:20:15 +02005122 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005123
5124 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5125 if (IS_ERR(new_root)) {
5126 err = PTR_ERR(new_root);
5127 goto out;
5128 }
5129
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005130 *sub_root = new_root;
5131 location->objectid = btrfs_root_dirid(&new_root->root_item);
5132 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005133 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005134 err = 0;
5135out:
5136 btrfs_free_path(path);
5137 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005138}
5139
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005140static void inode_tree_add(struct inode *inode)
5141{
5142 struct btrfs_root *root = BTRFS_I(inode)->root;
5143 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005144 struct rb_node **p;
5145 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005146 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005147 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005148
Al Viro1d3382cb2010-10-23 15:19:20 -04005149 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005150 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005151 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005152 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005153 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005154 while (*p) {
5155 parent = *p;
5156 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5157
Li Zefan33345d012011-04-20 10:31:50 +08005158 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005159 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005160 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005161 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005162 else {
5163 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005164 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005165 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005166 RB_CLEAR_NODE(parent);
5167 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005168 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005169 }
5170 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005171 rb_link_node(new, parent, p);
5172 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005173 spin_unlock(&root->inode_lock);
5174}
5175
5176static void inode_tree_del(struct inode *inode)
5177{
5178 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005179 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005180
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005181 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005182 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005183 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005184 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005185 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005186 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005187 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005188
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005189 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005190 synchronize_srcu(&root->fs_info->subvol_srcu);
5191 spin_lock(&root->inode_lock);
5192 empty = RB_EMPTY_ROOT(&root->inode_tree);
5193 spin_unlock(&root->inode_lock);
5194 if (empty)
5195 btrfs_add_dead_root(root);
5196 }
5197}
5198
Jeff Mahoney143bede2012-03-01 14:56:26 +01005199void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005200{
5201 struct rb_node *node;
5202 struct rb_node *prev;
5203 struct btrfs_inode *entry;
5204 struct inode *inode;
5205 u64 objectid = 0;
5206
Liu Bo7813b3d2014-02-10 17:37:25 +08005207 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5208 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005209
5210 spin_lock(&root->inode_lock);
5211again:
5212 node = root->inode_tree.rb_node;
5213 prev = NULL;
5214 while (node) {
5215 prev = node;
5216 entry = rb_entry(node, struct btrfs_inode, rb_node);
5217
Li Zefan33345d012011-04-20 10:31:50 +08005218 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005219 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005220 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005221 node = node->rb_right;
5222 else
5223 break;
5224 }
5225 if (!node) {
5226 while (prev) {
5227 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005228 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005229 node = prev;
5230 break;
5231 }
5232 prev = rb_next(prev);
5233 }
5234 }
5235 while (node) {
5236 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005237 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005238 inode = igrab(&entry->vfs_inode);
5239 if (inode) {
5240 spin_unlock(&root->inode_lock);
5241 if (atomic_read(&inode->i_count) > 1)
5242 d_prune_aliases(inode);
5243 /*
Al Viro45321ac2010-06-07 13:43:19 -04005244 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005245 * the inode cache when its usage count
5246 * hits zero.
5247 */
5248 iput(inode);
5249 cond_resched();
5250 spin_lock(&root->inode_lock);
5251 goto again;
5252 }
5253
5254 if (cond_resched_lock(&root->inode_lock))
5255 goto again;
5256
5257 node = rb_next(node);
5258 }
5259 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005260}
5261
Chris Masone02119d2008-09-05 16:13:11 -04005262static int btrfs_init_locked_inode(struct inode *inode, void *p)
5263{
5264 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005265 inode->i_ino = args->location->objectid;
5266 memcpy(&BTRFS_I(inode)->location, args->location,
5267 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005268 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005269 return 0;
5270}
5271
5272static int btrfs_find_actor(struct inode *inode, void *opaque)
5273{
5274 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005275 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005276 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005277}
5278
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005279static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005280 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005281 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005282{
5283 struct inode *inode;
5284 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005285 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005286
Chris Mason90d3e592014-01-09 17:28:00 -08005287 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005288 args.root = root;
5289
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005290 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005291 btrfs_init_locked_inode,
5292 (void *)&args);
5293 return inode;
5294}
5295
Balaji Rao1a54ef82008-07-21 02:01:04 +05305296/* Get an inode object given its location and corresponding root.
5297 * Returns in *is_new if the inode was read from disk
5298 */
5299struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005300 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305301{
5302 struct inode *inode;
5303
Chris Mason90d3e592014-01-09 17:28:00 -08005304 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305305 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005306 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305307
5308 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305309 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005310 if (!is_bad_inode(inode)) {
5311 inode_tree_add(inode);
5312 unlock_new_inode(inode);
5313 if (new)
5314 *new = 1;
5315 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005316 unlock_new_inode(inode);
5317 iput(inode);
5318 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005319 }
5320 }
5321
Balaji Rao1a54ef82008-07-21 02:01:04 +05305322 return inode;
5323}
5324
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005325static struct inode *new_simple_dir(struct super_block *s,
5326 struct btrfs_key *key,
5327 struct btrfs_root *root)
5328{
5329 struct inode *inode = new_inode(s);
5330
5331 if (!inode)
5332 return ERR_PTR(-ENOMEM);
5333
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005334 BTRFS_I(inode)->root = root;
5335 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005336 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005337
5338 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005339 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005340 inode->i_fop = &simple_dir_operations;
5341 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
chandan r9cc97d62012-07-04 12:48:07 +05305342 inode->i_mtime = CURRENT_TIME;
5343 inode->i_atime = inode->i_mtime;
5344 inode->i_ctime = inode->i_mtime;
5345 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005346
5347 return inode;
5348}
5349
Chris Mason3de45862008-11-17 21:02:50 -05005350struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005351{
Chris Masond3977122009-01-05 21:25:51 -05005352 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005353 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005354 struct btrfs_root *sub_root = root;
5355 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005356 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005357 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005358
5359 if (dentry->d_name.len > BTRFS_NAME_LEN)
5360 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005361
Jeff Layton39e3c952012-11-28 11:30:53 -05005362 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005363 if (ret < 0)
5364 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005365
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005366 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005367 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005368
5369 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005370 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005371 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005372 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005373
5374 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5375
Yan, Zheng76dda932009-09-21 16:00:26 -04005376 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005377 ret = fixup_tree_root_location(root, dir, dentry,
5378 &location, &sub_root);
5379 if (ret < 0) {
5380 if (ret != -ENOENT)
5381 inode = ERR_PTR(ret);
5382 else
5383 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5384 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005385 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005386 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005387 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5388
Julia Lawall34d19ba2011-01-24 19:55:19 +00005389 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005390 down_read(&root->fs_info->cleanup_work_sem);
5391 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005392 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005393 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005394 if (ret) {
5395 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005396 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005397 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005398 }
5399
Chris Mason3de45862008-11-17 21:02:50 -05005400 return inode;
5401}
5402
Nick Pigginfe15ce42011-01-07 17:49:23 +11005403static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005404{
5405 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005406 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005407
Li Zefan848cce02012-02-21 17:04:28 +08005408 if (!inode && !IS_ROOT(dentry))
5409 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005410
Li Zefan848cce02012-02-21 17:04:28 +08005411 if (inode) {
5412 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005413 if (btrfs_root_refs(&root->root_item) == 0)
5414 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005415
5416 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5417 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005418 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005419 return 0;
5420}
5421
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005422static void btrfs_dentry_release(struct dentry *dentry)
5423{
Daeseok Youn944a4512014-04-14 15:37:02 +09005424 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005425}
5426
Chris Mason3de45862008-11-17 21:02:50 -05005427static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005428 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005429{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005430 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005431
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005432 inode = btrfs_lookup_dentry(dir, dentry);
5433 if (IS_ERR(inode)) {
5434 if (PTR_ERR(inode) == -ENOENT)
5435 inode = NULL;
5436 else
5437 return ERR_CAST(inode);
5438 }
5439
Al Viro41d28bc2014-10-12 22:24:21 -04005440 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005441}
5442
Miao Xie16cdcec2011-04-22 18:12:22 +08005443unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005444 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5445};
5446
Al Viro9cdda8d2013-05-22 16:48:09 -04005447static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005448{
Al Viro9cdda8d2013-05-22 16:48:09 -04005449 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005450 struct btrfs_root *root = BTRFS_I(inode)->root;
5451 struct btrfs_item *item;
5452 struct btrfs_dir_item *di;
5453 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005454 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005455 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005456 struct list_head ins_list;
5457 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005458 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005459 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005460 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005461 unsigned char d_type;
5462 int over = 0;
5463 u32 di_cur;
5464 u32 di_total;
5465 u32 di_len;
5466 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005467 char tmp_name[32];
5468 char *name_ptr;
5469 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005470 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005471
5472 /* FIXME, use a real flag for deciding about the key type */
5473 if (root->fs_info->tree_root == root)
5474 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005475
Al Viro9cdda8d2013-05-22 16:48:09 -04005476 if (!dir_emit_dots(file, ctx))
5477 return 0;
5478
David Woodhouse49593bf2008-08-17 17:08:36 +01005479 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005480 if (!path)
5481 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005482
Josef Bacik026fd312011-05-13 10:32:11 -04005483 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005484
Miao Xie16cdcec2011-04-22 18:12:22 +08005485 if (key_type == BTRFS_DIR_INDEX_KEY) {
5486 INIT_LIST_HEAD(&ins_list);
5487 INIT_LIST_HEAD(&del_list);
5488 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5489 }
5490
David Sterba962a2982014-06-04 18:41:45 +02005491 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005492 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005493 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005494
Chris Mason39279cc2007-06-12 06:35:45 -04005495 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5496 if (ret < 0)
5497 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005498
5499 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005500 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005501 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005502 if (slot >= btrfs_header_nritems(leaf)) {
5503 ret = btrfs_next_leaf(root, path);
5504 if (ret < 0)
5505 goto err;
5506 else if (ret > 0)
5507 break;
5508 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005509 }
Chris Mason3de45862008-11-17 21:02:50 -05005510
Ross Kirkdd3cc162013-09-16 15:58:09 +01005511 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005512 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5513
5514 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005515 break;
David Sterba962a2982014-06-04 18:41:45 +02005516 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005517 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005518 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005519 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005520 if (key_type == BTRFS_DIR_INDEX_KEY &&
5521 btrfs_should_delete_dir_index(&del_list,
5522 found_key.offset))
5523 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005524
Al Viro9cdda8d2013-05-22 16:48:09 -04005525 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005526 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005527
Chris Mason39279cc2007-06-12 06:35:45 -04005528 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5529 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005530 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005531
5532 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005533 struct btrfs_key location;
5534
Josef Bacik22a94d42011-03-16 16:47:17 -04005535 if (verify_dir_item(root, leaf, di))
5536 break;
5537
Chris Mason5f39d392007-10-15 16:14:19 -04005538 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005539 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005540 name_ptr = tmp_name;
5541 } else {
5542 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005543 if (!name_ptr) {
5544 ret = -ENOMEM;
5545 goto err;
5546 }
Chris Mason5f39d392007-10-15 16:14:19 -04005547 }
5548 read_extent_buffer(leaf, name_ptr,
5549 (unsigned long)(di + 1), name_len);
5550
5551 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5552 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005553
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005554
Chris Mason3de45862008-11-17 21:02:50 -05005555 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005556 * skip it.
5557 *
5558 * In contrast to old kernels, we insert the snapshot's
5559 * dir item and dir index after it has been created, so
5560 * we won't find a reference to our own snapshot. We
5561 * still keep the following code for backward
5562 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005563 */
5564 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5565 location.objectid == root->root_key.objectid) {
5566 over = 0;
5567 goto skip;
5568 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005569 over = !dir_emit(ctx, name_ptr, name_len,
5570 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005571
Chris Mason3de45862008-11-17 21:02:50 -05005572skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005573 if (name_ptr != tmp_name)
5574 kfree(name_ptr);
5575
Chris Mason39279cc2007-06-12 06:35:45 -04005576 if (over)
5577 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005578 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005579 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005580 di_cur += di_len;
5581 di = (struct btrfs_dir_item *)((char *)di + di_len);
5582 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005583next:
5584 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005585 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005586
Miao Xie16cdcec2011-04-22 18:12:22 +08005587 if (key_type == BTRFS_DIR_INDEX_KEY) {
5588 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005589 ctx->pos++;
5590 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005591 if (ret)
5592 goto nopos;
5593 }
5594
David Woodhouse49593bf2008-08-17 17:08:36 +01005595 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005596 ctx->pos++;
5597
5598 /*
5599 * Stop new entries from being returned after we return the last
5600 * entry.
5601 *
5602 * New directory entries are assigned a strictly increasing
5603 * offset. This means that new entries created during readdir
5604 * are *guaranteed* to be seen in the future by that readdir.
5605 * This has broken buggy programs which operate on names as
5606 * they're returned by readdir. Until we re-use freed offsets
5607 * we have this hack to stop new entries from being returned
5608 * under the assumption that they'll never reach this huge
5609 * offset.
5610 *
5611 * This is being careful not to overflow 32bit loff_t unless the
5612 * last entry requires it because doing so has broken 32bit apps
5613 * in the past.
5614 */
5615 if (key_type == BTRFS_DIR_INDEX_KEY) {
5616 if (ctx->pos >= INT_MAX)
5617 ctx->pos = LLONG_MAX;
5618 else
5619 ctx->pos = INT_MAX;
5620 }
Chris Mason39279cc2007-06-12 06:35:45 -04005621nopos:
5622 ret = 0;
5623err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005624 if (key_type == BTRFS_DIR_INDEX_KEY)
5625 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005626 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005627 return ret;
5628}
5629
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005630int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005631{
5632 struct btrfs_root *root = BTRFS_I(inode)->root;
5633 struct btrfs_trans_handle *trans;
5634 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005635 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005636
Josef Bacik72ac3c02012-05-23 14:13:11 -04005637 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005638 return 0;
5639
Liu Bo83eea1f2012-07-10 05:28:39 -06005640 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005641 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005642
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005643 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005644 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005645 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005646 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005647 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005648 if (IS_ERR(trans))
5649 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005650 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005651 }
5652 return ret;
5653}
5654
5655/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005656 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005657 * inode changes. But, it is most likely to find the inode in cache.
5658 * FIXME, needs more benchmarking...there are no reasons other than performance
5659 * to keep or drop this code.
5660 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005661static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005662{
5663 struct btrfs_root *root = BTRFS_I(inode)->root;
5664 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005665 int ret;
5666
Josef Bacik72ac3c02012-05-23 14:13:11 -04005667 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005668 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005669
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005670 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005671 if (IS_ERR(trans))
5672 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005673
5674 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005675 if (ret && ret == -ENOSPC) {
5676 /* whoops, lets try again with the full transaction */
5677 btrfs_end_transaction(trans, root);
5678 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005679 if (IS_ERR(trans))
5680 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005681
Chris Mason94b60442010-05-26 11:02:00 -04005682 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005683 }
Chris Mason39279cc2007-06-12 06:35:45 -04005684 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005685 if (BTRFS_I(inode)->delayed_node)
5686 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005687
5688 return ret;
5689}
5690
5691/*
5692 * This is a copy of file_update_time. We need this so we can return error on
5693 * ENOSPC for updating the inode in the case of file write and mmap writes.
5694 */
Josef Bacike41f9412012-03-26 09:46:47 -04005695static int btrfs_update_time(struct inode *inode, struct timespec *now,
5696 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005697{
Alexander Block2bc5565282012-06-15 09:49:33 +02005698 struct btrfs_root *root = BTRFS_I(inode)->root;
5699
5700 if (btrfs_root_readonly(root))
5701 return -EROFS;
5702
Josef Bacike41f9412012-03-26 09:46:47 -04005703 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005704 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005705 if (flags & S_CTIME)
5706 inode->i_ctime = *now;
5707 if (flags & S_MTIME)
5708 inode->i_mtime = *now;
5709 if (flags & S_ATIME)
5710 inode->i_atime = *now;
5711 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005712}
5713
Chris Masond352ac62008-09-29 15:18:18 -04005714/*
5715 * find the highest existing sequence number in a directory
5716 * and then set the in-memory index_cnt variable to reflect
5717 * free sequence numbers
5718 */
Josef Bacikaec74772008-07-24 12:12:38 -04005719static int btrfs_set_inode_index_count(struct inode *inode)
5720{
5721 struct btrfs_root *root = BTRFS_I(inode)->root;
5722 struct btrfs_key key, found_key;
5723 struct btrfs_path *path;
5724 struct extent_buffer *leaf;
5725 int ret;
5726
Li Zefan33345d012011-04-20 10:31:50 +08005727 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005728 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005729 key.offset = (u64)-1;
5730
5731 path = btrfs_alloc_path();
5732 if (!path)
5733 return -ENOMEM;
5734
5735 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5736 if (ret < 0)
5737 goto out;
5738 /* FIXME: we should be able to handle this */
5739 if (ret == 0)
5740 goto out;
5741 ret = 0;
5742
5743 /*
5744 * MAGIC NUMBER EXPLANATION:
5745 * since we search a directory based on f_pos we have to start at 2
5746 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5747 * else has to start at 2
5748 */
5749 if (path->slots[0] == 0) {
5750 BTRFS_I(inode)->index_cnt = 2;
5751 goto out;
5752 }
5753
5754 path->slots[0]--;
5755
5756 leaf = path->nodes[0];
5757 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5758
Li Zefan33345d012011-04-20 10:31:50 +08005759 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005760 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04005761 BTRFS_I(inode)->index_cnt = 2;
5762 goto out;
5763 }
5764
5765 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5766out:
5767 btrfs_free_path(path);
5768 return ret;
5769}
5770
Chris Masond352ac62008-09-29 15:18:18 -04005771/*
5772 * helper to find a free sequence number in a given directory. This current
5773 * code is very simple, later versions will do smarter things in the btree
5774 */
Chris Mason3de45862008-11-17 21:02:50 -05005775int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005776{
5777 int ret = 0;
5778
5779 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005780 ret = btrfs_inode_delayed_dir_index_count(dir);
5781 if (ret) {
5782 ret = btrfs_set_inode_index_count(dir);
5783 if (ret)
5784 return ret;
5785 }
Josef Bacikaec74772008-07-24 12:12:38 -04005786 }
5787
Chris Mason00e4e6b2008-08-05 11:18:09 -04005788 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005789 BTRFS_I(dir)->index_cnt++;
5790
5791 return ret;
5792}
5793
Chris Masonb0d5d102014-09-08 13:08:51 -07005794static int btrfs_insert_inode_locked(struct inode *inode)
5795{
5796 struct btrfs_iget_args args;
5797 args.location = &BTRFS_I(inode)->location;
5798 args.root = BTRFS_I(inode)->root;
5799
5800 return insert_inode_locked4(inode,
5801 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5802 btrfs_find_actor, &args);
5803}
5804
Chris Mason39279cc2007-06-12 06:35:45 -04005805static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5806 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005807 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005808 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005809 u64 ref_objectid, u64 objectid,
5810 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005811{
5812 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005813 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005814 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005815 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005816 struct btrfs_inode_ref *ref;
5817 struct btrfs_key key[2];
5818 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005819 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005820 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005821 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005822
Chris Mason5f39d392007-10-15 16:14:19 -04005823 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005824 if (!path)
5825 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005826
Chris Mason39279cc2007-06-12 06:35:45 -04005827 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005828 if (!inode) {
5829 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005830 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005831 }
Chris Mason39279cc2007-06-12 06:35:45 -04005832
Li Zefan581bb052011-04-20 10:06:11 +08005833 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005834 * O_TMPFILE, set link count to 0, so that after this point,
5835 * we fill in an inode item with the correct link count.
5836 */
5837 if (!name)
5838 set_nlink(inode, 0);
5839
5840 /*
Li Zefan581bb052011-04-20 10:06:11 +08005841 * we have to initialize this early, so we can reclaim the inode
5842 * number if we fail afterwards in this function.
5843 */
5844 inode->i_ino = objectid;
5845
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005846 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005847 trace_btrfs_inode_request(dir);
5848
Chris Mason3de45862008-11-17 21:02:50 -05005849 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005850 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005851 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005852 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005853 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005854 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005855 } else if (dir) {
5856 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005857 }
5858 /*
5859 * index_cnt is ignored for everything but a dir,
5860 * btrfs_get_inode_index_count has an explanation for the magic
5861 * number
5862 */
5863 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005864 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005865 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005866 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005867 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005868
Josef Bacik5dc562c2012-08-17 13:14:17 -04005869 /*
5870 * We could have gotten an inode number from somebody who was fsynced
5871 * and then removed in this same transaction, so let's just set full
5872 * sync since it will be a full sync anyway and this will blow away the
5873 * old info in the log.
5874 */
5875 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5876
Chris Mason9c583092008-01-29 15:15:18 -05005877 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005878 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005879 key[0].offset = 0;
5880
Chris Mason9c583092008-01-29 15:15:18 -05005881 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005882
5883 if (name) {
5884 /*
5885 * Start new inodes with an inode_ref. This is slightly more
5886 * efficient for small numbers of hard links since they will
5887 * be packed into one item. Extended refs will kick in if we
5888 * add more hard links than can fit in the ref item.
5889 */
5890 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005891 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005892 key[1].offset = ref_objectid;
5893
5894 sizes[1] = name_len + sizeof(*ref);
5895 }
Chris Mason9c583092008-01-29 15:15:18 -05005896
Chris Masonb0d5d102014-09-08 13:08:51 -07005897 location = &BTRFS_I(inode)->location;
5898 location->objectid = objectid;
5899 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02005900 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07005901
5902 ret = btrfs_insert_inode_locked(inode);
5903 if (ret < 0)
5904 goto fail;
5905
Chris Masonb9473432009-03-13 11:00:37 -04005906 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005907 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005908 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07005909 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04005910
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005911 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005912 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05305913
5914 inode->i_mtime = CURRENT_TIME;
5915 inode->i_atime = inode->i_mtime;
5916 inode->i_ctime = inode->i_mtime;
5917 BTRFS_I(inode)->i_otime = inode->i_mtime;
5918
Chris Mason5f39d392007-10-15 16:14:19 -04005919 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5920 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005921 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5922 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005923 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005924
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005925 if (name) {
5926 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5927 struct btrfs_inode_ref);
5928 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5929 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5930 ptr = (unsigned long)(ref + 1);
5931 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5932 }
Chris Mason9c583092008-01-29 15:15:18 -05005933
Chris Mason5f39d392007-10-15 16:14:19 -04005934 btrfs_mark_buffer_dirty(path->nodes[0]);
5935 btrfs_free_path(path);
5936
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005937 btrfs_inherit_iflags(inode, dir);
5938
Al Viro569254b2011-07-24 17:08:40 -04005939 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005940 if (btrfs_test_opt(root, NODATASUM))
5941 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005942 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005943 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5944 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005945 }
5946
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005947 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005948
5949 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005950 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005951
Alexander Block8ea05e32012-07-25 17:35:53 +02005952 btrfs_update_root_times(trans, root);
5953
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005954 ret = btrfs_inode_inherit_props(trans, inode, dir);
5955 if (ret)
5956 btrfs_err(root->fs_info,
5957 "error inheriting props for ino %llu (root %llu): %d",
5958 btrfs_ino(inode), root->root_key.objectid, ret);
5959
Chris Mason39279cc2007-06-12 06:35:45 -04005960 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07005961
5962fail_unlock:
5963 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005964fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005965 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04005966 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005967 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005968 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005969 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005970}
5971
5972static inline u8 btrfs_inode_type(struct inode *inode)
5973{
5974 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5975}
5976
Chris Masond352ac62008-09-29 15:18:18 -04005977/*
5978 * utility function to add 'inode' into 'parent_inode' with
5979 * a give name and a given sequence number.
5980 * if 'add_backref' is true, also insert a backref from the
5981 * inode to the parent directory.
5982 */
Chris Masone02119d2008-09-05 16:13:11 -04005983int btrfs_add_link(struct btrfs_trans_handle *trans,
5984 struct inode *parent_inode, struct inode *inode,
5985 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005986{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005987 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005988 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005989 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005990 u64 ino = btrfs_ino(inode);
5991 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005992
Li Zefan33345d012011-04-20 10:31:50 +08005993 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005994 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5995 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005996 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02005997 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005998 key.offset = 0;
5999 }
Chris Mason39279cc2007-06-12 06:35:45 -04006000
Li Zefan33345d012011-04-20 10:31:50 +08006001 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006002 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
6003 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08006004 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006005 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006006 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6007 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006008 }
6009
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006010 /* Nothing to clean up yet */
6011 if (ret)
6012 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006013
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006014 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6015 parent_inode, &key,
6016 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006017 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006018 goto fail_dir_item;
6019 else if (ret) {
6020 btrfs_abort_transaction(trans, root, ret);
6021 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006022 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006023
6024 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6025 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006026 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006027 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
6028 ret = btrfs_update_inode(trans, root, parent_inode);
6029 if (ret)
6030 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006031 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006032
6033fail_dir_item:
6034 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6035 u64 local_index;
6036 int err;
6037 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6038 key.objectid, root->root_key.objectid,
6039 parent_ino, &local_index, name, name_len);
6040
6041 } else if (add_backref) {
6042 u64 local_index;
6043 int err;
6044
6045 err = btrfs_del_inode_ref(trans, root, name, name_len,
6046 ino, parent_ino, &local_index);
6047 }
6048 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006049}
6050
6051static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006052 struct inode *dir, struct dentry *dentry,
6053 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006054{
Josef Bacika1b075d2010-11-19 20:36:11 +00006055 int err = btrfs_add_link(trans, dir, inode,
6056 dentry->d_name.name, dentry->d_name.len,
6057 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006058 if (err > 0)
6059 err = -EEXIST;
6060 return err;
6061}
6062
Josef Bacik618e21d2007-07-11 10:18:17 -04006063static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006064 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006065{
6066 struct btrfs_trans_handle *trans;
6067 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006068 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006069 int err;
6070 int drop_inode = 0;
6071 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006072 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006073
6074 if (!new_valid_dev(rdev))
6075 return -EINVAL;
6076
Josef Bacik9ed74f22009-09-11 16:12:44 -04006077 /*
6078 * 2 for inode item and ref
6079 * 2 for dir items
6080 * 1 for xattr if selinux is on
6081 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006082 trans = btrfs_start_transaction(root, 5);
6083 if (IS_ERR(trans))
6084 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006085
Li Zefan581bb052011-04-20 10:06:11 +08006086 err = btrfs_find_free_ino(root, &objectid);
6087 if (err)
6088 goto out_unlock;
6089
Josef Bacikaec74772008-07-24 12:12:38 -04006090 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006091 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006092 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006093 if (IS_ERR(inode)) {
6094 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006095 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006096 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006097
Casey Schauflerad19db72011-12-15 10:09:07 -05006098 /*
6099 * If the active LSM wants to access the inode during
6100 * d_instantiate it needs these. Smack checks to see
6101 * if the filesystem supports xattrs by looking at the
6102 * ops vector.
6103 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006104 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006105 init_special_inode(inode, inode->i_mode, rdev);
6106
6107 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006108 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006109 goto out_unlock_inode;
6110
6111 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6112 if (err) {
6113 goto out_unlock_inode;
6114 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006115 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006116 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006117 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006118 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006119
Josef Bacik618e21d2007-07-11 10:18:17 -04006120out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006121 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006122 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006123 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006124 if (drop_inode) {
6125 inode_dec_link_count(inode);
6126 iput(inode);
6127 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006128 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006129
6130out_unlock_inode:
6131 drop_inode = 1;
6132 unlock_new_inode(inode);
6133 goto out_unlock;
6134
Josef Bacik618e21d2007-07-11 10:18:17 -04006135}
6136
Chris Mason39279cc2007-06-12 06:35:45 -04006137static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006138 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006139{
6140 struct btrfs_trans_handle *trans;
6141 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006142 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006143 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006144 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006145 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006146 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006147
Josef Bacik9ed74f22009-09-11 16:12:44 -04006148 /*
6149 * 2 for inode item and ref
6150 * 2 for dir items
6151 * 1 for xattr if selinux is on
6152 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006153 trans = btrfs_start_transaction(root, 5);
6154 if (IS_ERR(trans))
6155 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006156
Li Zefan581bb052011-04-20 10:06:11 +08006157 err = btrfs_find_free_ino(root, &objectid);
6158 if (err)
6159 goto out_unlock;
6160
Josef Bacikaec74772008-07-24 12:12:38 -04006161 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006162 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006163 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006164 if (IS_ERR(inode)) {
6165 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006166 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006167 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006168 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006169 /*
6170 * If the active LSM wants to access the inode during
6171 * d_instantiate it needs these. Smack checks to see
6172 * if the filesystem supports xattrs by looking at the
6173 * ops vector.
6174 */
6175 inode->i_fop = &btrfs_file_operations;
6176 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006177 inode->i_mapping->a_ops = &btrfs_aops;
6178 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
6179
6180 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6181 if (err)
6182 goto out_unlock_inode;
6183
6184 err = btrfs_update_inode(trans, root, inode);
6185 if (err)
6186 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006187
Josef Bacika1b075d2010-11-19 20:36:11 +00006188 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006189 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006190 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006191
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006192 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006193 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006194 d_instantiate(dentry, inode);
6195
Chris Mason39279cc2007-06-12 06:35:45 -04006196out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006197 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006198 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006199 inode_dec_link_count(inode);
6200 iput(inode);
6201 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006202 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006203 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006204 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006205
6206out_unlock_inode:
6207 unlock_new_inode(inode);
6208 goto out_unlock;
6209
Chris Mason39279cc2007-06-12 06:35:45 -04006210}
6211
6212static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6213 struct dentry *dentry)
6214{
6215 struct btrfs_trans_handle *trans;
6216 struct btrfs_root *root = BTRFS_I(dir)->root;
6217 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006218 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006219 int err;
6220 int drop_inode = 0;
6221
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006222 /* do not allow sys_link's with other subvols of the same device */
6223 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006224 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006225
Mark Fashehf1863732012-08-08 11:32:27 -07006226 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006227 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006228
Chris Mason3de45862008-11-17 21:02:50 -05006229 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006230 if (err)
6231 goto fail;
6232
Yan, Zhenga22285a2010-05-16 10:48:46 -04006233 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006234 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006235 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006236 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006237 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006238 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006239 if (IS_ERR(trans)) {
6240 err = PTR_ERR(trans);
6241 goto fail;
6242 }
Chris Mason5f39d392007-10-15 16:14:19 -04006243
Miao Xie67de1172013-12-26 13:07:06 +08006244 /* There are several dir indexes for this inode, clear the cache. */
6245 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006246 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006247 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006248 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006249 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006250 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006251
Josef Bacika1b075d2010-11-19 20:36:11 +00006252 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006253
Yan, Zhenga5719522009-09-24 09:17:31 -04006254 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006255 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006256 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006257 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006258 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006259 if (err)
6260 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006261 if (inode->i_nlink == 1) {
6262 /*
6263 * If new hard link count is 1, it's a file created
6264 * with open(2) O_TMPFILE flag.
6265 */
6266 err = btrfs_orphan_del(trans, inode);
6267 if (err)
6268 goto fail;
6269 }
Al Viro08c422c2011-12-23 07:58:13 -05006270 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006271 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006272 }
Chris Mason39279cc2007-06-12 06:35:45 -04006273
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006274 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006275 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006276fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006277 if (drop_inode) {
6278 inode_dec_link_count(inode);
6279 iput(inode);
6280 }
Liu Bob53d3f52012-11-14 14:34:34 +00006281 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006282 return err;
6283}
6284
Al Viro18bb1db2011-07-26 01:41:39 -04006285static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006286{
Chris Masonb9d86662008-05-02 16:13:49 -04006287 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006288 struct btrfs_trans_handle *trans;
6289 struct btrfs_root *root = BTRFS_I(dir)->root;
6290 int err = 0;
6291 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006292 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006293 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006294
Josef Bacik9ed74f22009-09-11 16:12:44 -04006295 /*
6296 * 2 items for inode and ref
6297 * 2 items for dir items
6298 * 1 for xattr if selinux is on
6299 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006300 trans = btrfs_start_transaction(root, 5);
6301 if (IS_ERR(trans))
6302 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006303
Li Zefan581bb052011-04-20 10:06:11 +08006304 err = btrfs_find_free_ino(root, &objectid);
6305 if (err)
6306 goto out_fail;
6307
Josef Bacikaec74772008-07-24 12:12:38 -04006308 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006309 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006310 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006311 if (IS_ERR(inode)) {
6312 err = PTR_ERR(inode);
6313 goto out_fail;
6314 }
Chris Mason5f39d392007-10-15 16:14:19 -04006315
Chris Mason39279cc2007-06-12 06:35:45 -04006316 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006317 /* these must be set before we unlock the inode */
6318 inode->i_op = &btrfs_dir_inode_operations;
6319 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006320
Eric Paris2a7dba32011-02-01 11:05:39 -05006321 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006322 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006323 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006324
Chris Masondbe674a2008-07-17 12:54:05 -04006325 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006326 err = btrfs_update_inode(trans, root, inode);
6327 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006328 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006329
Josef Bacika1b075d2010-11-19 20:36:11 +00006330 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6331 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006332 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006333 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006334
Chris Mason39279cc2007-06-12 06:35:45 -04006335 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006336 /*
6337 * mkdir is special. We're unlocking after we call d_instantiate
6338 * to avoid a race with nfsd calling d_instantiate.
6339 */
6340 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006341 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006342
6343out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006344 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006345 if (drop_on_err) {
6346 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006347 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006348 }
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_fail_inode:
6354 unlock_new_inode(inode);
6355 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006356}
6357
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006358/* Find next extent map of a given extent map, caller needs to ensure locks */
6359static struct extent_map *next_extent_map(struct extent_map *em)
6360{
6361 struct rb_node *next;
6362
6363 next = rb_next(&em->rb_node);
6364 if (!next)
6365 return NULL;
6366 return container_of(next, struct extent_map, rb_node);
6367}
6368
6369static struct extent_map *prev_extent_map(struct extent_map *em)
6370{
6371 struct rb_node *prev;
6372
6373 prev = rb_prev(&em->rb_node);
6374 if (!prev)
6375 return NULL;
6376 return container_of(prev, struct extent_map, rb_node);
6377}
6378
Chris Masond352ac62008-09-29 15:18:18 -04006379/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006380 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006381 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006382 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006383 */
Chris Mason3b951512008-04-17 11:29:12 -04006384static int merge_extent_mapping(struct extent_map_tree *em_tree,
6385 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006386 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006387 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006388{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006389 struct extent_map *prev;
6390 struct extent_map *next;
6391 u64 start;
6392 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006393 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006394
Chris Masone6dcd2d2008-07-17 12:53:50 -04006395 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006396
6397 if (existing->start > map_start) {
6398 next = existing;
6399 prev = prev_extent_map(next);
6400 } else {
6401 prev = existing;
6402 next = next_extent_map(prev);
6403 }
6404
6405 start = prev ? extent_map_end(prev) : em->start;
6406 start = max_t(u64, start, em->start);
6407 end = next ? next->start : extent_map_end(em);
6408 end = min_t(u64, end, extent_map_end(em));
6409 start_diff = start - em->start;
6410 em->start = start;
6411 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006412 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6413 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006414 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006415 em->block_len -= start_diff;
6416 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006417 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006418}
6419
Chris Masonc8b97812008-10-29 14:49:59 -04006420static noinline int uncompress_inline(struct btrfs_path *path,
6421 struct inode *inode, struct page *page,
6422 size_t pg_offset, u64 extent_offset,
6423 struct btrfs_file_extent_item *item)
6424{
6425 int ret;
6426 struct extent_buffer *leaf = path->nodes[0];
6427 char *tmp;
6428 size_t max_size;
6429 unsigned long inline_size;
6430 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006431 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006432
6433 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006434 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006435 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6436 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006437 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006438 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006439 if (!tmp)
6440 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006441 ptr = btrfs_file_extent_inline_start(item);
6442
6443 read_extent_buffer(leaf, tmp, ptr, inline_size);
6444
Chris Mason5b050f02008-11-11 09:34:41 -05006445 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006446 ret = btrfs_decompress(compress_type, tmp, page,
6447 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006448 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006449 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006450}
6451
Chris Masond352ac62008-09-29 15:18:18 -04006452/*
6453 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006454 * the ugly parts come from merging extents from the disk with the in-ram
6455 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006456 * where the in-ram extents might be locked pending data=ordered completion.
6457 *
6458 * This also copies inline extents directly into the page.
6459 */
Chris Masond3977122009-01-05 21:25:51 -05006460
Chris Masona52d9a82007-08-27 16:49:44 -04006461struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006462 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006463 int create)
6464{
6465 int ret;
6466 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006467 u64 extent_start = 0;
6468 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006469 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006470 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006471 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006472 struct btrfs_root *root = BTRFS_I(inode)->root;
6473 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006474 struct extent_buffer *leaf;
6475 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006476 struct extent_map *em = NULL;
6477 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006478 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006479 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006480 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006481
Chris Masona52d9a82007-08-27 16:49:44 -04006482again:
Chris Mason890871b2009-09-02 16:24:52 -04006483 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006484 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006485 if (em)
6486 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006487 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006488
Chris Masona52d9a82007-08-27 16:49:44 -04006489 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006490 if (em->start > start || em->start + em->len <= start)
6491 free_extent_map(em);
6492 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006493 free_extent_map(em);
6494 else
6495 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006496 }
David Sterba172ddd62011-04-21 00:48:27 +02006497 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006498 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006499 err = -ENOMEM;
6500 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006501 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006502 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006503 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006504 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006505 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006506 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006507
6508 if (!path) {
6509 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006510 if (!path) {
6511 err = -ENOMEM;
6512 goto out;
6513 }
6514 /*
6515 * Chances are we'll be called again, so go ahead and do
6516 * readahead
6517 */
6518 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006519 }
6520
Chris Mason179e29e2007-11-01 11:28:41 -04006521 ret = btrfs_lookup_file_extent(trans, root, path,
6522 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006523 if (ret < 0) {
6524 err = ret;
6525 goto out;
6526 }
6527
6528 if (ret != 0) {
6529 if (path->slots[0] == 0)
6530 goto not_found;
6531 path->slots[0]--;
6532 }
6533
Chris Mason5f39d392007-10-15 16:14:19 -04006534 leaf = path->nodes[0];
6535 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006536 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006537 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006538 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006539 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006540 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006541 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006542 /*
6543 * If we backup past the first extent we want to move forward
6544 * and see if there is an extent in front of us, otherwise we'll
6545 * say there is a hole for our whole search range which can
6546 * cause problems.
6547 */
6548 extent_end = start;
6549 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006550 }
6551
Chris Mason5f39d392007-10-15 16:14:19 -04006552 found_type = btrfs_file_extent_type(leaf, item);
6553 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006554 if (found_type == BTRFS_FILE_EXTENT_REG ||
6555 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006556 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006557 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006558 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6559 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006560 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006561 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006562 }
Josef Bacik25a50342013-10-14 12:08:38 -04006563next:
Yan Zheng9036c102008-10-30 14:19:41 -04006564 if (start >= extent_end) {
6565 path->slots[0]++;
6566 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6567 ret = btrfs_next_leaf(root, path);
6568 if (ret < 0) {
6569 err = ret;
6570 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006571 }
Yan Zheng9036c102008-10-30 14:19:41 -04006572 if (ret > 0)
6573 goto not_found;
6574 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006575 }
Yan Zheng9036c102008-10-30 14:19:41 -04006576 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6577 if (found_key.objectid != objectid ||
6578 found_key.type != BTRFS_EXTENT_DATA_KEY)
6579 goto not_found;
6580 if (start + len <= found_key.offset)
6581 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006582 if (start > found_key.offset)
6583 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006584 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006585 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006586 em->len = found_key.offset - start;
6587 goto not_found_em;
6588 }
6589
Filipe Manana7ffbb592014-06-09 03:48:05 +01006590 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6591
Yan Zhengd899e052008-10-30 14:25:28 -04006592 if (found_type == BTRFS_FILE_EXTENT_REG ||
6593 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006594 goto insert;
6595 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006596 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006597 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006598 size_t size;
6599 size_t extent_offset;
6600 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006601
Filipe Manana7ffbb592014-06-09 03:48:05 +01006602 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006603 goto out;
Yan689f9342007-10-29 11:41:07 -04006604
Chris Mason514ac8a2014-01-03 21:07:00 -08006605 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006606 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006607 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006608 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006609 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006610 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006611 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006612 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006613 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006614 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006615 if (btrfs_file_extent_compression(leaf, item) !=
6616 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006617 ret = uncompress_inline(path, inode, page,
6618 pg_offset,
6619 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006620 if (ret) {
6621 err = ret;
6622 goto out;
6623 }
Chris Masonc8b97812008-10-29 14:49:59 -04006624 } else {
6625 map = kmap(page);
6626 read_extent_buffer(leaf, map + pg_offset, ptr,
6627 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006628 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6629 memset(map + pg_offset + copy_size, 0,
6630 PAGE_CACHE_SIZE - pg_offset -
6631 copy_size);
6632 }
Chris Masonc8b97812008-10-29 14:49:59 -04006633 kunmap(page);
6634 }
Chris Mason179e29e2007-11-01 11:28:41 -04006635 flush_dcache_page(page);
6636 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006637 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006638 if (!trans) {
6639 kunmap(page);
6640 free_extent_map(em);
6641 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006642
David Sterbab3b4aa72011-04-21 01:20:15 +02006643 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006644 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006645
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006646 if (IS_ERR(trans))
6647 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006648 goto again;
6649 }
Chris Masonc8b97812008-10-29 14:49:59 -04006650 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006651 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006652 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006653 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006654 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006655 }
Chris Masond1310b22008-01-24 16:13:08 -05006656 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006657 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006658 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006659 }
6660not_found:
6661 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006662 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006663 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006664not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006665 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006666 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006667insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006668 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006669 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006670 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006671 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006672 err = -EIO;
6673 goto out;
6674 }
Chris Masond1310b22008-01-24 16:13:08 -05006675
6676 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006677 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006678 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006679 /* it is possible that someone inserted the extent into the tree
6680 * while we had the lock dropped. It is also possible that
6681 * an overlapping map exists in the tree
6682 */
Chris Masona52d9a82007-08-27 16:49:44 -04006683 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006684 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006685
6686 ret = 0;
6687
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006688 existing = search_extent_mapping(em_tree, start, len);
6689 /*
6690 * existing will always be non-NULL, since there must be
6691 * extent causing the -EEXIST.
6692 */
6693 if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08006694 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006695 /*
6696 * The existing extent map is the one nearest to
6697 * the [start, start + len) range which overlaps
6698 */
6699 err = merge_extent_mapping(em_tree, existing,
6700 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04006701 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006702 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04006703 free_extent_map(em);
6704 em = NULL;
6705 }
6706 } else {
6707 free_extent_map(em);
6708 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006709 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006710 }
Chris Masona52d9a82007-08-27 16:49:44 -04006711 }
Chris Mason890871b2009-09-02 16:24:52 -04006712 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006713out:
liubo1abe9b82011-03-24 11:18:59 +00006714
Steven Rostedt4cd8587c2013-11-14 22:57:29 -05006715 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006716
Chris Masonf4219502008-07-22 11:18:09 -04006717 if (path)
6718 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006719 if (trans) {
6720 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006721 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006722 err = ret;
6723 }
Chris Masona52d9a82007-08-27 16:49:44 -04006724 if (err) {
6725 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006726 return ERR_PTR(err);
6727 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006728 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006729 return em;
6730}
6731
Chris Masonec29ed52011-02-23 16:23:20 -05006732struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6733 size_t pg_offset, u64 start, u64 len,
6734 int create)
6735{
6736 struct extent_map *em;
6737 struct extent_map *hole_em = NULL;
6738 u64 range_start = start;
6739 u64 end;
6740 u64 found;
6741 u64 found_end;
6742 int err = 0;
6743
6744 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6745 if (IS_ERR(em))
6746 return em;
6747 if (em) {
6748 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006749 * if our em maps to
6750 * - a hole or
6751 * - a pre-alloc extent,
6752 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006753 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006754 if (em->block_start != EXTENT_MAP_HOLE &&
6755 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006756 return em;
6757 else
6758 hole_em = em;
6759 }
6760
6761 /* check to see if we've wrapped (len == -1 or similar) */
6762 end = start + len;
6763 if (end < start)
6764 end = (u64)-1;
6765 else
6766 end -= 1;
6767
6768 em = NULL;
6769
6770 /* ok, we didn't find anything, lets look for delalloc */
6771 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6772 end, len, EXTENT_DELALLOC, 1);
6773 found_end = range_start + found;
6774 if (found_end < range_start)
6775 found_end = (u64)-1;
6776
6777 /*
6778 * we didn't find anything useful, return
6779 * the original results from get_extent()
6780 */
6781 if (range_start > end || found_end <= start) {
6782 em = hole_em;
6783 hole_em = NULL;
6784 goto out;
6785 }
6786
6787 /* adjust the range_start to make sure it doesn't
6788 * go backwards from the start they passed in
6789 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306790 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006791 found = found_end - range_start;
6792
6793 if (found > 0) {
6794 u64 hole_start = start;
6795 u64 hole_len = len;
6796
David Sterba172ddd62011-04-21 00:48:27 +02006797 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006798 if (!em) {
6799 err = -ENOMEM;
6800 goto out;
6801 }
6802 /*
6803 * when btrfs_get_extent can't find anything it
6804 * returns one huge hole
6805 *
6806 * make sure what it found really fits our range, and
6807 * adjust to make sure it is based on the start from
6808 * the caller
6809 */
6810 if (hole_em) {
6811 u64 calc_end = extent_map_end(hole_em);
6812
6813 if (calc_end <= start || (hole_em->start > end)) {
6814 free_extent_map(hole_em);
6815 hole_em = NULL;
6816 } else {
6817 hole_start = max(hole_em->start, start);
6818 hole_len = calc_end - hole_start;
6819 }
6820 }
6821 em->bdev = NULL;
6822 if (hole_em && range_start > hole_start) {
6823 /* our hole starts before our delalloc, so we
6824 * have to return just the parts of the hole
6825 * that go until the delalloc starts
6826 */
6827 em->len = min(hole_len,
6828 range_start - hole_start);
6829 em->start = hole_start;
6830 em->orig_start = hole_start;
6831 /*
6832 * don't adjust block start at all,
6833 * it is fixed at EXTENT_MAP_HOLE
6834 */
6835 em->block_start = hole_em->block_start;
6836 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006837 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6838 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006839 } else {
6840 em->start = range_start;
6841 em->len = found;
6842 em->orig_start = range_start;
6843 em->block_start = EXTENT_MAP_DELALLOC;
6844 em->block_len = found;
6845 }
6846 } else if (hole_em) {
6847 return hole_em;
6848 }
6849out:
6850
6851 free_extent_map(hole_em);
6852 if (err) {
6853 free_extent_map(em);
6854 return ERR_PTR(err);
6855 }
6856 return em;
6857}
6858
Josef Bacik4b46fce2010-05-23 11:00:55 -04006859static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6860 u64 start, u64 len)
6861{
6862 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006863 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006864 struct btrfs_key ins;
6865 u64 alloc_hint;
6866 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006867
Josef Bacik4b46fce2010-05-23 11:00:55 -04006868 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006869 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08006870 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006871 if (ret)
6872 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006873
Josef Bacik70c8a912012-10-11 16:54:30 -04006874 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006875 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006876 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08006877 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006878 return em;
6879 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006880
6881 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6882 ins.offset, ins.offset, 0);
6883 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08006884 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006885 free_extent_map(em);
6886 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006887 }
Josef Bacik00361582013-08-14 14:02:47 -04006888
Josef Bacik4b46fce2010-05-23 11:00:55 -04006889 return em;
6890}
6891
Chris Mason46bfbb52010-05-26 11:04:10 -04006892/*
6893 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6894 * block must be cow'd
6895 */
Josef Bacik00361582013-08-14 14:02:47 -04006896noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006897 u64 *orig_start, u64 *orig_block_len,
6898 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006899{
Josef Bacik00361582013-08-14 14:02:47 -04006900 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006901 struct btrfs_path *path;
6902 int ret;
6903 struct extent_buffer *leaf;
6904 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006905 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006906 struct btrfs_file_extent_item *fi;
6907 struct btrfs_key key;
6908 u64 disk_bytenr;
6909 u64 backref_offset;
6910 u64 extent_end;
6911 u64 num_bytes;
6912 int slot;
6913 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006914 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006915
Chris Mason46bfbb52010-05-26 11:04:10 -04006916 path = btrfs_alloc_path();
6917 if (!path)
6918 return -ENOMEM;
6919
Josef Bacik00361582013-08-14 14:02:47 -04006920 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006921 offset, 0);
6922 if (ret < 0)
6923 goto out;
6924
6925 slot = path->slots[0];
6926 if (ret == 1) {
6927 if (slot == 0) {
6928 /* can't find the item, must cow */
6929 ret = 0;
6930 goto out;
6931 }
6932 slot--;
6933 }
6934 ret = 0;
6935 leaf = path->nodes[0];
6936 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006937 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006938 key.type != BTRFS_EXTENT_DATA_KEY) {
6939 /* not our file or wrong item type, must cow */
6940 goto out;
6941 }
6942
6943 if (key.offset > offset) {
6944 /* Wrong offset, must cow */
6945 goto out;
6946 }
6947
6948 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6949 found_type = btrfs_file_extent_type(leaf, fi);
6950 if (found_type != BTRFS_FILE_EXTENT_REG &&
6951 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6952 /* not a regular extent, must cow */
6953 goto out;
6954 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006955
6956 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6957 goto out;
6958
Miao Xiee77751a2013-12-27 21:11:50 +08006959 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6960 if (extent_end <= offset)
6961 goto out;
6962
Chris Mason46bfbb52010-05-26 11:04:10 -04006963 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006964 if (disk_bytenr == 0)
6965 goto out;
6966
6967 if (btrfs_file_extent_compression(leaf, fi) ||
6968 btrfs_file_extent_encryption(leaf, fi) ||
6969 btrfs_file_extent_other_encoding(leaf, fi))
6970 goto out;
6971
Chris Mason46bfbb52010-05-26 11:04:10 -04006972 backref_offset = btrfs_file_extent_offset(leaf, fi);
6973
Josef Bacik7ee9e442013-06-21 16:37:03 -04006974 if (orig_start) {
6975 *orig_start = key.offset - backref_offset;
6976 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6977 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6978 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006979
Chris Mason46bfbb52010-05-26 11:04:10 -04006980 if (btrfs_extent_readonly(root, disk_bytenr))
6981 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006982
6983 num_bytes = min(offset + *len, extent_end) - offset;
6984 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6985 u64 range_end;
6986
6987 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
6988 ret = test_range_bit(io_tree, offset, range_end,
6989 EXTENT_DELALLOC, 0, NULL);
6990 if (ret) {
6991 ret = -EAGAIN;
6992 goto out;
6993 }
6994 }
6995
Josef Bacik1bda19e2013-10-18 12:10:36 -04006996 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006997
6998 /*
6999 * look for other files referencing this extent, if we
7000 * find any we must cow
7001 */
Josef Bacik00361582013-08-14 14:02:47 -04007002 trans = btrfs_join_transaction(root);
7003 if (IS_ERR(trans)) {
7004 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007005 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007006 }
7007
7008 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7009 key.offset - backref_offset, disk_bytenr);
7010 btrfs_end_transaction(trans, root);
7011 if (ret) {
7012 ret = 0;
7013 goto out;
7014 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007015
7016 /*
7017 * adjust disk_bytenr and num_bytes to cover just the bytes
7018 * in this extent we are about to write. If there
7019 * are any csums in that range we have to cow in order
7020 * to keep the csums correct
7021 */
7022 disk_bytenr += backref_offset;
7023 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04007024 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7025 goto out;
7026 /*
7027 * all of the above have passed, it is safe to overwrite this extent
7028 * without cow
7029 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007030 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007031 ret = 1;
7032out:
7033 btrfs_free_path(path);
7034 return ret;
7035}
7036
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007037bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7038{
7039 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7040 int found = false;
7041 void **pagep = NULL;
7042 struct page *page = NULL;
7043 int start_idx;
7044 int end_idx;
7045
7046 start_idx = start >> PAGE_CACHE_SHIFT;
7047
7048 /*
7049 * end is the last byte in the last page. end == start is legal
7050 */
7051 end_idx = end >> PAGE_CACHE_SHIFT;
7052
7053 rcu_read_lock();
7054
7055 /* Most of the code in this while loop is lifted from
7056 * find_get_page. It's been modified to begin searching from a
7057 * page and return just the first page found in that range. If the
7058 * found idx is less than or equal to the end idx then we know that
7059 * a page exists. If no pages are found or if those pages are
7060 * outside of the range then we're fine (yay!) */
7061 while (page == NULL &&
7062 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7063 page = radix_tree_deref_slot(pagep);
7064 if (unlikely(!page))
7065 break;
7066
7067 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007068 if (radix_tree_deref_retry(page)) {
7069 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007070 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007071 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007072 /*
7073 * Otherwise, shmem/tmpfs must be storing a swap entry
7074 * here as an exceptional entry: so return it without
7075 * attempting to raise page count.
7076 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007077 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007078 break; /* TODO: Is this relevant for this use case? */
7079 }
7080
Filipe Manana91405152014-06-05 13:22:24 +01007081 if (!page_cache_get_speculative(page)) {
7082 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007083 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007084 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007085
7086 /*
7087 * Has the page moved?
7088 * This is part of the lockless pagecache protocol. See
7089 * include/linux/pagemap.h for details.
7090 */
7091 if (unlikely(page != *pagep)) {
7092 page_cache_release(page);
7093 page = NULL;
7094 }
7095 }
7096
7097 if (page) {
7098 if (page->index <= end_idx)
7099 found = true;
7100 page_cache_release(page);
7101 }
7102
7103 rcu_read_unlock();
7104 return found;
7105}
7106
Josef Bacikeb838e72012-07-31 16:28:48 -04007107static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7108 struct extent_state **cached_state, int writing)
7109{
7110 struct btrfs_ordered_extent *ordered;
7111 int ret = 0;
7112
7113 while (1) {
7114 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7115 0, cached_state);
7116 /*
7117 * We're concerned with the entire range that we're going to be
7118 * doing DIO to, so we need to make sure theres no ordered
7119 * extents in this range.
7120 */
7121 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7122 lockend - lockstart + 1);
7123
7124 /*
7125 * We need to make sure there are no buffered pages in this
7126 * range either, we could have raced between the invalidate in
7127 * generic_file_direct_write and locking the extent. The
7128 * invalidate needs to happen so that reads after a write do not
7129 * get stale data.
7130 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007131 if (!ordered &&
7132 (!writing ||
7133 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007134 break;
7135
7136 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7137 cached_state, GFP_NOFS);
7138
7139 if (ordered) {
7140 btrfs_start_ordered_extent(inode, ordered, 1);
7141 btrfs_put_ordered_extent(ordered);
7142 } else {
7143 /* Screw you mmap */
Filipe Manana728404d2014-10-10 09:43:11 +01007144 ret = btrfs_fdatawrite_range(inode, lockstart, lockend);
Filipe Manana075bdbd2014-10-09 21:18:55 +01007145 if (ret)
7146 break;
7147 ret = filemap_fdatawait_range(inode->i_mapping,
7148 lockstart,
7149 lockend);
Josef Bacikeb838e72012-07-31 16:28:48 -04007150 if (ret)
7151 break;
7152
7153 /*
7154 * If we found a page that couldn't be invalidated just
7155 * fall back to buffered.
7156 */
7157 ret = invalidate_inode_pages2_range(inode->i_mapping,
7158 lockstart >> PAGE_CACHE_SHIFT,
7159 lockend >> PAGE_CACHE_SHIFT);
7160 if (ret)
7161 break;
7162 }
7163
7164 cond_resched();
7165 }
7166
7167 return ret;
7168}
7169
Josef Bacik69ffb542012-09-11 15:40:07 -04007170static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7171 u64 len, u64 orig_start,
7172 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007173 u64 orig_block_len, u64 ram_bytes,
7174 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007175{
7176 struct extent_map_tree *em_tree;
7177 struct extent_map *em;
7178 struct btrfs_root *root = BTRFS_I(inode)->root;
7179 int ret;
7180
7181 em_tree = &BTRFS_I(inode)->extent_tree;
7182 em = alloc_extent_map();
7183 if (!em)
7184 return ERR_PTR(-ENOMEM);
7185
7186 em->start = start;
7187 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007188 em->mod_start = start;
7189 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007190 em->len = len;
7191 em->block_len = block_len;
7192 em->block_start = block_start;
7193 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007194 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007195 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007196 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007197 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7198 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007199 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007200
7201 do {
7202 btrfs_drop_extent_cache(inode, em->start,
7203 em->start + em->len - 1, 0);
7204 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007205 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007206 write_unlock(&em_tree->lock);
7207 } while (ret == -EEXIST);
7208
7209 if (ret) {
7210 free_extent_map(em);
7211 return ERR_PTR(ret);
7212 }
7213
7214 return em;
7215}
7216
7217
Josef Bacik4b46fce2010-05-23 11:00:55 -04007218static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7219 struct buffer_head *bh_result, int create)
7220{
7221 struct extent_map *em;
7222 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007223 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007224 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007225 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007226 u64 len = bh_result->b_size;
Josef Bacik3e05bde2015-02-11 15:08:57 -05007227 u64 orig_len = len;
Josef Bacikeb838e72012-07-31 16:28:48 -04007228 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007229 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007230
Miao Xie172a5042013-02-21 02:48:22 -07007231 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007232 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007233 else
Josef Bacikc3298612012-08-03 16:49:19 -04007234 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007235
Josef Bacikc3298612012-08-03 16:49:19 -04007236 lockstart = start;
7237 lockend = start + len - 1;
7238
Josef Bacikeb838e72012-07-31 16:28:48 -04007239 /*
7240 * If this errors out it's because we couldn't invalidate pagecache for
7241 * this range and we need to fallback to buffered.
7242 */
7243 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7244 return -ENOTBLK;
7245
Josef Bacik4b46fce2010-05-23 11:00:55 -04007246 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007247 if (IS_ERR(em)) {
7248 ret = PTR_ERR(em);
7249 goto unlock_err;
7250 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007251
7252 /*
7253 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7254 * io. INLINE is special, and we could probably kludge it in here, but
7255 * it's still buffered so for safety lets just fall back to the generic
7256 * buffered path.
7257 *
7258 * For COMPRESSED we _have_ to read the entire extent in so we can
7259 * decompress it, so there will be buffering required no matter what we
7260 * do, so go ahead and fallback to buffered.
7261 *
7262 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7263 * to buffered IO. Don't blame me, this is the price we pay for using
7264 * the generic code.
7265 */
7266 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7267 em->block_start == EXTENT_MAP_INLINE) {
7268 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007269 ret = -ENOTBLK;
7270 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007271 }
7272
7273 /* Just a good old fashioned hole, return */
7274 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7275 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7276 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007277 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007278 }
7279
7280 /*
7281 * We don't allocate a new extent in the following cases
7282 *
7283 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7284 * existing extent.
7285 * 2) The extent is marked as PREALLOC. We're good to go here and can
7286 * just use the extent.
7287 *
7288 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007289 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007290 len = min(len, em->len - (start - em->start));
7291 lockstart = start + len;
7292 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007293 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007294
7295 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7296 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7297 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007298 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007299 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007300
7301 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7302 type = BTRFS_ORDERED_PREALLOC;
7303 else
7304 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007305 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007306 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007307
Josef Bacik00361582013-08-14 14:02:47 -04007308 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007309 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007310 if (type == BTRFS_ORDERED_PREALLOC) {
7311 free_extent_map(em);
7312 em = create_pinned_em(inode, start, len,
7313 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007314 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007315 orig_block_len,
7316 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007317 if (IS_ERR(em)) {
7318 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007319 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007320 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007321 }
7322
Chris Mason46bfbb52010-05-26 11:04:10 -04007323 ret = btrfs_add_ordered_extent_dio(inode, start,
7324 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007325 if (ret) {
7326 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007327 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007328 }
7329 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007330 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007331 }
Josef Bacik00361582013-08-14 14:02:47 -04007332
Chris Mason46bfbb52010-05-26 11:04:10 -04007333 /*
7334 * this will cow the extent, reset the len in case we changed
7335 * it above
7336 */
7337 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007338 free_extent_map(em);
7339 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007340 if (IS_ERR(em)) {
7341 ret = PTR_ERR(em);
7342 goto unlock_err;
7343 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007344 len = min(len, em->len - (start - em->start));
7345unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007346 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7347 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007348 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007349 bh_result->b_bdev = em->bdev;
7350 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007351 if (create) {
7352 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7353 set_buffer_new(bh_result);
7354
7355 /*
7356 * Need to update the i_size under the extent lock so buffered
7357 * readers will get the updated i_size when we unlock.
7358 */
7359 if (start + len > i_size_read(inode))
7360 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007361
Josef Bacik3e05bde2015-02-11 15:08:57 -05007362 if (len < orig_len) {
7363 spin_lock(&BTRFS_I(inode)->lock);
7364 BTRFS_I(inode)->outstanding_extents++;
7365 spin_unlock(&BTRFS_I(inode)->lock);
7366 }
Josef Bacik32667892015-02-11 15:08:58 -05007367 btrfs_free_reserved_data_space(inode, len);
Josef Bacikc3473e82012-06-19 10:59:00 -04007368 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007369
Josef Bacikeb838e72012-07-31 16:28:48 -04007370 /*
7371 * In the case of write we need to clear and unlock the entire range,
7372 * in the case of read we need to unlock only the end area that we
7373 * aren't using if there is any left over space.
7374 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007375 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007376 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7377 lockend, unlock_bits, 1, 0,
7378 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007379 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007380 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007381 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007382
Josef Bacik4b46fce2010-05-23 11:00:55 -04007383 free_extent_map(em);
7384
7385 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007386
7387unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007388 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7389 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
7390 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007391}
7392
Miao Xie8b110e32014-09-12 18:44:03 +08007393static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7394 int rw, int mirror_num)
7395{
7396 struct btrfs_root *root = BTRFS_I(inode)->root;
7397 int ret;
7398
7399 BUG_ON(rw & REQ_WRITE);
7400
7401 bio_get(bio);
7402
7403 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7404 BTRFS_WQ_ENDIO_DIO_REPAIR);
7405 if (ret)
7406 goto err;
7407
7408 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7409err:
7410 bio_put(bio);
7411 return ret;
7412}
7413
7414static int btrfs_check_dio_repairable(struct inode *inode,
7415 struct bio *failed_bio,
7416 struct io_failure_record *failrec,
7417 int failed_mirror)
7418{
7419 int num_copies;
7420
7421 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7422 failrec->logical, failrec->len);
7423 if (num_copies == 1) {
7424 /*
7425 * we only have a single copy of the data, so don't bother with
7426 * all the retry and error correction code that follows. no
7427 * matter what the error is, it is very likely to persist.
7428 */
7429 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7430 num_copies, failrec->this_mirror, failed_mirror);
7431 return 0;
7432 }
7433
7434 failrec->failed_mirror = failed_mirror;
7435 failrec->this_mirror++;
7436 if (failrec->this_mirror == failed_mirror)
7437 failrec->this_mirror++;
7438
7439 if (failrec->this_mirror > num_copies) {
7440 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7441 num_copies, failrec->this_mirror, failed_mirror);
7442 return 0;
7443 }
7444
7445 return 1;
7446}
7447
7448static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7449 struct page *page, u64 start, u64 end,
7450 int failed_mirror, bio_end_io_t *repair_endio,
7451 void *repair_arg)
7452{
7453 struct io_failure_record *failrec;
7454 struct bio *bio;
7455 int isector;
7456 int read_mode;
7457 int ret;
7458
7459 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7460
7461 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7462 if (ret)
7463 return ret;
7464
7465 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7466 failed_mirror);
7467 if (!ret) {
7468 free_io_failure(inode, failrec);
7469 return -EIO;
7470 }
7471
7472 if (failed_bio->bi_vcnt > 1)
7473 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7474 else
7475 read_mode = READ_SYNC;
7476
7477 isector = start - btrfs_io_bio(failed_bio)->logical;
7478 isector >>= inode->i_sb->s_blocksize_bits;
7479 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7480 0, isector, repair_endio, repair_arg);
7481 if (!bio) {
7482 free_io_failure(inode, failrec);
7483 return -EIO;
7484 }
7485
7486 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7487 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7488 read_mode, failrec->this_mirror, failrec->in_validation);
7489
7490 ret = submit_dio_repair_bio(inode, bio, read_mode,
7491 failrec->this_mirror);
7492 if (ret) {
7493 free_io_failure(inode, failrec);
7494 bio_put(bio);
7495 }
7496
7497 return ret;
7498}
7499
7500struct btrfs_retry_complete {
7501 struct completion done;
7502 struct inode *inode;
7503 u64 start;
7504 int uptodate;
7505};
7506
7507static void btrfs_retry_endio_nocsum(struct bio *bio, int err)
7508{
7509 struct btrfs_retry_complete *done = bio->bi_private;
7510 struct bio_vec *bvec;
7511 int i;
7512
7513 if (err)
7514 goto end;
7515
7516 done->uptodate = 1;
7517 bio_for_each_segment_all(bvec, bio, i)
7518 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7519end:
7520 complete(&done->done);
7521 bio_put(bio);
7522}
7523
7524static int __btrfs_correct_data_nocsum(struct inode *inode,
7525 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007526{
Kent Overstreet2c30c712013-11-07 12:20:26 -08007527 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007528 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007529 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007530 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007531 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007532
Miao Xiec1dc0892014-09-12 18:43:56 +08007533 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007534 done.inode = inode;
7535
7536 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7537try_again:
7538 done.uptodate = 0;
7539 done.start = start;
7540 init_completion(&done.done);
7541
7542 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7543 start + bvec->bv_len - 1,
7544 io_bio->mirror_num,
7545 btrfs_retry_endio_nocsum, &done);
7546 if (ret)
7547 return ret;
7548
7549 wait_for_completion(&done.done);
7550
7551 if (!done.uptodate) {
7552 /* We might have another mirror, so try again */
7553 goto try_again;
7554 }
7555
7556 start += bvec->bv_len;
7557 }
7558
7559 return 0;
7560}
7561
7562static void btrfs_retry_endio(struct bio *bio, int err)
7563{
7564 struct btrfs_retry_complete *done = bio->bi_private;
7565 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7566 struct bio_vec *bvec;
7567 int uptodate;
7568 int ret;
7569 int i;
7570
7571 if (err)
7572 goto end;
7573
7574 uptodate = 1;
7575 bio_for_each_segment_all(bvec, bio, i) {
7576 ret = __readpage_endio_check(done->inode, io_bio, i,
7577 bvec->bv_page, 0,
7578 done->start, bvec->bv_len);
7579 if (!ret)
7580 clean_io_failure(done->inode, done->start,
7581 bvec->bv_page, 0);
7582 else
7583 uptodate = 0;
7584 }
7585
7586 done->uptodate = uptodate;
7587end:
7588 complete(&done->done);
7589 bio_put(bio);
7590}
7591
7592static int __btrfs_subio_endio_read(struct inode *inode,
7593 struct btrfs_io_bio *io_bio, int err)
7594{
7595 struct bio_vec *bvec;
7596 struct btrfs_retry_complete done;
7597 u64 start;
7598 u64 offset = 0;
7599 int i;
7600 int ret;
7601
7602 err = 0;
7603 start = io_bio->logical;
7604 done.inode = inode;
7605
Miao Xiec1dc0892014-09-12 18:43:56 +08007606 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Miao Xiedc380ae2014-09-12 18:43:55 +08007607 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7608 0, start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007609 if (likely(!ret))
7610 goto next;
7611try_again:
7612 done.uptodate = 0;
7613 done.start = start;
7614 init_completion(&done.done);
7615
7616 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7617 start + bvec->bv_len - 1,
7618 io_bio->mirror_num,
7619 btrfs_retry_endio, &done);
7620 if (ret) {
7621 err = ret;
7622 goto next;
7623 }
7624
7625 wait_for_completion(&done.done);
7626
7627 if (!done.uptodate) {
7628 /* We might have another mirror, so try again */
7629 goto try_again;
7630 }
7631next:
7632 offset += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007633 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007634 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007635
7636 return err;
7637}
7638
Miao Xie8b110e32014-09-12 18:44:03 +08007639static int btrfs_subio_endio_read(struct inode *inode,
7640 struct btrfs_io_bio *io_bio, int err)
7641{
7642 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7643
7644 if (skip_csum) {
7645 if (unlikely(err))
7646 return __btrfs_correct_data_nocsum(inode, io_bio);
7647 else
7648 return 0;
7649 } else {
7650 return __btrfs_subio_endio_read(inode, io_bio, err);
7651 }
7652}
7653
Miao Xiec1dc0892014-09-12 18:43:56 +08007654static void btrfs_endio_direct_read(struct bio *bio, int err)
7655{
7656 struct btrfs_dio_private *dip = bio->bi_private;
7657 struct inode *inode = dip->inode;
7658 struct bio *dio_bio;
7659 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7660
Miao Xie8b110e32014-09-12 18:44:03 +08007661 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
7662 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007663
Josef Bacik4b46fce2010-05-23 11:00:55 -04007664 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007665 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007666 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007667
Josef Bacik4b46fce2010-05-23 11:00:55 -04007668 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007669
7670 /* If we had a csum failure make sure to clear the uptodate flag */
7671 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007672 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7673 dio_end_io(dio_bio, err);
Miao Xie23ea8e52014-09-12 18:43:54 +08007674
7675 if (io_bio->end_io)
7676 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04007677 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007678}
7679
7680static void btrfs_endio_direct_write(struct bio *bio, int err)
7681{
7682 struct btrfs_dio_private *dip = bio->bi_private;
7683 struct inode *inode = dip->inode;
7684 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007685 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007686 u64 ordered_offset = dip->logical_offset;
7687 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007688 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007689 int ret;
7690
7691 if (err)
7692 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007693again:
7694 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7695 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007696 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007697 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007698 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007699
Liu Bo9e0af232014-08-15 23:36:53 +08007700 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7701 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007702 btrfs_queue_work(root->fs_info->endio_write_workers,
7703 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007704out_test:
7705 /*
7706 * our bio might span multiple ordered extents. If we haven't
7707 * completed the accounting for the whole dio, go back and try again
7708 */
7709 if (ordered_offset < dip->logical_offset + dip->bytes) {
7710 ordered_bytes = dip->logical_offset + dip->bytes -
7711 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007712 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007713 goto again;
7714 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007715out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007716 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007717
Josef Bacik4b46fce2010-05-23 11:00:55 -04007718 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007719
7720 /* If we had an error make sure to clear the uptodate flag */
7721 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007722 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7723 dio_end_io(dio_bio, err);
7724 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007725}
7726
Chris Masoneaf25d92010-05-25 09:48:28 -04007727static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7728 struct bio *bio, int mirror_num,
7729 unsigned long bio_flags, u64 offset)
7730{
7731 int ret;
7732 struct btrfs_root *root = BTRFS_I(inode)->root;
7733 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007734 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007735 return 0;
7736}
7737
Miao Xiee65e1532010-11-22 03:04:43 +00007738static void btrfs_end_dio_bio(struct bio *bio, int err)
7739{
7740 struct btrfs_dio_private *dip = bio->bi_private;
7741
Miao Xie8b110e32014-09-12 18:44:03 +08007742 if (err)
7743 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
7744 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
7745 btrfs_ino(dip->inode), bio->bi_rw,
7746 (unsigned long long)bio->bi_iter.bi_sector,
7747 bio->bi_iter.bi_size, err);
7748
7749 if (dip->subio_endio)
7750 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007751
7752 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00007753 dip->errors = 1;
7754
7755 /*
7756 * before atomic variable goto zero, we must make sure
7757 * dip->errors is perceived to be set.
7758 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007759 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007760 }
7761
7762 /* if there are more bios still pending for this dio, just exit */
7763 if (!atomic_dec_and_test(&dip->pending_bios))
7764 goto out;
7765
Chris Mason9be33952013-05-17 18:30:14 -04007766 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007767 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007768 } else {
7769 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007770 bio_endio(dip->orig_bio, 0);
7771 }
7772out:
7773 bio_put(bio);
7774}
7775
7776static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7777 u64 first_sector, gfp_t gfp_flags)
7778{
7779 int nr_vecs = bio_get_nr_vecs(bdev);
7780 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7781}
7782
Miao Xiec1dc0892014-09-12 18:43:56 +08007783static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
7784 struct inode *inode,
7785 struct btrfs_dio_private *dip,
7786 struct bio *bio,
7787 u64 file_offset)
7788{
7789 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7790 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
7791 int ret;
7792
7793 /*
7794 * We load all the csum data we need when we submit
7795 * the first bio to reduce the csum tree search and
7796 * contention.
7797 */
7798 if (dip->logical_offset == file_offset) {
7799 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
7800 file_offset);
7801 if (ret)
7802 return ret;
7803 }
7804
7805 if (bio == dip->orig_bio)
7806 return 0;
7807
7808 file_offset -= dip->logical_offset;
7809 file_offset >>= inode->i_sb->s_blocksize_bits;
7810 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
7811
7812 return 0;
7813}
7814
Miao Xiee65e1532010-11-22 03:04:43 +00007815static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7816 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007817 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007818{
Miao Xiefacc8a222013-07-25 19:22:34 +08007819 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007820 int write = rw & REQ_WRITE;
7821 struct btrfs_root *root = BTRFS_I(inode)->root;
7822 int ret;
7823
Josef Bacikb812ce22012-11-16 13:56:32 -05007824 if (async_submit)
7825 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7826
Miao Xiee65e1532010-11-22 03:04:43 +00007827 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007828
7829 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02007830 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7831 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007832 if (ret)
7833 goto err;
7834 }
Miao Xiee65e1532010-11-22 03:04:43 +00007835
Josef Bacik1ae39932011-04-06 14:41:34 -04007836 if (skip_sum)
7837 goto map;
7838
7839 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007840 ret = btrfs_wq_submit_bio(root->fs_info,
7841 inode, rw, bio, 0, 0,
7842 file_offset,
7843 __btrfs_submit_bio_start_direct_io,
7844 __btrfs_submit_bio_done);
7845 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007846 } else if (write) {
7847 /*
7848 * If we aren't doing async submit, calculate the csum of the
7849 * bio now.
7850 */
7851 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7852 if (ret)
7853 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007854 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08007855 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
7856 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007857 if (ret)
7858 goto err;
7859 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007860map:
7861 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007862err:
7863 bio_put(bio);
7864 return ret;
7865}
7866
7867static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7868 int skip_sum)
7869{
7870 struct inode *inode = dip->inode;
7871 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007872 struct bio *bio;
7873 struct bio *orig_bio = dip->orig_bio;
7874 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007875 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00007876 u64 file_offset = dip->logical_offset;
7877 u64 submit_len = 0;
7878 u64 map_length;
7879 int nr_pages = 0;
Miao Xie23ea8e52014-09-12 18:43:54 +08007880 int ret;
Josef Bacik1ae39932011-04-06 14:41:34 -04007881 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007882
Kent Overstreet4f024f32013-10-11 15:44:27 -07007883 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007884 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007885 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08007886 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00007887 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08007888
Kent Overstreet4f024f32013-10-11 15:44:27 -07007889 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04007890 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007891 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04007892 goto submit;
7893 }
7894
David Woodhouse53b381b2013-01-29 18:40:14 -05007895 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08007896 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05007897 async_submit = 0;
7898 else
7899 async_submit = 1;
7900
Josef Bacik02f57c72011-04-06 14:25:44 -04007901 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7902 if (!bio)
7903 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08007904
Josef Bacik02f57c72011-04-06 14:25:44 -04007905 bio->bi_private = dip;
7906 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007907 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04007908 atomic_inc(&dip->pending_bios);
7909
Miao Xiee65e1532010-11-22 03:04:43 +00007910 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
David Sterbaee39b432014-09-30 01:33:33 +02007911 if (map_length < submit_len + bvec->bv_len ||
Miao Xiee65e1532010-11-22 03:04:43 +00007912 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
David Sterbaee39b432014-09-30 01:33:33 +02007913 bvec->bv_offset) < bvec->bv_len) {
Miao Xiee65e1532010-11-22 03:04:43 +00007914 /*
7915 * inc the count before we submit the bio so
7916 * we know the end IO handler won't happen before
7917 * we inc the count. Otherwise, the dip might get freed
7918 * before we're done setting it up
7919 */
7920 atomic_inc(&dip->pending_bios);
7921 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7922 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007923 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007924 if (ret) {
7925 bio_put(bio);
7926 atomic_dec(&dip->pending_bios);
7927 goto out_err;
7928 }
7929
Miao Xiee65e1532010-11-22 03:04:43 +00007930 start_sector += submit_len >> 9;
7931 file_offset += submit_len;
7932
7933 submit_len = 0;
7934 nr_pages = 0;
7935
7936 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7937 start_sector, GFP_NOFS);
7938 if (!bio)
7939 goto out_err;
7940 bio->bi_private = dip;
7941 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007942 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007943
Kent Overstreet4f024f32013-10-11 15:44:27 -07007944 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007945 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007946 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007947 &map_length, NULL, 0);
7948 if (ret) {
7949 bio_put(bio);
7950 goto out_err;
7951 }
7952 } else {
7953 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307954 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00007955 bvec++;
7956 }
7957 }
7958
Josef Bacik02f57c72011-04-06 14:25:44 -04007959submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007960 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007961 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007962 if (!ret)
7963 return 0;
7964
7965 bio_put(bio);
7966out_err:
7967 dip->errors = 1;
7968 /*
7969 * before atomic variable goto zero, we must
7970 * make sure dip->errors is perceived to be set.
7971 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007972 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007973 if (atomic_dec_and_test(&dip->pending_bios))
7974 bio_io_error(dip->orig_bio);
7975
7976 /* bio_end_io() will handle error, so we needn't return it */
7977 return 0;
7978}
7979
Chris Mason9be33952013-05-17 18:30:14 -04007980static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7981 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007982{
7983 struct btrfs_root *root = BTRFS_I(inode)->root;
7984 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007985 struct bio *io_bio;
Miao Xie23ea8e52014-09-12 18:43:54 +08007986 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007987 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007988 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007989 int ret = 0;
7990
7991 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7992
Chris Mason9be33952013-05-17 18:30:14 -04007993 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007994 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007995 ret = -ENOMEM;
7996 goto free_ordered;
7997 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007998
Miao Xiec1dc0892014-09-12 18:43:56 +08007999 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008000 if (!dip) {
8001 ret = -ENOMEM;
8002 goto free_io_bio;
8003 }
8004
8005 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008006 dip->inode = inode;
8007 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008008 dip->bytes = dio_bio->bi_iter.bi_size;
8009 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008010 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008011 dip->orig_bio = io_bio;
8012 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008013 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008014 btrfs_bio = btrfs_io_bio(io_bio);
8015 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008016
Miao Xiec1dc0892014-09-12 18:43:56 +08008017 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008018 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008019 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008020 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008021 dip->subio_endio = btrfs_subio_endio_read;
8022 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008023
Miao Xiee65e1532010-11-22 03:04:43 +00008024 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
8025 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008026 return;
Chris Mason9be33952013-05-17 18:30:14 -04008027
Miao Xie23ea8e52014-09-12 18:43:54 +08008028 if (btrfs_bio->end_io)
8029 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008030free_io_bio:
8031 bio_put(io_bio);
8032
Josef Bacik4b46fce2010-05-23 11:00:55 -04008033free_ordered:
8034 /*
8035 * If this is a write, we need to clean up the reserved space and kill
8036 * the ordered extent.
8037 */
8038 if (write) {
8039 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05008040 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008041 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
8042 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
8043 btrfs_free_reserved_extent(root, ordered->start,
Miao Xiee570fd22014-06-19 10:42:50 +08008044 ordered->disk_len, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008045 btrfs_put_ordered_extent(ordered);
8046 btrfs_put_ordered_extent(ordered);
8047 }
Chris Mason9be33952013-05-17 18:30:14 -04008048 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008049}
8050
Chris Mason5a5f79b2010-05-26 21:33:37 -04008051static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04008052 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008053{
8054 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008055 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008056 unsigned blocksize_mask = root->sectorsize - 1;
8057 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008058
8059 if (offset & blocksize_mask)
8060 goto out;
8061
Al Viro28060d52014-03-22 05:15:17 -04008062 if (iov_iter_alignment(iter) & blocksize_mask)
8063 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008064
Al Viro28060d52014-03-22 05:15:17 -04008065 /* If this is a write we don't need to check anymore */
8066 if (rw & WRITE)
8067 return 0;
8068 /*
8069 * Check to make sure we don't have duplicate iov_base's in this
8070 * iovec, if so return EINVAL, otherwise we'll get csum errors
8071 * when reading back.
8072 */
8073 for (seg = 0; seg < iter->nr_segs; seg++) {
8074 for (i = seg + 1; i < iter->nr_segs; i++) {
8075 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008076 goto out;
8077 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008078 }
8079 retval = 0;
8080out:
8081 return retval;
8082}
Josef Bacikeb838e72012-07-31 16:28:48 -04008083
Chris Mason16432982008-04-10 10:23:21 -04008084static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05008085 struct iov_iter *iter, loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04008086{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008087 struct file *file = iocb->ki_filp;
8088 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00008089 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008090 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008091 bool wakeup = true;
8092 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008093 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008094
Al Viro28060d52014-03-22 05:15:17 -04008095 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008096 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008097
Miao Xie38851cc2013-02-08 07:04:11 +00008098 atomic_inc(&inode->i_dio_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008099 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008100
Josef Bacik0e267c42013-07-02 10:38:02 -04008101 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008102 * The generic stuff only does filemap_write_and_wait_range, which
8103 * isn't enough if we've written compressed pages to this area, so
8104 * we need to flush the dirty pages again to make absolutely sure
8105 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008106 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008107 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008108 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8109 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008110 filemap_fdatawrite_range(inode->i_mapping, offset,
8111 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008112
Miao Xie09348562013-02-07 10:12:07 +00008113 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008114 /*
8115 * If the write DIO is beyond the EOF, we need update
8116 * the isize, but it is protected by i_mutex. So we can
8117 * not unlock the i_mutex at this case.
8118 */
8119 if (offset + count <= inode->i_size) {
8120 mutex_unlock(&inode->i_mutex);
8121 relock = true;
8122 }
Miao Xie09348562013-02-07 10:12:07 +00008123 ret = btrfs_delalloc_reserve_space(inode, count);
8124 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008125 goto out;
David Sterbaee39b432014-09-30 01:33:33 +02008126 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8127 &BTRFS_I(inode)->runtime_flags)) {
Miao Xie38851cc2013-02-08 07:04:11 +00008128 inode_dio_done(inode);
8129 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8130 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008131 }
8132
8133 ret = __blockdev_direct_IO(rw, iocb, inode,
8134 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Al Viro31b14032014-03-05 01:33:16 -05008135 iter, offset, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00008136 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00008137 if (rw & WRITE) {
8138 if (ret < 0 && ret != -EIOCBQUEUED)
8139 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07008140 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00008141 btrfs_delalloc_release_space(inode,
8142 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008143 }
Miao Xie38851cc2013-02-08 07:04:11 +00008144out:
Miao Xie2e60a512013-02-08 07:01:08 +00008145 if (wakeup)
8146 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008147 if (relock)
8148 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00008149
8150 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008151}
8152
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008153#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8154
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008155static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8156 __u64 start, __u64 len)
8157{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008158 int ret;
8159
8160 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8161 if (ret)
8162 return ret;
8163
Chris Masonec29ed52011-02-23 16:23:20 -05008164 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008165}
8166
Chris Mason9ebefb182007-06-15 13:50:00 -04008167int btrfs_readpage(struct file *file, struct page *page)
8168{
Chris Masond1310b22008-01-24 16:13:08 -05008169 struct extent_io_tree *tree;
8170 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008171 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008172}
Chris Mason1832a6d2007-12-21 16:27:21 -05008173
Chris Mason39279cc2007-06-12 06:35:45 -04008174static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8175{
Chris Masond1310b22008-01-24 16:13:08 -05008176 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04008177
8178
8179 if (current->flags & PF_MEMALLOC) {
8180 redirty_page_for_writepage(wbc, page);
8181 unlock_page(page);
8182 return 0;
8183 }
Chris Masond1310b22008-01-24 16:13:08 -05008184 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04008185 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8186}
Chris Mason39279cc2007-06-12 06:35:45 -04008187
Eric Sandeen48a3b632013-04-25 20:41:01 +00008188static int btrfs_writepages(struct address_space *mapping,
8189 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008190{
Chris Masond1310b22008-01-24 16:13:08 -05008191 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008192
Chris Masond1310b22008-01-24 16:13:08 -05008193 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008194 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8195}
8196
Chris Mason3ab2fb52007-11-08 10:59:22 -05008197static int
8198btrfs_readpages(struct file *file, struct address_space *mapping,
8199 struct list_head *pages, unsigned nr_pages)
8200{
Chris Masond1310b22008-01-24 16:13:08 -05008201 struct extent_io_tree *tree;
8202 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008203 return extent_readpages(tree, mapping, pages, nr_pages,
8204 btrfs_get_extent);
8205}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008206static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008207{
Chris Masond1310b22008-01-24 16:13:08 -05008208 struct extent_io_tree *tree;
8209 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008210 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008211
Chris Masond1310b22008-01-24 16:13:08 -05008212 tree = &BTRFS_I(page->mapping->host)->io_tree;
8213 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008214 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008215 if (ret == 1) {
8216 ClearPagePrivate(page);
8217 set_page_private(page, 0);
8218 page_cache_release(page);
8219 }
8220 return ret;
8221}
Chris Mason39279cc2007-06-12 06:35:45 -04008222
Chris Masone6dcd2d2008-07-17 12:53:50 -04008223static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8224{
Chris Mason98509cfc2008-09-11 15:51:43 -04008225 if (PageWriteback(page) || PageDirty(page))
8226 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008227 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008228}
8229
Lukas Czernerd47992f2013-05-21 23:17:23 -04008230static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8231 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008232{
Josef Bacik5fd02042012-05-02 14:00:54 -04008233 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008234 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008235 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008236 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008237 u64 page_start = page_offset(page);
8238 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008239 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008240
Chris Mason8b62b722009-09-02 16:53:46 -04008241 /*
8242 * we have the page locked, so new writeback can't start,
8243 * and the dirty bit won't be cleared while we are here.
8244 *
8245 * Wait for IO on this page so that we can safely clear
8246 * the PagePrivate2 bit and do ordered accounting
8247 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008248 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008249
Josef Bacik5fd02042012-05-02 14:00:54 -04008250 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008251 if (offset) {
8252 btrfs_releasepage(page, GFP_NOFS);
8253 return;
8254 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008255
8256 if (!inode_evicting)
8257 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
8258 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008259 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04008260 /*
8261 * IO on this page will never be started, so we need
8262 * to account for any ordered extents now
8263 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008264 if (!inode_evicting)
8265 clear_extent_bit(tree, page_start, page_end,
8266 EXTENT_DIRTY | EXTENT_DELALLOC |
8267 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8268 EXTENT_DEFRAG, 1, 0, &cached_state,
8269 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008270 /*
8271 * whoever cleared the private bit is responsible
8272 * for the finish_ordered_io
8273 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008274 if (TestClearPagePrivate2(page)) {
8275 struct btrfs_ordered_inode_tree *tree;
8276 u64 new_len;
8277
8278 tree = &BTRFS_I(inode)->ordered_tree;
8279
8280 spin_lock_irq(&tree->lock);
8281 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8282 new_len = page_start - ordered->file_offset;
8283 if (new_len < ordered->truncated_len)
8284 ordered->truncated_len = new_len;
8285 spin_unlock_irq(&tree->lock);
8286
8287 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8288 page_start,
8289 PAGE_CACHE_SIZE, 1))
8290 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008291 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008292 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008293 if (!inode_evicting) {
8294 cached_state = NULL;
8295 lock_extent_bits(tree, page_start, page_end, 0,
8296 &cached_state);
8297 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008298 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008299
8300 if (!inode_evicting) {
8301 clear_extent_bit(tree, page_start, page_end,
8302 EXTENT_LOCKED | EXTENT_DIRTY |
8303 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8304 EXTENT_DEFRAG, 1, 1,
8305 &cached_state, GFP_NOFS);
8306
8307 __btrfs_releasepage(page, GFP_NOFS);
8308 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008309
Chris Mason4a096752008-07-21 10:29:44 -04008310 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008311 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008312 ClearPagePrivate(page);
8313 set_page_private(page, 0);
8314 page_cache_release(page);
8315 }
Chris Mason39279cc2007-06-12 06:35:45 -04008316}
8317
Chris Mason9ebefb182007-06-15 13:50:00 -04008318/*
8319 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8320 * called from a page fault handler when a page is first dirtied. Hence we must
8321 * be careful to check for EOF conditions here. We set the page up correctly
8322 * for a written page which means we get ENOSPC checking when writing into
8323 * holes and correct delalloc and unwritten extent mapping on filesystems that
8324 * support these features.
8325 *
8326 * We are not allowed to take the i_mutex here so we have to play games to
8327 * protect against truncate races as the page could now be beyond EOF. Because
8328 * vmtruncate() writes the inode size before removing pages, once we have the
8329 * page lock we can determine safely if the page is beyond EOF. If it is not
8330 * beyond EOF, then the page is guaranteed safe against truncation until we
8331 * unlock the page.
8332 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008333int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008334{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008335 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008336 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008337 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008338 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8339 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008340 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008341 char *kaddr;
8342 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008343 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008344 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008345 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04008346 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008347 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04008348
Jan Karab2b5ef52012-06-12 16:20:45 +02008349 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04008350 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008351 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008352 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008353 reserved = 1;
8354 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008355 if (ret) {
8356 if (ret == -ENOMEM)
8357 ret = VM_FAULT_OOM;
8358 else /* -ENOSPC, -EIO, etc */
8359 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008360 if (reserved)
8361 goto out;
8362 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008363 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008364
Nick Piggin56a76f82009-03-31 15:23:23 -07008365 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008366again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008367 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008368 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008369 page_start = page_offset(page);
8370 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04008371
Chris Mason9ebefb182007-06-15 13:50:00 -04008372 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008373 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008374 /* page got truncated out from underneath us */
8375 goto out_unlock;
8376 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008377 wait_on_page_writeback(page);
8378
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008379 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008380 set_page_extent_mapped(page);
8381
Chris Masoneb84ae02008-07-17 13:53:27 -04008382 /*
8383 * we can't set the delalloc bits if there are pending ordered
8384 * extents. Drop our locks and wait for them to finish
8385 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008386 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8387 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008388 unlock_extent_cached(io_tree, page_start, page_end,
8389 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008390 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008391 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008392 btrfs_put_ordered_extent(ordered);
8393 goto again;
8394 }
8395
Josef Bacikfbf19082009-10-01 17:10:23 -04008396 /*
8397 * XXX - page_mkwrite gets called every time the page is dirtied, even
8398 * if it was already dirty, so for space accounting reasons we need to
8399 * clear any delalloc bits for the range we are fixing to save. There
8400 * is probably a better way to do this, but for now keep consistent with
8401 * prepare_pages in the normal write path.
8402 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00008403 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008404 EXTENT_DIRTY | EXTENT_DELALLOC |
8405 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008406 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008407
Josef Bacik2ac55d42010-02-03 19:33:23 +00008408 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
8409 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008410 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008411 unlock_extent_cached(io_tree, page_start, page_end,
8412 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008413 ret = VM_FAULT_SIGBUS;
8414 goto out_unlock;
8415 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008416 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008417
8418 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04008419 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008420 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008421 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008422 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008423
Chris Masone6dcd2d2008-07-17 12:53:50 -04008424 if (zero_start != PAGE_CACHE_SIZE) {
8425 kaddr = kmap(page);
8426 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8427 flush_dcache_page(page);
8428 kunmap(page);
8429 }
Chris Mason247e7432008-07-17 12:53:51 -04008430 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008431 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008432 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008433
Chris Mason257c62e2009-10-13 13:21:08 -04008434 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8435 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008436 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008437
Josef Bacik2ac55d42010-02-03 19:33:23 +00008438 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008439
8440out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008441 if (!ret) {
8442 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008443 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008444 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008445 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008446out:
Josef Bacikec39e182012-01-12 19:10:12 -05008447 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008448out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008449 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008450 return ret;
8451}
8452
Josef Bacika41ad392011-01-31 15:30:16 -05008453static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008454{
8455 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008456 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008457 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008458 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008459 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008460 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008461 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008462
Josef Bacik0ef8b722013-10-25 16:13:35 -04008463 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8464 (u64)-1);
8465 if (ret)
8466 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008467
Josef Bacikfcb80c22011-05-03 10:40:22 -04008468 /*
8469 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8470 * 3 things going on here
8471 *
8472 * 1) We need to reserve space for our orphan item and the space to
8473 * delete our orphan item. Lord knows we don't want to have a dangling
8474 * orphan item because we didn't reserve space to remove it.
8475 *
8476 * 2) We need to reserve space to update our inode.
8477 *
8478 * 3) We need to have something to cache all the space that is going to
8479 * be free'd up by the truncate operation, but also have some slack
8480 * space reserved in case it uses space during the truncate (thank you
8481 * very much snapshotting).
8482 *
8483 * And we need these to all be seperate. The fact is we can use alot of
8484 * space doing the truncate, and we have no earthly idea how much space
8485 * we will use, so we need the truncate reservation to be seperate so it
8486 * doesn't end up using space reserved for updating the inode or
8487 * removing the orphan item. We also need to be able to stop the
8488 * transaction and start a new one, which means we need to be able to
8489 * update the inode several times, and we have no idea of knowing how
8490 * many times that will be, so we can't just reserve 1 item for the
8491 * entirety of the opration, so that has to be done seperately as well.
8492 * Then there is the orphan item, which does indeed need to be held on
8493 * to for the whole operation, and we need nobody to touch this reserved
8494 * space except the orphan code.
8495 *
8496 * So that leaves us with
8497 *
8498 * 1) root->orphan_block_rsv - for the orphan deletion.
8499 * 2) rsv - for the truncate reservation, which we will steal from the
8500 * transaction reservation.
8501 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8502 * updating the inode.
8503 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008504 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008505 if (!rsv)
8506 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008507 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008508 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008509
Josef Bacik907cbce2011-08-08 13:46:15 -04008510 /*
Josef Bacik07127182011-08-19 10:29:59 -04008511 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008512 * 1 for updating the inode.
8513 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008514 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008515 if (IS_ERR(trans)) {
8516 err = PTR_ERR(trans);
8517 goto out;
8518 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008519
Josef Bacik907cbce2011-08-08 13:46:15 -04008520 /* Migrate the slack space for the truncate to our reserve */
8521 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8522 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008523 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008524
Chris Mason5a3f23d2009-03-31 13:27:11 -04008525 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008526 * So if we truncate and then write and fsync we normally would just
8527 * write the extents that changed, which is a problem if we need to
8528 * first truncate that entire inode. So set this flag so we write out
8529 * all of the extents in the inode to the sync log so we're completely
8530 * safe.
8531 */
8532 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008533 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008534
Yan, Zheng80825102009-11-12 09:35:36 +00008535 while (1) {
8536 ret = btrfs_truncate_inode_items(trans, root, inode,
8537 inode->i_size,
8538 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008539 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05008540 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008541 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008542 }
Chris Mason39279cc2007-06-12 06:35:45 -04008543
Josef Bacikfcb80c22011-05-03 10:40:22 -04008544 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008545 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008546 if (ret) {
8547 err = ret;
8548 break;
8549 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008550
Yan, Zheng80825102009-11-12 09:35:36 +00008551 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008552 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008553
8554 trans = btrfs_start_transaction(root, 2);
8555 if (IS_ERR(trans)) {
8556 ret = err = PTR_ERR(trans);
8557 trans = NULL;
8558 break;
8559 }
8560
8561 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8562 rsv, min_size);
8563 BUG_ON(ret); /* shouldn't happen */
8564 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008565 }
8566
8567 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008568 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008569 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008570 if (ret)
8571 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008572 }
8573
Chris Mason917c16b2011-11-08 14:49:59 -05008574 if (trans) {
8575 trans->block_rsv = &root->fs_info->trans_block_rsv;
8576 ret = btrfs_update_inode(trans, root, inode);
8577 if (ret && !err)
8578 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008579
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008580 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008581 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008582 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008583
8584out:
8585 btrfs_free_block_rsv(root, rsv);
8586
Josef Bacik3893e332011-01-31 16:03:11 -05008587 if (ret && !err)
8588 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008589
Josef Bacik3893e332011-01-31 16:03:11 -05008590 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008591}
8592
Sven Wegener3b963622008-06-09 21:57:42 -04008593/*
Chris Masond352ac62008-09-29 15:18:18 -04008594 * create a new subvolume directory/inode (helper for the ioctl).
8595 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008596int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008597 struct btrfs_root *new_root,
8598 struct btrfs_root *parent_root,
8599 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008600{
Chris Mason39279cc2007-06-12 06:35:45 -04008601 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008602 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008603 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008604
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008605 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8606 new_dirid, new_dirid,
8607 S_IFDIR | (~current_umask() & S_IRWXUGO),
8608 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008609 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008610 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008611 inode->i_op = &btrfs_dir_inode_operations;
8612 inode->i_fop = &btrfs_dir_file_operations;
8613
Miklos Szeredibfe86842011-10-28 14:13:29 +02008614 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008615 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008616 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008617
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008618 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8619 if (err)
8620 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008621 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008622 new_root->root_key.objectid, err);
8623
Yan, Zheng76dda932009-09-21 16:00:26 -04008624 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008625
Yan, Zheng76dda932009-09-21 16:00:26 -04008626 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008627 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008628}
8629
Chris Mason39279cc2007-06-12 06:35:45 -04008630struct inode *btrfs_alloc_inode(struct super_block *sb)
8631{
8632 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008633 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008634
8635 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8636 if (!ei)
8637 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008638
8639 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008640 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008641 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008642 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008643 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008644 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008645 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008646 ei->disk_i_size = 0;
8647 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008648 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008649 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008650 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008651 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008652 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008653
Josef Bacik9e0baf62011-07-15 15:16:44 +00008654 spin_lock_init(&ei->lock);
8655 ei->outstanding_extents = 0;
8656 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008657
Josef Bacik72ac3c02012-05-23 14:13:11 -04008658 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008659 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008660
Miao Xie16cdcec2011-04-22 18:12:22 +08008661 ei->delayed_node = NULL;
8662
chandan r9cc97d62012-07-04 12:48:07 +05308663 ei->i_otime.tv_sec = 0;
8664 ei->i_otime.tv_nsec = 0;
8665
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008666 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008667 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008668 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8669 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008670 ei->io_tree.track_uptodate = 1;
8671 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008672 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008673 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008674 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008675 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008676 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008677 RB_CLEAR_NODE(&ei->rb_node);
8678
8679 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008680}
8681
Josef Bacikaaedb552013-10-11 14:44:09 -04008682#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8683void btrfs_test_destroy_inode(struct inode *inode)
8684{
8685 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8686 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8687}
8688#endif
8689
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008690static void btrfs_i_callback(struct rcu_head *head)
8691{
8692 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008693 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8694}
8695
Chris Mason39279cc2007-06-12 06:35:45 -04008696void btrfs_destroy_inode(struct inode *inode)
8697{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008698 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008699 struct btrfs_root *root = BTRFS_I(inode)->root;
8700
Al Virob3d9b7a2012-06-09 13:51:19 -04008701 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008702 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008703 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8704 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008705 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8706 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008707 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008708
Chris Mason5a3f23d2009-03-31 13:27:11 -04008709 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008710 * This can happen where we create an inode, but somebody else also
8711 * created the same inode and we need to destroy the one we already
8712 * created.
8713 */
8714 if (!root)
8715 goto free;
8716
Josef Bacik8a35d952012-05-23 14:26:42 -04008717 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8718 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008719 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008720 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008721 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008722 }
Josef Bacik7b128762008-07-24 12:17:14 -04008723
Chris Masond3977122009-01-05 21:25:51 -05008724 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008725 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8726 if (!ordered)
8727 break;
8728 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008729 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008730 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008731 btrfs_remove_ordered_extent(inode, ordered);
8732 btrfs_put_ordered_extent(ordered);
8733 btrfs_put_ordered_extent(ordered);
8734 }
8735 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008736 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008737 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008738free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008739 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008740}
8741
Al Viro45321ac2010-06-07 13:43:19 -04008742int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008743{
8744 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008745
Naohiro Aota6379ef92013-06-06 09:56:34 +00008746 if (root == NULL)
8747 return 1;
8748
Liu Bofa6ac872013-02-20 14:10:23 +00008749 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008750 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008751 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008752 else
Al Viro45321ac2010-06-07 13:43:19 -04008753 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008754}
8755
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008756static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008757{
8758 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8759
8760 inode_init_once(&ei->vfs_inode);
8761}
8762
8763void btrfs_destroy_cachep(void)
8764{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008765 /*
8766 * Make sure all delayed rcu free inodes are flushed before we
8767 * destroy cache.
8768 */
8769 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008770 if (btrfs_inode_cachep)
8771 kmem_cache_destroy(btrfs_inode_cachep);
8772 if (btrfs_trans_handle_cachep)
8773 kmem_cache_destroy(btrfs_trans_handle_cachep);
8774 if (btrfs_transaction_cachep)
8775 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008776 if (btrfs_path_cachep)
8777 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008778 if (btrfs_free_space_cachep)
8779 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008780 if (btrfs_delalloc_work_cachep)
8781 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008782}
8783
8784int btrfs_init_cachep(void)
8785{
David Sterba837e1972012-09-07 03:00:48 -06008786 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008787 sizeof(struct btrfs_inode), 0,
8788 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008789 if (!btrfs_inode_cachep)
8790 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008791
David Sterba837e1972012-09-07 03:00:48 -06008792 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008793 sizeof(struct btrfs_trans_handle), 0,
8794 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008795 if (!btrfs_trans_handle_cachep)
8796 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008797
David Sterba837e1972012-09-07 03:00:48 -06008798 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008799 sizeof(struct btrfs_transaction), 0,
8800 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008801 if (!btrfs_transaction_cachep)
8802 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008803
David Sterba837e1972012-09-07 03:00:48 -06008804 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008805 sizeof(struct btrfs_path), 0,
8806 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008807 if (!btrfs_path_cachep)
8808 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008809
David Sterba837e1972012-09-07 03:00:48 -06008810 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008811 sizeof(struct btrfs_free_space), 0,
8812 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8813 if (!btrfs_free_space_cachep)
8814 goto fail;
8815
Miao Xie8ccf6f192012-10-25 09:28:04 +00008816 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8817 sizeof(struct btrfs_delalloc_work), 0,
8818 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8819 NULL);
8820 if (!btrfs_delalloc_work_cachep)
8821 goto fail;
8822
Chris Mason39279cc2007-06-12 06:35:45 -04008823 return 0;
8824fail:
8825 btrfs_destroy_cachep();
8826 return -ENOMEM;
8827}
8828
8829static int btrfs_getattr(struct vfsmount *mnt,
8830 struct dentry *dentry, struct kstat *stat)
8831{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008832 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008833 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008834 u32 blocksize = inode->i_sb->s_blocksize;
8835
Chris Mason39279cc2007-06-12 06:35:45 -04008836 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008837 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008838 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008839
8840 spin_lock(&BTRFS_I(inode)->lock);
8841 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8842 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008843 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008844 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008845 return 0;
8846}
8847
Chris Masond3977122009-01-05 21:25:51 -05008848static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8849 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008850{
8851 struct btrfs_trans_handle *trans;
8852 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008853 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008854 struct inode *new_inode = new_dentry->d_inode;
8855 struct inode *old_inode = old_dentry->d_inode;
8856 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008857 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008858 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008859 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008860 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008861
Li Zefan33345d012011-04-20 10:31:50 +08008862 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008863 return -EPERM;
8864
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008865 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008866 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008867 return -EXDEV;
8868
Li Zefan33345d012011-04-20 10:31:50 +08008869 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8870 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008871 return -ENOTEMPTY;
8872
Chris Mason39279cc2007-06-12 06:35:45 -04008873 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008874 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008875 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008876
8877
8878 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008879 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008880 new_dentry->d_name.name,
8881 new_dentry->d_name.len);
8882
8883 if (ret) {
8884 if (ret == -EEXIST) {
8885 /* we shouldn't get
8886 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308887 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008888 return ret;
8889 }
8890 } else {
8891 /* maybe -EOVERFLOW */
8892 return ret;
8893 }
8894 }
8895 ret = 0;
8896
Chris Mason5a3f23d2009-03-31 13:27:11 -04008897 /*
Chris Mason8d875f92014-08-12 10:47:42 -07008898 * we're using rename to replace one file with another. Start IO on it
8899 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04008900 */
Chris Mason8d875f92014-08-12 10:47:42 -07008901 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04008902 filemap_flush(old_inode->i_mapping);
8903
Yan, Zheng76dda932009-09-21 16:00:26 -04008904 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008905 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008906 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008907 /*
8908 * We want to reserve the absolute worst case amount of items. So if
8909 * both inodes are subvols and we need to unlink them then that would
8910 * require 4 item modifications, but if they are both normal inodes it
8911 * would require 5 item modifications, so we'll assume their normal
8912 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8913 * should cover the worst case number of items we'll modify.
8914 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008915 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008916 if (IS_ERR(trans)) {
8917 ret = PTR_ERR(trans);
8918 goto out_notrans;
8919 }
Chris Mason5f39d392007-10-15 16:14:19 -04008920
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008921 if (dest != root)
8922 btrfs_record_root_in_trans(trans, dest);
8923
Yan, Zhenga5719522009-09-24 09:17:31 -04008924 ret = btrfs_set_inode_index(new_dir, &index);
8925 if (ret)
8926 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008927
Miao Xie67de1172013-12-26 13:07:06 +08008928 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08008929 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008930 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08008931 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008932 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008933 ret = btrfs_insert_inode_ref(trans, dest,
8934 new_dentry->d_name.name,
8935 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008936 old_ino,
8937 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008938 if (ret)
8939 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008940 /*
8941 * this is an ugly little race, but the rename is required
8942 * to make sure that if we crash, the inode is either at the
8943 * old name or the new one. pinning the log transaction lets
8944 * us make sure we don't allow a log commit to come in after
8945 * we unlink the name but before we add the new name back in.
8946 */
8947 btrfs_pin_log_trans(root);
8948 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04008949
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008950 inode_inc_iversion(old_dir);
8951 inode_inc_iversion(new_dir);
8952 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008953 old_dir->i_ctime = old_dir->i_mtime = ctime;
8954 new_dir->i_ctime = new_dir->i_mtime = ctime;
8955 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008956
Chris Mason12fcfd22009-03-24 10:24:20 -04008957 if (old_dentry->d_parent != new_dentry->d_parent)
8958 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8959
Li Zefan33345d012011-04-20 10:31:50 +08008960 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008961 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8962 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8963 old_dentry->d_name.name,
8964 old_dentry->d_name.len);
8965 } else {
Al Viro92986792011-03-04 17:14:37 +00008966 ret = __btrfs_unlink_inode(trans, root, old_dir,
8967 old_dentry->d_inode,
8968 old_dentry->d_name.name,
8969 old_dentry->d_name.len);
8970 if (!ret)
8971 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008972 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008973 if (ret) {
8974 btrfs_abort_transaction(trans, root, ret);
8975 goto out_fail;
8976 }
Chris Mason39279cc2007-06-12 06:35:45 -04008977
8978 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008979 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008980 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008981 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008982 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8983 root_objectid = BTRFS_I(new_inode)->location.objectid;
8984 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8985 root_objectid,
8986 new_dentry->d_name.name,
8987 new_dentry->d_name.len);
8988 BUG_ON(new_inode->i_nlink == 0);
8989 } else {
8990 ret = btrfs_unlink_inode(trans, dest, new_dir,
8991 new_dentry->d_inode,
8992 new_dentry->d_name.name,
8993 new_dentry->d_name.len);
8994 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008995 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008996 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008997 if (ret) {
8998 btrfs_abort_transaction(trans, root, ret);
8999 goto out_fail;
9000 }
Chris Mason39279cc2007-06-12 06:35:45 -04009001 }
Josef Bacikaec74772008-07-24 12:12:38 -04009002
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009003 ret = btrfs_add_link(trans, new_dir, old_inode,
9004 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009005 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009006 if (ret) {
9007 btrfs_abort_transaction(trans, root, ret);
9008 goto out_fail;
9009 }
Chris Mason39279cc2007-06-12 06:35:45 -04009010
Miao Xie67de1172013-12-26 13:07:06 +08009011 if (old_inode->i_nlink == 1)
9012 BTRFS_I(old_inode)->dir_index = index;
9013
Li Zefan33345d012011-04-20 10:31:50 +08009014 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04009015 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00009016 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009017 btrfs_end_log_trans(root);
9018 }
Chris Mason39279cc2007-06-12 06:35:45 -04009019out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009020 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009021out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009022 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009023 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009024
Chris Mason39279cc2007-06-12 06:35:45 -04009025 return ret;
9026}
9027
Miklos Szeredi80ace852014-07-23 15:15:32 +02009028static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9029 struct inode *new_dir, struct dentry *new_dentry,
9030 unsigned int flags)
9031{
9032 if (flags & ~RENAME_NOREPLACE)
9033 return -EINVAL;
9034
9035 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
9036}
9037
Miao Xie8ccf6f192012-10-25 09:28:04 +00009038static void btrfs_run_delalloc_work(struct btrfs_work *work)
9039{
9040 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009041 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009042
9043 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9044 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009045 inode = delalloc_work->inode;
9046 if (delalloc_work->wait) {
9047 btrfs_wait_ordered_range(inode, 0, (u64)-1);
9048 } else {
9049 filemap_flush(inode->i_mapping);
9050 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9051 &BTRFS_I(inode)->runtime_flags))
9052 filemap_flush(inode->i_mapping);
9053 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00009054
9055 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009056 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009057 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009058 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009059 complete(&delalloc_work->completion);
9060}
9061
9062struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
9063 int wait, int delay_iput)
9064{
9065 struct btrfs_delalloc_work *work;
9066
9067 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
9068 if (!work)
9069 return NULL;
9070
9071 init_completion(&work->completion);
9072 INIT_LIST_HEAD(&work->list);
9073 work->inode = inode;
9074 work->wait = wait;
9075 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009076 WARN_ON_ONCE(!inode);
9077 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9078 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009079
9080 return work;
9081}
9082
9083void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9084{
9085 wait_for_completion(&work->completion);
9086 kmem_cache_free(btrfs_delalloc_work_cachep, work);
9087}
9088
Chris Masond352ac62008-09-29 15:18:18 -04009089/*
9090 * some fairly slow code that needs optimization. This walks the list
9091 * of all the inodes with pending delalloc and forces them to disk.
9092 */
Miao Xie6c255e62014-03-06 13:55:01 +08009093static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9094 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009095{
Chris Masonea8c2812008-08-04 23:17:27 -04009096 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009097 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009098 struct btrfs_delalloc_work *work, *next;
9099 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009100 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009101 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009102
Miao Xie8ccf6f192012-10-25 09:28:04 +00009103 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009104 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009105
Miao Xie573bfb72014-03-06 13:55:03 +08009106 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009107 spin_lock(&root->delalloc_lock);
9108 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009109 while (!list_empty(&splice)) {
9110 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009111 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009112
Miao Xieeb73c1b2013-05-15 07:48:22 +00009113 list_move_tail(&binode->delalloc_inodes,
9114 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009115 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009116 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009117 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009118 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009119 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009120 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009121
9122 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +02009123 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04009124 if (delay_iput)
9125 btrfs_add_delayed_iput(inode);
9126 else
9127 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009128 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009129 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009130 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009131 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009132 btrfs_queue_work(root->fs_info->flush_workers,
9133 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009134 ret++;
9135 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009136 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009137 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009138 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009139 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009140 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009141
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009142out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009143 list_for_each_entry_safe(work, next, &works, list) {
9144 list_del_init(&work->list);
9145 btrfs_wait_and_free_delalloc_work(work);
9146 }
9147
Miao Xieeb73c1b2013-05-15 07:48:22 +00009148 if (!list_empty_careful(&splice)) {
9149 spin_lock(&root->delalloc_lock);
9150 list_splice_tail(&splice, &root->delalloc_inodes);
9151 spin_unlock(&root->delalloc_lock);
9152 }
Miao Xie573bfb72014-03-06 13:55:03 +08009153 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009154 return ret;
9155}
9156
9157int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
9158{
9159 int ret;
9160
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009161 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009162 return -EROFS;
9163
Miao Xie6c255e62014-03-06 13:55:01 +08009164 ret = __start_delalloc_inodes(root, delay_iput, -1);
9165 if (ret > 0)
9166 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009167 /*
9168 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04009169 * we have to make sure the IO is actually started and that
9170 * ordered extents get created before we return
9171 */
9172 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05009173 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05009174 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04009175 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05009176 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
9177 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04009178 }
9179 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009180 return ret;
9181}
9182
Miao Xie6c255e62014-03-06 13:55:01 +08009183int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
9184 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009185{
9186 struct btrfs_root *root;
9187 struct list_head splice;
9188 int ret;
9189
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009190 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009191 return -EROFS;
9192
9193 INIT_LIST_HEAD(&splice);
9194
Miao Xie573bfb72014-03-06 13:55:03 +08009195 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009196 spin_lock(&fs_info->delalloc_root_lock);
9197 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009198 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009199 root = list_first_entry(&splice, struct btrfs_root,
9200 delalloc_root);
9201 root = btrfs_grab_fs_root(root);
9202 BUG_ON(!root);
9203 list_move_tail(&root->delalloc_root,
9204 &fs_info->delalloc_roots);
9205 spin_unlock(&fs_info->delalloc_root_lock);
9206
Miao Xie6c255e62014-03-06 13:55:01 +08009207 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009208 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009209 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009210 goto out;
9211
Miao Xie6c255e62014-03-06 13:55:01 +08009212 if (nr != -1) {
9213 nr -= ret;
9214 WARN_ON(nr < 0);
9215 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009216 spin_lock(&fs_info->delalloc_root_lock);
9217 }
9218 spin_unlock(&fs_info->delalloc_root_lock);
9219
Miao Xie6c255e62014-03-06 13:55:01 +08009220 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009221 atomic_inc(&fs_info->async_submit_draining);
9222 while (atomic_read(&fs_info->nr_async_submits) ||
9223 atomic_read(&fs_info->async_delalloc_pages)) {
9224 wait_event(fs_info->async_submit_wait,
9225 (atomic_read(&fs_info->nr_async_submits) == 0 &&
9226 atomic_read(&fs_info->async_delalloc_pages) == 0));
9227 }
9228 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009229out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009230 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009231 spin_lock(&fs_info->delalloc_root_lock);
9232 list_splice_tail(&splice, &fs_info->delalloc_roots);
9233 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009234 }
Miao Xie573bfb72014-03-06 13:55:03 +08009235 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009236 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009237}
9238
Chris Mason39279cc2007-06-12 06:35:45 -04009239static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9240 const char *symname)
9241{
9242 struct btrfs_trans_handle *trans;
9243 struct btrfs_root *root = BTRFS_I(dir)->root;
9244 struct btrfs_path *path;
9245 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009246 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009247 int err;
9248 int drop_inode = 0;
9249 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309250 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009251 int name_len;
9252 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009253 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009254 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009255 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009256
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009257 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04009258 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
9259 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009260
Josef Bacik9ed74f22009-09-11 16:12:44 -04009261 /*
9262 * 2 items for inode item and ref
9263 * 2 items for dir items
9264 * 1 item for xattr if selinux is on
9265 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04009266 trans = btrfs_start_transaction(root, 5);
9267 if (IS_ERR(trans))
9268 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009269
Li Zefan581bb052011-04-20 10:06:11 +08009270 err = btrfs_find_free_ino(root, &objectid);
9271 if (err)
9272 goto out_unlock;
9273
Josef Bacikaec74772008-07-24 12:12:38 -04009274 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08009275 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04009276 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009277 if (IS_ERR(inode)) {
9278 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009279 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009280 }
Chris Mason39279cc2007-06-12 06:35:45 -04009281
Casey Schauflerad19db72011-12-15 10:09:07 -05009282 /*
9283 * If the active LSM wants to access the inode during
9284 * d_instantiate it needs these. Smack checks to see
9285 * if the filesystem supports xattrs by looking at the
9286 * ops vector.
9287 */
9288 inode->i_fop = &btrfs_file_operations;
9289 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009290 inode->i_mapping->a_ops = &btrfs_aops;
9291 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9292 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9293
9294 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9295 if (err)
9296 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05009297
Josef Bacika1b075d2010-11-19 20:36:11 +00009298 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04009299 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07009300 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009301
9302 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009303 if (!path) {
9304 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07009305 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009306 }
Li Zefan33345d012011-04-20 10:31:50 +08009307 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009308 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009309 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009310 datasize = btrfs_file_extent_calc_inline_size(name_len);
9311 err = btrfs_insert_empty_item(trans, root, path, &key,
9312 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009313 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009314 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07009315 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04009316 }
Chris Mason5f39d392007-10-15 16:14:19 -04009317 leaf = path->nodes[0];
9318 ei = btrfs_item_ptr(leaf, path->slots[0],
9319 struct btrfs_file_extent_item);
9320 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9321 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009322 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009323 btrfs_set_file_extent_encryption(leaf, ei, 0);
9324 btrfs_set_file_extent_compression(leaf, ei, 0);
9325 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9326 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9327
Chris Mason39279cc2007-06-12 06:35:45 -04009328 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009329 write_extent_buffer(leaf, symname, ptr, name_len);
9330 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009331 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009332
Chris Mason39279cc2007-06-12 06:35:45 -04009333 inode->i_op = &btrfs_symlink_inode_operations;
9334 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04009335 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04009336 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009337 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009338 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07009339 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04009340 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07009341 goto out_unlock_inode;
9342 }
9343
9344 unlock_new_inode(inode);
9345 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009346
9347out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009348 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04009349 if (drop_inode) {
9350 inode_dec_link_count(inode);
9351 iput(inode);
9352 }
Liu Bob53d3f52012-11-14 14:34:34 +00009353 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04009354 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07009355
9356out_unlock_inode:
9357 drop_inode = 1;
9358 unlock_new_inode(inode);
9359 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04009360}
Chris Mason16432982008-04-10 10:23:21 -04009361
Josef Bacik0af3d002010-06-21 14:48:16 -04009362static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9363 u64 start, u64 num_bytes, u64 min_size,
9364 loff_t actual_len, u64 *alloc_hint,
9365 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009366{
Josef Bacik5dc562c2012-08-17 13:14:17 -04009367 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9368 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009369 struct btrfs_root *root = BTRFS_I(inode)->root;
9370 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009371 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009372 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009373 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04009374 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009375 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04009376
Josef Bacik0af3d002010-06-21 14:48:16 -04009377 if (trans)
9378 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009379 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009380 if (own_trans) {
9381 trans = btrfs_start_transaction(root, 3);
9382 if (IS_ERR(trans)) {
9383 ret = PTR_ERR(trans);
9384 break;
9385 }
Yan Zhengd899e052008-10-30 14:25:28 -04009386 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009387
Chris Mason154ea282013-03-05 11:11:26 -05009388 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
9389 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04009390 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08009391 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009392 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009393 if (own_trans)
9394 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009395 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009396 }
9397
Yan Zhengd899e052008-10-30 14:25:28 -04009398 ret = insert_reserved_file_extent(trans, inode,
9399 cur_offset, ins.objectid,
9400 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009401 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009402 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009403 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04009404 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009405 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009406 btrfs_abort_transaction(trans, root, ret);
9407 if (own_trans)
9408 btrfs_end_transaction(trans, root);
9409 break;
9410 }
Chris Masona1ed8352009-09-11 12:27:37 -04009411 btrfs_drop_extent_cache(inode, cur_offset,
9412 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009413
Josef Bacik5dc562c2012-08-17 13:14:17 -04009414 em = alloc_extent_map();
9415 if (!em) {
9416 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9417 &BTRFS_I(inode)->runtime_flags);
9418 goto next;
9419 }
9420
9421 em->start = cur_offset;
9422 em->orig_start = cur_offset;
9423 em->len = ins.offset;
9424 em->block_start = ins.objectid;
9425 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009426 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009427 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009428 em->bdev = root->fs_info->fs_devices->latest_bdev;
9429 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9430 em->generation = trans->transid;
9431
9432 while (1) {
9433 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009434 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009435 write_unlock(&em_tree->lock);
9436 if (ret != -EEXIST)
9437 break;
9438 btrfs_drop_extent_cache(inode, cur_offset,
9439 cur_offset + ins.offset - 1,
9440 0);
9441 }
9442 free_extent_map(em);
9443next:
Yan Zhengd899e052008-10-30 14:25:28 -04009444 num_bytes -= ins.offset;
9445 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009446 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009447
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009448 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009449 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009450 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009451 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009452 (actual_len > inode->i_size) &&
9453 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009454 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009455 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009456 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009457 i_size = cur_offset;
9458 i_size_write(inode, i_size);
9459 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009460 }
9461
Yan Zhengd899e052008-10-30 14:25:28 -04009462 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009463
9464 if (ret) {
9465 btrfs_abort_transaction(trans, root, ret);
9466 if (own_trans)
9467 btrfs_end_transaction(trans, root);
9468 break;
9469 }
Yan Zhengd899e052008-10-30 14:25:28 -04009470
Josef Bacik0af3d002010-06-21 14:48:16 -04009471 if (own_trans)
9472 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009473 }
Yan Zhengd899e052008-10-30 14:25:28 -04009474 return ret;
9475}
9476
Josef Bacik0af3d002010-06-21 14:48:16 -04009477int btrfs_prealloc_file_range(struct inode *inode, int mode,
9478 u64 start, u64 num_bytes, u64 min_size,
9479 loff_t actual_len, u64 *alloc_hint)
9480{
9481 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9482 min_size, actual_len, alloc_hint,
9483 NULL);
9484}
9485
9486int btrfs_prealloc_file_range_trans(struct inode *inode,
9487 struct btrfs_trans_handle *trans, int mode,
9488 u64 start, u64 num_bytes, u64 min_size,
9489 loff_t actual_len, u64 *alloc_hint)
9490{
9491 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9492 min_size, actual_len, alloc_hint, trans);
9493}
9494
Chris Masone6dcd2d2008-07-17 12:53:50 -04009495static int btrfs_set_page_dirty(struct page *page)
9496{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009497 return __set_page_dirty_nobuffers(page);
9498}
9499
Al Viro10556cb2011-06-20 19:28:19 -04009500static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009501{
Li Zefanb83cc962010-12-20 16:04:08 +08009502 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009503 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009504
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009505 if (mask & MAY_WRITE &&
9506 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9507 if (btrfs_root_readonly(root))
9508 return -EROFS;
9509 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9510 return -EACCES;
9511 }
Al Viro2830ba72011-06-20 19:16:29 -04009512 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009513}
Chris Mason39279cc2007-06-12 06:35:45 -04009514
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009515static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9516{
9517 struct btrfs_trans_handle *trans;
9518 struct btrfs_root *root = BTRFS_I(dir)->root;
9519 struct inode *inode = NULL;
9520 u64 objectid;
9521 u64 index;
9522 int ret = 0;
9523
9524 /*
9525 * 5 units required for adding orphan entry
9526 */
9527 trans = btrfs_start_transaction(root, 5);
9528 if (IS_ERR(trans))
9529 return PTR_ERR(trans);
9530
9531 ret = btrfs_find_free_ino(root, &objectid);
9532 if (ret)
9533 goto out;
9534
9535 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9536 btrfs_ino(dir), objectid, mode, &index);
9537 if (IS_ERR(inode)) {
9538 ret = PTR_ERR(inode);
9539 inode = NULL;
9540 goto out;
9541 }
9542
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009543 inode->i_fop = &btrfs_file_operations;
9544 inode->i_op = &btrfs_file_inode_operations;
9545
9546 inode->i_mapping->a_ops = &btrfs_aops;
9547 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9548 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9549
Chris Masonb0d5d102014-09-08 13:08:51 -07009550 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9551 if (ret)
9552 goto out_inode;
9553
9554 ret = btrfs_update_inode(trans, root, inode);
9555 if (ret)
9556 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009557 ret = btrfs_orphan_add(trans, inode);
9558 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009559 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009560
Filipe Manana5762b5c2014-08-01 00:10:32 +01009561 /*
9562 * We set number of links to 0 in btrfs_new_inode(), and here we set
9563 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9564 * through:
9565 *
9566 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9567 */
9568 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009569 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009570 d_tmpfile(dentry, inode);
9571 mark_inode_dirty(inode);
9572
9573out:
9574 btrfs_end_transaction(trans, root);
9575 if (ret)
9576 iput(inode);
9577 btrfs_balance_delayed_items(root);
9578 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009579 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009580
9581out_inode:
9582 unlock_new_inode(inode);
9583 goto out;
9584
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009585}
9586
Filipe Mananab38ef712014-11-13 17:01:45 +00009587/* Inspired by filemap_check_errors() */
9588int btrfs_inode_check_errors(struct inode *inode)
9589{
9590 int ret = 0;
9591
9592 if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) &&
9593 test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags))
9594 ret = -ENOSPC;
9595 if (test_bit(AS_EIO, &inode->i_mapping->flags) &&
9596 test_and_clear_bit(AS_EIO, &inode->i_mapping->flags))
9597 ret = -EIO;
9598
9599 return ret;
9600}
9601
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009602static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009603 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009604 .lookup = btrfs_lookup,
9605 .create = btrfs_create,
9606 .unlink = btrfs_unlink,
9607 .link = btrfs_link,
9608 .mkdir = btrfs_mkdir,
9609 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +02009610 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -04009611 .symlink = btrfs_symlink,
9612 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009613 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009614 .setxattr = btrfs_setxattr,
9615 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009616 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009617 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009618 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009619 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009620 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009621 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009622 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009623};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009624static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009625 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009626 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009627 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009628 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009629 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009630};
Yan, Zheng76dda932009-09-21 16:00:26 -04009631
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009632static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009633 .llseek = generic_file_llseek,
9634 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009635 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009636 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009637#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009638 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009639#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009640 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009641 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009642};
9643
Chris Masond1310b22008-01-24 16:13:08 -05009644static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009645 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009646 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009647 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009648 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009649 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009650 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009651 .set_bit_hook = btrfs_set_bit_hook,
9652 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009653 .merge_extent_hook = btrfs_merge_extent_hook,
9654 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009655};
9656
Chris Mason35054392009-01-21 13:11:13 -05009657/*
9658 * btrfs doesn't support the bmap operation because swapfiles
9659 * use bmap to make a mapping of extents in the file. They assume
9660 * these extents won't change over the life of the file and they
9661 * use the bmap result to do IO directly to the drive.
9662 *
9663 * the btrfs bmap call would return logical addresses that aren't
9664 * suitable for IO and they also will change frequently as COW
9665 * operations happen. So, swapfile + btrfs == corruption.
9666 *
9667 * For now we're avoiding this by dropping bmap.
9668 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009669static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009670 .readpage = btrfs_readpage,
9671 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04009672 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009673 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009674 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009675 .invalidatepage = btrfs_invalidatepage,
9676 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009677 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009678 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009679};
9680
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009681static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009682 .readpage = btrfs_readpage,
9683 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009684 .invalidatepage = btrfs_invalidatepage,
9685 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009686};
9687
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009688static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009689 .getattr = btrfs_getattr,
9690 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009691 .setxattr = btrfs_setxattr,
9692 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009693 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009694 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009695 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009696 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009697 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009698 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009699 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009700};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009701static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009702 .getattr = btrfs_getattr,
9703 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009704 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009705 .setxattr = btrfs_setxattr,
9706 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009707 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009708 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009709 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009710 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009711 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04009712};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009713static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009714 .readlink = generic_readlink,
9715 .follow_link = page_follow_link_light,
9716 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009717 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009718 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009719 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009720 .setxattr = btrfs_setxattr,
9721 .getxattr = btrfs_getxattr,
9722 .listxattr = btrfs_listxattr,
9723 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009724 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009725};
Yan, Zheng76dda932009-09-21 16:00:26 -04009726
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009727const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009728 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009729 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009730};