blob: 75c6de26405cd561420e951c7a6608bd27b5e158 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070035#include <linux/aio.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050037#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040038#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040039#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020041#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050042#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000043#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050044#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040045#include <linux/posix_acl_xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040060#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061#include "props.h"
Chris Mason39279cc2007-06-12 06:35:45 -040062
63struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080064 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040065 struct btrfs_root *root;
66};
67
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070068static const struct inode_operations btrfs_dir_inode_operations;
69static const struct inode_operations btrfs_symlink_inode_operations;
70static const struct inode_operations btrfs_dir_ro_inode_operations;
71static const struct inode_operations btrfs_special_inode_operations;
72static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070073static const struct address_space_operations btrfs_aops;
74static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070075static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050076static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040077
78static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000079static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040080struct kmem_cache *btrfs_trans_handle_cachep;
81struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040082struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050083struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040084
85#define S_SHIFT 12
86static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
87 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
88 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
89 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
90 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
91 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
92 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
93 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
94};
95
Eric Sandeen3972f262013-01-12 02:57:22 +000096static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050097static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040098static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050099static noinline int cow_file_range(struct inode *inode,
100 struct page *locked_page,
101 u64 start, u64 end, int *page_started,
102 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400103static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
104 u64 len, u64 orig_start,
105 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400106 u64 orig_block_len, u64 ram_bytes,
107 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400108
Eric Sandeen48a3b632013-04-25 20:41:01 +0000109static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400110
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000111static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500112 struct inode *inode, struct inode *dir,
113 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500114{
115 int err;
116
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000117 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500118 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500119 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500120 return err;
121}
122
Chris Masond352ac62008-09-29 15:18:18 -0400123/*
Chris Masonc8b97812008-10-29 14:49:59 -0400124 * this does all the hard work for inserting an inline extent into
125 * the btree. The caller should have done a btrfs_drop_extents so that
126 * no overlapping inline items exist in the btree
127 */
Chris Mason40f76582014-05-21 13:35:51 -0700128static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000129 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400130 struct btrfs_root *root, struct inode *inode,
131 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000132 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400133 struct page **compressed_pages)
134{
Chris Masonc8b97812008-10-29 14:49:59 -0400135 struct extent_buffer *leaf;
136 struct page *page = NULL;
137 char *kaddr;
138 unsigned long ptr;
139 struct btrfs_file_extent_item *ei;
140 int err = 0;
141 int ret;
142 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400143 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400144
Li Zefanfe3f5662011-03-28 08:30:38 +0000145 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400146 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400147
Chris Masonc8b97812008-10-29 14:49:59 -0400148 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000149
150 if (!extent_inserted) {
151 struct btrfs_key key;
152 size_t datasize;
153
154 key.objectid = btrfs_ino(inode);
155 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200156 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000157
158 datasize = btrfs_file_extent_calc_inline_size(cur_size);
159 path->leave_spinning = 1;
160 ret = btrfs_insert_empty_item(trans, root, path, &key,
161 datasize);
162 if (ret) {
163 err = ret;
164 goto fail;
165 }
Chris Masonc8b97812008-10-29 14:49:59 -0400166 }
167 leaf = path->nodes[0];
168 ei = btrfs_item_ptr(leaf, path->slots[0],
169 struct btrfs_file_extent_item);
170 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
171 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
172 btrfs_set_file_extent_encryption(leaf, ei, 0);
173 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
174 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
175 ptr = btrfs_file_extent_inline_start(ei);
176
Li Zefan261507a02010-12-17 14:21:50 +0800177 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400178 struct page *cpage;
179 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500180 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400181 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500182 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400183 PAGE_CACHE_SIZE);
184
Cong Wang7ac687d2011-11-25 23:14:28 +0800185 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400186 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800187 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400188
189 i++;
190 ptr += cur_size;
191 compressed_size -= cur_size;
192 }
193 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800194 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400195 } else {
196 page = find_get_page(inode->i_mapping,
197 start >> PAGE_CACHE_SHIFT);
198 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800199 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400200 offset = start & (PAGE_CACHE_SIZE - 1);
201 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800202 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400203 page_cache_release(page);
204 }
205 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000206 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400207
Yan, Zhengc2167752009-11-12 09:34:21 +0000208 /*
209 * we're an inline extent, so nobody can
210 * extend the file past i_size without locking
211 * a page we already have locked.
212 *
213 * We must do any isize and inode updates
214 * before we unlock the pages. Otherwise we
215 * could end up racing with unlink.
216 */
Chris Masonc8b97812008-10-29 14:49:59 -0400217 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100218 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000219
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100220 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400221fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400222 return err;
223}
224
225
226/*
227 * conditionally insert an inline extent into the file. This
228 * does the checks required to make sure the data is small enough
229 * to fit as an inline extent.
230 */
Josef Bacik00361582013-08-14 14:02:47 -0400231static noinline int cow_file_range_inline(struct btrfs_root *root,
232 struct inode *inode, u64 start,
233 u64 end, size_t compressed_size,
234 int compress_type,
235 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400236{
Josef Bacik00361582013-08-14 14:02:47 -0400237 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400238 u64 isize = i_size_read(inode);
239 u64 actual_end = min(end + 1, isize);
240 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000241 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400242 u64 data_len = inline_len;
243 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000244 struct btrfs_path *path;
245 int extent_inserted = 0;
246 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400247
248 if (compressed_size)
249 data_len = compressed_size;
250
251 if (start > 0 ||
Wang Shilong354877b2014-07-17 11:44:11 +0800252 actual_end > PAGE_CACHE_SIZE ||
253 data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400254 (!compressed_size &&
255 (actual_end & (root->sectorsize - 1)) == 0) ||
256 end + 1 < isize ||
257 data_len > root->fs_info->max_inline) {
258 return 1;
259 }
260
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000261 path = btrfs_alloc_path();
262 if (!path)
263 return -ENOMEM;
264
Josef Bacik00361582013-08-14 14:02:47 -0400265 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000266 if (IS_ERR(trans)) {
267 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400268 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000269 }
Josef Bacik00361582013-08-14 14:02:47 -0400270 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
271
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000272 if (compressed_size && compressed_pages)
273 extent_item_size = btrfs_file_extent_calc_inline_size(
274 compressed_size);
275 else
276 extent_item_size = btrfs_file_extent_calc_inline_size(
277 inline_len);
278
279 ret = __btrfs_drop_extents(trans, root, inode, path,
280 start, aligned_end, NULL,
281 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400282 if (ret) {
283 btrfs_abort_transaction(trans, root, ret);
284 goto out;
285 }
Chris Masonc8b97812008-10-29 14:49:59 -0400286
287 if (isize > actual_end)
288 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000289 ret = insert_inline_extent(trans, path, extent_inserted,
290 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400291 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000292 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400293 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100294 btrfs_abort_transaction(trans, root, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400295 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400296 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400297 ret = 1;
298 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100299 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400300
Josef Bacikbdc20e62013-02-28 13:23:38 -0500301 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400302 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400303 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400304out:
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000305 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400306 btrfs_end_transaction(trans, root);
307 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400308}
309
Chris Mason771ed682008-11-06 22:02:51 -0500310struct async_extent {
311 u64 start;
312 u64 ram_size;
313 u64 compressed_size;
314 struct page **pages;
315 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800316 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500317 struct list_head list;
318};
319
320struct async_cow {
321 struct inode *inode;
322 struct btrfs_root *root;
323 struct page *locked_page;
324 u64 start;
325 u64 end;
326 struct list_head extents;
327 struct btrfs_work work;
328};
329
330static noinline int add_async_extent(struct async_cow *cow,
331 u64 start, u64 ram_size,
332 u64 compressed_size,
333 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800334 unsigned long nr_pages,
335 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500336{
337 struct async_extent *async_extent;
338
339 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100340 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500341 async_extent->start = start;
342 async_extent->ram_size = ram_size;
343 async_extent->compressed_size = compressed_size;
344 async_extent->pages = pages;
345 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800346 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500347 list_add_tail(&async_extent->list, &cow->extents);
348 return 0;
349}
350
Wang Shilongf79707b2014-07-17 11:44:09 +0800351static inline int inode_need_compress(struct inode *inode)
352{
353 struct btrfs_root *root = BTRFS_I(inode)->root;
354
355 /* force compress */
356 if (btrfs_test_opt(root, FORCE_COMPRESS))
357 return 1;
358 /* bad compression ratios */
359 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
360 return 0;
361 if (btrfs_test_opt(root, COMPRESS) ||
362 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
363 BTRFS_I(inode)->force_compress)
364 return 1;
365 return 0;
366}
367
Chris Masonc8b97812008-10-29 14:49:59 -0400368/*
Chris Mason771ed682008-11-06 22:02:51 -0500369 * we create compressed extents in two phases. The first
370 * phase compresses a range of pages that have already been
371 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400372 *
Chris Mason771ed682008-11-06 22:02:51 -0500373 * This is done inside an ordered work queue, and the compression
374 * is spread across many cpus. The actual IO submission is step
375 * two, and the ordered work queue takes care of making sure that
376 * happens in the same order things were put onto the queue by
377 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400378 *
Chris Mason771ed682008-11-06 22:02:51 -0500379 * If this code finds it can't get good compression, it puts an
380 * entry onto the work queue to write the uncompressed bytes. This
381 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300382 * are written in the same order that the flusher thread sent them
383 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400384 */
Chris Mason771ed682008-11-06 22:02:51 -0500385static noinline int compress_file_range(struct inode *inode,
386 struct page *locked_page,
387 u64 start, u64 end,
388 struct async_cow *async_cow,
389 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400390{
391 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400392 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400393 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400394 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500395 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400396 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400397 struct page **pages = NULL;
398 unsigned long nr_pages;
399 unsigned long nr_pages_ret = 0;
400 unsigned long total_compressed = 0;
401 unsigned long total_in = 0;
402 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500403 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400404 int i;
405 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800406 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400407 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400408
Liu Bo4cb13e52012-03-29 09:57:45 -0400409 /* if this is a small write inside eof, kick off a defrag */
410 if ((end - start + 1) < 16 * 1024 &&
411 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400412 btrfs_add_inode_defrag(NULL, inode);
413
Wang Shilong68bb4622014-04-01 18:01:42 +0800414 /*
415 * skip compression for a small file range(<=blocksize) that
416 * isn't an inline extent, since it dosen't save disk space at all.
417 */
418 if ((end - start + 1) <= blocksize &&
419 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
420 goto cleanup_and_bail_uncompressed;
421
Chris Mason42dc7ba2008-12-15 11:44:56 -0500422 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400423again:
424 will_compress = 0;
425 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
426 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
427
Chris Masonf03d9302009-02-04 09:31:06 -0500428 /*
429 * we don't want to send crud past the end of i_size through
430 * compression, that's just a waste of CPU time. So, if the
431 * end of the file is before the start of our current
432 * requested range of bytes, we bail out to the uncompressed
433 * cleanup code that can deal with all of this.
434 *
435 * It isn't really the fastest way to fix things, but this is a
436 * very uncommon corner.
437 */
438 if (actual_end <= start)
439 goto cleanup_and_bail_uncompressed;
440
Chris Masonc8b97812008-10-29 14:49:59 -0400441 total_compressed = actual_end - start;
442
443 /* we want to make sure that amount of ram required to uncompress
444 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500445 * of a compressed extent to 128k. This is a crucial number
446 * because it also controls how easily we can spread reads across
447 * cpus for decompression.
448 *
449 * We also want to make sure the amount of IO required to do
450 * a random read is reasonably small, so we limit the size of
451 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400452 */
453 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000454 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500455 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400456 total_in = 0;
457 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400458
Chris Mason771ed682008-11-06 22:02:51 -0500459 /*
460 * we do compression for mount -o compress and when the
461 * inode has not been flagged as nocompress. This flag can
462 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400463 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800464 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400465 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400466 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800467 if (!pages) {
468 /* just bail out to the uncompressed code */
469 goto cont;
470 }
Chris Mason179e29e2007-11-01 11:28:41 -0400471
Li Zefan261507a02010-12-17 14:21:50 +0800472 if (BTRFS_I(inode)->force_compress)
473 compress_type = BTRFS_I(inode)->force_compress;
474
Chris Mason4adaa612013-03-26 13:07:00 -0400475 /*
476 * we need to call clear_page_dirty_for_io on each
477 * page in the range. Otherwise applications with the file
478 * mmap'd can wander in and change the page contents while
479 * we are compressing them.
480 *
481 * If the compression fails for any reason, we set the pages
482 * dirty again later on.
483 */
484 extent_range_clear_dirty_for_io(inode, start, end);
485 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800486 ret = btrfs_compress_pages(compress_type,
487 inode->i_mapping, start,
488 total_compressed, pages,
489 nr_pages, &nr_pages_ret,
490 &total_in,
491 &total_compressed,
492 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400493
494 if (!ret) {
495 unsigned long offset = total_compressed &
496 (PAGE_CACHE_SIZE - 1);
497 struct page *page = pages[nr_pages_ret - 1];
498 char *kaddr;
499
500 /* zero the tail end of the last page, we might be
501 * sending it down to disk
502 */
503 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800504 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400505 memset(kaddr + offset, 0,
506 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800507 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400508 }
509 will_compress = 1;
510 }
511 }
Li Zefan560f7d72011-09-08 10:22:01 +0800512cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400513 if (start == 0) {
514 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500515 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400516 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500517 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400518 */
Josef Bacik00361582013-08-14 14:02:47 -0400519 ret = cow_file_range_inline(root, inode, start, end,
520 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400521 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500522 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400523 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000524 total_compressed,
525 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400526 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100527 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400528 unsigned long clear_flags = EXTENT_DELALLOC |
529 EXTENT_DEFRAG;
530 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
531
Chris Mason771ed682008-11-06 22:02:51 -0500532 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100533 * inline extent creation worked or returned error,
534 * we don't need to create any more async work items.
535 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500536 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400537 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400538 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400539 PAGE_CLEAR_DIRTY |
540 PAGE_SET_WRITEBACK |
541 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400542 goto free_pages_out;
543 }
544 }
545
546 if (will_compress) {
547 /*
548 * we aren't doing an inline extent round the compressed size
549 * up to a block size boundary so the allocator does sane
550 * things
551 */
Qu Wenruofda28322013-02-26 08:10:22 +0000552 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400553
554 /*
555 * one last check to make sure the compression is really a
556 * win, compare the page count read with the blocks on disk
557 */
Qu Wenruofda28322013-02-26 08:10:22 +0000558 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400559 if (total_compressed >= total_in) {
560 will_compress = 0;
561 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400562 num_bytes = total_in;
563 }
564 }
565 if (!will_compress && pages) {
566 /*
567 * the compression code ran but failed to make things smaller,
568 * free any pages it allocated and our page pointer array
569 */
570 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400571 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400572 page_cache_release(pages[i]);
573 }
574 kfree(pages);
575 pages = NULL;
576 total_compressed = 0;
577 nr_pages_ret = 0;
578
579 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500580 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
581 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500582 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500583 }
Chris Masonc8b97812008-10-29 14:49:59 -0400584 }
Chris Mason771ed682008-11-06 22:02:51 -0500585 if (will_compress) {
586 *num_added += 1;
587
588 /* the async work queues will take care of doing actual
589 * allocation on disk for these compressed pages,
590 * and will submit them to the elevator.
591 */
592 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800593 total_compressed, pages, nr_pages_ret,
594 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500595
Yan, Zheng24ae6362010-12-06 07:02:36 +0000596 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500597 start += num_bytes;
598 pages = NULL;
599 cond_resched();
600 goto again;
601 }
602 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500603cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500604 /*
605 * No compression, but we still need to write the pages in
606 * the file we've been given so far. redirty the locked
607 * page if it corresponds to our extent and set things up
608 * for the async work queue to run cow_file_range to do
609 * the normal delalloc dance
610 */
611 if (page_offset(locked_page) >= start &&
612 page_offset(locked_page) <= end) {
613 __set_page_dirty_nobuffers(locked_page);
614 /* unlocked later on in the async handlers */
615 }
Chris Mason4adaa612013-03-26 13:07:00 -0400616 if (redirty)
617 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800618 add_async_extent(async_cow, start, end - start + 1,
619 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500620 *num_added += 1;
621 }
622
623out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100624 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500625
626free_pages_out:
627 for (i = 0; i < nr_pages_ret; i++) {
628 WARN_ON(pages[i]->mapping);
629 page_cache_release(pages[i]);
630 }
Chris Masond3977122009-01-05 21:25:51 -0500631 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500632
633 goto out;
634}
635
636/*
637 * phase two of compressed writeback. This is the ordered portion
638 * of the code, which only gets called in the order the work was
639 * queued. We walk all the async extents created by compress_file_range
640 * and send them down to the disk.
641 */
642static noinline int submit_compressed_extents(struct inode *inode,
643 struct async_cow *async_cow)
644{
645 struct async_extent *async_extent;
646 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500647 struct btrfs_key ins;
648 struct extent_map *em;
649 struct btrfs_root *root = BTRFS_I(inode)->root;
650 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
651 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500652 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500653
654 if (list_empty(&async_cow->extents))
655 return 0;
656
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500657again:
Chris Masond3977122009-01-05 21:25:51 -0500658 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500659 async_extent = list_entry(async_cow->extents.next,
660 struct async_extent, list);
661 list_del(&async_extent->list);
662
663 io_tree = &BTRFS_I(inode)->io_tree;
664
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500665retry:
Chris Mason771ed682008-11-06 22:02:51 -0500666 /* did the compression code fall back to uncompressed IO? */
667 if (!async_extent->pages) {
668 int page_started = 0;
669 unsigned long nr_written = 0;
670
671 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000672 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100673 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500674
675 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500676 ret = cow_file_range(inode, async_cow->locked_page,
677 async_extent->start,
678 async_extent->start +
679 async_extent->ram_size - 1,
680 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500681
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100682 /* JDM XXX */
683
Chris Mason771ed682008-11-06 22:02:51 -0500684 /*
685 * if page_started, cow_file_range inserted an
686 * inline extent and took care of all the unlocking
687 * and IO for us. Otherwise, we need to submit
688 * all those pages down to the drive.
689 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500690 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500691 extent_write_locked_range(io_tree,
692 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500693 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500694 async_extent->ram_size - 1,
695 btrfs_get_extent,
696 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500697 else if (ret)
698 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500699 kfree(async_extent);
700 cond_resched();
701 continue;
702 }
703
704 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100705 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500706
Josef Bacik00361582013-08-14 14:02:47 -0400707 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500708 async_extent->compressed_size,
709 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800710 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500711 if (ret) {
712 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500713
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500714 for (i = 0; i < async_extent->nr_pages; i++) {
715 WARN_ON(async_extent->pages[i]->mapping);
716 page_cache_release(async_extent->pages[i]);
717 }
718 kfree(async_extent->pages);
719 async_extent->nr_pages = 0;
720 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500721
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400722 if (ret == -ENOSPC) {
723 unlock_extent(io_tree, async_extent->start,
724 async_extent->start +
725 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800726
727 /*
728 * we need to redirty the pages if we decide to
729 * fallback to uncompressed IO, otherwise we
730 * will not submit these pages down to lower
731 * layers.
732 */
733 extent_range_redirty_for_io(inode,
734 async_extent->start,
735 async_extent->start +
736 async_extent->ram_size - 1);
737
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100738 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400739 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500740 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500741 }
742
Yan, Zhengc2167752009-11-12 09:34:21 +0000743 /*
744 * here we're doing allocation and writeback of the
745 * compressed pages
746 */
747 btrfs_drop_extent_cache(inode, async_extent->start,
748 async_extent->start +
749 async_extent->ram_size - 1, 0);
750
David Sterba172ddd62011-04-21 00:48:27 +0200751 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000752 if (!em) {
753 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500754 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000755 }
Chris Mason771ed682008-11-06 22:02:51 -0500756 em->start = async_extent->start;
757 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500758 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400759 em->mod_start = em->start;
760 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500761
762 em->block_start = ins.objectid;
763 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500764 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400765 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500766 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800767 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500768 set_bit(EXTENT_FLAG_PINNED, &em->flags);
769 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400770 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500771
Chris Masond3977122009-01-05 21:25:51 -0500772 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400773 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400774 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400775 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500776 if (ret != -EEXIST) {
777 free_extent_map(em);
778 break;
779 }
780 btrfs_drop_extent_cache(inode, async_extent->start,
781 async_extent->start +
782 async_extent->ram_size - 1, 0);
783 }
784
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500785 if (ret)
786 goto out_free_reserve;
787
Li Zefan261507a02010-12-17 14:21:50 +0800788 ret = btrfs_add_ordered_extent_compress(inode,
789 async_extent->start,
790 ins.objectid,
791 async_extent->ram_size,
792 ins.offset,
793 BTRFS_ORDERED_COMPRESSED,
794 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100795 if (ret) {
796 btrfs_drop_extent_cache(inode, async_extent->start,
797 async_extent->start +
798 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500799 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100800 }
Chris Mason771ed682008-11-06 22:02:51 -0500801
Chris Mason771ed682008-11-06 22:02:51 -0500802 /*
803 * clear dirty, set writeback and unlock the pages.
804 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400805 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400806 async_extent->start +
807 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400808 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
809 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400810 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500811 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500812 async_extent->start,
813 async_extent->ram_size,
814 ins.objectid,
815 ins.offset, async_extent->pages,
816 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500817 alloc_hint = ins.objectid + ins.offset;
818 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500819 if (ret)
820 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500821 cond_resched();
822 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100823 ret = 0;
824out:
825 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500826out_free_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +0800827 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100828out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400829 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500830 async_extent->start +
831 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400832 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400833 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
834 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
835 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100836 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500837 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500838}
839
Josef Bacik4b46fce2010-05-23 11:00:55 -0400840static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
841 u64 num_bytes)
842{
843 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
844 struct extent_map *em;
845 u64 alloc_hint = 0;
846
847 read_lock(&em_tree->lock);
848 em = search_extent_mapping(em_tree, start, num_bytes);
849 if (em) {
850 /*
851 * if block start isn't an actual block number then find the
852 * first block in this inode and use that as a hint. If that
853 * block is also bogus then just don't worry about it.
854 */
855 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
856 free_extent_map(em);
857 em = search_extent_mapping(em_tree, 0, 0);
858 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
859 alloc_hint = em->block_start;
860 if (em)
861 free_extent_map(em);
862 } else {
863 alloc_hint = em->block_start;
864 free_extent_map(em);
865 }
866 }
867 read_unlock(&em_tree->lock);
868
869 return alloc_hint;
870}
871
Chris Mason771ed682008-11-06 22:02:51 -0500872/*
873 * when extent_io.c finds a delayed allocation range in the file,
874 * the call backs end up in this code. The basic idea is to
875 * allocate extents on disk for the range, and create ordered data structs
876 * in ram to track those extents.
877 *
878 * locked_page is the page that writepage had locked already. We use
879 * it to make sure we don't do extra locks or unlocks.
880 *
881 * *page_started is set to one if we unlock locked_page and do everything
882 * required to start IO on it. It may be clean and already done with
883 * IO when we return.
884 */
Josef Bacik00361582013-08-14 14:02:47 -0400885static noinline int cow_file_range(struct inode *inode,
886 struct page *locked_page,
887 u64 start, u64 end, int *page_started,
888 unsigned long *nr_written,
889 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500890{
Josef Bacik00361582013-08-14 14:02:47 -0400891 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500892 u64 alloc_hint = 0;
893 u64 num_bytes;
894 unsigned long ram_size;
895 u64 disk_num_bytes;
896 u64 cur_alloc_size;
897 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500898 struct btrfs_key ins;
899 struct extent_map *em;
900 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
901 int ret = 0;
902
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400903 if (btrfs_is_free_space_inode(inode)) {
904 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500905 ret = -EINVAL;
906 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400907 }
Chris Mason771ed682008-11-06 22:02:51 -0500908
Qu Wenruofda28322013-02-26 08:10:22 +0000909 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500910 num_bytes = max(blocksize, num_bytes);
911 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500912
Chris Mason4cb53002011-05-24 15:35:30 -0400913 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400914 if (num_bytes < 64 * 1024 &&
915 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400916 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400917
Chris Mason771ed682008-11-06 22:02:51 -0500918 if (start == 0) {
919 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400920 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
921 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500922 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400923 extent_clear_unlock_delalloc(inode, start, end, NULL,
924 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400925 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400926 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
927 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000928
Chris Mason771ed682008-11-06 22:02:51 -0500929 *nr_written = *nr_written +
930 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
931 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500932 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100933 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100934 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500935 }
936 }
Chris Masonc8b97812008-10-29 14:49:59 -0400937
938 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200939 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400940
Josef Bacik4b46fce2010-05-23 11:00:55 -0400941 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400942 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400943
Chris Masond3977122009-01-05 21:25:51 -0500944 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400945 unsigned long op;
946
Josef Bacik287a0ab2010-03-19 18:07:23 +0000947 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400948 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500949 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800950 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400951 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100952 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500953
David Sterba172ddd62011-04-21 00:48:27 +0200954 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000955 if (!em) {
956 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000957 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000958 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400959 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500960 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500961 ram_size = ins.offset;
962 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400963 em->mod_start = em->start;
964 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400965
Chris Masone6dcd2d2008-07-17 12:53:50 -0400966 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400967 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500968 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400969 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400970 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400971 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400972 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400973
Chris Masond3977122009-01-05 21:25:51 -0500974 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400975 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400976 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400977 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400978 if (ret != -EEXIST) {
979 free_extent_map(em);
980 break;
981 }
982 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400983 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400984 }
Liu Boace68ba2013-04-22 10:53:47 +0000985 if (ret)
986 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400987
Chris Mason98d20f62008-04-14 09:46:10 -0400988 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400989 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500990 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000991 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +0100992 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -0400993
Yan Zheng17d217f2008-12-12 10:03:38 -0500994 if (root->root_key.objectid ==
995 BTRFS_DATA_RELOC_TREE_OBJECTID) {
996 ret = btrfs_reloc_clone_csums(inode, start,
997 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -0400998 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +0100999 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001000 }
1001
Chris Masond3977122009-01-05 21:25:51 -05001002 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001003 break;
Chris Masond3977122009-01-05 21:25:51 -05001004
Chris Masonc8b97812008-10-29 14:49:59 -04001005 /* we're not doing compressed IO, don't unlock the first
1006 * page (which the caller expects to stay locked), don't
1007 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001008 *
1009 * Do set the Private2 bit so we know this page was properly
1010 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001011 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001012 op = unlock ? PAGE_UNLOCK : 0;
1013 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001014
Josef Bacikc2790a22013-07-29 11:20:47 -04001015 extent_clear_unlock_delalloc(inode, start,
1016 start + ram_size - 1, locked_page,
1017 EXTENT_LOCKED | EXTENT_DELALLOC,
1018 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001019 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001020 num_bytes -= cur_alloc_size;
1021 alloc_hint = ins.objectid + ins.offset;
1022 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001023 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001024out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001025 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001026
Filipe Mananad9f85962014-08-25 10:43:00 +01001027out_drop_extent_cache:
1028 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001029out_reserve:
Miao Xiee570fd22014-06-19 10:42:50 +08001030 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001031out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001032 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001033 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1034 EXTENT_DELALLOC | EXTENT_DEFRAG,
1035 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1036 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001037 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001038}
Chris Masonc8b97812008-10-29 14:49:59 -04001039
Chris Mason771ed682008-11-06 22:02:51 -05001040/*
1041 * work queue call back to started compression on a file and pages
1042 */
1043static noinline void async_cow_start(struct btrfs_work *work)
1044{
1045 struct async_cow *async_cow;
1046 int num_added = 0;
1047 async_cow = container_of(work, struct async_cow, work);
1048
1049 compress_file_range(async_cow->inode, async_cow->locked_page,
1050 async_cow->start, async_cow->end, async_cow,
1051 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001052 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001053 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001054 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001055 }
Chris Mason771ed682008-11-06 22:02:51 -05001056}
1057
1058/*
1059 * work queue call back to submit previously compressed pages
1060 */
1061static noinline void async_cow_submit(struct btrfs_work *work)
1062{
1063 struct async_cow *async_cow;
1064 struct btrfs_root *root;
1065 unsigned long nr_pages;
1066
1067 async_cow = container_of(work, struct async_cow, work);
1068
1069 root = async_cow->root;
1070 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1071 PAGE_CACHE_SHIFT;
1072
Josef Bacik66657b32012-08-01 15:36:24 -04001073 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001074 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001075 waitqueue_active(&root->fs_info->async_submit_wait))
1076 wake_up(&root->fs_info->async_submit_wait);
1077
Chris Masond3977122009-01-05 21:25:51 -05001078 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001079 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001080}
Chris Masonc8b97812008-10-29 14:49:59 -04001081
Chris Mason771ed682008-11-06 22:02:51 -05001082static noinline void async_cow_free(struct btrfs_work *work)
1083{
1084 struct async_cow *async_cow;
1085 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001086 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001087 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001088 kfree(async_cow);
1089}
1090
1091static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1092 u64 start, u64 end, int *page_started,
1093 unsigned long *nr_written)
1094{
1095 struct async_cow *async_cow;
1096 struct btrfs_root *root = BTRFS_I(inode)->root;
1097 unsigned long nr_pages;
1098 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001099 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001100
Chris Masona3429ab2009-10-08 12:30:20 -04001101 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1102 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001103 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001104 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001105 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001106 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001107 async_cow->root = root;
1108 async_cow->locked_page = locked_page;
1109 async_cow->start = start;
1110
Wang Shilongf79707b2014-07-17 11:44:09 +08001111 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1112 !btrfs_test_opt(root, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001113 cur_end = end;
1114 else
1115 cur_end = min(end, start + 512 * 1024 - 1);
1116
1117 async_cow->end = cur_end;
1118 INIT_LIST_HEAD(&async_cow->extents);
1119
Liu Bo9e0af232014-08-15 23:36:53 +08001120 btrfs_init_work(&async_cow->work,
1121 btrfs_delalloc_helper,
1122 async_cow_start, async_cow_submit,
1123 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001124
Chris Mason771ed682008-11-06 22:02:51 -05001125 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1126 PAGE_CACHE_SHIFT;
1127 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1128
Qu Wenruoafe3d242014-02-28 10:46:07 +08001129 btrfs_queue_work(root->fs_info->delalloc_workers,
1130 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001131
1132 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1133 wait_event(root->fs_info->async_submit_wait,
1134 (atomic_read(&root->fs_info->async_delalloc_pages) <
1135 limit));
1136 }
1137
Chris Masond3977122009-01-05 21:25:51 -05001138 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001139 atomic_read(&root->fs_info->async_delalloc_pages)) {
1140 wait_event(root->fs_info->async_submit_wait,
1141 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1142 0));
1143 }
1144
1145 *nr_written += nr_pages;
1146 start = cur_end + 1;
1147 }
1148 *page_started = 1;
1149 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001150}
1151
Chris Masond3977122009-01-05 21:25:51 -05001152static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001153 u64 bytenr, u64 num_bytes)
1154{
1155 int ret;
1156 struct btrfs_ordered_sum *sums;
1157 LIST_HEAD(list);
1158
Yan Zheng07d400a2009-01-06 11:42:00 -05001159 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001160 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001161 if (ret == 0 && list_empty(&list))
1162 return 0;
1163
1164 while (!list_empty(&list)) {
1165 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1166 list_del(&sums->list);
1167 kfree(sums);
1168 }
1169 return 1;
1170}
1171
Chris Masond352ac62008-09-29 15:18:18 -04001172/*
1173 * when nowcow writeback call back. This checks for snapshots or COW copies
1174 * of the extents that exist in the file, and COWs the file as required.
1175 *
1176 * If no cow copies or snapshots exist, we write directly to the existing
1177 * blocks on disk
1178 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001179static noinline int run_delalloc_nocow(struct inode *inode,
1180 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001181 u64 start, u64 end, int *page_started, int force,
1182 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001183{
Chris Masonbe20aa92007-12-17 20:14:01 -05001184 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001185 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001186 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001187 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001188 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001189 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001190 u64 cow_start;
1191 u64 cur_offset;
1192 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001193 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001194 u64 disk_bytenr;
1195 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001196 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001197 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001198 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001199 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001200 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001201 int nocow;
1202 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001203 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001204 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001205
1206 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001207 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001208 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1209 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001210 EXTENT_DO_ACCOUNTING |
1211 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001212 PAGE_CLEAR_DIRTY |
1213 PAGE_SET_WRITEBACK |
1214 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001215 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001216 }
Li Zefan82d59022011-04-20 10:33:24 +08001217
Liu Bo83eea1f2012-07-10 05:28:39 -06001218 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001219
1220 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001221 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001222 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001223 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001224
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001225 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001226 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1227 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001228 EXTENT_DO_ACCOUNTING |
1229 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001230 PAGE_CLEAR_DIRTY |
1231 PAGE_SET_WRITEBACK |
1232 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001233 btrfs_free_path(path);
1234 return PTR_ERR(trans);
1235 }
1236
Josef Bacik74b21072011-04-13 12:02:53 -04001237 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001238
Yan Zheng80ff3852008-10-30 14:20:02 -04001239 cow_start = (u64)-1;
1240 cur_offset = start;
1241 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001242 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001243 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001244 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001245 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001246 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1247 leaf = path->nodes[0];
1248 btrfs_item_key_to_cpu(leaf, &found_key,
1249 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001250 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001251 found_key.type == BTRFS_EXTENT_DATA_KEY)
1252 path->slots[0]--;
1253 }
1254 check_prev = 0;
1255next_slot:
1256 leaf = path->nodes[0];
1257 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1258 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001259 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001260 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001261 if (ret > 0)
1262 break;
1263 leaf = path->nodes[0];
1264 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001265
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 nocow = 0;
1267 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001268 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001270
Li Zefan33345d012011-04-20 10:31:50 +08001271 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001272 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1273 found_key.offset > end)
1274 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001275
Yan Zheng80ff3852008-10-30 14:20:02 -04001276 if (found_key.offset > cur_offset) {
1277 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001278 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001279 goto out_check;
1280 }
Chris Masonc31f8832008-01-08 15:46:31 -05001281
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 fi = btrfs_item_ptr(leaf, path->slots[0],
1283 struct btrfs_file_extent_item);
1284 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001285
Josef Bacikcc95bef2013-04-04 14:31:27 -04001286 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001287 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1288 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001289 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001290 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001291 extent_end = found_key.offset +
1292 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001293 disk_num_bytes =
1294 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001295 if (extent_end <= start) {
1296 path->slots[0]++;
1297 goto next_slot;
1298 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001299 if (disk_bytenr == 0)
1300 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001301 if (btrfs_file_extent_compression(leaf, fi) ||
1302 btrfs_file_extent_encryption(leaf, fi) ||
1303 btrfs_file_extent_other_encoding(leaf, fi))
1304 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001305 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1306 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001307 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001309 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001310 found_key.offset -
1311 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001312 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001313 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001314 disk_bytenr += cur_offset - found_key.offset;
1315 num_bytes = min(end + 1, extent_end) - cur_offset;
1316 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001317 * if there are pending snapshots for this root,
1318 * we fall into common COW way.
1319 */
1320 if (!nolock) {
1321 err = btrfs_start_nocow_write(root);
1322 if (!err)
1323 goto out_check;
1324 }
1325 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001326 * force cow if csum exists in the range.
1327 * this ensure that csum for a given extent are
1328 * either valid or do not exist.
1329 */
1330 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1331 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001332 nocow = 1;
1333 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1334 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001335 btrfs_file_extent_inline_len(leaf,
1336 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001337 extent_end = ALIGN(extent_end, root->sectorsize);
1338 } else {
1339 BUG_ON(1);
1340 }
1341out_check:
1342 if (extent_end <= start) {
1343 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001344 if (!nolock && nocow)
1345 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001346 goto next_slot;
1347 }
1348 if (!nocow) {
1349 if (cow_start == (u64)-1)
1350 cow_start = cur_offset;
1351 cur_offset = extent_end;
1352 if (cur_offset > end)
1353 break;
1354 path->slots[0]++;
1355 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001356 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001357
David Sterbab3b4aa72011-04-21 01:20:15 +02001358 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001359 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001360 ret = cow_file_range(inode, locked_page,
1361 cow_start, found_key.offset - 1,
1362 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001363 if (ret) {
1364 if (!nolock && nocow)
1365 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001366 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001367 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001368 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001369 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001370
Yan Zhengd899e052008-10-30 14:25:28 -04001371 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1372 struct extent_map *em;
1373 struct extent_map_tree *em_tree;
1374 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001375 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001376 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001377 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001378 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001379 em->len = num_bytes;
1380 em->block_len = num_bytes;
1381 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001382 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001383 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001384 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001385 em->mod_start = em->start;
1386 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001387 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001388 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001389 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001390 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001391 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001392 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001393 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001394 if (ret != -EEXIST) {
1395 free_extent_map(em);
1396 break;
1397 }
1398 btrfs_drop_extent_cache(inode, em->start,
1399 em->start + em->len - 1, 0);
1400 }
1401 type = BTRFS_ORDERED_PREALLOC;
1402 } else {
1403 type = BTRFS_ORDERED_NOCOW;
1404 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001405
1406 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001407 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001408 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001409
Yan, Zhengefa56462010-05-16 10:49:59 -04001410 if (root->root_key.objectid ==
1411 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1412 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1413 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001414 if (ret) {
1415 if (!nolock && nocow)
1416 btrfs_end_nocow_write(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001417 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001418 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001419 }
1420
Josef Bacikc2790a22013-07-29 11:20:47 -04001421 extent_clear_unlock_delalloc(inode, cur_offset,
1422 cur_offset + num_bytes - 1,
1423 locked_page, EXTENT_LOCKED |
1424 EXTENT_DELALLOC, PAGE_UNLOCK |
1425 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001426 if (!nolock && nocow)
1427 btrfs_end_nocow_write(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001428 cur_offset = extent_end;
1429 if (cur_offset > end)
1430 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001431 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001432 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001433
Josef Bacik17ca04a2012-05-31 15:58:55 -04001434 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001435 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001436 cur_offset = end;
1437 }
1438
Yan Zheng80ff3852008-10-30 14:20:02 -04001439 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001440 ret = cow_file_range(inode, locked_page, cow_start, end,
1441 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001442 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001443 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001444 }
1445
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001446error:
Miao Xiea698d0752012-09-20 01:51:59 -06001447 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001448 if (!ret)
1449 ret = err;
1450
Josef Bacik17ca04a2012-05-31 15:58:55 -04001451 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001452 extent_clear_unlock_delalloc(inode, cur_offset, end,
1453 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001454 EXTENT_DELALLOC | EXTENT_DEFRAG |
1455 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1456 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001457 PAGE_SET_WRITEBACK |
1458 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001459 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001460 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001461}
1462
Wang Shilong47059d92014-07-03 18:22:07 +08001463static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1464{
1465
1466 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1467 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1468 return 0;
1469
1470 /*
1471 * @defrag_bytes is a hint value, no spinlock held here,
1472 * if is not zero, it means the file is defragging.
1473 * Force cow if given extent needs to be defragged.
1474 */
1475 if (BTRFS_I(inode)->defrag_bytes &&
1476 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1477 EXTENT_DEFRAG, 0, NULL))
1478 return 1;
1479
1480 return 0;
1481}
1482
Chris Masond352ac62008-09-29 15:18:18 -04001483/*
1484 * extent_io.c call back to do delayed allocation processing
1485 */
Chris Masonc8b97812008-10-29 14:49:59 -04001486static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001487 u64 start, u64 end, int *page_started,
1488 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001489{
Chris Masonbe20aa92007-12-17 20:14:01 -05001490 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001491 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001492
Wang Shilong47059d92014-07-03 18:22:07 +08001493 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001494 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001495 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001496 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001497 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001498 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001499 } else if (!inode_need_compress(inode)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001500 ret = cow_file_range(inode, locked_page, start, end,
1501 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001502 } else {
1503 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1504 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001505 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001506 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001507 }
Chris Masonb888db22007-08-27 16:49:44 -04001508 return ret;
1509}
1510
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001511static void btrfs_split_extent_hook(struct inode *inode,
1512 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001513{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001514 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001515 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001516 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001517
Josef Bacik9e0baf62011-07-15 15:16:44 +00001518 spin_lock(&BTRFS_I(inode)->lock);
1519 BTRFS_I(inode)->outstanding_extents++;
1520 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001521}
1522
1523/*
1524 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1525 * extents so we can keep track of new extents that are just merged onto old
1526 * extents, such as when we are doing sequential writes, so we can properly
1527 * account for the metadata space we'll need.
1528 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001529static void btrfs_merge_extent_hook(struct inode *inode,
1530 struct extent_state *new,
1531 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001532{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001533 /* not delalloc, ignore it */
1534 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001535 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001536
Josef Bacik9e0baf62011-07-15 15:16:44 +00001537 spin_lock(&BTRFS_I(inode)->lock);
1538 BTRFS_I(inode)->outstanding_extents--;
1539 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001540}
1541
Miao Xieeb73c1b2013-05-15 07:48:22 +00001542static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1543 struct inode *inode)
1544{
1545 spin_lock(&root->delalloc_lock);
1546 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1547 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1548 &root->delalloc_inodes);
1549 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1550 &BTRFS_I(inode)->runtime_flags);
1551 root->nr_delalloc_inodes++;
1552 if (root->nr_delalloc_inodes == 1) {
1553 spin_lock(&root->fs_info->delalloc_root_lock);
1554 BUG_ON(!list_empty(&root->delalloc_root));
1555 list_add_tail(&root->delalloc_root,
1556 &root->fs_info->delalloc_roots);
1557 spin_unlock(&root->fs_info->delalloc_root_lock);
1558 }
1559 }
1560 spin_unlock(&root->delalloc_lock);
1561}
1562
1563static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1564 struct inode *inode)
1565{
1566 spin_lock(&root->delalloc_lock);
1567 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1568 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1569 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1570 &BTRFS_I(inode)->runtime_flags);
1571 root->nr_delalloc_inodes--;
1572 if (!root->nr_delalloc_inodes) {
1573 spin_lock(&root->fs_info->delalloc_root_lock);
1574 BUG_ON(list_empty(&root->delalloc_root));
1575 list_del_init(&root->delalloc_root);
1576 spin_unlock(&root->fs_info->delalloc_root_lock);
1577 }
1578 }
1579 spin_unlock(&root->delalloc_lock);
1580}
1581
Chris Masond352ac62008-09-29 15:18:18 -04001582/*
1583 * extent_io.c set_bit_hook, used to track delayed allocation
1584 * bytes in this file, and to maintain the list of inodes that
1585 * have pending delalloc work to be done.
1586 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001587static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001588 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001589{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001590
Wang Shilong47059d92014-07-03 18:22:07 +08001591 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1592 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001593 /*
1594 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001595 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001596 * bit, which is only set or cleared with irqs on
1597 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001598 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001599 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001600 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001601 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001602
Josef Bacik9e0baf62011-07-15 15:16:44 +00001603 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001604 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001605 } else {
1606 spin_lock(&BTRFS_I(inode)->lock);
1607 BTRFS_I(inode)->outstanding_extents++;
1608 spin_unlock(&BTRFS_I(inode)->lock);
1609 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001610
Miao Xie963d6782013-01-29 10:10:51 +00001611 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1612 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001613 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001614 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001615 if (*bits & EXTENT_DEFRAG)
1616 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001617 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001618 &BTRFS_I(inode)->runtime_flags))
1619 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001620 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001621 }
Chris Mason291d6732008-01-29 15:55:23 -05001622}
1623
Chris Masond352ac62008-09-29 15:18:18 -04001624/*
1625 * extent_io.c clear_bit_hook, see set_bit_hook for why
1626 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001627static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001628 struct extent_state *state,
1629 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001630{
Wang Shilong47059d92014-07-03 18:22:07 +08001631 u64 len = state->end + 1 - state->start;
1632
1633 spin_lock(&BTRFS_I(inode)->lock);
1634 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1635 BTRFS_I(inode)->defrag_bytes -= len;
1636 spin_unlock(&BTRFS_I(inode)->lock);
1637
Chris Mason75eff682008-12-15 15:54:40 -05001638 /*
1639 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001640 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001641 * bit, which is only set or cleared with irqs on
1642 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001643 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001644 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001645 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001646
Josef Bacik9e0baf62011-07-15 15:16:44 +00001647 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001648 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001649 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1650 spin_lock(&BTRFS_I(inode)->lock);
1651 BTRFS_I(inode)->outstanding_extents--;
1652 spin_unlock(&BTRFS_I(inode)->lock);
1653 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001654
Josef Bacikb6d08f02013-09-27 14:57:43 -04001655 /*
1656 * We don't reserve metadata space for space cache inodes so we
1657 * don't need to call dellalloc_release_metadata if there is an
1658 * error.
1659 */
1660 if (*bits & EXTENT_DO_ACCOUNTING &&
1661 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001662 btrfs_delalloc_release_metadata(inode, len);
1663
Josef Bacik0cb59c92010-07-02 12:14:14 -04001664 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001665 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001666 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001667
Miao Xie963d6782013-01-29 10:10:51 +00001668 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1669 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001670 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001671 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001672 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001673 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001674 &BTRFS_I(inode)->runtime_flags))
1675 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001676 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001677 }
Chris Mason291d6732008-01-29 15:55:23 -05001678}
1679
Chris Masond352ac62008-09-29 15:18:18 -04001680/*
1681 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1682 * we don't create bios that span stripes or chunks
1683 */
David Woodhouse64a16702009-07-15 23:29:37 +01001684int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001685 size_t size, struct bio *bio,
1686 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001687{
1688 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001689 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001690 u64 length = 0;
1691 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001692 int ret;
1693
Chris Mason771ed682008-11-06 22:02:51 -05001694 if (bio_flags & EXTENT_BIO_COMPRESSED)
1695 return 0;
1696
Kent Overstreet4f024f32013-10-11 15:44:27 -07001697 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001698 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001699 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001700 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001701 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001702 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001703 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001704 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001705 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001706}
1707
Chris Masond352ac62008-09-29 15:18:18 -04001708/*
1709 * in order to insert checksums into the metadata in large chunks,
1710 * we wait until bio submission time. All the pages in the bio are
1711 * checksummed and sums are attached onto the ordered extent record.
1712 *
1713 * At IO completion time the cums attached on the ordered extent record
1714 * are inserted into the btree
1715 */
Chris Masond3977122009-01-05 21:25:51 -05001716static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1717 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001718 unsigned long bio_flags,
1719 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001720{
Chris Mason065631f2008-02-20 12:07:25 -05001721 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001722 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001723
Chris Masond20f7042008-12-08 16:58:54 -05001724 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001725 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001726 return 0;
1727}
Chris Masone0156402008-04-16 11:15:20 -04001728
Chris Mason4a69a412008-11-06 22:03:00 -05001729/*
1730 * in order to insert checksums into the metadata in large chunks,
1731 * we wait until bio submission time. All the pages in the bio are
1732 * checksummed and sums are attached onto the ordered extent record.
1733 *
1734 * At IO completion time the cums attached on the ordered extent record
1735 * are inserted into the btree
1736 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001737static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001738 int mirror_num, unsigned long bio_flags,
1739 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001740{
1741 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001742 int ret;
1743
1744 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1745 if (ret)
1746 bio_endio(bio, ret);
1747 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001748}
1749
Chris Masond352ac62008-09-29 15:18:18 -04001750/*
Chris Masoncad321a2008-12-17 14:51:42 -05001751 * extent_io.c submission hook. This does the right thing for csum calculation
1752 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001753 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001754static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001755 int mirror_num, unsigned long bio_flags,
1756 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001757{
1758 struct btrfs_root *root = BTRFS_I(inode)->root;
1759 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001760 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001761 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001762 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001763
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001764 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001765
Liu Bo83eea1f2012-07-10 05:28:39 -06001766 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001767 metadata = 2;
1768
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001769 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001770 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1771 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001772 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001773
Chris Masond20f7042008-12-08 16:58:54 -05001774 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001775 ret = btrfs_submit_compressed_read(inode, bio,
1776 mirror_num,
1777 bio_flags);
1778 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001779 } else if (!skip_sum) {
1780 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1781 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001782 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001783 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001784 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001785 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001786 /* csum items have already been cloned */
1787 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1788 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001789 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001790 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001791 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001792 bio_flags, bio_offset,
1793 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001794 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001795 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001796 } else if (!skip_sum) {
1797 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1798 if (ret)
1799 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001800 }
1801
Chris Mason0b86a832008-03-24 15:01:56 -04001802mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001803 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1804
1805out:
1806 if (ret < 0)
1807 bio_endio(bio, ret);
1808 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001809}
Chris Mason6885f302008-02-20 16:11:05 -05001810
Chris Masond352ac62008-09-29 15:18:18 -04001811/*
1812 * given a list of ordered sums record them in the inode. This happens
1813 * at IO completion time based on sums calculated at bio submission time.
1814 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001815static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001816 struct inode *inode, u64 file_offset,
1817 struct list_head *list)
1818{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001819 struct btrfs_ordered_sum *sum;
1820
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001821 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001822 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001823 btrfs_csum_file_blocks(trans,
1824 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001825 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001826 }
1827 return 0;
1828}
1829
Josef Bacik2ac55d42010-02-03 19:33:23 +00001830int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1831 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001832{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001833 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001834 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001835 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001836}
1837
Chris Masond352ac62008-09-29 15:18:18 -04001838/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001839struct btrfs_writepage_fixup {
1840 struct page *page;
1841 struct btrfs_work work;
1842};
1843
Christoph Hellwigb2950862008-12-02 09:54:17 -05001844static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001845{
1846 struct btrfs_writepage_fixup *fixup;
1847 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001848 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001849 struct page *page;
1850 struct inode *inode;
1851 u64 page_start;
1852 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001853 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001854
1855 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1856 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001857again:
Chris Mason247e7432008-07-17 12:53:51 -04001858 lock_page(page);
1859 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1860 ClearPageChecked(page);
1861 goto out_page;
1862 }
1863
1864 inode = page->mapping->host;
1865 page_start = page_offset(page);
1866 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1867
Josef Bacik2ac55d42010-02-03 19:33:23 +00001868 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001869 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001870
1871 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001872 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001873 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001874
1875 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1876 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001877 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1878 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001879 unlock_page(page);
1880 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001881 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001882 goto again;
1883 }
Chris Mason247e7432008-07-17 12:53:51 -04001884
Jeff Mahoney87826df2012-02-15 16:23:57 +01001885 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1886 if (ret) {
1887 mapping_set_error(page->mapping, ret);
1888 end_extent_writepage(page, ret, page_start, page_end);
1889 ClearPageChecked(page);
1890 goto out;
1891 }
1892
Josef Bacik2ac55d42010-02-03 19:33:23 +00001893 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001894 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001895 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001896out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001897 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1898 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001899out_page:
1900 unlock_page(page);
1901 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001902 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001903}
1904
1905/*
1906 * There are a few paths in the higher layers of the kernel that directly
1907 * set the page dirty bit without asking the filesystem if it is a
1908 * good idea. This causes problems because we want to make sure COW
1909 * properly happens and the data=ordered rules are followed.
1910 *
Chris Masonc8b97812008-10-29 14:49:59 -04001911 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001912 * hasn't been properly setup for IO. We kick off an async process
1913 * to fix it up. The async helper will wait for ordered extents, set
1914 * the delalloc bit and make it safe to write the page.
1915 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001916static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001917{
1918 struct inode *inode = page->mapping->host;
1919 struct btrfs_writepage_fixup *fixup;
1920 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001921
Chris Mason8b62b722009-09-02 16:53:46 -04001922 /* this page is properly in the ordered list */
1923 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001924 return 0;
1925
1926 if (PageChecked(page))
1927 return -EAGAIN;
1928
1929 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1930 if (!fixup)
1931 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001932
Chris Mason247e7432008-07-17 12:53:51 -04001933 SetPageChecked(page);
1934 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08001935 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
1936 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04001937 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08001938 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001939 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001940}
1941
Yan Zhengd899e052008-10-30 14:25:28 -04001942static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1943 struct inode *inode, u64 file_pos,
1944 u64 disk_bytenr, u64 disk_num_bytes,
1945 u64 num_bytes, u64 ram_bytes,
1946 u8 compression, u8 encryption,
1947 u16 other_encoding, int extent_type)
1948{
1949 struct btrfs_root *root = BTRFS_I(inode)->root;
1950 struct btrfs_file_extent_item *fi;
1951 struct btrfs_path *path;
1952 struct extent_buffer *leaf;
1953 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001954 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04001955 int ret;
1956
1957 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001958 if (!path)
1959 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001960
Chris Masona1ed8352009-09-11 12:27:37 -04001961 /*
1962 * we may be replacing one extent in the tree with another.
1963 * The new extent is pinned in the extent map, and we don't want
1964 * to drop it from the cache until it is completely in the btree.
1965 *
1966 * So, tell btrfs_drop_extents to leave this extent in the cache.
1967 * the caller is expected to unpin it and allow it to be merged
1968 * with the others.
1969 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001970 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
1971 file_pos + num_bytes, NULL, 0,
1972 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001973 if (ret)
1974 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001975
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001976 if (!extent_inserted) {
1977 ins.objectid = btrfs_ino(inode);
1978 ins.offset = file_pos;
1979 ins.type = BTRFS_EXTENT_DATA_KEY;
1980
1981 path->leave_spinning = 1;
1982 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1983 sizeof(*fi));
1984 if (ret)
1985 goto out;
1986 }
Yan Zhengd899e052008-10-30 14:25:28 -04001987 leaf = path->nodes[0];
1988 fi = btrfs_item_ptr(leaf, path->slots[0],
1989 struct btrfs_file_extent_item);
1990 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1991 btrfs_set_file_extent_type(leaf, fi, extent_type);
1992 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1993 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1994 btrfs_set_file_extent_offset(leaf, fi, 0);
1995 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1996 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1997 btrfs_set_file_extent_compression(leaf, fi, compression);
1998 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1999 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002000
Yan Zhengd899e052008-10-30 14:25:28 -04002001 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002002 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002003
2004 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002005
2006 ins.objectid = disk_bytenr;
2007 ins.offset = disk_num_bytes;
2008 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002009 ret = btrfs_alloc_reserved_file_extent(trans, root,
2010 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002011 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002012out:
Yan Zhengd899e052008-10-30 14:25:28 -04002013 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002014
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002015 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002016}
2017
Liu Bo38c227d2013-01-29 03:18:40 +00002018/* snapshot-aware defrag */
2019struct sa_defrag_extent_backref {
2020 struct rb_node node;
2021 struct old_sa_defrag_extent *old;
2022 u64 root_id;
2023 u64 inum;
2024 u64 file_pos;
2025 u64 extent_offset;
2026 u64 num_bytes;
2027 u64 generation;
2028};
2029
2030struct old_sa_defrag_extent {
2031 struct list_head list;
2032 struct new_sa_defrag_extent *new;
2033
2034 u64 extent_offset;
2035 u64 bytenr;
2036 u64 offset;
2037 u64 len;
2038 int count;
2039};
2040
2041struct new_sa_defrag_extent {
2042 struct rb_root root;
2043 struct list_head head;
2044 struct btrfs_path *path;
2045 struct inode *inode;
2046 u64 file_pos;
2047 u64 len;
2048 u64 bytenr;
2049 u64 disk_len;
2050 u8 compress_type;
2051};
2052
2053static int backref_comp(struct sa_defrag_extent_backref *b1,
2054 struct sa_defrag_extent_backref *b2)
2055{
2056 if (b1->root_id < b2->root_id)
2057 return -1;
2058 else if (b1->root_id > b2->root_id)
2059 return 1;
2060
2061 if (b1->inum < b2->inum)
2062 return -1;
2063 else if (b1->inum > b2->inum)
2064 return 1;
2065
2066 if (b1->file_pos < b2->file_pos)
2067 return -1;
2068 else if (b1->file_pos > b2->file_pos)
2069 return 1;
2070
2071 /*
2072 * [------------------------------] ===> (a range of space)
2073 * |<--->| |<---->| =============> (fs/file tree A)
2074 * |<---------------------------->| ===> (fs/file tree B)
2075 *
2076 * A range of space can refer to two file extents in one tree while
2077 * refer to only one file extent in another tree.
2078 *
2079 * So we may process a disk offset more than one time(two extents in A)
2080 * and locate at the same extent(one extent in B), then insert two same
2081 * backrefs(both refer to the extent in B).
2082 */
2083 return 0;
2084}
2085
2086static void backref_insert(struct rb_root *root,
2087 struct sa_defrag_extent_backref *backref)
2088{
2089 struct rb_node **p = &root->rb_node;
2090 struct rb_node *parent = NULL;
2091 struct sa_defrag_extent_backref *entry;
2092 int ret;
2093
2094 while (*p) {
2095 parent = *p;
2096 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2097
2098 ret = backref_comp(backref, entry);
2099 if (ret < 0)
2100 p = &(*p)->rb_left;
2101 else
2102 p = &(*p)->rb_right;
2103 }
2104
2105 rb_link_node(&backref->node, parent, p);
2106 rb_insert_color(&backref->node, root);
2107}
2108
2109/*
2110 * Note the backref might has changed, and in this case we just return 0.
2111 */
2112static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2113 void *ctx)
2114{
2115 struct btrfs_file_extent_item *extent;
2116 struct btrfs_fs_info *fs_info;
2117 struct old_sa_defrag_extent *old = ctx;
2118 struct new_sa_defrag_extent *new = old->new;
2119 struct btrfs_path *path = new->path;
2120 struct btrfs_key key;
2121 struct btrfs_root *root;
2122 struct sa_defrag_extent_backref *backref;
2123 struct extent_buffer *leaf;
2124 struct inode *inode = new->inode;
2125 int slot;
2126 int ret;
2127 u64 extent_offset;
2128 u64 num_bytes;
2129
2130 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2131 inum == btrfs_ino(inode))
2132 return 0;
2133
2134 key.objectid = root_id;
2135 key.type = BTRFS_ROOT_ITEM_KEY;
2136 key.offset = (u64)-1;
2137
2138 fs_info = BTRFS_I(inode)->root->fs_info;
2139 root = btrfs_read_fs_root_no_name(fs_info, &key);
2140 if (IS_ERR(root)) {
2141 if (PTR_ERR(root) == -ENOENT)
2142 return 0;
2143 WARN_ON(1);
2144 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2145 inum, offset, root_id);
2146 return PTR_ERR(root);
2147 }
2148
2149 key.objectid = inum;
2150 key.type = BTRFS_EXTENT_DATA_KEY;
2151 if (offset > (u64)-1 << 32)
2152 key.offset = 0;
2153 else
2154 key.offset = offset;
2155
2156 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302157 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002158 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002159 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002160
2161 while (1) {
2162 cond_resched();
2163
2164 leaf = path->nodes[0];
2165 slot = path->slots[0];
2166
2167 if (slot >= btrfs_header_nritems(leaf)) {
2168 ret = btrfs_next_leaf(root, path);
2169 if (ret < 0) {
2170 goto out;
2171 } else if (ret > 0) {
2172 ret = 0;
2173 goto out;
2174 }
2175 continue;
2176 }
2177
2178 path->slots[0]++;
2179
2180 btrfs_item_key_to_cpu(leaf, &key, slot);
2181
2182 if (key.objectid > inum)
2183 goto out;
2184
2185 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2186 continue;
2187
2188 extent = btrfs_item_ptr(leaf, slot,
2189 struct btrfs_file_extent_item);
2190
2191 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2192 continue;
2193
Liu Boe68afa42013-07-01 22:13:26 +08002194 /*
2195 * 'offset' refers to the exact key.offset,
2196 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2197 * (key.offset - extent_offset).
2198 */
2199 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002200 continue;
2201
Liu Boe68afa42013-07-01 22:13:26 +08002202 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002203 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002204
Liu Bo38c227d2013-01-29 03:18:40 +00002205 if (extent_offset >= old->extent_offset + old->offset +
2206 old->len || extent_offset + num_bytes <=
2207 old->extent_offset + old->offset)
2208 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002209 break;
2210 }
2211
2212 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2213 if (!backref) {
2214 ret = -ENOENT;
2215 goto out;
2216 }
2217
2218 backref->root_id = root_id;
2219 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002220 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002221 backref->num_bytes = num_bytes;
2222 backref->extent_offset = extent_offset;
2223 backref->generation = btrfs_file_extent_generation(leaf, extent);
2224 backref->old = old;
2225 backref_insert(&new->root, backref);
2226 old->count++;
2227out:
2228 btrfs_release_path(path);
2229 WARN_ON(ret);
2230 return ret;
2231}
2232
2233static noinline bool record_extent_backrefs(struct btrfs_path *path,
2234 struct new_sa_defrag_extent *new)
2235{
2236 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2237 struct old_sa_defrag_extent *old, *tmp;
2238 int ret;
2239
2240 new->path = path;
2241
2242 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002243 ret = iterate_inodes_from_logical(old->bytenr +
2244 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002245 path, record_one_backref,
2246 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002247 if (ret < 0 && ret != -ENOENT)
2248 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002249
2250 /* no backref to be processed for this extent */
2251 if (!old->count) {
2252 list_del(&old->list);
2253 kfree(old);
2254 }
2255 }
2256
2257 if (list_empty(&new->head))
2258 return false;
2259
2260 return true;
2261}
2262
2263static int relink_is_mergable(struct extent_buffer *leaf,
2264 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002265 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002266{
Liu Bo116e0022013-08-02 16:30:40 +08002267 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002268 return 0;
2269
2270 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2271 return 0;
2272
Liu Bo116e0022013-08-02 16:30:40 +08002273 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2274 return 0;
2275
2276 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002277 btrfs_file_extent_other_encoding(leaf, fi))
2278 return 0;
2279
2280 return 1;
2281}
2282
2283/*
2284 * Note the backref might has changed, and in this case we just return 0.
2285 */
2286static noinline int relink_extent_backref(struct btrfs_path *path,
2287 struct sa_defrag_extent_backref *prev,
2288 struct sa_defrag_extent_backref *backref)
2289{
2290 struct btrfs_file_extent_item *extent;
2291 struct btrfs_file_extent_item *item;
2292 struct btrfs_ordered_extent *ordered;
2293 struct btrfs_trans_handle *trans;
2294 struct btrfs_fs_info *fs_info;
2295 struct btrfs_root *root;
2296 struct btrfs_key key;
2297 struct extent_buffer *leaf;
2298 struct old_sa_defrag_extent *old = backref->old;
2299 struct new_sa_defrag_extent *new = old->new;
2300 struct inode *src_inode = new->inode;
2301 struct inode *inode;
2302 struct extent_state *cached = NULL;
2303 int ret = 0;
2304 u64 start;
2305 u64 len;
2306 u64 lock_start;
2307 u64 lock_end;
2308 bool merge = false;
2309 int index;
2310
2311 if (prev && prev->root_id == backref->root_id &&
2312 prev->inum == backref->inum &&
2313 prev->file_pos + prev->num_bytes == backref->file_pos)
2314 merge = true;
2315
2316 /* step 1: get root */
2317 key.objectid = backref->root_id;
2318 key.type = BTRFS_ROOT_ITEM_KEY;
2319 key.offset = (u64)-1;
2320
2321 fs_info = BTRFS_I(src_inode)->root->fs_info;
2322 index = srcu_read_lock(&fs_info->subvol_srcu);
2323
2324 root = btrfs_read_fs_root_no_name(fs_info, &key);
2325 if (IS_ERR(root)) {
2326 srcu_read_unlock(&fs_info->subvol_srcu, index);
2327 if (PTR_ERR(root) == -ENOENT)
2328 return 0;
2329 return PTR_ERR(root);
2330 }
Liu Bo38c227d2013-01-29 03:18:40 +00002331
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002332 if (btrfs_root_readonly(root)) {
2333 srcu_read_unlock(&fs_info->subvol_srcu, index);
2334 return 0;
2335 }
2336
Liu Bo38c227d2013-01-29 03:18:40 +00002337 /* step 2: get inode */
2338 key.objectid = backref->inum;
2339 key.type = BTRFS_INODE_ITEM_KEY;
2340 key.offset = 0;
2341
2342 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2343 if (IS_ERR(inode)) {
2344 srcu_read_unlock(&fs_info->subvol_srcu, index);
2345 return 0;
2346 }
2347
2348 srcu_read_unlock(&fs_info->subvol_srcu, index);
2349
2350 /* step 3: relink backref */
2351 lock_start = backref->file_pos;
2352 lock_end = backref->file_pos + backref->num_bytes - 1;
2353 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2354 0, &cached);
2355
2356 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2357 if (ordered) {
2358 btrfs_put_ordered_extent(ordered);
2359 goto out_unlock;
2360 }
2361
2362 trans = btrfs_join_transaction(root);
2363 if (IS_ERR(trans)) {
2364 ret = PTR_ERR(trans);
2365 goto out_unlock;
2366 }
2367
2368 key.objectid = backref->inum;
2369 key.type = BTRFS_EXTENT_DATA_KEY;
2370 key.offset = backref->file_pos;
2371
2372 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2373 if (ret < 0) {
2374 goto out_free_path;
2375 } else if (ret > 0) {
2376 ret = 0;
2377 goto out_free_path;
2378 }
2379
2380 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2381 struct btrfs_file_extent_item);
2382
2383 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2384 backref->generation)
2385 goto out_free_path;
2386
2387 btrfs_release_path(path);
2388
2389 start = backref->file_pos;
2390 if (backref->extent_offset < old->extent_offset + old->offset)
2391 start += old->extent_offset + old->offset -
2392 backref->extent_offset;
2393
2394 len = min(backref->extent_offset + backref->num_bytes,
2395 old->extent_offset + old->offset + old->len);
2396 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2397
2398 ret = btrfs_drop_extents(trans, root, inode, start,
2399 start + len, 1);
2400 if (ret)
2401 goto out_free_path;
2402again:
2403 key.objectid = btrfs_ino(inode);
2404 key.type = BTRFS_EXTENT_DATA_KEY;
2405 key.offset = start;
2406
Liu Boa09a0a72013-03-11 09:20:58 +00002407 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002408 if (merge) {
2409 struct btrfs_file_extent_item *fi;
2410 u64 extent_len;
2411 struct btrfs_key found_key;
2412
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002413 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002414 if (ret < 0)
2415 goto out_free_path;
2416
2417 path->slots[0]--;
2418 leaf = path->nodes[0];
2419 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2420
2421 fi = btrfs_item_ptr(leaf, path->slots[0],
2422 struct btrfs_file_extent_item);
2423 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2424
Liu Bo116e0022013-08-02 16:30:40 +08002425 if (extent_len + found_key.offset == start &&
2426 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002427 btrfs_set_file_extent_num_bytes(leaf, fi,
2428 extent_len + len);
2429 btrfs_mark_buffer_dirty(leaf);
2430 inode_add_bytes(inode, len);
2431
2432 ret = 1;
2433 goto out_free_path;
2434 } else {
2435 merge = false;
2436 btrfs_release_path(path);
2437 goto again;
2438 }
2439 }
2440
2441 ret = btrfs_insert_empty_item(trans, root, path, &key,
2442 sizeof(*extent));
2443 if (ret) {
2444 btrfs_abort_transaction(trans, root, ret);
2445 goto out_free_path;
2446 }
2447
2448 leaf = path->nodes[0];
2449 item = btrfs_item_ptr(leaf, path->slots[0],
2450 struct btrfs_file_extent_item);
2451 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2452 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2453 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2454 btrfs_set_file_extent_num_bytes(leaf, item, len);
2455 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2456 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2457 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2458 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2459 btrfs_set_file_extent_encryption(leaf, item, 0);
2460 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2461
2462 btrfs_mark_buffer_dirty(leaf);
2463 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002464 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002465
2466 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2467 new->disk_len, 0,
2468 backref->root_id, backref->inum,
2469 new->file_pos, 0); /* start - extent_offset */
2470 if (ret) {
2471 btrfs_abort_transaction(trans, root, ret);
2472 goto out_free_path;
2473 }
2474
2475 ret = 1;
2476out_free_path:
2477 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002478 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002479 btrfs_end_transaction(trans, root);
2480out_unlock:
2481 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2482 &cached, GFP_NOFS);
2483 iput(inode);
2484 return ret;
2485}
2486
Liu Bo6f519562013-10-29 10:45:05 +08002487static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2488{
2489 struct old_sa_defrag_extent *old, *tmp;
2490
2491 if (!new)
2492 return;
2493
2494 list_for_each_entry_safe(old, tmp, &new->head, list) {
2495 list_del(&old->list);
2496 kfree(old);
2497 }
2498 kfree(new);
2499}
2500
Liu Bo38c227d2013-01-29 03:18:40 +00002501static void relink_file_extents(struct new_sa_defrag_extent *new)
2502{
2503 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002504 struct sa_defrag_extent_backref *backref;
2505 struct sa_defrag_extent_backref *prev = NULL;
2506 struct inode *inode;
2507 struct btrfs_root *root;
2508 struct rb_node *node;
2509 int ret;
2510
2511 inode = new->inode;
2512 root = BTRFS_I(inode)->root;
2513
2514 path = btrfs_alloc_path();
2515 if (!path)
2516 return;
2517
2518 if (!record_extent_backrefs(path, new)) {
2519 btrfs_free_path(path);
2520 goto out;
2521 }
2522 btrfs_release_path(path);
2523
2524 while (1) {
2525 node = rb_first(&new->root);
2526 if (!node)
2527 break;
2528 rb_erase(node, &new->root);
2529
2530 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2531
2532 ret = relink_extent_backref(path, prev, backref);
2533 WARN_ON(ret < 0);
2534
2535 kfree(prev);
2536
2537 if (ret == 1)
2538 prev = backref;
2539 else
2540 prev = NULL;
2541 cond_resched();
2542 }
2543 kfree(prev);
2544
2545 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002546out:
Liu Bo6f519562013-10-29 10:45:05 +08002547 free_sa_defrag_extent(new);
2548
Liu Bo38c227d2013-01-29 03:18:40 +00002549 atomic_dec(&root->fs_info->defrag_running);
2550 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002551}
2552
2553static struct new_sa_defrag_extent *
2554record_old_file_extents(struct inode *inode,
2555 struct btrfs_ordered_extent *ordered)
2556{
2557 struct btrfs_root *root = BTRFS_I(inode)->root;
2558 struct btrfs_path *path;
2559 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002560 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002561 struct new_sa_defrag_extent *new;
2562 int ret;
2563
2564 new = kmalloc(sizeof(*new), GFP_NOFS);
2565 if (!new)
2566 return NULL;
2567
2568 new->inode = inode;
2569 new->file_pos = ordered->file_offset;
2570 new->len = ordered->len;
2571 new->bytenr = ordered->start;
2572 new->disk_len = ordered->disk_len;
2573 new->compress_type = ordered->compress_type;
2574 new->root = RB_ROOT;
2575 INIT_LIST_HEAD(&new->head);
2576
2577 path = btrfs_alloc_path();
2578 if (!path)
2579 goto out_kfree;
2580
2581 key.objectid = btrfs_ino(inode);
2582 key.type = BTRFS_EXTENT_DATA_KEY;
2583 key.offset = new->file_pos;
2584
2585 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2586 if (ret < 0)
2587 goto out_free_path;
2588 if (ret > 0 && path->slots[0] > 0)
2589 path->slots[0]--;
2590
2591 /* find out all the old extents for the file range */
2592 while (1) {
2593 struct btrfs_file_extent_item *extent;
2594 struct extent_buffer *l;
2595 int slot;
2596 u64 num_bytes;
2597 u64 offset;
2598 u64 end;
2599 u64 disk_bytenr;
2600 u64 extent_offset;
2601
2602 l = path->nodes[0];
2603 slot = path->slots[0];
2604
2605 if (slot >= btrfs_header_nritems(l)) {
2606 ret = btrfs_next_leaf(root, path);
2607 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002608 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002609 else if (ret > 0)
2610 break;
2611 continue;
2612 }
2613
2614 btrfs_item_key_to_cpu(l, &key, slot);
2615
2616 if (key.objectid != btrfs_ino(inode))
2617 break;
2618 if (key.type != BTRFS_EXTENT_DATA_KEY)
2619 break;
2620 if (key.offset >= new->file_pos + new->len)
2621 break;
2622
2623 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2624
2625 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2626 if (key.offset + num_bytes < new->file_pos)
2627 goto next;
2628
2629 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2630 if (!disk_bytenr)
2631 goto next;
2632
2633 extent_offset = btrfs_file_extent_offset(l, extent);
2634
2635 old = kmalloc(sizeof(*old), GFP_NOFS);
2636 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002637 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002638
2639 offset = max(new->file_pos, key.offset);
2640 end = min(new->file_pos + new->len, key.offset + num_bytes);
2641
2642 old->bytenr = disk_bytenr;
2643 old->extent_offset = extent_offset;
2644 old->offset = offset - key.offset;
2645 old->len = end - offset;
2646 old->new = new;
2647 old->count = 0;
2648 list_add_tail(&old->list, &new->head);
2649next:
2650 path->slots[0]++;
2651 cond_resched();
2652 }
2653
2654 btrfs_free_path(path);
2655 atomic_inc(&root->fs_info->defrag_running);
2656
2657 return new;
2658
Liu Bo38c227d2013-01-29 03:18:40 +00002659out_free_path:
2660 btrfs_free_path(path);
2661out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002662 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002663 return NULL;
2664}
2665
Miao Xiee570fd22014-06-19 10:42:50 +08002666static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2667 u64 start, u64 len)
2668{
2669 struct btrfs_block_group_cache *cache;
2670
2671 cache = btrfs_lookup_block_group(root->fs_info, start);
2672 ASSERT(cache);
2673
2674 spin_lock(&cache->lock);
2675 cache->delalloc_bytes -= len;
2676 spin_unlock(&cache->lock);
2677
2678 btrfs_put_block_group(cache);
2679}
2680
Chris Masond352ac62008-09-29 15:18:18 -04002681/* as ordered data IO finishes, this gets called so we can finish
2682 * an ordered extent if the range of bytes in the file it covers are
2683 * fully written.
2684 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002685static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002686{
Josef Bacik5fd02042012-05-02 14:00:54 -04002687 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002688 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002689 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002690 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002691 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002692 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002693 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002694 int ret = 0;
2695 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002696 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002697 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002698
Liu Bo83eea1f2012-07-10 05:28:39 -06002699 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002700
Josef Bacik5fd02042012-05-02 14:00:54 -04002701 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2702 ret = -EIO;
2703 goto out;
2704 }
2705
Josef Bacik77cef2e2013-08-29 13:57:21 -04002706 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2707 truncated = true;
2708 logical_len = ordered_extent->truncated_len;
2709 /* Truncated the entire extent, don't bother adding */
2710 if (!logical_len)
2711 goto out;
2712 }
2713
Yan, Zhengc2167752009-11-12 09:34:21 +00002714 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002715 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002716 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2717 if (nolock)
2718 trans = btrfs_join_transaction_nolock(root);
2719 else
2720 trans = btrfs_join_transaction(root);
2721 if (IS_ERR(trans)) {
2722 ret = PTR_ERR(trans);
2723 trans = NULL;
2724 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002725 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002726 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2727 ret = btrfs_update_inode_fallback(trans, root, inode);
2728 if (ret) /* -ENOMEM or corruption */
2729 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002730 goto out;
2731 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002732
Josef Bacik2ac55d42010-02-03 19:33:23 +00002733 lock_extent_bits(io_tree, ordered_extent->file_offset,
2734 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002735 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002736
Liu Bo38c227d2013-01-29 03:18:40 +00002737 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2738 ordered_extent->file_offset + ordered_extent->len - 1,
2739 EXTENT_DEFRAG, 1, cached_state);
2740 if (ret) {
2741 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002742 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002743 /* the inode is shared */
2744 new = record_old_file_extents(inode, ordered_extent);
2745
2746 clear_extent_bit(io_tree, ordered_extent->file_offset,
2747 ordered_extent->file_offset + ordered_extent->len - 1,
2748 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2749 }
2750
Josef Bacik0cb59c92010-07-02 12:14:14 -04002751 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002752 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002753 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002754 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002755 if (IS_ERR(trans)) {
2756 ret = PTR_ERR(trans);
2757 trans = NULL;
2758 goto out_unlock;
2759 }
Chris Masona79b7d42014-05-22 16:18:52 -07002760
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002761 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002762
Chris Masonc8b97812008-10-29 14:49:59 -04002763 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002764 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002765 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002766 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002767 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002768 ordered_extent->file_offset,
2769 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002770 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002771 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002772 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002773 ret = insert_reserved_file_extent(trans, inode,
2774 ordered_extent->file_offset,
2775 ordered_extent->start,
2776 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002777 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002778 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002779 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002780 if (!ret)
2781 btrfs_release_delalloc_bytes(root,
2782 ordered_extent->start,
2783 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002784 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002785 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2786 ordered_extent->file_offset, ordered_extent->len,
2787 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002788 if (ret < 0) {
2789 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002790 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002791 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002792
Chris Masone6dcd2d2008-07-17 12:53:50 -04002793 add_pending_csums(trans, inode, ordered_extent->file_offset,
2794 &ordered_extent->list);
2795
Josef Bacik6c760c02012-11-09 10:53:21 -05002796 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2797 ret = btrfs_update_inode_fallback(trans, root, inode);
2798 if (ret) { /* -ENOMEM or corruption */
2799 btrfs_abort_transaction(trans, root, ret);
2800 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002801 }
2802 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002803out_unlock:
2804 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2805 ordered_extent->file_offset +
2806 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002807out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002808 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002809 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002810 if (trans)
2811 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002812
Josef Bacik77cef2e2013-08-29 13:57:21 -04002813 if (ret || truncated) {
2814 u64 start, end;
2815
2816 if (truncated)
2817 start = ordered_extent->file_offset + logical_len;
2818 else
2819 start = ordered_extent->file_offset;
2820 end = ordered_extent->file_offset + ordered_extent->len - 1;
2821 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2822
2823 /* Drop the cache for the part of the extent we didn't write. */
2824 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002825
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002826 /*
2827 * If the ordered extent had an IOERR or something else went
2828 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002829 * back to the allocator. We only free the extent in the
2830 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002831 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002832 if ((ret || !logical_len) &&
2833 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002834 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2835 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002836 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002837 }
2838
2839
Josef Bacik5fd02042012-05-02 14:00:54 -04002840 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002841 * This needs to be done to make sure anybody waiting knows we are done
2842 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002843 */
2844 btrfs_remove_ordered_extent(inode, ordered_extent);
2845
Liu Bo38c227d2013-01-29 03:18:40 +00002846 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002847 if (new) {
2848 if (ret) {
2849 free_sa_defrag_extent(new);
2850 atomic_dec(&root->fs_info->defrag_running);
2851 } else {
2852 relink_file_extents(new);
2853 }
2854 }
Liu Bo38c227d2013-01-29 03:18:40 +00002855
Chris Masone6dcd2d2008-07-17 12:53:50 -04002856 /* once for us */
2857 btrfs_put_ordered_extent(ordered_extent);
2858 /* once for the tree */
2859 btrfs_put_ordered_extent(ordered_extent);
2860
Josef Bacik5fd02042012-05-02 14:00:54 -04002861 return ret;
2862}
2863
2864static void finish_ordered_fn(struct btrfs_work *work)
2865{
2866 struct btrfs_ordered_extent *ordered_extent;
2867 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2868 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002869}
2870
Christoph Hellwigb2950862008-12-02 09:54:17 -05002871static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002872 struct extent_state *state, int uptodate)
2873{
Josef Bacik5fd02042012-05-02 14:00:54 -04002874 struct inode *inode = page->mapping->host;
2875 struct btrfs_root *root = BTRFS_I(inode)->root;
2876 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002877 struct btrfs_workqueue *wq;
2878 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002879
liubo1abe9b82011-03-24 11:18:59 +00002880 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2881
Chris Mason8b62b722009-09-02 16:53:46 -04002882 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002883 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2884 end - start + 1, uptodate))
2885 return 0;
2886
Liu Bo9e0af232014-08-15 23:36:53 +08002887 if (btrfs_is_free_space_inode(inode)) {
2888 wq = root->fs_info->endio_freespace_worker;
2889 func = btrfs_freespace_write_helper;
2890 } else {
2891 wq = root->fs_info->endio_write_workers;
2892 func = btrfs_endio_write_helper;
2893 }
Josef Bacik5fd02042012-05-02 14:00:54 -04002894
Liu Bo9e0af232014-08-15 23:36:53 +08002895 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
2896 NULL);
2897 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04002898
2899 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002900}
2901
Chris Masond352ac62008-09-29 15:18:18 -04002902/*
Chris Masond352ac62008-09-29 15:18:18 -04002903 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002904 * if there's a match, we allow the bio to finish. If not, the code in
2905 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002906 */
Miao Xiefacc8a22013-07-25 19:22:34 +08002907static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2908 u64 phy_offset, struct page *page,
2909 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002910{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002911 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002912 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002913 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002914 char *kaddr;
Chris Masonff79f812007-10-15 16:22:25 -04002915 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiefacc8a22013-07-25 19:22:34 +08002916 u32 csum_expected;
Chris Masonff79f812007-10-15 16:22:25 -04002917 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002918 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2919 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002920
Chris Masond20f7042008-12-08 16:58:54 -05002921 if (PageChecked(page)) {
2922 ClearPageChecked(page);
2923 goto good;
2924 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002925
2926 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002927 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002928
Yan Zheng17d217f2008-12-12 10:03:38 -05002929 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002930 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002931 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2932 GFP_NOFS);
2933 return 0;
2934 }
2935
Miao Xiefacc8a22013-07-25 19:22:34 +08002936 phy_offset >>= inode->i_sb->s_blocksize_bits;
2937 csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
Chris Masond3977122009-01-05 21:25:51 -05002938
Miao Xiefacc8a22013-07-25 19:22:34 +08002939 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00002940 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002941 btrfs_csum_final(csum, (char *)&csum);
Miao Xiefacc8a22013-07-25 19:22:34 +08002942 if (csum != csum_expected)
Chris Mason07157aa2007-08-30 08:50:51 -04002943 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002944
Cong Wang7ac687d2011-11-25 23:14:28 +08002945 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002946good:
Chris Mason07157aa2007-08-30 08:50:51 -04002947 return 0;
2948
2949zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002950 if (__ratelimit(&_rs))
Miao Xiefacc8a22013-07-25 19:22:34 +08002951 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002952 btrfs_ino(page->mapping->host), start, csum, csum_expected);
Chris Masondb945352007-10-15 16:15:53 -04002953 memset(kaddr + offset, 1, end - start + 1);
2954 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002955 kunmap_atomic(kaddr);
Miao Xiefacc8a22013-07-25 19:22:34 +08002956 if (csum_expected == 0)
Chris Mason3b951512008-04-17 11:29:12 -04002957 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002958 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002959}
Chris Masonb888db22007-08-27 16:49:44 -04002960
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002961struct delayed_iput {
2962 struct list_head list;
2963 struct inode *inode;
2964};
2965
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002966/* JDM: If this is fs-wide, why can't we add a pointer to
2967 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002968void btrfs_add_delayed_iput(struct inode *inode)
2969{
2970 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2971 struct delayed_iput *delayed;
2972
2973 if (atomic_add_unless(&inode->i_count, -1, 1))
2974 return;
2975
2976 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2977 delayed->inode = inode;
2978
2979 spin_lock(&fs_info->delayed_iput_lock);
2980 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2981 spin_unlock(&fs_info->delayed_iput_lock);
2982}
2983
2984void btrfs_run_delayed_iputs(struct btrfs_root *root)
2985{
2986 LIST_HEAD(list);
2987 struct btrfs_fs_info *fs_info = root->fs_info;
2988 struct delayed_iput *delayed;
2989 int empty;
2990
2991 spin_lock(&fs_info->delayed_iput_lock);
2992 empty = list_empty(&fs_info->delayed_iputs);
2993 spin_unlock(&fs_info->delayed_iput_lock);
2994 if (empty)
2995 return;
2996
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002997 spin_lock(&fs_info->delayed_iput_lock);
2998 list_splice_init(&fs_info->delayed_iputs, &list);
2999 spin_unlock(&fs_info->delayed_iput_lock);
3000
3001 while (!list_empty(&list)) {
3002 delayed = list_entry(list.next, struct delayed_iput, list);
3003 list_del(&delayed->list);
3004 iput(delayed->inode);
3005 kfree(delayed);
3006 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003007}
3008
Yan, Zhengd68fc572010-05-16 10:49:58 -04003009/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003010 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003011 * files in the subvolume, it removes orphan item and frees block_rsv
3012 * structure.
3013 */
3014void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3015 struct btrfs_root *root)
3016{
Josef Bacik90290e12011-12-02 15:44:12 -05003017 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003018 int ret;
3019
Josef Bacik8a35d952012-05-23 14:26:42 -04003020 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003021 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3022 return;
3023
Josef Bacik90290e12011-12-02 15:44:12 -05003024 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003025 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003026 spin_unlock(&root->orphan_lock);
3027 return;
3028 }
3029
3030 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3031 spin_unlock(&root->orphan_lock);
3032 return;
3033 }
3034
3035 block_rsv = root->orphan_block_rsv;
3036 root->orphan_block_rsv = NULL;
3037 spin_unlock(&root->orphan_lock);
3038
Miao Xie27cdeb72014-04-02 19:51:05 +08003039 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003040 btrfs_root_refs(&root->root_item) > 0) {
3041 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3042 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003043 if (ret)
3044 btrfs_abort_transaction(trans, root, ret);
3045 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003046 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3047 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003048 }
3049
Josef Bacik90290e12011-12-02 15:44:12 -05003050 if (block_rsv) {
3051 WARN_ON(block_rsv->size > 0);
3052 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003053 }
3054}
3055
3056/*
Josef Bacik7b128762008-07-24 12:17:14 -04003057 * This creates an orphan entry for the given inode in case something goes
3058 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003059 *
3060 * NOTE: caller of this function should reserve 5 units of metadata for
3061 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003062 */
3063int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3064{
3065 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003066 struct btrfs_block_rsv *block_rsv = NULL;
3067 int reserve = 0;
3068 int insert = 0;
3069 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003070
Yan, Zhengd68fc572010-05-16 10:49:58 -04003071 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003072 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003073 if (!block_rsv)
3074 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003075 }
3076
Yan, Zhengd68fc572010-05-16 10:49:58 -04003077 spin_lock(&root->orphan_lock);
3078 if (!root->orphan_block_rsv) {
3079 root->orphan_block_rsv = block_rsv;
3080 } else if (block_rsv) {
3081 btrfs_free_block_rsv(root, block_rsv);
3082 block_rsv = NULL;
3083 }
Josef Bacik7b128762008-07-24 12:17:14 -04003084
Josef Bacik8a35d952012-05-23 14:26:42 -04003085 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3086 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003087#if 0
3088 /*
3089 * For proper ENOSPC handling, we should do orphan
3090 * cleanup when mounting. But this introduces backward
3091 * compatibility issue.
3092 */
3093 if (!xchg(&root->orphan_item_inserted, 1))
3094 insert = 2;
3095 else
3096 insert = 1;
3097#endif
3098 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003099 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003100 }
Josef Bacik7b128762008-07-24 12:17:14 -04003101
Josef Bacik72ac3c02012-05-23 14:13:11 -04003102 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3103 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003104 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003105 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003106
Yan, Zhengd68fc572010-05-16 10:49:58 -04003107 /* grab metadata reservation from transaction handle */
3108 if (reserve) {
3109 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003110 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003111 }
3112
3113 /* insert an orphan item to track this unlinked/truncated file */
3114 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003115 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003116 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003117 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003118 if (reserve) {
3119 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3120 &BTRFS_I(inode)->runtime_flags);
3121 btrfs_orphan_release_metadata(inode);
3122 }
3123 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003124 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3125 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003126 btrfs_abort_transaction(trans, root, ret);
3127 return ret;
3128 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003129 }
3130 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003131 }
3132
3133 /* insert an orphan item to track subvolume contains orphan files */
3134 if (insert >= 2) {
3135 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3136 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003137 if (ret && ret != -EEXIST) {
3138 btrfs_abort_transaction(trans, root, ret);
3139 return ret;
3140 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003141 }
3142 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003143}
3144
3145/*
3146 * We have done the truncate/delete so we can go ahead and remove the orphan
3147 * item for this particular inode.
3148 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003149static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3150 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003151{
3152 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003153 int delete_item = 0;
3154 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003155 int ret = 0;
3156
Yan, Zhengd68fc572010-05-16 10:49:58 -04003157 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003158 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3159 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003160 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003161
Josef Bacik72ac3c02012-05-23 14:13:11 -04003162 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3163 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003164 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003165 spin_unlock(&root->orphan_lock);
3166
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003167 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003168 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003169 if (trans)
3170 ret = btrfs_del_orphan_item(trans, root,
3171 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003172 }
Josef Bacik7b128762008-07-24 12:17:14 -04003173
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003174 if (release_rsv)
3175 btrfs_orphan_release_metadata(inode);
3176
Josef Bacik4ef31a42013-08-13 14:10:08 -04003177 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003178}
3179
3180/*
3181 * this cleans up any orphans that may be left on the list from the last use
3182 * of this root.
3183 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003184int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003185{
3186 struct btrfs_path *path;
3187 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003188 struct btrfs_key key, found_key;
3189 struct btrfs_trans_handle *trans;
3190 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003191 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003192 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3193
Yan, Zhengd68fc572010-05-16 10:49:58 -04003194 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003195 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003196
3197 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003198 if (!path) {
3199 ret = -ENOMEM;
3200 goto out;
3201 }
Josef Bacik7b128762008-07-24 12:17:14 -04003202 path->reada = -1;
3203
3204 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003205 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003206 key.offset = (u64)-1;
3207
Josef Bacik7b128762008-07-24 12:17:14 -04003208 while (1) {
3209 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003210 if (ret < 0)
3211 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003212
3213 /*
3214 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003215 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003216 * find the key and see if we have stuff that matches
3217 */
3218 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003219 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003220 if (path->slots[0] == 0)
3221 break;
3222 path->slots[0]--;
3223 }
3224
3225 /* pull out the item */
3226 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003227 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3228
3229 /* make sure the item matches what we want */
3230 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3231 break;
David Sterba962a2982014-06-04 18:41:45 +02003232 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003233 break;
3234
3235 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003236 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003237
3238 /*
3239 * this is where we are basically btrfs_lookup, without the
3240 * crossing root thing. we store the inode number in the
3241 * offset of the orphan item.
3242 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003243
3244 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003245 btrfs_err(root->fs_info,
3246 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003247 ret = -EINVAL;
3248 goto out;
3249 }
3250
3251 last_objectid = found_key.offset;
3252
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003253 found_key.objectid = found_key.offset;
3254 found_key.type = BTRFS_INODE_ITEM_KEY;
3255 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003256 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303257 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003258 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003259 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003260
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003261 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3262 struct btrfs_root *dead_root;
3263 struct btrfs_fs_info *fs_info = root->fs_info;
3264 int is_dead_root = 0;
3265
3266 /*
3267 * this is an orphan in the tree root. Currently these
3268 * could come from 2 sources:
3269 * a) a snapshot deletion in progress
3270 * b) a free space cache inode
3271 * We need to distinguish those two, as the snapshot
3272 * orphan must not get deleted.
3273 * find_dead_roots already ran before us, so if this
3274 * is a snapshot deletion, we should find the root
3275 * in the dead_roots list
3276 */
3277 spin_lock(&fs_info->trans_lock);
3278 list_for_each_entry(dead_root, &fs_info->dead_roots,
3279 root_list) {
3280 if (dead_root->root_key.objectid ==
3281 found_key.objectid) {
3282 is_dead_root = 1;
3283 break;
3284 }
3285 }
3286 spin_unlock(&fs_info->trans_lock);
3287 if (is_dead_root) {
3288 /* prevent this orphan from being found again */
3289 key.offset = found_key.objectid - 1;
3290 continue;
3291 }
3292 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003293 /*
3294 * Inode is already gone but the orphan item is still there,
3295 * kill the orphan item.
3296 */
3297 if (ret == -ESTALE) {
3298 trans = btrfs_start_transaction(root, 1);
3299 if (IS_ERR(trans)) {
3300 ret = PTR_ERR(trans);
3301 goto out;
3302 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003303 btrfs_debug(root->fs_info, "auto deleting %Lu",
3304 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003305 ret = btrfs_del_orphan_item(trans, root,
3306 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003307 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003308 if (ret)
3309 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003310 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003311 }
Josef Bacik7b128762008-07-24 12:17:14 -04003312
Josef Bacik7b128762008-07-24 12:17:14 -04003313 /*
3314 * add this inode to the orphan list so btrfs_orphan_del does
3315 * the proper thing when we hit it
3316 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003317 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3318 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003319 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003320
Josef Bacik7b128762008-07-24 12:17:14 -04003321 /* if we have links, this was a truncate, lets do that */
3322 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303323 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003324 iput(inode);
3325 continue;
3326 }
Josef Bacik7b128762008-07-24 12:17:14 -04003327 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003328
3329 /* 1 for the orphan item deletion. */
3330 trans = btrfs_start_transaction(root, 1);
3331 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003332 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003333 ret = PTR_ERR(trans);
3334 goto out;
3335 }
3336 ret = btrfs_orphan_add(trans, inode);
3337 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003338 if (ret) {
3339 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003340 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003341 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003342
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003343 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003344 if (ret)
3345 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003346 } else {
3347 nr_unlink++;
3348 }
3349
3350 /* this will do delete_inode and everything for us */
3351 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003352 if (ret)
3353 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003354 }
Miao Xie3254c872011-11-10 20:45:05 -05003355 /* release the path since we're done with it */
3356 btrfs_release_path(path);
3357
Yan, Zhengd68fc572010-05-16 10:49:58 -04003358 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3359
3360 if (root->orphan_block_rsv)
3361 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3362 (u64)-1);
3363
Miao Xie27cdeb72014-04-02 19:51:05 +08003364 if (root->orphan_block_rsv ||
3365 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003366 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003367 if (!IS_ERR(trans))
3368 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003369 }
Josef Bacik7b128762008-07-24 12:17:14 -04003370
3371 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003372 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003373 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003374 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003375
3376out:
3377 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003378 btrfs_crit(root->fs_info,
3379 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003380 btrfs_free_path(path);
3381 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003382}
3383
Chris Masond352ac62008-09-29 15:18:18 -04003384/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003385 * very simple check to peek ahead in the leaf looking for xattrs. If we
3386 * don't find any xattrs, we know there can't be any acls.
3387 *
3388 * slot is the slot the inode is in, objectid is the objectid of the inode
3389 */
3390static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003391 int slot, u64 objectid,
3392 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003393{
3394 u32 nritems = btrfs_header_nritems(leaf);
3395 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003396 static u64 xattr_access = 0;
3397 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003398 int scanned = 0;
3399
Josef Bacikf23b5a52013-06-19 10:16:26 -04003400 if (!xattr_access) {
3401 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3402 strlen(POSIX_ACL_XATTR_ACCESS));
3403 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3404 strlen(POSIX_ACL_XATTR_DEFAULT));
3405 }
3406
Chris Mason46a53cc2009-04-27 11:47:50 -04003407 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003408 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003409 while (slot < nritems) {
3410 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3411
3412 /* we found a different objectid, there must not be acls */
3413 if (found_key.objectid != objectid)
3414 return 0;
3415
3416 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003417 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003418 if (*first_xattr_slot == -1)
3419 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003420 if (found_key.offset == xattr_access ||
3421 found_key.offset == xattr_default)
3422 return 1;
3423 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003424
3425 /*
3426 * we found a key greater than an xattr key, there can't
3427 * be any acls later on
3428 */
3429 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3430 return 0;
3431
3432 slot++;
3433 scanned++;
3434
3435 /*
3436 * it goes inode, inode backrefs, xattrs, extents,
3437 * so if there are a ton of hard links to an inode there can
3438 * be a lot of backrefs. Don't waste time searching too hard,
3439 * this is just an optimization
3440 */
3441 if (scanned >= 8)
3442 break;
3443 }
3444 /* we hit the end of the leaf before we found an xattr or
3445 * something larger than an xattr. We have to assume the inode
3446 * has acls
3447 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003448 if (*first_xattr_slot == -1)
3449 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003450 return 1;
3451}
3452
3453/*
Chris Masond352ac62008-09-29 15:18:18 -04003454 * read an inode from the btree into the in-memory inode
3455 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003456static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003457{
3458 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003459 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003460 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003461 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003462 struct btrfs_root *root = BTRFS_I(inode)->root;
3463 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003464 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003465 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003466 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003467 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003468 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003469 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003470
3471 ret = btrfs_fill_inode(inode, &rdev);
3472 if (!ret)
3473 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003474
3475 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003476 if (!path)
3477 goto make_bad;
3478
Chris Mason39279cc2007-06-12 06:35:45 -04003479 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003480
Chris Mason39279cc2007-06-12 06:35:45 -04003481 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003482 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003483 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003484
Chris Mason5f39d392007-10-15 16:14:19 -04003485 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003486
3487 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003488 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003489
Chris Mason5f39d392007-10-15 16:14:19 -04003490 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3491 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003492 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003493 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003494 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3495 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003496 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003497
3498 tspec = btrfs_inode_atime(inode_item);
3499 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3500 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3501
3502 tspec = btrfs_inode_mtime(inode_item);
3503 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3504 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3505
3506 tspec = btrfs_inode_ctime(inode_item);
3507 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3508 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3509
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003510 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003511 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003512 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3513
3514 /*
3515 * If we were modified in the current generation and evicted from memory
3516 * and then re-read we need to do a full sync since we don't have any
3517 * idea about which extents were modified before we were evicted from
3518 * cache.
3519 */
3520 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3521 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3522 &BTRFS_I(inode)->runtime_flags);
3523
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003524 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003525 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003526 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003527 rdev = btrfs_inode_rdev(leaf, inode_item);
3528
Josef Bacikaec74772008-07-24 12:12:38 -04003529 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003530 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003531
3532cache_index:
3533 path->slots[0]++;
3534 if (inode->i_nlink != 1 ||
3535 path->slots[0] >= btrfs_header_nritems(leaf))
3536 goto cache_acl;
3537
3538 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3539 if (location.objectid != btrfs_ino(inode))
3540 goto cache_acl;
3541
3542 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3543 if (location.type == BTRFS_INODE_REF_KEY) {
3544 struct btrfs_inode_ref *ref;
3545
3546 ref = (struct btrfs_inode_ref *)ptr;
3547 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3548 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3549 struct btrfs_inode_extref *extref;
3550
3551 extref = (struct btrfs_inode_extref *)ptr;
3552 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3553 extref);
3554 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003555cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003556 /*
3557 * try to precache a NULL acl entry for files that don't have
3558 * any xattrs or acls
3559 */
Li Zefan33345d012011-04-20 10:31:50 +08003560 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003561 btrfs_ino(inode), &first_xattr_slot);
3562 if (first_xattr_slot != -1) {
3563 path->slots[0] = first_xattr_slot;
3564 ret = btrfs_load_inode_props(inode, path);
3565 if (ret)
3566 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003567 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003568 btrfs_ino(inode),
3569 root->root_key.objectid, ret);
3570 }
3571 btrfs_free_path(path);
3572
Al Viro72c04902009-06-24 16:58:48 -04003573 if (!maybe_acls)
3574 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003575
Chris Mason39279cc2007-06-12 06:35:45 -04003576 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003577 case S_IFREG:
3578 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003579 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003580 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003581 inode->i_fop = &btrfs_file_operations;
3582 inode->i_op = &btrfs_file_inode_operations;
3583 break;
3584 case S_IFDIR:
3585 inode->i_fop = &btrfs_dir_file_operations;
3586 if (root == root->fs_info->tree_root)
3587 inode->i_op = &btrfs_dir_ro_inode_operations;
3588 else
3589 inode->i_op = &btrfs_dir_inode_operations;
3590 break;
3591 case S_IFLNK:
3592 inode->i_op = &btrfs_symlink_inode_operations;
3593 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003594 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003595 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003596 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003597 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003598 init_special_inode(inode, inode->i_mode, rdev);
3599 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003600 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003601
3602 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003603 return;
3604
3605make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003606 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003607 make_bad_inode(inode);
3608}
3609
Chris Masond352ac62008-09-29 15:18:18 -04003610/*
3611 * given a leaf and an inode, copy the inode fields into the leaf
3612 */
Chris Masone02119d2008-09-05 16:13:11 -04003613static void fill_inode_item(struct btrfs_trans_handle *trans,
3614 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003615 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003616 struct inode *inode)
3617{
Liu Bo51fab692012-12-27 09:01:21 +00003618 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003619
Liu Bo51fab692012-12-27 09:01:21 +00003620 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003621
Liu Bo51fab692012-12-27 09:01:21 +00003622 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3623 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3624 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3625 &token);
3626 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3627 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003628
Liu Bo51fab692012-12-27 09:01:21 +00003629 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3630 inode->i_atime.tv_sec, &token);
3631 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3632 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003633
Liu Bo51fab692012-12-27 09:01:21 +00003634 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3635 inode->i_mtime.tv_sec, &token);
3636 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3637 inode->i_mtime.tv_nsec, &token);
3638
3639 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3640 inode->i_ctime.tv_sec, &token);
3641 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3642 inode->i_ctime.tv_nsec, &token);
3643
3644 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3645 &token);
3646 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3647 &token);
3648 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3649 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3650 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3651 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3652 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003653}
3654
Chris Masond352ac62008-09-29 15:18:18 -04003655/*
3656 * copy everything in the in-memory inode into the btree.
3657 */
Chris Mason21151332011-11-10 20:39:08 -05003658static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003659 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003660{
3661 struct btrfs_inode_item *inode_item;
3662 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003663 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003664 int ret;
3665
3666 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003667 if (!path)
3668 return -ENOMEM;
3669
Chris Masonb9473432009-03-13 11:00:37 -04003670 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003671 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3672 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003673 if (ret) {
3674 if (ret > 0)
3675 ret = -ENOENT;
3676 goto failed;
3677 }
3678
Chris Mason5f39d392007-10-15 16:14:19 -04003679 leaf = path->nodes[0];
3680 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003681 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003682
Chris Masone02119d2008-09-05 16:13:11 -04003683 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003684 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003685 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003686 ret = 0;
3687failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003688 btrfs_free_path(path);
3689 return ret;
3690}
3691
Chris Masond352ac62008-09-29 15:18:18 -04003692/*
Chris Mason21151332011-11-10 20:39:08 -05003693 * copy everything in the in-memory inode into the btree.
3694 */
3695noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3696 struct btrfs_root *root, struct inode *inode)
3697{
3698 int ret;
3699
3700 /*
3701 * If the inode is a free space inode, we can deadlock during commit
3702 * if we put it into the delayed code.
3703 *
3704 * The data relocation inode should also be directly updated
3705 * without delay
3706 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003707 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003708 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003709 btrfs_update_root_times(trans, root);
3710
Chris Mason21151332011-11-10 20:39:08 -05003711 ret = btrfs_delayed_update_inode(trans, root, inode);
3712 if (!ret)
3713 btrfs_set_inode_last_trans(trans, inode);
3714 return ret;
3715 }
3716
3717 return btrfs_update_inode_item(trans, root, inode);
3718}
3719
Josef Bacikbe6aef62012-10-22 15:43:12 -04003720noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3721 struct btrfs_root *root,
3722 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003723{
3724 int ret;
3725
3726 ret = btrfs_update_inode(trans, root, inode);
3727 if (ret == -ENOSPC)
3728 return btrfs_update_inode_item(trans, root, inode);
3729 return ret;
3730}
3731
3732/*
Chris Masond352ac62008-09-29 15:18:18 -04003733 * unlink helper that gets used here in inode.c and in the tree logging
3734 * recovery code. It remove a link in a directory with a given name, and
3735 * also drops the back refs in the inode to the directory
3736 */
Al Viro92986792011-03-04 17:14:37 +00003737static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3738 struct btrfs_root *root,
3739 struct inode *dir, struct inode *inode,
3740 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003741{
3742 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003743 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003744 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003745 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003746 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003747 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003748 u64 ino = btrfs_ino(inode);
3749 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003750
3751 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003752 if (!path) {
3753 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003754 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003755 }
3756
Chris Masonb9473432009-03-13 11:00:37 -04003757 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003758 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003759 name, name_len, -1);
3760 if (IS_ERR(di)) {
3761 ret = PTR_ERR(di);
3762 goto err;
3763 }
3764 if (!di) {
3765 ret = -ENOENT;
3766 goto err;
3767 }
Chris Mason5f39d392007-10-15 16:14:19 -04003768 leaf = path->nodes[0];
3769 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003770 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003771 if (ret)
3772 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003773 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003774
Miao Xie67de1172013-12-26 13:07:06 +08003775 /*
3776 * If we don't have dir index, we have to get it by looking up
3777 * the inode ref, since we get the inode ref, remove it directly,
3778 * it is unnecessary to do delayed deletion.
3779 *
3780 * But if we have dir index, needn't search inode ref to get it.
3781 * Since the inode ref is close to the inode item, it is better
3782 * that we delay to delete it, and just do this deletion when
3783 * we update the inode item.
3784 */
3785 if (BTRFS_I(inode)->dir_index) {
3786 ret = btrfs_delayed_delete_inode_ref(inode);
3787 if (!ret) {
3788 index = BTRFS_I(inode)->dir_index;
3789 goto skip_backref;
3790 }
3791 }
3792
Li Zefan33345d012011-04-20 10:31:50 +08003793 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3794 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003795 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003796 btrfs_info(root->fs_info,
3797 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003798 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003799 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003800 goto err;
3801 }
Miao Xie67de1172013-12-26 13:07:06 +08003802skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003803 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003804 if (ret) {
3805 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003806 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003807 }
Chris Mason39279cc2007-06-12 06:35:45 -04003808
Chris Masone02119d2008-09-05 16:13:11 -04003809 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003810 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003811 if (ret != 0 && ret != -ENOENT) {
3812 btrfs_abort_transaction(trans, root, ret);
3813 goto err;
3814 }
Chris Masone02119d2008-09-05 16:13:11 -04003815
3816 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3817 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003818 if (ret == -ENOENT)
3819 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003820 else if (ret)
3821 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003822err:
3823 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003824 if (ret)
3825 goto out;
3826
3827 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003828 inode_inc_iversion(inode);
3829 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003830 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003831 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003832out:
Chris Mason39279cc2007-06-12 06:35:45 -04003833 return ret;
3834}
3835
Al Viro92986792011-03-04 17:14:37 +00003836int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3837 struct btrfs_root *root,
3838 struct inode *dir, struct inode *inode,
3839 const char *name, int name_len)
3840{
3841 int ret;
3842 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3843 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003844 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003845 ret = btrfs_update_inode(trans, root, inode);
3846 }
3847 return ret;
3848}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003849
3850/*
3851 * helper to start transaction for unlink and rmdir.
3852 *
Josef Bacikd52be812013-05-29 14:54:47 -04003853 * unlink and rmdir are special in btrfs, they do not always free space, so
3854 * if we cannot make our reservations the normal way try and see if there is
3855 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3856 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003857 */
Josef Bacikd52be812013-05-29 14:54:47 -04003858static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003859{
3860 struct btrfs_trans_handle *trans;
3861 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003862 int ret;
3863
Josef Bacike70bea52011-10-11 14:18:24 -04003864 /*
3865 * 1 for the possible orphan item
3866 * 1 for the dir item
3867 * 1 for the dir index
3868 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003869 * 1 for the inode
3870 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003871 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003872 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3873 return trans;
3874
Josef Bacikd52be812013-05-29 14:54:47 -04003875 if (PTR_ERR(trans) == -ENOSPC) {
3876 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003877
Josef Bacikd52be812013-05-29 14:54:47 -04003878 trans = btrfs_start_transaction(root, 0);
3879 if (IS_ERR(trans))
3880 return trans;
3881 ret = btrfs_cond_migrate_bytes(root->fs_info,
3882 &root->fs_info->trans_block_rsv,
3883 num_bytes, 5);
3884 if (ret) {
3885 btrfs_end_transaction(trans, root);
3886 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003887 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003888 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003889 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003890 }
Josef Bacikd52be812013-05-29 14:54:47 -04003891 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003892}
3893
Chris Mason39279cc2007-06-12 06:35:45 -04003894static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3895{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003896 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003897 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003898 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003899 int ret;
3900
Josef Bacikd52be812013-05-29 14:54:47 -04003901 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003902 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003903 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003904
Chris Mason12fcfd22009-03-24 10:24:20 -04003905 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3906
Chris Masone02119d2008-09-05 16:13:11 -04003907 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3908 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003909 if (ret)
3910 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003911
Yan, Zhenga22285a2010-05-16 10:48:46 -04003912 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003913 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003914 if (ret)
3915 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003916 }
Josef Bacik7b128762008-07-24 12:17:14 -04003917
Tsutomu Itohb5324022011-07-19 07:27:20 +00003918out:
Josef Bacikd52be812013-05-29 14:54:47 -04003919 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003920 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003921 return ret;
3922}
3923
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003924int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3925 struct btrfs_root *root,
3926 struct inode *dir, u64 objectid,
3927 const char *name, int name_len)
3928{
3929 struct btrfs_path *path;
3930 struct extent_buffer *leaf;
3931 struct btrfs_dir_item *di;
3932 struct btrfs_key key;
3933 u64 index;
3934 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003935 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003936
3937 path = btrfs_alloc_path();
3938 if (!path)
3939 return -ENOMEM;
3940
Li Zefan33345d012011-04-20 10:31:50 +08003941 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003942 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003943 if (IS_ERR_OR_NULL(di)) {
3944 if (!di)
3945 ret = -ENOENT;
3946 else
3947 ret = PTR_ERR(di);
3948 goto out;
3949 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003950
3951 leaf = path->nodes[0];
3952 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3953 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3954 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003955 if (ret) {
3956 btrfs_abort_transaction(trans, root, ret);
3957 goto out;
3958 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003959 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003960
3961 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3962 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003963 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003964 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003965 if (ret != -ENOENT) {
3966 btrfs_abort_transaction(trans, root, ret);
3967 goto out;
3968 }
Li Zefan33345d012011-04-20 10:31:50 +08003969 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003970 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003971 if (IS_ERR_OR_NULL(di)) {
3972 if (!di)
3973 ret = -ENOENT;
3974 else
3975 ret = PTR_ERR(di);
3976 btrfs_abort_transaction(trans, root, ret);
3977 goto out;
3978 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003979
3980 leaf = path->nodes[0];
3981 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003982 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003983 index = key.offset;
3984 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003985 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003986
Miao Xie16cdcec2011-04-22 18:12:22 +08003987 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003988 if (ret) {
3989 btrfs_abort_transaction(trans, root, ret);
3990 goto out;
3991 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003992
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003993 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003994 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003995 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003996 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003997 if (ret)
3998 btrfs_abort_transaction(trans, root, ret);
3999out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004000 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004001 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004002}
4003
Chris Mason39279cc2007-06-12 06:35:45 -04004004static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4005{
4006 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004007 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004008 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004009 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004010
David Sterbab3ae2442012-09-13 16:04:34 -06004011 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004012 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004013 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4014 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004015
Josef Bacikd52be812013-05-29 14:54:47 -04004016 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004017 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004018 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004019
Li Zefan33345d012011-04-20 10:31:50 +08004020 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004021 err = btrfs_unlink_subvol(trans, root, dir,
4022 BTRFS_I(inode)->location.objectid,
4023 dentry->d_name.name,
4024 dentry->d_name.len);
4025 goto out;
4026 }
4027
Josef Bacik7b128762008-07-24 12:17:14 -04004028 err = btrfs_orphan_add(trans, inode);
4029 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004030 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004031
Chris Mason39279cc2007-06-12 06:35:45 -04004032 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004033 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4034 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004035 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004036 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004037out:
Josef Bacikd52be812013-05-29 14:54:47 -04004038 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004039 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004040
Chris Mason39279cc2007-06-12 06:35:45 -04004041 return err;
4042}
4043
Chris Mason323ac952008-10-01 19:05:46 -04004044/*
Chris Mason39279cc2007-06-12 06:35:45 -04004045 * this can truncate away extent items, csum items and directory items.
4046 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004047 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004048 *
4049 * csum items that cross the new i_size are truncated to the new size
4050 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004051 *
4052 * min_type is the minimum key type to truncate down to. If set to 0, this
4053 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004054 */
Yan, Zheng80825102009-11-12 09:35:36 +00004055int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4056 struct btrfs_root *root,
4057 struct inode *inode,
4058 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004059{
Chris Mason39279cc2007-06-12 06:35:45 -04004060 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004061 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004062 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004063 struct btrfs_key key;
4064 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004065 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004066 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004067 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004068 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004069 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00004070 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004071 int found_extent;
4072 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004073 int pending_del_nr = 0;
4074 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004075 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004076 int ret;
4077 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004078 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004079
4080 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004081
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004082 path = btrfs_alloc_path();
4083 if (!path)
4084 return -ENOMEM;
4085 path->reada = -1;
4086
Josef Bacik5dc562c2012-08-17 13:14:17 -04004087 /*
4088 * We want to drop from the next block forward in case this new size is
4089 * not block aligned since we will be keeping the last block of the
4090 * extent just the way it is.
4091 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004092 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4093 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004094 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4095 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004096
Miao Xie16cdcec2011-04-22 18:12:22 +08004097 /*
4098 * This function is also used to drop the items in the log tree before
4099 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4100 * it is used to drop the loged items. So we shouldn't kill the delayed
4101 * items.
4102 */
4103 if (min_type == 0 && root == BTRFS_I(inode)->root)
4104 btrfs_kill_delayed_inode_items(inode);
4105
Li Zefan33345d012011-04-20 10:31:50 +08004106 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004107 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004108 key.type = (u8)-1;
4109
Chris Mason85e21ba2008-01-29 15:11:36 -05004110search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004111 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004112 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004113 if (ret < 0) {
4114 err = ret;
4115 goto out;
4116 }
Chris Masond3977122009-01-05 21:25:51 -05004117
Chris Mason85e21ba2008-01-29 15:11:36 -05004118 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004119 /* there are no items in the tree for us to truncate, we're
4120 * done
4121 */
Yan, Zheng80825102009-11-12 09:35:36 +00004122 if (path->slots[0] == 0)
4123 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004124 path->slots[0]--;
4125 }
4126
Chris Masond3977122009-01-05 21:25:51 -05004127 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004128 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004129 leaf = path->nodes[0];
4130 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004131 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004132
Li Zefan33345d012011-04-20 10:31:50 +08004133 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004134 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004135
Chris Mason85e21ba2008-01-29 15:11:36 -05004136 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004137 break;
4138
Chris Mason5f39d392007-10-15 16:14:19 -04004139 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004140 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004141 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004142 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004143 extent_type = btrfs_file_extent_type(leaf, fi);
4144 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004145 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004146 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004147 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004148 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004149 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004150 }
Yan008630c2007-11-07 13:31:09 -05004151 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004152 }
Yan, Zheng80825102009-11-12 09:35:36 +00004153 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004154 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004155 } else {
4156 if (item_end < new_size)
4157 break;
4158 if (found_key.offset >= new_size)
4159 del_item = 1;
4160 else
4161 del_item = 0;
4162 }
Chris Mason39279cc2007-06-12 06:35:45 -04004163 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004164 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004165 if (found_type != BTRFS_EXTENT_DATA_KEY)
4166 goto delete;
4167
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004168 if (del_item)
4169 last_size = found_key.offset;
4170 else
4171 last_size = new_size;
4172
Chris Mason179e29e2007-11-01 11:28:41 -04004173 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004174 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004175 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004176 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004177 u64 orig_num_bytes =
4178 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004179 extent_num_bytes = ALIGN(new_size -
4180 found_key.offset,
4181 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004182 btrfs_set_file_extent_num_bytes(leaf, fi,
4183 extent_num_bytes);
4184 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004185 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004186 if (test_bit(BTRFS_ROOT_REF_COWS,
4187 &root->state) &&
4188 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004189 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004190 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004191 } else {
Chris Masondb945352007-10-15 16:15:53 -04004192 extent_num_bytes =
4193 btrfs_file_extent_disk_num_bytes(leaf,
4194 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004195 extent_offset = found_key.offset -
4196 btrfs_file_extent_offset(leaf, fi);
4197
Chris Mason39279cc2007-06-12 06:35:45 -04004198 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004199 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004200 if (extent_start != 0) {
4201 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004202 if (test_bit(BTRFS_ROOT_REF_COWS,
4203 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004204 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004205 }
4206 }
Chris Mason90692182008-02-08 13:49:28 -05004207 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004208 /*
4209 * we can't truncate inline items that have had
4210 * special encodings
4211 */
4212 if (!del_item &&
4213 btrfs_file_extent_compression(leaf, fi) == 0 &&
4214 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4215 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004216 u32 size = new_size - found_key.offset;
4217
Miao Xie27cdeb72014-04-02 19:51:05 +08004218 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004219 inode_sub_bytes(inode, item_end + 1 -
4220 new_size);
Chris Mason514ac8a2014-01-03 21:07:00 -08004221
4222 /*
4223 * update the ram bytes to properly reflect
4224 * the new size of our item
4225 */
4226 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004227 size =
4228 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004229 btrfs_truncate_item(root, path, size, 1);
Miao Xie27cdeb72014-04-02 19:51:05 +08004230 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4231 &root->state)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004232 inode_sub_bytes(inode, item_end + 1 -
4233 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004234 }
Chris Mason39279cc2007-06-12 06:35:45 -04004235 }
Chris Mason179e29e2007-11-01 11:28:41 -04004236delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004237 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004238 if (!pending_del_nr) {
4239 /* no pending yet, add ourselves */
4240 pending_del_slot = path->slots[0];
4241 pending_del_nr = 1;
4242 } else if (pending_del_nr &&
4243 path->slots[0] + 1 == pending_del_slot) {
4244 /* hop on the pending chunk */
4245 pending_del_nr++;
4246 pending_del_slot = path->slots[0];
4247 } else {
Chris Masond3977122009-01-05 21:25:51 -05004248 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004249 }
Chris Mason39279cc2007-06-12 06:35:45 -04004250 } else {
4251 break;
4252 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004253 if (found_extent &&
4254 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4255 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004256 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004257 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004258 extent_num_bytes, 0,
4259 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004260 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004261 BUG_ON(ret);
4262 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004263
Yan, Zheng80825102009-11-12 09:35:36 +00004264 if (found_type == BTRFS_INODE_ITEM_KEY)
4265 break;
4266
4267 if (path->slots[0] == 0 ||
4268 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004269 if (pending_del_nr) {
4270 ret = btrfs_del_items(trans, root, path,
4271 pending_del_slot,
4272 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004273 if (ret) {
4274 btrfs_abort_transaction(trans,
4275 root, ret);
4276 goto error;
4277 }
Yan, Zheng80825102009-11-12 09:35:36 +00004278 pending_del_nr = 0;
4279 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004280 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004281 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004282 } else {
4283 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004284 }
Chris Mason39279cc2007-06-12 06:35:45 -04004285 }
Yan, Zheng80825102009-11-12 09:35:36 +00004286out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004287 if (pending_del_nr) {
4288 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4289 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004290 if (ret)
4291 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004292 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004293error:
Filipe Mananadac57052014-08-29 20:54:26 +01004294 if (last_size != (u64)-1 &&
4295 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004296 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004297 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004298 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004299}
4300
Chris Masona52d9a82007-08-27 16:49:44 -04004301/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004302 * btrfs_truncate_page - read, zero a chunk and write a page
4303 * @inode - inode that we're zeroing
4304 * @from - the offset to start zeroing
4305 * @len - the length to zero, 0 to zero the entire range respective to the
4306 * offset
4307 * @front - zero up to the offset instead of from the offset on
4308 *
4309 * This will find the page for the "from" offset and cow the page and zero the
4310 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004311 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004312int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4313 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004314{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004315 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004316 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004317 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4318 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004319 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004320 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004321 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004322 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4323 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4324 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004325 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004326 int ret = 0;
4327 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004328 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004329
Josef Bacik2aaa6652012-08-29 14:27:18 -04004330 if ((offset & (blocksize - 1)) == 0 &&
4331 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004332 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004333 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004334 if (ret)
4335 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004336
Chris Mason211c17f2008-05-15 09:13:45 -04004337again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004338 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004339 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004340 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004341 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004342 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004343 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004344
4345 page_start = page_offset(page);
4346 page_end = page_start + PAGE_CACHE_SIZE - 1;
4347
Chris Masona52d9a82007-08-27 16:49:44 -04004348 if (!PageUptodate(page)) {
4349 ret = btrfs_readpage(NULL, page);
4350 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004351 if (page->mapping != mapping) {
4352 unlock_page(page);
4353 page_cache_release(page);
4354 goto again;
4355 }
Chris Masona52d9a82007-08-27 16:49:44 -04004356 if (!PageUptodate(page)) {
4357 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004358 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004359 }
4360 }
Chris Mason211c17f2008-05-15 09:13:45 -04004361 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004362
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004363 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004364 set_page_extent_mapped(page);
4365
4366 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4367 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004368 unlock_extent_cached(io_tree, page_start, page_end,
4369 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004370 unlock_page(page);
4371 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004372 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004373 btrfs_put_ordered_extent(ordered);
4374 goto again;
4375 }
4376
Josef Bacik2ac55d42010-02-03 19:33:23 +00004377 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004378 EXTENT_DIRTY | EXTENT_DELALLOC |
4379 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004380 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004381
Josef Bacik2ac55d42010-02-03 19:33:23 +00004382 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4383 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004384 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004385 unlock_extent_cached(io_tree, page_start, page_end,
4386 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004387 goto out_unlock;
4388 }
4389
Chris Masone6dcd2d2008-07-17 12:53:50 -04004390 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004391 if (!len)
4392 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004393 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004394 if (front)
4395 memset(kaddr, 0, offset);
4396 else
4397 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004398 flush_dcache_page(page);
4399 kunmap(page);
4400 }
Chris Mason247e7432008-07-17 12:53:51 -04004401 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004402 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004403 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4404 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004405
Chris Mason89642222008-07-24 09:41:53 -04004406out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004407 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004408 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004409 unlock_page(page);
4410 page_cache_release(page);
4411out:
4412 return ret;
4413}
4414
Josef Bacik16e75492013-10-22 12:18:51 -04004415static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4416 u64 offset, u64 len)
4417{
4418 struct btrfs_trans_handle *trans;
4419 int ret;
4420
4421 /*
4422 * Still need to make sure the inode looks like it's been updated so
4423 * that any holes get logged if we fsync.
4424 */
4425 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4426 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4427 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4428 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4429 return 0;
4430 }
4431
4432 /*
4433 * 1 - for the one we're dropping
4434 * 1 - for the one we're adding
4435 * 1 - for updating the inode.
4436 */
4437 trans = btrfs_start_transaction(root, 3);
4438 if (IS_ERR(trans))
4439 return PTR_ERR(trans);
4440
4441 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4442 if (ret) {
4443 btrfs_abort_transaction(trans, root, ret);
4444 btrfs_end_transaction(trans, root);
4445 return ret;
4446 }
4447
4448 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4449 0, 0, len, 0, len, 0, 0, 0);
4450 if (ret)
4451 btrfs_abort_transaction(trans, root, ret);
4452 else
4453 btrfs_update_inode(trans, root, inode);
4454 btrfs_end_transaction(trans, root);
4455 return ret;
4456}
4457
Josef Bacik695a0d02011-03-04 15:46:53 -05004458/*
4459 * This function puts in dummy file extents for the area we're creating a hole
4460 * for. So if we are truncating this file to a larger size we need to insert
4461 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4462 * the range between oldsize and size
4463 */
Josef Bacika41ad392011-01-31 15:30:16 -05004464int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004465{
Yan Zheng9036c102008-10-30 14:19:41 -04004466 struct btrfs_root *root = BTRFS_I(inode)->root;
4467 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004468 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004469 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004470 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004471 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4472 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004473 u64 last_byte;
4474 u64 cur_offset;
4475 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004476 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004477
Josef Bacika71754f2013-06-17 17:14:39 -04004478 /*
4479 * If our size started in the middle of a page we need to zero out the
4480 * rest of the page before we expand the i_size, otherwise we could
4481 * expose stale data.
4482 */
4483 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4484 if (err)
4485 return err;
4486
Yan Zheng9036c102008-10-30 14:19:41 -04004487 if (size <= hole_start)
4488 return 0;
4489
Yan Zheng9036c102008-10-30 14:19:41 -04004490 while (1) {
4491 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004492
Josef Bacik2ac55d42010-02-03 19:33:23 +00004493 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004494 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004495 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4496 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004497 if (!ordered)
4498 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004499 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4500 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004501 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004502 btrfs_put_ordered_extent(ordered);
4503 }
4504
Yan Zheng9036c102008-10-30 14:19:41 -04004505 cur_offset = hole_start;
4506 while (1) {
4507 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4508 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004509 if (IS_ERR(em)) {
4510 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004511 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004512 break;
4513 }
Yan Zheng9036c102008-10-30 14:19:41 -04004514 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004515 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004516 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004517 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004518 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004519
Josef Bacik16e75492013-10-22 12:18:51 -04004520 err = maybe_insert_hole(root, inode, cur_offset,
4521 hole_size);
4522 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004523 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004524 btrfs_drop_extent_cache(inode, cur_offset,
4525 cur_offset + hole_size - 1, 0);
4526 hole_em = alloc_extent_map();
4527 if (!hole_em) {
4528 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4529 &BTRFS_I(inode)->runtime_flags);
4530 goto next;
4531 }
4532 hole_em->start = cur_offset;
4533 hole_em->len = hole_size;
4534 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004535
Josef Bacik5dc562c2012-08-17 13:14:17 -04004536 hole_em->block_start = EXTENT_MAP_HOLE;
4537 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004538 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004539 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004540 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4541 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004542 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004543
4544 while (1) {
4545 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004546 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004547 write_unlock(&em_tree->lock);
4548 if (err != -EEXIST)
4549 break;
4550 btrfs_drop_extent_cache(inode, cur_offset,
4551 cur_offset +
4552 hole_size - 1, 0);
4553 }
4554 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004555 }
Josef Bacik16e75492013-10-22 12:18:51 -04004556next:
Yan Zheng9036c102008-10-30 14:19:41 -04004557 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004558 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004559 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004560 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004561 break;
4562 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004563 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004564 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4565 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004566 return err;
4567}
4568
Eric Sandeen3972f262013-01-12 02:57:22 +00004569static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004570{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004571 struct btrfs_root *root = BTRFS_I(inode)->root;
4572 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004573 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004574 loff_t newsize = attr->ia_size;
4575 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004576 int ret;
4577
Eric Sandeen3972f262013-01-12 02:57:22 +00004578 /*
4579 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4580 * special case where we need to update the times despite not having
4581 * these flags set. For all other operations the VFS set these flags
4582 * explicitly if it wants a timestamp update.
4583 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004584 if (newsize != oldsize) {
4585 inode_inc_iversion(inode);
4586 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4587 inode->i_ctime = inode->i_mtime =
4588 current_fs_time(inode->i_sb);
4589 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004590
Josef Bacika41ad392011-01-31 15:30:16 -05004591 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004592 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004593 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004594 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004595 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004596
Miao Xief4a2f4c2011-12-14 20:12:01 -05004597 trans = btrfs_start_transaction(root, 1);
4598 if (IS_ERR(trans))
4599 return PTR_ERR(trans);
4600
4601 i_size_write(inode, newsize);
4602 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4603 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004604 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004605 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004606
Josef Bacika41ad392011-01-31 15:30:16 -05004607 /*
4608 * We're truncating a file that used to have good data down to
4609 * zero. Make sure it gets into the ordered flush list so that
4610 * any new writes get down to disk quickly.
4611 */
4612 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004613 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4614 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004615
Josef Bacikf3fe8202013-01-07 17:03:21 -05004616 /*
4617 * 1 for the orphan item we're going to add
4618 * 1 for the orphan item deletion.
4619 */
4620 trans = btrfs_start_transaction(root, 2);
4621 if (IS_ERR(trans))
4622 return PTR_ERR(trans);
4623
4624 /*
4625 * We need to do this in case we fail at _any_ point during the
4626 * actual truncate. Once we do the truncate_setsize we could
4627 * invalidate pages which forces any outstanding ordered io to
4628 * be instantly completed which will give us extents that need
4629 * to be truncated. If we fail to get an orphan inode down we
4630 * could have left over extents that were never meant to live,
4631 * so we need to garuntee from this point on that everything
4632 * will be consistent.
4633 */
4634 ret = btrfs_orphan_add(trans, inode);
4635 btrfs_end_transaction(trans, root);
4636 if (ret)
4637 return ret;
4638
Josef Bacika41ad392011-01-31 15:30:16 -05004639 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4640 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004641
4642 /* Disable nonlocked read DIO to avoid the end less truncate */
4643 btrfs_inode_block_unlocked_dio(inode);
4644 inode_dio_wait(inode);
4645 btrfs_inode_resume_unlocked_dio(inode);
4646
Josef Bacika41ad392011-01-31 15:30:16 -05004647 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004648 if (ret && inode->i_nlink) {
4649 int err;
4650
4651 /*
4652 * failed to truncate, disk_i_size is only adjusted down
4653 * as we remove extents, so it should represent the true
4654 * size of the inode, so reset the in memory size and
4655 * delete our orphan entry.
4656 */
4657 trans = btrfs_join_transaction(root);
4658 if (IS_ERR(trans)) {
4659 btrfs_orphan_del(NULL, inode);
4660 return ret;
4661 }
4662 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4663 err = btrfs_orphan_del(trans, inode);
4664 if (err)
4665 btrfs_abort_transaction(trans, root, err);
4666 btrfs_end_transaction(trans, root);
4667 }
Yan, Zheng80825102009-11-12 09:35:36 +00004668 }
4669
Josef Bacika41ad392011-01-31 15:30:16 -05004670 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004671}
4672
Chris Mason39279cc2007-06-12 06:35:45 -04004673static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4674{
4675 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004676 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004677 int err;
4678
Li Zefanb83cc962010-12-20 16:04:08 +08004679 if (btrfs_root_readonly(root))
4680 return -EROFS;
4681
Chris Mason39279cc2007-06-12 06:35:45 -04004682 err = inode_change_ok(inode, attr);
4683 if (err)
4684 return err;
4685
Chris Mason5a3f23d2009-03-31 13:27:11 -04004686 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004687 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004688 if (err)
4689 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004690 }
Yan Zheng9036c102008-10-30 14:19:41 -04004691
Christoph Hellwig10257742010-06-04 11:30:02 +02004692 if (attr->ia_valid) {
4693 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004694 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004695 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004696
Josef Bacik22c44fe2011-11-30 10:45:38 -05004697 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004698 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004699 }
4700
Chris Mason39279cc2007-06-12 06:35:45 -04004701 return err;
4702}
Chris Mason61295eb2008-01-14 16:24:38 -05004703
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004704/*
4705 * While truncating the inode pages during eviction, we get the VFS calling
4706 * btrfs_invalidatepage() against each page of the inode. This is slow because
4707 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4708 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4709 * extent_state structures over and over, wasting lots of time.
4710 *
4711 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4712 * those expensive operations on a per page basis and do only the ordered io
4713 * finishing, while we release here the extent_map and extent_state structures,
4714 * without the excessive merging and splitting.
4715 */
4716static void evict_inode_truncate_pages(struct inode *inode)
4717{
4718 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4719 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4720 struct rb_node *node;
4721
4722 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004723 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004724
4725 write_lock(&map_tree->lock);
4726 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4727 struct extent_map *em;
4728
4729 node = rb_first(&map_tree->map);
4730 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004731 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4732 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004733 remove_extent_mapping(map_tree, em);
4734 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004735 if (need_resched()) {
4736 write_unlock(&map_tree->lock);
4737 cond_resched();
4738 write_lock(&map_tree->lock);
4739 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004740 }
4741 write_unlock(&map_tree->lock);
4742
4743 spin_lock(&io_tree->lock);
4744 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4745 struct extent_state *state;
4746 struct extent_state *cached_state = NULL;
4747
4748 node = rb_first(&io_tree->state);
4749 state = rb_entry(node, struct extent_state, rb_node);
4750 atomic_inc(&state->refs);
4751 spin_unlock(&io_tree->lock);
4752
4753 lock_extent_bits(io_tree, state->start, state->end,
4754 0, &cached_state);
4755 clear_extent_bit(io_tree, state->start, state->end,
4756 EXTENT_LOCKED | EXTENT_DIRTY |
4757 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4758 EXTENT_DEFRAG, 1, 1,
4759 &cached_state, GFP_NOFS);
4760 free_extent_state(state);
4761
Filipe Manana7064dd52014-08-08 02:47:05 +01004762 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004763 spin_lock(&io_tree->lock);
4764 }
4765 spin_unlock(&io_tree->lock);
4766}
4767
Al Virobd555972010-06-07 11:35:40 -04004768void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004769{
4770 struct btrfs_trans_handle *trans;
4771 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004772 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004773 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004774 int ret;
4775
liubo1abe9b82011-03-24 11:18:59 +00004776 trace_btrfs_inode_evict(inode);
4777
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004778 evict_inode_truncate_pages(inode);
4779
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004780 if (inode->i_nlink &&
4781 ((btrfs_root_refs(&root->root_item) != 0 &&
4782 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4783 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004784 goto no_delete;
4785
Chris Mason39279cc2007-06-12 06:35:45 -04004786 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004787 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004788 goto no_delete;
4789 }
Al Virobd555972010-06-07 11:35:40 -04004790 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004791 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004792
Yan, Zhengc71bf092009-11-12 09:34:40 +00004793 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004794 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004795 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004796 goto no_delete;
4797 }
4798
Yan, Zheng76dda932009-09-21 16:00:26 -04004799 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004800 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4801 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004802 goto no_delete;
4803 }
4804
Miao Xie0e8c36a2012-12-19 06:59:51 +00004805 ret = btrfs_commit_inode_delayed_inode(inode);
4806 if (ret) {
4807 btrfs_orphan_del(NULL, inode);
4808 goto no_delete;
4809 }
4810
Miao Xie66d8f3d2012-09-06 04:02:28 -06004811 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004812 if (!rsv) {
4813 btrfs_orphan_del(NULL, inode);
4814 goto no_delete;
4815 }
Josef Bacik4a338542011-08-29 11:01:31 -04004816 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004817 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004818 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004819
Chris Masondbe674a2008-07-17 12:54:05 -04004820 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004821
Josef Bacik4289a662011-08-05 13:22:24 -04004822 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004823 * This is a bit simpler than btrfs_truncate since we've already
4824 * reserved our space for our orphan item in the unlink, so we just
4825 * need to reserve some slack space in case we add bytes and update
4826 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004827 */
Yan, Zheng80825102009-11-12 09:35:36 +00004828 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004829 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4830 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004831
Josef Bacik726c35f2011-09-26 15:46:06 -04004832 /*
4833 * Try and steal from the global reserve since we will
4834 * likely not use this space anyway, we want to try as
4835 * hard as possible to get this to work.
4836 */
4837 if (ret)
4838 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4839
Yan, Zhengd68fc572010-05-16 10:49:58 -04004840 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004841 btrfs_warn(root->fs_info,
4842 "Could not get space for a delete, will truncate on mount %d",
4843 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004844 btrfs_orphan_del(NULL, inode);
4845 btrfs_free_block_rsv(root, rsv);
4846 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004847 }
4848
Miao Xie0e8c36a2012-12-19 06:59:51 +00004849 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004850 if (IS_ERR(trans)) {
4851 btrfs_orphan_del(NULL, inode);
4852 btrfs_free_block_rsv(root, rsv);
4853 goto no_delete;
4854 }
4855
4856 trans->block_rsv = rsv;
4857
Yan, Zhengd68fc572010-05-16 10:49:58 -04004858 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004859 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004860 break;
4861
Miao Xie8407aa42012-09-07 01:43:32 -06004862 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004863 btrfs_end_transaction(trans, root);
4864 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004865 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004866 }
4867
Josef Bacik4289a662011-08-05 13:22:24 -04004868 btrfs_free_block_rsv(root, rsv);
4869
Josef Bacik4ef31a42013-08-13 14:10:08 -04004870 /*
4871 * Errors here aren't a big deal, it just means we leave orphan items
4872 * in the tree. They will be cleaned up on the next mount.
4873 */
Yan, Zheng80825102009-11-12 09:35:36 +00004874 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004875 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004876 btrfs_orphan_del(trans, inode);
4877 } else {
4878 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004879 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004880
Josef Bacik4289a662011-08-05 13:22:24 -04004881 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004882 if (!(root == root->fs_info->tree_root ||
4883 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004884 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004885
Chris Mason54aa1f42007-06-22 14:16:25 -04004886 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004887 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004888no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004889 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004890 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004891 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004892}
4893
4894/*
4895 * this returns the key found in the dir entry in the location pointer.
4896 * If no dir entries were found, location->objectid is 0.
4897 */
4898static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4899 struct btrfs_key *location)
4900{
4901 const char *name = dentry->d_name.name;
4902 int namelen = dentry->d_name.len;
4903 struct btrfs_dir_item *di;
4904 struct btrfs_path *path;
4905 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004906 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004907
4908 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004909 if (!path)
4910 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004911
Li Zefan33345d012011-04-20 10:31:50 +08004912 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004913 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004914 if (IS_ERR(di))
4915 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004916
David Sterbac7040052011-04-19 18:00:01 +02004917 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004918 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004919
Chris Mason5f39d392007-10-15 16:14:19 -04004920 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004921out:
Chris Mason39279cc2007-06-12 06:35:45 -04004922 btrfs_free_path(path);
4923 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004924out_err:
4925 location->objectid = 0;
4926 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004927}
4928
4929/*
4930 * when we hit a tree root in a directory, the btrfs part of the inode
4931 * needs to be changed to reflect the root directory of the tree root. This
4932 * is kind of like crossing a mount point.
4933 */
4934static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004935 struct inode *dir,
4936 struct dentry *dentry,
4937 struct btrfs_key *location,
4938 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004939{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004940 struct btrfs_path *path;
4941 struct btrfs_root *new_root;
4942 struct btrfs_root_ref *ref;
4943 struct extent_buffer *leaf;
4944 int ret;
4945 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004946
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004947 path = btrfs_alloc_path();
4948 if (!path) {
4949 err = -ENOMEM;
4950 goto out;
4951 }
Chris Mason39279cc2007-06-12 06:35:45 -04004952
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004953 err = -ENOENT;
Kelley Nielsen75ac2dd2013-11-04 19:35:58 -08004954 ret = btrfs_find_item(root->fs_info->tree_root, path,
4955 BTRFS_I(dir)->root->root_key.objectid,
4956 location->objectid, BTRFS_ROOT_REF_KEY, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004957 if (ret) {
4958 if (ret < 0)
4959 err = ret;
4960 goto out;
4961 }
Chris Mason39279cc2007-06-12 06:35:45 -04004962
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004963 leaf = path->nodes[0];
4964 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004965 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004966 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4967 goto out;
4968
4969 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4970 (unsigned long)(ref + 1),
4971 dentry->d_name.len);
4972 if (ret)
4973 goto out;
4974
David Sterbab3b4aa72011-04-21 01:20:15 +02004975 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004976
4977 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4978 if (IS_ERR(new_root)) {
4979 err = PTR_ERR(new_root);
4980 goto out;
4981 }
4982
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004983 *sub_root = new_root;
4984 location->objectid = btrfs_root_dirid(&new_root->root_item);
4985 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004986 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004987 err = 0;
4988out:
4989 btrfs_free_path(path);
4990 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004991}
4992
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004993static void inode_tree_add(struct inode *inode)
4994{
4995 struct btrfs_root *root = BTRFS_I(inode)->root;
4996 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004997 struct rb_node **p;
4998 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004999 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005000 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005001
Al Viro1d3382cb2010-10-23 15:19:20 -04005002 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005003 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005004 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005005 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005006 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005007 while (*p) {
5008 parent = *p;
5009 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5010
Li Zefan33345d012011-04-20 10:31:50 +08005011 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005012 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005013 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005014 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005015 else {
5016 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005017 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005018 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005019 RB_CLEAR_NODE(parent);
5020 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005021 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005022 }
5023 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005024 rb_link_node(new, parent, p);
5025 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005026 spin_unlock(&root->inode_lock);
5027}
5028
5029static void inode_tree_del(struct inode *inode)
5030{
5031 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005032 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005033
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005034 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005035 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005036 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005037 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005038 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005039 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005040 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005041
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005042 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005043 synchronize_srcu(&root->fs_info->subvol_srcu);
5044 spin_lock(&root->inode_lock);
5045 empty = RB_EMPTY_ROOT(&root->inode_tree);
5046 spin_unlock(&root->inode_lock);
5047 if (empty)
5048 btrfs_add_dead_root(root);
5049 }
5050}
5051
Jeff Mahoney143bede2012-03-01 14:56:26 +01005052void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005053{
5054 struct rb_node *node;
5055 struct rb_node *prev;
5056 struct btrfs_inode *entry;
5057 struct inode *inode;
5058 u64 objectid = 0;
5059
Liu Bo7813b3d2014-02-10 17:37:25 +08005060 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5061 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005062
5063 spin_lock(&root->inode_lock);
5064again:
5065 node = root->inode_tree.rb_node;
5066 prev = NULL;
5067 while (node) {
5068 prev = node;
5069 entry = rb_entry(node, struct btrfs_inode, rb_node);
5070
Li Zefan33345d012011-04-20 10:31:50 +08005071 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005072 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005073 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005074 node = node->rb_right;
5075 else
5076 break;
5077 }
5078 if (!node) {
5079 while (prev) {
5080 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005081 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005082 node = prev;
5083 break;
5084 }
5085 prev = rb_next(prev);
5086 }
5087 }
5088 while (node) {
5089 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005090 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005091 inode = igrab(&entry->vfs_inode);
5092 if (inode) {
5093 spin_unlock(&root->inode_lock);
5094 if (atomic_read(&inode->i_count) > 1)
5095 d_prune_aliases(inode);
5096 /*
Al Viro45321ac2010-06-07 13:43:19 -04005097 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005098 * the inode cache when its usage count
5099 * hits zero.
5100 */
5101 iput(inode);
5102 cond_resched();
5103 spin_lock(&root->inode_lock);
5104 goto again;
5105 }
5106
5107 if (cond_resched_lock(&root->inode_lock))
5108 goto again;
5109
5110 node = rb_next(node);
5111 }
5112 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005113}
5114
Chris Masone02119d2008-09-05 16:13:11 -04005115static int btrfs_init_locked_inode(struct inode *inode, void *p)
5116{
5117 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005118 inode->i_ino = args->location->objectid;
5119 memcpy(&BTRFS_I(inode)->location, args->location,
5120 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005121 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005122 return 0;
5123}
5124
5125static int btrfs_find_actor(struct inode *inode, void *opaque)
5126{
5127 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005128 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005129 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005130}
5131
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005132static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005133 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005134 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005135{
5136 struct inode *inode;
5137 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005138 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005139
Chris Mason90d3e592014-01-09 17:28:00 -08005140 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005141 args.root = root;
5142
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005143 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005144 btrfs_init_locked_inode,
5145 (void *)&args);
5146 return inode;
5147}
5148
Balaji Rao1a54ef82008-07-21 02:01:04 +05305149/* Get an inode object given its location and corresponding root.
5150 * Returns in *is_new if the inode was read from disk
5151 */
5152struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005153 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305154{
5155 struct inode *inode;
5156
Chris Mason90d3e592014-01-09 17:28:00 -08005157 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305158 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005159 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305160
5161 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305162 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005163 if (!is_bad_inode(inode)) {
5164 inode_tree_add(inode);
5165 unlock_new_inode(inode);
5166 if (new)
5167 *new = 1;
5168 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005169 unlock_new_inode(inode);
5170 iput(inode);
5171 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005172 }
5173 }
5174
Balaji Rao1a54ef82008-07-21 02:01:04 +05305175 return inode;
5176}
5177
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005178static struct inode *new_simple_dir(struct super_block *s,
5179 struct btrfs_key *key,
5180 struct btrfs_root *root)
5181{
5182 struct inode *inode = new_inode(s);
5183
5184 if (!inode)
5185 return ERR_PTR(-ENOMEM);
5186
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005187 BTRFS_I(inode)->root = root;
5188 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005189 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005190
5191 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005192 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005193 inode->i_fop = &simple_dir_operations;
5194 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5195 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5196
5197 return inode;
5198}
5199
Chris Mason3de45862008-11-17 21:02:50 -05005200struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005201{
Chris Masond3977122009-01-05 21:25:51 -05005202 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005203 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005204 struct btrfs_root *sub_root = root;
5205 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005206 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005207 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005208
5209 if (dentry->d_name.len > BTRFS_NAME_LEN)
5210 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005211
Jeff Layton39e3c952012-11-28 11:30:53 -05005212 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005213 if (ret < 0)
5214 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005215
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005216 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005217 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005218
5219 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005220 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005221 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005222 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005223
5224 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5225
Yan, Zheng76dda932009-09-21 16:00:26 -04005226 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005227 ret = fixup_tree_root_location(root, dir, dentry,
5228 &location, &sub_root);
5229 if (ret < 0) {
5230 if (ret != -ENOENT)
5231 inode = ERR_PTR(ret);
5232 else
5233 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5234 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005235 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005236 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005237 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5238
Julia Lawall34d19ba2011-01-24 19:55:19 +00005239 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005240 down_read(&root->fs_info->cleanup_work_sem);
5241 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005242 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005243 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005244 if (ret) {
5245 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005246 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005247 }
Filipe Manana9c3b3062014-07-31 14:41:07 +01005248 /*
5249 * If orphan cleanup did remove any orphans, it means the tree
5250 * was modified and therefore the commit root is not the same as
5251 * the current root anymore. This is a problem, because send
5252 * uses the commit root and therefore can see inode items that
5253 * don't exist in the current root anymore, and for example make
5254 * calls to btrfs_iget, which will do tree lookups based on the
5255 * current root and not on the commit root. Those lookups will
5256 * fail, returning a -ESTALE error, and making send fail with
5257 * that error. So make sure a send does not see any orphans we
5258 * have just removed, and that it will see the same inodes
5259 * regardless of whether a transaction commit happened before
5260 * it started (meaning that the commit root will be the same as
5261 * the current root) or not.
5262 */
5263 if (sub_root->node != sub_root->commit_root) {
5264 u64 sub_flags = btrfs_root_flags(&sub_root->root_item);
5265
5266 if (sub_flags & BTRFS_ROOT_SUBVOL_RDONLY) {
5267 struct extent_buffer *eb;
5268
5269 /*
5270 * Assert we can't have races between dentry
5271 * lookup called through the snapshot creation
5272 * ioctl and the VFS.
5273 */
5274 ASSERT(mutex_is_locked(&dir->i_mutex));
5275
5276 down_write(&root->fs_info->commit_root_sem);
5277 eb = sub_root->commit_root;
5278 sub_root->commit_root =
5279 btrfs_root_node(sub_root);
5280 up_write(&root->fs_info->commit_root_sem);
5281 free_extent_buffer(eb);
5282 }
5283 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005284 }
5285
Chris Mason3de45862008-11-17 21:02:50 -05005286 return inode;
5287}
5288
Nick Pigginfe15ce42011-01-07 17:49:23 +11005289static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005290{
5291 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005292 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005293
Li Zefan848cce02012-02-21 17:04:28 +08005294 if (!inode && !IS_ROOT(dentry))
5295 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005296
Li Zefan848cce02012-02-21 17:04:28 +08005297 if (inode) {
5298 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005299 if (btrfs_root_refs(&root->root_item) == 0)
5300 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005301
5302 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5303 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005304 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005305 return 0;
5306}
5307
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005308static void btrfs_dentry_release(struct dentry *dentry)
5309{
Daeseok Youn944a4512014-04-14 15:37:02 +09005310 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005311}
5312
Chris Mason3de45862008-11-17 21:02:50 -05005313static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005314 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005315{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005316 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005317
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005318 inode = btrfs_lookup_dentry(dir, dentry);
5319 if (IS_ERR(inode)) {
5320 if (PTR_ERR(inode) == -ENOENT)
5321 inode = NULL;
5322 else
5323 return ERR_CAST(inode);
5324 }
5325
Josef Bacik3a0dfa62014-02-14 13:43:48 -05005326 return d_materialise_unique(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005327}
5328
Miao Xie16cdcec2011-04-22 18:12:22 +08005329unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005330 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5331};
5332
Al Viro9cdda8d2013-05-22 16:48:09 -04005333static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005334{
Al Viro9cdda8d2013-05-22 16:48:09 -04005335 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005336 struct btrfs_root *root = BTRFS_I(inode)->root;
5337 struct btrfs_item *item;
5338 struct btrfs_dir_item *di;
5339 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005340 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005341 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005342 struct list_head ins_list;
5343 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005344 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005345 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005346 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005347 unsigned char d_type;
5348 int over = 0;
5349 u32 di_cur;
5350 u32 di_total;
5351 u32 di_len;
5352 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005353 char tmp_name[32];
5354 char *name_ptr;
5355 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005356 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005357
5358 /* FIXME, use a real flag for deciding about the key type */
5359 if (root->fs_info->tree_root == root)
5360 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005361
Al Viro9cdda8d2013-05-22 16:48:09 -04005362 if (!dir_emit_dots(file, ctx))
5363 return 0;
5364
David Woodhouse49593bf2008-08-17 17:08:36 +01005365 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005366 if (!path)
5367 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005368
Josef Bacik026fd312011-05-13 10:32:11 -04005369 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005370
Miao Xie16cdcec2011-04-22 18:12:22 +08005371 if (key_type == BTRFS_DIR_INDEX_KEY) {
5372 INIT_LIST_HEAD(&ins_list);
5373 INIT_LIST_HEAD(&del_list);
5374 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5375 }
5376
David Sterba962a2982014-06-04 18:41:45 +02005377 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005378 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005379 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005380
Chris Mason39279cc2007-06-12 06:35:45 -04005381 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5382 if (ret < 0)
5383 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005384
5385 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005386 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005387 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005388 if (slot >= btrfs_header_nritems(leaf)) {
5389 ret = btrfs_next_leaf(root, path);
5390 if (ret < 0)
5391 goto err;
5392 else if (ret > 0)
5393 break;
5394 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005395 }
Chris Mason3de45862008-11-17 21:02:50 -05005396
Ross Kirkdd3cc162013-09-16 15:58:09 +01005397 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005398 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5399
5400 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005401 break;
David Sterba962a2982014-06-04 18:41:45 +02005402 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005403 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005404 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005405 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005406 if (key_type == BTRFS_DIR_INDEX_KEY &&
5407 btrfs_should_delete_dir_index(&del_list,
5408 found_key.offset))
5409 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005410
Al Viro9cdda8d2013-05-22 16:48:09 -04005411 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005412 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005413
Chris Mason39279cc2007-06-12 06:35:45 -04005414 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5415 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005416 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005417
5418 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005419 struct btrfs_key location;
5420
Josef Bacik22a94d42011-03-16 16:47:17 -04005421 if (verify_dir_item(root, leaf, di))
5422 break;
5423
Chris Mason5f39d392007-10-15 16:14:19 -04005424 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005425 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005426 name_ptr = tmp_name;
5427 } else {
5428 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005429 if (!name_ptr) {
5430 ret = -ENOMEM;
5431 goto err;
5432 }
Chris Mason5f39d392007-10-15 16:14:19 -04005433 }
5434 read_extent_buffer(leaf, name_ptr,
5435 (unsigned long)(di + 1), name_len);
5436
5437 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5438 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005439
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005440
Chris Mason3de45862008-11-17 21:02:50 -05005441 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005442 * skip it.
5443 *
5444 * In contrast to old kernels, we insert the snapshot's
5445 * dir item and dir index after it has been created, so
5446 * we won't find a reference to our own snapshot. We
5447 * still keep the following code for backward
5448 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005449 */
5450 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5451 location.objectid == root->root_key.objectid) {
5452 over = 0;
5453 goto skip;
5454 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005455 over = !dir_emit(ctx, name_ptr, name_len,
5456 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005457
Chris Mason3de45862008-11-17 21:02:50 -05005458skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005459 if (name_ptr != tmp_name)
5460 kfree(name_ptr);
5461
Chris Mason39279cc2007-06-12 06:35:45 -04005462 if (over)
5463 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005464 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005465 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005466 di_cur += di_len;
5467 di = (struct btrfs_dir_item *)((char *)di + di_len);
5468 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005469next:
5470 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005471 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005472
Miao Xie16cdcec2011-04-22 18:12:22 +08005473 if (key_type == BTRFS_DIR_INDEX_KEY) {
5474 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005475 ctx->pos++;
5476 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005477 if (ret)
5478 goto nopos;
5479 }
5480
David Woodhouse49593bf2008-08-17 17:08:36 +01005481 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005482 ctx->pos++;
5483
5484 /*
5485 * Stop new entries from being returned after we return the last
5486 * entry.
5487 *
5488 * New directory entries are assigned a strictly increasing
5489 * offset. This means that new entries created during readdir
5490 * are *guaranteed* to be seen in the future by that readdir.
5491 * This has broken buggy programs which operate on names as
5492 * they're returned by readdir. Until we re-use freed offsets
5493 * we have this hack to stop new entries from being returned
5494 * under the assumption that they'll never reach this huge
5495 * offset.
5496 *
5497 * This is being careful not to overflow 32bit loff_t unless the
5498 * last entry requires it because doing so has broken 32bit apps
5499 * in the past.
5500 */
5501 if (key_type == BTRFS_DIR_INDEX_KEY) {
5502 if (ctx->pos >= INT_MAX)
5503 ctx->pos = LLONG_MAX;
5504 else
5505 ctx->pos = INT_MAX;
5506 }
Chris Mason39279cc2007-06-12 06:35:45 -04005507nopos:
5508 ret = 0;
5509err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005510 if (key_type == BTRFS_DIR_INDEX_KEY)
5511 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005512 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005513 return ret;
5514}
5515
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005516int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005517{
5518 struct btrfs_root *root = BTRFS_I(inode)->root;
5519 struct btrfs_trans_handle *trans;
5520 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005521 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005522
Josef Bacik72ac3c02012-05-23 14:13:11 -04005523 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005524 return 0;
5525
Liu Bo83eea1f2012-07-10 05:28:39 -06005526 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005527 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005528
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005529 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005530 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005531 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005532 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005533 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005534 if (IS_ERR(trans))
5535 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005536 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005537 }
5538 return ret;
5539}
5540
5541/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005542 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005543 * inode changes. But, it is most likely to find the inode in cache.
5544 * FIXME, needs more benchmarking...there are no reasons other than performance
5545 * to keep or drop this code.
5546 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005547static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005548{
5549 struct btrfs_root *root = BTRFS_I(inode)->root;
5550 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005551 int ret;
5552
Josef Bacik72ac3c02012-05-23 14:13:11 -04005553 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005554 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005555
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005556 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005557 if (IS_ERR(trans))
5558 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005559
5560 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005561 if (ret && ret == -ENOSPC) {
5562 /* whoops, lets try again with the full transaction */
5563 btrfs_end_transaction(trans, root);
5564 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005565 if (IS_ERR(trans))
5566 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005567
Chris Mason94b60442010-05-26 11:02:00 -04005568 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005569 }
Chris Mason39279cc2007-06-12 06:35:45 -04005570 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005571 if (BTRFS_I(inode)->delayed_node)
5572 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005573
5574 return ret;
5575}
5576
5577/*
5578 * This is a copy of file_update_time. We need this so we can return error on
5579 * ENOSPC for updating the inode in the case of file write and mmap writes.
5580 */
Josef Bacike41f9412012-03-26 09:46:47 -04005581static int btrfs_update_time(struct inode *inode, struct timespec *now,
5582 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005583{
Alexander Block2bc5565282012-06-15 09:49:33 +02005584 struct btrfs_root *root = BTRFS_I(inode)->root;
5585
5586 if (btrfs_root_readonly(root))
5587 return -EROFS;
5588
Josef Bacike41f9412012-03-26 09:46:47 -04005589 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005590 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005591 if (flags & S_CTIME)
5592 inode->i_ctime = *now;
5593 if (flags & S_MTIME)
5594 inode->i_mtime = *now;
5595 if (flags & S_ATIME)
5596 inode->i_atime = *now;
5597 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005598}
5599
Chris Masond352ac62008-09-29 15:18:18 -04005600/*
5601 * find the highest existing sequence number in a directory
5602 * and then set the in-memory index_cnt variable to reflect
5603 * free sequence numbers
5604 */
Josef Bacikaec74772008-07-24 12:12:38 -04005605static int btrfs_set_inode_index_count(struct inode *inode)
5606{
5607 struct btrfs_root *root = BTRFS_I(inode)->root;
5608 struct btrfs_key key, found_key;
5609 struct btrfs_path *path;
5610 struct extent_buffer *leaf;
5611 int ret;
5612
Li Zefan33345d012011-04-20 10:31:50 +08005613 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005614 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005615 key.offset = (u64)-1;
5616
5617 path = btrfs_alloc_path();
5618 if (!path)
5619 return -ENOMEM;
5620
5621 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5622 if (ret < 0)
5623 goto out;
5624 /* FIXME: we should be able to handle this */
5625 if (ret == 0)
5626 goto out;
5627 ret = 0;
5628
5629 /*
5630 * MAGIC NUMBER EXPLANATION:
5631 * since we search a directory based on f_pos we have to start at 2
5632 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5633 * else has to start at 2
5634 */
5635 if (path->slots[0] == 0) {
5636 BTRFS_I(inode)->index_cnt = 2;
5637 goto out;
5638 }
5639
5640 path->slots[0]--;
5641
5642 leaf = path->nodes[0];
5643 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5644
Li Zefan33345d012011-04-20 10:31:50 +08005645 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005646 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04005647 BTRFS_I(inode)->index_cnt = 2;
5648 goto out;
5649 }
5650
5651 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5652out:
5653 btrfs_free_path(path);
5654 return ret;
5655}
5656
Chris Masond352ac62008-09-29 15:18:18 -04005657/*
5658 * helper to find a free sequence number in a given directory. This current
5659 * code is very simple, later versions will do smarter things in the btree
5660 */
Chris Mason3de45862008-11-17 21:02:50 -05005661int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005662{
5663 int ret = 0;
5664
5665 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005666 ret = btrfs_inode_delayed_dir_index_count(dir);
5667 if (ret) {
5668 ret = btrfs_set_inode_index_count(dir);
5669 if (ret)
5670 return ret;
5671 }
Josef Bacikaec74772008-07-24 12:12:38 -04005672 }
5673
Chris Mason00e4e6b2008-08-05 11:18:09 -04005674 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005675 BTRFS_I(dir)->index_cnt++;
5676
5677 return ret;
5678}
5679
Chris Masonb0d5d102014-09-08 13:08:51 -07005680static int btrfs_insert_inode_locked(struct inode *inode)
5681{
5682 struct btrfs_iget_args args;
5683 args.location = &BTRFS_I(inode)->location;
5684 args.root = BTRFS_I(inode)->root;
5685
5686 return insert_inode_locked4(inode,
5687 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5688 btrfs_find_actor, &args);
5689}
5690
Chris Mason39279cc2007-06-12 06:35:45 -04005691static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5692 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005693 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005694 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005695 u64 ref_objectid, u64 objectid,
5696 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005697{
5698 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005699 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005700 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005701 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005702 struct btrfs_inode_ref *ref;
5703 struct btrfs_key key[2];
5704 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005705 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005706 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005707 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005708
Chris Mason5f39d392007-10-15 16:14:19 -04005709 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005710 if (!path)
5711 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005712
Chris Mason39279cc2007-06-12 06:35:45 -04005713 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005714 if (!inode) {
5715 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005716 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005717 }
Chris Mason39279cc2007-06-12 06:35:45 -04005718
Li Zefan581bb052011-04-20 10:06:11 +08005719 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005720 * O_TMPFILE, set link count to 0, so that after this point,
5721 * we fill in an inode item with the correct link count.
5722 */
5723 if (!name)
5724 set_nlink(inode, 0);
5725
5726 /*
Li Zefan581bb052011-04-20 10:06:11 +08005727 * we have to initialize this early, so we can reclaim the inode
5728 * number if we fail afterwards in this function.
5729 */
5730 inode->i_ino = objectid;
5731
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005732 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005733 trace_btrfs_inode_request(dir);
5734
Chris Mason3de45862008-11-17 21:02:50 -05005735 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005736 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005737 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005738 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005739 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005740 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005741 } else if (dir) {
5742 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005743 }
5744 /*
5745 * index_cnt is ignored for everything but a dir,
5746 * btrfs_get_inode_index_count has an explanation for the magic
5747 * number
5748 */
5749 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005750 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005751 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005752 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005753 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005754
Josef Bacik5dc562c2012-08-17 13:14:17 -04005755 /*
5756 * We could have gotten an inode number from somebody who was fsynced
5757 * and then removed in this same transaction, so let's just set full
5758 * sync since it will be a full sync anyway and this will blow away the
5759 * old info in the log.
5760 */
5761 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5762
Chris Mason9c583092008-01-29 15:15:18 -05005763 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005764 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005765 key[0].offset = 0;
5766
Chris Mason9c583092008-01-29 15:15:18 -05005767 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005768
5769 if (name) {
5770 /*
5771 * Start new inodes with an inode_ref. This is slightly more
5772 * efficient for small numbers of hard links since they will
5773 * be packed into one item. Extended refs will kick in if we
5774 * add more hard links than can fit in the ref item.
5775 */
5776 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005777 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005778 key[1].offset = ref_objectid;
5779
5780 sizes[1] = name_len + sizeof(*ref);
5781 }
Chris Mason9c583092008-01-29 15:15:18 -05005782
Chris Masonb0d5d102014-09-08 13:08:51 -07005783 location = &BTRFS_I(inode)->location;
5784 location->objectid = objectid;
5785 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02005786 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07005787
5788 ret = btrfs_insert_inode_locked(inode);
5789 if (ret < 0)
5790 goto fail;
5791
Chris Masonb9473432009-03-13 11:00:37 -04005792 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005793 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005794 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07005795 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04005796
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005797 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005798 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005799 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005800 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5801 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005802 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5803 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005804 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005805
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005806 if (name) {
5807 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5808 struct btrfs_inode_ref);
5809 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5810 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5811 ptr = (unsigned long)(ref + 1);
5812 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5813 }
Chris Mason9c583092008-01-29 15:15:18 -05005814
Chris Mason5f39d392007-10-15 16:14:19 -04005815 btrfs_mark_buffer_dirty(path->nodes[0]);
5816 btrfs_free_path(path);
5817
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005818 btrfs_inherit_iflags(inode, dir);
5819
Al Viro569254b2011-07-24 17:08:40 -04005820 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005821 if (btrfs_test_opt(root, NODATASUM))
5822 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005823 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005824 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5825 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005826 }
5827
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005828 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005829
5830 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005831 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005832
Alexander Block8ea05e32012-07-25 17:35:53 +02005833 btrfs_update_root_times(trans, root);
5834
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005835 ret = btrfs_inode_inherit_props(trans, inode, dir);
5836 if (ret)
5837 btrfs_err(root->fs_info,
5838 "error inheriting props for ino %llu (root %llu): %d",
5839 btrfs_ino(inode), root->root_key.objectid, ret);
5840
Chris Mason39279cc2007-06-12 06:35:45 -04005841 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07005842
5843fail_unlock:
5844 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005845fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005846 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04005847 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005848 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005849 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005850 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005851}
5852
5853static inline u8 btrfs_inode_type(struct inode *inode)
5854{
5855 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5856}
5857
Chris Masond352ac62008-09-29 15:18:18 -04005858/*
5859 * utility function to add 'inode' into 'parent_inode' with
5860 * a give name and a given sequence number.
5861 * if 'add_backref' is true, also insert a backref from the
5862 * inode to the parent directory.
5863 */
Chris Masone02119d2008-09-05 16:13:11 -04005864int btrfs_add_link(struct btrfs_trans_handle *trans,
5865 struct inode *parent_inode, struct inode *inode,
5866 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005867{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005868 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005869 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005870 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005871 u64 ino = btrfs_ino(inode);
5872 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005873
Li Zefan33345d012011-04-20 10:31:50 +08005874 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005875 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5876 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005877 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02005878 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005879 key.offset = 0;
5880 }
Chris Mason39279cc2007-06-12 06:35:45 -04005881
Li Zefan33345d012011-04-20 10:31:50 +08005882 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005883 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5884 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005885 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005886 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005887 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5888 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005889 }
5890
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005891 /* Nothing to clean up yet */
5892 if (ret)
5893 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005894
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005895 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5896 parent_inode, &key,
5897 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005898 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005899 goto fail_dir_item;
5900 else if (ret) {
5901 btrfs_abort_transaction(trans, root, ret);
5902 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005903 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005904
5905 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5906 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005907 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005908 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5909 ret = btrfs_update_inode(trans, root, parent_inode);
5910 if (ret)
5911 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005912 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005913
5914fail_dir_item:
5915 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5916 u64 local_index;
5917 int err;
5918 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5919 key.objectid, root->root_key.objectid,
5920 parent_ino, &local_index, name, name_len);
5921
5922 } else if (add_backref) {
5923 u64 local_index;
5924 int err;
5925
5926 err = btrfs_del_inode_ref(trans, root, name, name_len,
5927 ino, parent_ino, &local_index);
5928 }
5929 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005930}
5931
5932static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005933 struct inode *dir, struct dentry *dentry,
5934 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005935{
Josef Bacika1b075d2010-11-19 20:36:11 +00005936 int err = btrfs_add_link(trans, dir, inode,
5937 dentry->d_name.name, dentry->d_name.len,
5938 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005939 if (err > 0)
5940 err = -EEXIST;
5941 return err;
5942}
5943
Josef Bacik618e21d2007-07-11 10:18:17 -04005944static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005945 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005946{
5947 struct btrfs_trans_handle *trans;
5948 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005949 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005950 int err;
5951 int drop_inode = 0;
5952 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005953 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005954
5955 if (!new_valid_dev(rdev))
5956 return -EINVAL;
5957
Josef Bacik9ed74f22009-09-11 16:12:44 -04005958 /*
5959 * 2 for inode item and ref
5960 * 2 for dir items
5961 * 1 for xattr if selinux is on
5962 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005963 trans = btrfs_start_transaction(root, 5);
5964 if (IS_ERR(trans))
5965 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005966
Li Zefan581bb052011-04-20 10:06:11 +08005967 err = btrfs_find_free_ino(root, &objectid);
5968 if (err)
5969 goto out_unlock;
5970
Josef Bacikaec74772008-07-24 12:12:38 -04005971 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005972 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005973 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005974 if (IS_ERR(inode)) {
5975 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005976 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005977 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005978
Casey Schauflerad19db72011-12-15 10:09:07 -05005979 /*
5980 * If the active LSM wants to access the inode during
5981 * d_instantiate it needs these. Smack checks to see
5982 * if the filesystem supports xattrs by looking at the
5983 * ops vector.
5984 */
Casey Schauflerad19db72011-12-15 10:09:07 -05005985 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07005986 init_special_inode(inode, inode->i_mode, rdev);
5987
5988 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04005989 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07005990 goto out_unlock_inode;
5991
5992 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
5993 if (err) {
5994 goto out_unlock_inode;
5995 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04005996 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07005997 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05005998 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005999 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006000
Josef Bacik618e21d2007-07-11 10:18:17 -04006001out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006002 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006003 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006004 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006005 if (drop_inode) {
6006 inode_dec_link_count(inode);
6007 iput(inode);
6008 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006009 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006010
6011out_unlock_inode:
6012 drop_inode = 1;
6013 unlock_new_inode(inode);
6014 goto out_unlock;
6015
Josef Bacik618e21d2007-07-11 10:18:17 -04006016}
6017
Chris Mason39279cc2007-06-12 06:35:45 -04006018static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006019 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006020{
6021 struct btrfs_trans_handle *trans;
6022 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006023 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006024 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006025 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006026 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006027 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006028
Josef Bacik9ed74f22009-09-11 16:12:44 -04006029 /*
6030 * 2 for inode item and ref
6031 * 2 for dir items
6032 * 1 for xattr if selinux is on
6033 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006034 trans = btrfs_start_transaction(root, 5);
6035 if (IS_ERR(trans))
6036 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006037
Li Zefan581bb052011-04-20 10:06:11 +08006038 err = btrfs_find_free_ino(root, &objectid);
6039 if (err)
6040 goto out_unlock;
6041
Josef Bacikaec74772008-07-24 12:12:38 -04006042 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006043 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006044 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006045 if (IS_ERR(inode)) {
6046 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006047 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006048 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006049 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006050 /*
6051 * If the active LSM wants to access the inode during
6052 * d_instantiate it needs these. Smack checks to see
6053 * if the filesystem supports xattrs by looking at the
6054 * ops vector.
6055 */
6056 inode->i_fop = &btrfs_file_operations;
6057 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006058 inode->i_mapping->a_ops = &btrfs_aops;
6059 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
6060
6061 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6062 if (err)
6063 goto out_unlock_inode;
6064
6065 err = btrfs_update_inode(trans, root, inode);
6066 if (err)
6067 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006068
Josef Bacika1b075d2010-11-19 20:36:11 +00006069 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006070 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006071 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006072
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006073 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006074 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006075 d_instantiate(dentry, inode);
6076
Chris Mason39279cc2007-06-12 06:35:45 -04006077out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006078 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006079 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006080 inode_dec_link_count(inode);
6081 iput(inode);
6082 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006083 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006084 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006085 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006086
6087out_unlock_inode:
6088 unlock_new_inode(inode);
6089 goto out_unlock;
6090
Chris Mason39279cc2007-06-12 06:35:45 -04006091}
6092
6093static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6094 struct dentry *dentry)
6095{
6096 struct btrfs_trans_handle *trans;
6097 struct btrfs_root *root = BTRFS_I(dir)->root;
6098 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006099 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006100 int err;
6101 int drop_inode = 0;
6102
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006103 /* do not allow sys_link's with other subvols of the same device */
6104 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006105 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006106
Mark Fashehf1863732012-08-08 11:32:27 -07006107 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006108 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006109
Chris Mason3de45862008-11-17 21:02:50 -05006110 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006111 if (err)
6112 goto fail;
6113
Yan, Zhenga22285a2010-05-16 10:48:46 -04006114 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006115 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006116 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006117 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006118 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006119 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006120 if (IS_ERR(trans)) {
6121 err = PTR_ERR(trans);
6122 goto fail;
6123 }
Chris Mason5f39d392007-10-15 16:14:19 -04006124
Miao Xie67de1172013-12-26 13:07:06 +08006125 /* There are several dir indexes for this inode, clear the cache. */
6126 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006127 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006128 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006129 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006130 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006131 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006132
Josef Bacika1b075d2010-11-19 20:36:11 +00006133 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006134
Yan, Zhenga5719522009-09-24 09:17:31 -04006135 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006136 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006137 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006138 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006139 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006140 if (err)
6141 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006142 if (inode->i_nlink == 1) {
6143 /*
6144 * If new hard link count is 1, it's a file created
6145 * with open(2) O_TMPFILE flag.
6146 */
6147 err = btrfs_orphan_del(trans, inode);
6148 if (err)
6149 goto fail;
6150 }
Al Viro08c422c2011-12-23 07:58:13 -05006151 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006152 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006153 }
Chris Mason39279cc2007-06-12 06:35:45 -04006154
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006155 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006156 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006157fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006158 if (drop_inode) {
6159 inode_dec_link_count(inode);
6160 iput(inode);
6161 }
Liu Bob53d3f52012-11-14 14:34:34 +00006162 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006163 return err;
6164}
6165
Al Viro18bb1db2011-07-26 01:41:39 -04006166static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006167{
Chris Masonb9d86662008-05-02 16:13:49 -04006168 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006169 struct btrfs_trans_handle *trans;
6170 struct btrfs_root *root = BTRFS_I(dir)->root;
6171 int err = 0;
6172 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006173 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006174 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006175
Josef Bacik9ed74f22009-09-11 16:12:44 -04006176 /*
6177 * 2 items for inode and ref
6178 * 2 items for dir items
6179 * 1 for xattr if selinux is on
6180 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006181 trans = btrfs_start_transaction(root, 5);
6182 if (IS_ERR(trans))
6183 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006184
Li Zefan581bb052011-04-20 10:06:11 +08006185 err = btrfs_find_free_ino(root, &objectid);
6186 if (err)
6187 goto out_fail;
6188
Josef Bacikaec74772008-07-24 12:12:38 -04006189 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006190 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006191 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006192 if (IS_ERR(inode)) {
6193 err = PTR_ERR(inode);
6194 goto out_fail;
6195 }
Chris Mason5f39d392007-10-15 16:14:19 -04006196
Chris Mason39279cc2007-06-12 06:35:45 -04006197 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006198 /* these must be set before we unlock the inode */
6199 inode->i_op = &btrfs_dir_inode_operations;
6200 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006201
Eric Paris2a7dba32011-02-01 11:05:39 -05006202 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006203 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006204 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006205
Chris Masondbe674a2008-07-17 12:54:05 -04006206 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006207 err = btrfs_update_inode(trans, root, inode);
6208 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006209 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006210
Josef Bacika1b075d2010-11-19 20:36:11 +00006211 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6212 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006213 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006214 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006215
Chris Mason39279cc2007-06-12 06:35:45 -04006216 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006217 /*
6218 * mkdir is special. We're unlocking after we call d_instantiate
6219 * to avoid a race with nfsd calling d_instantiate.
6220 */
6221 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006222 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006223
6224out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006225 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006226 if (drop_on_err)
6227 iput(inode);
Josef Bacikc581afc2014-02-06 16:06:06 -05006228 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006229 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006230 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006231
6232out_fail_inode:
6233 unlock_new_inode(inode);
6234 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006235}
6236
Chris Masond352ac62008-09-29 15:18:18 -04006237/* helper for btfs_get_extent. Given an existing extent in the tree,
6238 * and an extent that you want to insert, deal with overlap and insert
6239 * the new extent into the tree.
6240 */
Chris Mason3b951512008-04-17 11:29:12 -04006241static int merge_extent_mapping(struct extent_map_tree *em_tree,
6242 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006243 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006244 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006245{
6246 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006247
Chris Masone6dcd2d2008-07-17 12:53:50 -04006248 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
6249 start_diff = map_start - em->start;
6250 em->start = map_start;
Qu Wenruo51f395a2014-08-08 13:06:20 +08006251 em->len = existing->start - em->start;
Chris Masonc8b97812008-10-29 14:49:59 -04006252 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6253 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006254 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006255 em->block_len -= start_diff;
6256 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006257 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006258}
6259
Chris Masonc8b97812008-10-29 14:49:59 -04006260static noinline int uncompress_inline(struct btrfs_path *path,
6261 struct inode *inode, struct page *page,
6262 size_t pg_offset, u64 extent_offset,
6263 struct btrfs_file_extent_item *item)
6264{
6265 int ret;
6266 struct extent_buffer *leaf = path->nodes[0];
6267 char *tmp;
6268 size_t max_size;
6269 unsigned long inline_size;
6270 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006271 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006272
6273 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006274 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006275 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6276 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006277 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006278 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006279 if (!tmp)
6280 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006281 ptr = btrfs_file_extent_inline_start(item);
6282
6283 read_extent_buffer(leaf, tmp, ptr, inline_size);
6284
Chris Mason5b050f02008-11-11 09:34:41 -05006285 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006286 ret = btrfs_decompress(compress_type, tmp, page,
6287 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006288 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006289 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006290}
6291
Chris Masond352ac62008-09-29 15:18:18 -04006292/*
6293 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006294 * the ugly parts come from merging extents from the disk with the in-ram
6295 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006296 * where the in-ram extents might be locked pending data=ordered completion.
6297 *
6298 * This also copies inline extents directly into the page.
6299 */
Chris Masond3977122009-01-05 21:25:51 -05006300
Chris Masona52d9a82007-08-27 16:49:44 -04006301struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006302 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006303 int create)
6304{
6305 int ret;
6306 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006307 u64 extent_start = 0;
6308 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006309 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006310 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006311 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006312 struct btrfs_root *root = BTRFS_I(inode)->root;
6313 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006314 struct extent_buffer *leaf;
6315 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006316 struct extent_map *em = NULL;
6317 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006318 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006319 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006320 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006321
Chris Masona52d9a82007-08-27 16:49:44 -04006322again:
Chris Mason890871b2009-09-02 16:24:52 -04006323 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006324 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006325 if (em)
6326 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006327 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006328
Chris Masona52d9a82007-08-27 16:49:44 -04006329 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006330 if (em->start > start || em->start + em->len <= start)
6331 free_extent_map(em);
6332 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006333 free_extent_map(em);
6334 else
6335 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006336 }
David Sterba172ddd62011-04-21 00:48:27 +02006337 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006338 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006339 err = -ENOMEM;
6340 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006341 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006342 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006343 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006344 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006345 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006346 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006347
6348 if (!path) {
6349 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006350 if (!path) {
6351 err = -ENOMEM;
6352 goto out;
6353 }
6354 /*
6355 * Chances are we'll be called again, so go ahead and do
6356 * readahead
6357 */
6358 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006359 }
6360
Chris Mason179e29e2007-11-01 11:28:41 -04006361 ret = btrfs_lookup_file_extent(trans, root, path,
6362 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006363 if (ret < 0) {
6364 err = ret;
6365 goto out;
6366 }
6367
6368 if (ret != 0) {
6369 if (path->slots[0] == 0)
6370 goto not_found;
6371 path->slots[0]--;
6372 }
6373
Chris Mason5f39d392007-10-15 16:14:19 -04006374 leaf = path->nodes[0];
6375 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006376 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006377 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006378 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006379 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006380 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006381 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006382 /*
6383 * If we backup past the first extent we want to move forward
6384 * and see if there is an extent in front of us, otherwise we'll
6385 * say there is a hole for our whole search range which can
6386 * cause problems.
6387 */
6388 extent_end = start;
6389 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006390 }
6391
Chris Mason5f39d392007-10-15 16:14:19 -04006392 found_type = btrfs_file_extent_type(leaf, item);
6393 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006394 if (found_type == BTRFS_FILE_EXTENT_REG ||
6395 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006396 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006397 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006398 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6399 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006400 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006401 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006402 }
Josef Bacik25a50342013-10-14 12:08:38 -04006403next:
Yan Zheng9036c102008-10-30 14:19:41 -04006404 if (start >= extent_end) {
6405 path->slots[0]++;
6406 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6407 ret = btrfs_next_leaf(root, path);
6408 if (ret < 0) {
6409 err = ret;
6410 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006411 }
Yan Zheng9036c102008-10-30 14:19:41 -04006412 if (ret > 0)
6413 goto not_found;
6414 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006415 }
Yan Zheng9036c102008-10-30 14:19:41 -04006416 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6417 if (found_key.objectid != objectid ||
6418 found_key.type != BTRFS_EXTENT_DATA_KEY)
6419 goto not_found;
6420 if (start + len <= found_key.offset)
6421 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006422 if (start > found_key.offset)
6423 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006424 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006425 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006426 em->len = found_key.offset - start;
6427 goto not_found_em;
6428 }
6429
Filipe Manana7ffbb592014-06-09 03:48:05 +01006430 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6431
Yan Zhengd899e052008-10-30 14:25:28 -04006432 if (found_type == BTRFS_FILE_EXTENT_REG ||
6433 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006434 goto insert;
6435 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006436 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006437 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006438 size_t size;
6439 size_t extent_offset;
6440 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006441
Filipe Manana7ffbb592014-06-09 03:48:05 +01006442 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006443 goto out;
Yan689f9342007-10-29 11:41:07 -04006444
Chris Mason514ac8a2014-01-03 21:07:00 -08006445 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006446 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006447 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006448 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006449 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006450 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006451 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006452 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006453 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006454 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006455 if (btrfs_file_extent_compression(leaf, item) !=
6456 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006457 ret = uncompress_inline(path, inode, page,
6458 pg_offset,
6459 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006460 if (ret) {
6461 err = ret;
6462 goto out;
6463 }
Chris Masonc8b97812008-10-29 14:49:59 -04006464 } else {
6465 map = kmap(page);
6466 read_extent_buffer(leaf, map + pg_offset, ptr,
6467 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006468 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6469 memset(map + pg_offset + copy_size, 0,
6470 PAGE_CACHE_SIZE - pg_offset -
6471 copy_size);
6472 }
Chris Masonc8b97812008-10-29 14:49:59 -04006473 kunmap(page);
6474 }
Chris Mason179e29e2007-11-01 11:28:41 -04006475 flush_dcache_page(page);
6476 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006477 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006478 if (!trans) {
6479 kunmap(page);
6480 free_extent_map(em);
6481 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006482
David Sterbab3b4aa72011-04-21 01:20:15 +02006483 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006484 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006485
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006486 if (IS_ERR(trans))
6487 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006488 goto again;
6489 }
Chris Masonc8b97812008-10-29 14:49:59 -04006490 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006491 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006492 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006493 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006494 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006495 }
Chris Masond1310b22008-01-24 16:13:08 -05006496 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006497 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006498 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006499 }
6500not_found:
6501 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006502 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006503 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006504not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006505 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006506 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006507insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006508 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006509 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006510 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006511 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006512 err = -EIO;
6513 goto out;
6514 }
Chris Masond1310b22008-01-24 16:13:08 -05006515
6516 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006517 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006518 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006519 /* it is possible that someone inserted the extent into the tree
6520 * while we had the lock dropped. It is also possible that
6521 * an overlapping map exists in the tree
6522 */
Chris Masona52d9a82007-08-27 16:49:44 -04006523 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006524 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006525
6526 ret = 0;
6527
Chris Mason3b951512008-04-17 11:29:12 -04006528 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006529 if (existing && (existing->start > start ||
6530 existing->start + existing->len <= start)) {
6531 free_extent_map(existing);
6532 existing = NULL;
6533 }
Chris Mason3b951512008-04-17 11:29:12 -04006534 if (!existing) {
6535 existing = lookup_extent_mapping(em_tree, em->start,
6536 em->len);
6537 if (existing) {
6538 err = merge_extent_mapping(em_tree, existing,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006539 em, start);
Chris Mason3b951512008-04-17 11:29:12 -04006540 free_extent_map(existing);
6541 if (err) {
6542 free_extent_map(em);
6543 em = NULL;
6544 }
6545 } else {
6546 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006547 free_extent_map(em);
6548 em = NULL;
6549 }
6550 } else {
6551 free_extent_map(em);
6552 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006553 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006554 }
Chris Masona52d9a82007-08-27 16:49:44 -04006555 }
Chris Mason890871b2009-09-02 16:24:52 -04006556 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006557out:
liubo1abe9b82011-03-24 11:18:59 +00006558
Steven Rostedt4cd85872013-11-14 22:57:29 -05006559 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006560
Chris Masonf4219502008-07-22 11:18:09 -04006561 if (path)
6562 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006563 if (trans) {
6564 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006565 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006566 err = ret;
6567 }
Chris Masona52d9a82007-08-27 16:49:44 -04006568 if (err) {
6569 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006570 return ERR_PTR(err);
6571 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006572 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006573 return em;
6574}
6575
Chris Masonec29ed52011-02-23 16:23:20 -05006576struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6577 size_t pg_offset, u64 start, u64 len,
6578 int create)
6579{
6580 struct extent_map *em;
6581 struct extent_map *hole_em = NULL;
6582 u64 range_start = start;
6583 u64 end;
6584 u64 found;
6585 u64 found_end;
6586 int err = 0;
6587
6588 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6589 if (IS_ERR(em))
6590 return em;
6591 if (em) {
6592 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006593 * if our em maps to
6594 * - a hole or
6595 * - a pre-alloc extent,
6596 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006597 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006598 if (em->block_start != EXTENT_MAP_HOLE &&
6599 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006600 return em;
6601 else
6602 hole_em = em;
6603 }
6604
6605 /* check to see if we've wrapped (len == -1 or similar) */
6606 end = start + len;
6607 if (end < start)
6608 end = (u64)-1;
6609 else
6610 end -= 1;
6611
6612 em = NULL;
6613
6614 /* ok, we didn't find anything, lets look for delalloc */
6615 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6616 end, len, EXTENT_DELALLOC, 1);
6617 found_end = range_start + found;
6618 if (found_end < range_start)
6619 found_end = (u64)-1;
6620
6621 /*
6622 * we didn't find anything useful, return
6623 * the original results from get_extent()
6624 */
6625 if (range_start > end || found_end <= start) {
6626 em = hole_em;
6627 hole_em = NULL;
6628 goto out;
6629 }
6630
6631 /* adjust the range_start to make sure it doesn't
6632 * go backwards from the start they passed in
6633 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306634 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006635 found = found_end - range_start;
6636
6637 if (found > 0) {
6638 u64 hole_start = start;
6639 u64 hole_len = len;
6640
David Sterba172ddd62011-04-21 00:48:27 +02006641 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006642 if (!em) {
6643 err = -ENOMEM;
6644 goto out;
6645 }
6646 /*
6647 * when btrfs_get_extent can't find anything it
6648 * returns one huge hole
6649 *
6650 * make sure what it found really fits our range, and
6651 * adjust to make sure it is based on the start from
6652 * the caller
6653 */
6654 if (hole_em) {
6655 u64 calc_end = extent_map_end(hole_em);
6656
6657 if (calc_end <= start || (hole_em->start > end)) {
6658 free_extent_map(hole_em);
6659 hole_em = NULL;
6660 } else {
6661 hole_start = max(hole_em->start, start);
6662 hole_len = calc_end - hole_start;
6663 }
6664 }
6665 em->bdev = NULL;
6666 if (hole_em && range_start > hole_start) {
6667 /* our hole starts before our delalloc, so we
6668 * have to return just the parts of the hole
6669 * that go until the delalloc starts
6670 */
6671 em->len = min(hole_len,
6672 range_start - hole_start);
6673 em->start = hole_start;
6674 em->orig_start = hole_start;
6675 /*
6676 * don't adjust block start at all,
6677 * it is fixed at EXTENT_MAP_HOLE
6678 */
6679 em->block_start = hole_em->block_start;
6680 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006681 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6682 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006683 } else {
6684 em->start = range_start;
6685 em->len = found;
6686 em->orig_start = range_start;
6687 em->block_start = EXTENT_MAP_DELALLOC;
6688 em->block_len = found;
6689 }
6690 } else if (hole_em) {
6691 return hole_em;
6692 }
6693out:
6694
6695 free_extent_map(hole_em);
6696 if (err) {
6697 free_extent_map(em);
6698 return ERR_PTR(err);
6699 }
6700 return em;
6701}
6702
Josef Bacik4b46fce2010-05-23 11:00:55 -04006703static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6704 u64 start, u64 len)
6705{
6706 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006707 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006708 struct btrfs_key ins;
6709 u64 alloc_hint;
6710 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006711
Josef Bacik4b46fce2010-05-23 11:00:55 -04006712 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006713 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08006714 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006715 if (ret)
6716 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006717
Josef Bacik70c8a912012-10-11 16:54:30 -04006718 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006719 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006720 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08006721 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006722 return em;
6723 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006724
6725 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6726 ins.offset, ins.offset, 0);
6727 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08006728 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006729 free_extent_map(em);
6730 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006731 }
Josef Bacik00361582013-08-14 14:02:47 -04006732
Josef Bacik4b46fce2010-05-23 11:00:55 -04006733 return em;
6734}
6735
Chris Mason46bfbb52010-05-26 11:04:10 -04006736/*
6737 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6738 * block must be cow'd
6739 */
Josef Bacik00361582013-08-14 14:02:47 -04006740noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006741 u64 *orig_start, u64 *orig_block_len,
6742 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006743{
Josef Bacik00361582013-08-14 14:02:47 -04006744 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006745 struct btrfs_path *path;
6746 int ret;
6747 struct extent_buffer *leaf;
6748 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006749 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006750 struct btrfs_file_extent_item *fi;
6751 struct btrfs_key key;
6752 u64 disk_bytenr;
6753 u64 backref_offset;
6754 u64 extent_end;
6755 u64 num_bytes;
6756 int slot;
6757 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006758 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006759
Chris Mason46bfbb52010-05-26 11:04:10 -04006760 path = btrfs_alloc_path();
6761 if (!path)
6762 return -ENOMEM;
6763
Josef Bacik00361582013-08-14 14:02:47 -04006764 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006765 offset, 0);
6766 if (ret < 0)
6767 goto out;
6768
6769 slot = path->slots[0];
6770 if (ret == 1) {
6771 if (slot == 0) {
6772 /* can't find the item, must cow */
6773 ret = 0;
6774 goto out;
6775 }
6776 slot--;
6777 }
6778 ret = 0;
6779 leaf = path->nodes[0];
6780 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006781 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006782 key.type != BTRFS_EXTENT_DATA_KEY) {
6783 /* not our file or wrong item type, must cow */
6784 goto out;
6785 }
6786
6787 if (key.offset > offset) {
6788 /* Wrong offset, must cow */
6789 goto out;
6790 }
6791
6792 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6793 found_type = btrfs_file_extent_type(leaf, fi);
6794 if (found_type != BTRFS_FILE_EXTENT_REG &&
6795 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6796 /* not a regular extent, must cow */
6797 goto out;
6798 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006799
6800 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6801 goto out;
6802
Miao Xiee77751a2013-12-27 21:11:50 +08006803 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6804 if (extent_end <= offset)
6805 goto out;
6806
Chris Mason46bfbb52010-05-26 11:04:10 -04006807 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006808 if (disk_bytenr == 0)
6809 goto out;
6810
6811 if (btrfs_file_extent_compression(leaf, fi) ||
6812 btrfs_file_extent_encryption(leaf, fi) ||
6813 btrfs_file_extent_other_encoding(leaf, fi))
6814 goto out;
6815
Chris Mason46bfbb52010-05-26 11:04:10 -04006816 backref_offset = btrfs_file_extent_offset(leaf, fi);
6817
Josef Bacik7ee9e442013-06-21 16:37:03 -04006818 if (orig_start) {
6819 *orig_start = key.offset - backref_offset;
6820 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6821 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6822 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006823
Chris Mason46bfbb52010-05-26 11:04:10 -04006824 if (btrfs_extent_readonly(root, disk_bytenr))
6825 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006826
6827 num_bytes = min(offset + *len, extent_end) - offset;
6828 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6829 u64 range_end;
6830
6831 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
6832 ret = test_range_bit(io_tree, offset, range_end,
6833 EXTENT_DELALLOC, 0, NULL);
6834 if (ret) {
6835 ret = -EAGAIN;
6836 goto out;
6837 }
6838 }
6839
Josef Bacik1bda19e2013-10-18 12:10:36 -04006840 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006841
6842 /*
6843 * look for other files referencing this extent, if we
6844 * find any we must cow
6845 */
Josef Bacik00361582013-08-14 14:02:47 -04006846 trans = btrfs_join_transaction(root);
6847 if (IS_ERR(trans)) {
6848 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006849 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006850 }
6851
6852 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6853 key.offset - backref_offset, disk_bytenr);
6854 btrfs_end_transaction(trans, root);
6855 if (ret) {
6856 ret = 0;
6857 goto out;
6858 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006859
6860 /*
6861 * adjust disk_bytenr and num_bytes to cover just the bytes
6862 * in this extent we are about to write. If there
6863 * are any csums in that range we have to cow in order
6864 * to keep the csums correct
6865 */
6866 disk_bytenr += backref_offset;
6867 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006868 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6869 goto out;
6870 /*
6871 * all of the above have passed, it is safe to overwrite this extent
6872 * without cow
6873 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006874 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006875 ret = 1;
6876out:
6877 btrfs_free_path(path);
6878 return ret;
6879}
6880
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006881bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
6882{
6883 struct radix_tree_root *root = &inode->i_mapping->page_tree;
6884 int found = false;
6885 void **pagep = NULL;
6886 struct page *page = NULL;
6887 int start_idx;
6888 int end_idx;
6889
6890 start_idx = start >> PAGE_CACHE_SHIFT;
6891
6892 /*
6893 * end is the last byte in the last page. end == start is legal
6894 */
6895 end_idx = end >> PAGE_CACHE_SHIFT;
6896
6897 rcu_read_lock();
6898
6899 /* Most of the code in this while loop is lifted from
6900 * find_get_page. It's been modified to begin searching from a
6901 * page and return just the first page found in that range. If the
6902 * found idx is less than or equal to the end idx then we know that
6903 * a page exists. If no pages are found or if those pages are
6904 * outside of the range then we're fine (yay!) */
6905 while (page == NULL &&
6906 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
6907 page = radix_tree_deref_slot(pagep);
6908 if (unlikely(!page))
6909 break;
6910
6911 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01006912 if (radix_tree_deref_retry(page)) {
6913 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006914 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01006915 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006916 /*
6917 * Otherwise, shmem/tmpfs must be storing a swap entry
6918 * here as an exceptional entry: so return it without
6919 * attempting to raise page count.
6920 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01006921 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006922 break; /* TODO: Is this relevant for this use case? */
6923 }
6924
Filipe Manana91405152014-06-05 13:22:24 +01006925 if (!page_cache_get_speculative(page)) {
6926 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006927 continue;
Filipe Manana91405152014-06-05 13:22:24 +01006928 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006929
6930 /*
6931 * Has the page moved?
6932 * This is part of the lockless pagecache protocol. See
6933 * include/linux/pagemap.h for details.
6934 */
6935 if (unlikely(page != *pagep)) {
6936 page_cache_release(page);
6937 page = NULL;
6938 }
6939 }
6940
6941 if (page) {
6942 if (page->index <= end_idx)
6943 found = true;
6944 page_cache_release(page);
6945 }
6946
6947 rcu_read_unlock();
6948 return found;
6949}
6950
Josef Bacikeb838e72012-07-31 16:28:48 -04006951static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6952 struct extent_state **cached_state, int writing)
6953{
6954 struct btrfs_ordered_extent *ordered;
6955 int ret = 0;
6956
6957 while (1) {
6958 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6959 0, cached_state);
6960 /*
6961 * We're concerned with the entire range that we're going to be
6962 * doing DIO to, so we need to make sure theres no ordered
6963 * extents in this range.
6964 */
6965 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6966 lockend - lockstart + 1);
6967
6968 /*
6969 * We need to make sure there are no buffered pages in this
6970 * range either, we could have raced between the invalidate in
6971 * generic_file_direct_write and locking the extent. The
6972 * invalidate needs to happen so that reads after a write do not
6973 * get stale data.
6974 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006975 if (!ordered &&
6976 (!writing ||
6977 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04006978 break;
6979
6980 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6981 cached_state, GFP_NOFS);
6982
6983 if (ordered) {
6984 btrfs_start_ordered_extent(inode, ordered, 1);
6985 btrfs_put_ordered_extent(ordered);
6986 } else {
6987 /* Screw you mmap */
6988 ret = filemap_write_and_wait_range(inode->i_mapping,
6989 lockstart,
6990 lockend);
6991 if (ret)
6992 break;
6993
6994 /*
6995 * If we found a page that couldn't be invalidated just
6996 * fall back to buffered.
6997 */
6998 ret = invalidate_inode_pages2_range(inode->i_mapping,
6999 lockstart >> PAGE_CACHE_SHIFT,
7000 lockend >> PAGE_CACHE_SHIFT);
7001 if (ret)
7002 break;
7003 }
7004
7005 cond_resched();
7006 }
7007
7008 return ret;
7009}
7010
Josef Bacik69ffb542012-09-11 15:40:07 -04007011static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7012 u64 len, u64 orig_start,
7013 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007014 u64 orig_block_len, u64 ram_bytes,
7015 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007016{
7017 struct extent_map_tree *em_tree;
7018 struct extent_map *em;
7019 struct btrfs_root *root = BTRFS_I(inode)->root;
7020 int ret;
7021
7022 em_tree = &BTRFS_I(inode)->extent_tree;
7023 em = alloc_extent_map();
7024 if (!em)
7025 return ERR_PTR(-ENOMEM);
7026
7027 em->start = start;
7028 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007029 em->mod_start = start;
7030 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007031 em->len = len;
7032 em->block_len = block_len;
7033 em->block_start = block_start;
7034 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007035 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007036 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007037 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007038 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7039 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007040 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007041
7042 do {
7043 btrfs_drop_extent_cache(inode, em->start,
7044 em->start + em->len - 1, 0);
7045 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007046 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007047 write_unlock(&em_tree->lock);
7048 } while (ret == -EEXIST);
7049
7050 if (ret) {
7051 free_extent_map(em);
7052 return ERR_PTR(ret);
7053 }
7054
7055 return em;
7056}
7057
7058
Josef Bacik4b46fce2010-05-23 11:00:55 -04007059static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7060 struct buffer_head *bh_result, int create)
7061{
7062 struct extent_map *em;
7063 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007064 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007065 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007066 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007067 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007068 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007069 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007070
Miao Xie172a5042013-02-21 02:48:22 -07007071 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04007072 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007073 else
Josef Bacikc3298612012-08-03 16:49:19 -04007074 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007075
Josef Bacikc3298612012-08-03 16:49:19 -04007076 lockstart = start;
7077 lockend = start + len - 1;
7078
Josef Bacikeb838e72012-07-31 16:28:48 -04007079 /*
7080 * If this errors out it's because we couldn't invalidate pagecache for
7081 * this range and we need to fallback to buffered.
7082 */
7083 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7084 return -ENOTBLK;
7085
Josef Bacik4b46fce2010-05-23 11:00:55 -04007086 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007087 if (IS_ERR(em)) {
7088 ret = PTR_ERR(em);
7089 goto unlock_err;
7090 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007091
7092 /*
7093 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7094 * io. INLINE is special, and we could probably kludge it in here, but
7095 * it's still buffered so for safety lets just fall back to the generic
7096 * buffered path.
7097 *
7098 * For COMPRESSED we _have_ to read the entire extent in so we can
7099 * decompress it, so there will be buffering required no matter what we
7100 * do, so go ahead and fallback to buffered.
7101 *
7102 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7103 * to buffered IO. Don't blame me, this is the price we pay for using
7104 * the generic code.
7105 */
7106 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7107 em->block_start == EXTENT_MAP_INLINE) {
7108 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007109 ret = -ENOTBLK;
7110 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007111 }
7112
7113 /* Just a good old fashioned hole, return */
7114 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7115 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7116 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007117 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007118 }
7119
7120 /*
7121 * We don't allocate a new extent in the following cases
7122 *
7123 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7124 * existing extent.
7125 * 2) The extent is marked as PREALLOC. We're good to go here and can
7126 * just use the extent.
7127 *
7128 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007129 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007130 len = min(len, em->len - (start - em->start));
7131 lockstart = start + len;
7132 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007133 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007134
7135 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7136 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7137 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007138 int type;
7139 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04007140 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007141
7142 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7143 type = BTRFS_ORDERED_PREALLOC;
7144 else
7145 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007146 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007147 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007148
Josef Bacik00361582013-08-14 14:02:47 -04007149 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007150 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007151 if (type == BTRFS_ORDERED_PREALLOC) {
7152 free_extent_map(em);
7153 em = create_pinned_em(inode, start, len,
7154 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007155 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007156 orig_block_len,
7157 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007158 if (IS_ERR(em)) {
7159 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007160 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007161 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007162 }
7163
Chris Mason46bfbb52010-05-26 11:04:10 -04007164 ret = btrfs_add_ordered_extent_dio(inode, start,
7165 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007166 if (ret) {
7167 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007168 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007169 }
7170 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007171 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007172 }
Josef Bacik00361582013-08-14 14:02:47 -04007173
Chris Mason46bfbb52010-05-26 11:04:10 -04007174 /*
7175 * this will cow the extent, reset the len in case we changed
7176 * it above
7177 */
7178 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007179 free_extent_map(em);
7180 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007181 if (IS_ERR(em)) {
7182 ret = PTR_ERR(em);
7183 goto unlock_err;
7184 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007185 len = min(len, em->len - (start - em->start));
7186unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007187 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7188 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007189 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007190 bh_result->b_bdev = em->bdev;
7191 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007192 if (create) {
7193 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7194 set_buffer_new(bh_result);
7195
7196 /*
7197 * Need to update the i_size under the extent lock so buffered
7198 * readers will get the updated i_size when we unlock.
7199 */
7200 if (start + len > i_size_read(inode))
7201 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007202
Miao Xie172a5042013-02-21 02:48:22 -07007203 spin_lock(&BTRFS_I(inode)->lock);
7204 BTRFS_I(inode)->outstanding_extents++;
7205 spin_unlock(&BTRFS_I(inode)->lock);
7206
Miao Xie09348562013-02-07 10:12:07 +00007207 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7208 lockstart + len - 1, EXTENT_DELALLOC, NULL,
7209 &cached_state, GFP_NOFS);
7210 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04007211 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007212
Josef Bacikeb838e72012-07-31 16:28:48 -04007213 /*
7214 * In the case of write we need to clear and unlock the entire range,
7215 * in the case of read we need to unlock only the end area that we
7216 * aren't using if there is any left over space.
7217 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007218 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007219 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7220 lockend, unlock_bits, 1, 0,
7221 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007222 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007223 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007224 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007225
Josef Bacik4b46fce2010-05-23 11:00:55 -04007226 free_extent_map(em);
7227
7228 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007229
7230unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007231 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7232 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
7233 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007234}
7235
Josef Bacik4b46fce2010-05-23 11:00:55 -04007236static void btrfs_endio_direct_read(struct bio *bio, int err)
7237{
Miao Xiee65e1532010-11-22 03:04:43 +00007238 struct btrfs_dio_private *dip = bio->bi_private;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007239 struct bio_vec *bvec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007240 struct inode *inode = dip->inode;
7241 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04007242 struct bio *dio_bio;
Miao Xiefacc8a22013-07-25 19:22:34 +08007243 u32 *csums = (u32 *)dip->csum;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007244 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007245 int i;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007246
7247 start = dip->logical_offset;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007248 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007249 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
7250 struct page *page = bvec->bv_page;
7251 char *kaddr;
7252 u32 csum = ~(u32)0;
7253 unsigned long flags;
7254
7255 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08007256 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00007257 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007258 csum, bvec->bv_len);
7259 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08007260 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007261 local_irq_restore(flags);
7262
7263 flush_dcache_page(bvec->bv_page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08007264 if (csum != csums[i]) {
Miao Xiefacc8a22013-07-25 19:22:34 +08007265 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007266 btrfs_ino(inode), start, csum,
Kent Overstreet2c30c712013-11-07 12:20:26 -08007267 csums[i]);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007268 err = -EIO;
7269 }
7270 }
7271
7272 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007273 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007274
7275 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007276 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007277 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007278
Josef Bacik4b46fce2010-05-23 11:00:55 -04007279 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007280
7281 /* If we had a csum failure make sure to clear the uptodate flag */
7282 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007283 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7284 dio_end_io(dio_bio, err);
7285 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007286}
7287
7288static void btrfs_endio_direct_write(struct bio *bio, int err)
7289{
7290 struct btrfs_dio_private *dip = bio->bi_private;
7291 struct inode *inode = dip->inode;
7292 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007293 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007294 u64 ordered_offset = dip->logical_offset;
7295 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007296 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007297 int ret;
7298
7299 if (err)
7300 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007301again:
7302 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7303 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007304 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007305 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007306 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007307
Liu Bo9e0af232014-08-15 23:36:53 +08007308 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7309 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007310 btrfs_queue_work(root->fs_info->endio_write_workers,
7311 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007312out_test:
7313 /*
7314 * our bio might span multiple ordered extents. If we haven't
7315 * completed the accounting for the whole dio, go back and try again
7316 */
7317 if (ordered_offset < dip->logical_offset + dip->bytes) {
7318 ordered_bytes = dip->logical_offset + dip->bytes -
7319 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007320 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007321 goto again;
7322 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007323out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007324 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007325
Josef Bacik4b46fce2010-05-23 11:00:55 -04007326 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007327
7328 /* If we had an error make sure to clear the uptodate flag */
7329 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007330 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7331 dio_end_io(dio_bio, err);
7332 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007333}
7334
Chris Masoneaf25d92010-05-25 09:48:28 -04007335static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7336 struct bio *bio, int mirror_num,
7337 unsigned long bio_flags, u64 offset)
7338{
7339 int ret;
7340 struct btrfs_root *root = BTRFS_I(inode)->root;
7341 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007342 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007343 return 0;
7344}
7345
Miao Xiee65e1532010-11-22 03:04:43 +00007346static void btrfs_end_dio_bio(struct bio *bio, int err)
7347{
7348 struct btrfs_dio_private *dip = bio->bi_private;
7349
7350 if (err) {
Frank Holtonefe120a2013-12-20 11:37:06 -05007351 btrfs_err(BTRFS_I(dip->inode)->root->fs_info,
7352 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007353 btrfs_ino(dip->inode), bio->bi_rw,
Kent Overstreet4f024f32013-10-11 15:44:27 -07007354 (unsigned long long)bio->bi_iter.bi_sector,
7355 bio->bi_iter.bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007356 dip->errors = 1;
7357
7358 /*
7359 * before atomic variable goto zero, we must make sure
7360 * dip->errors is perceived to be set.
7361 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007362 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007363 }
7364
7365 /* if there are more bios still pending for this dio, just exit */
7366 if (!atomic_dec_and_test(&dip->pending_bios))
7367 goto out;
7368
Chris Mason9be33952013-05-17 18:30:14 -04007369 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007370 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007371 } else {
7372 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007373 bio_endio(dip->orig_bio, 0);
7374 }
7375out:
7376 bio_put(bio);
7377}
7378
7379static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7380 u64 first_sector, gfp_t gfp_flags)
7381{
7382 int nr_vecs = bio_get_nr_vecs(bdev);
7383 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7384}
7385
7386static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7387 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007388 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007389{
Miao Xiefacc8a22013-07-25 19:22:34 +08007390 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007391 int write = rw & REQ_WRITE;
7392 struct btrfs_root *root = BTRFS_I(inode)->root;
7393 int ret;
7394
Josef Bacikb812ce22012-11-16 13:56:32 -05007395 if (async_submit)
7396 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7397
Miao Xiee65e1532010-11-22 03:04:43 +00007398 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007399
7400 if (!write) {
7401 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7402 if (ret)
7403 goto err;
7404 }
Miao Xiee65e1532010-11-22 03:04:43 +00007405
Josef Bacik1ae39932011-04-06 14:41:34 -04007406 if (skip_sum)
7407 goto map;
7408
7409 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007410 ret = btrfs_wq_submit_bio(root->fs_info,
7411 inode, rw, bio, 0, 0,
7412 file_offset,
7413 __btrfs_submit_bio_start_direct_io,
7414 __btrfs_submit_bio_done);
7415 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007416 } else if (write) {
7417 /*
7418 * If we aren't doing async submit, calculate the csum of the
7419 * bio now.
7420 */
7421 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7422 if (ret)
7423 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007424 } else if (!skip_sum) {
Miao Xiefacc8a22013-07-25 19:22:34 +08007425 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
7426 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007427 if (ret)
7428 goto err;
7429 }
Miao Xiee65e1532010-11-22 03:04:43 +00007430
Josef Bacik1ae39932011-04-06 14:41:34 -04007431map:
7432 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007433err:
7434 bio_put(bio);
7435 return ret;
7436}
7437
7438static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7439 int skip_sum)
7440{
7441 struct inode *inode = dip->inode;
7442 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007443 struct bio *bio;
7444 struct bio *orig_bio = dip->orig_bio;
7445 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007446 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00007447 u64 file_offset = dip->logical_offset;
7448 u64 submit_len = 0;
7449 u64 map_length;
7450 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007451 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007452 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007453
Kent Overstreet4f024f32013-10-11 15:44:27 -07007454 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007455 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007456 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08007457 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00007458 return -EIO;
Miao Xiefacc8a22013-07-25 19:22:34 +08007459
Kent Overstreet4f024f32013-10-11 15:44:27 -07007460 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04007461 bio = orig_bio;
7462 goto submit;
7463 }
7464
David Woodhouse53b381b2013-01-29 18:40:14 -05007465 /* async crcs make it difficult to collect full stripe writes. */
7466 if (btrfs_get_alloc_profile(root, 1) &
7467 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7468 async_submit = 0;
7469 else
7470 async_submit = 1;
7471
Josef Bacik02f57c72011-04-06 14:25:44 -04007472 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7473 if (!bio)
7474 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08007475
Josef Bacik02f57c72011-04-06 14:25:44 -04007476 bio->bi_private = dip;
7477 bio->bi_end_io = btrfs_end_dio_bio;
7478 atomic_inc(&dip->pending_bios);
7479
Miao Xiee65e1532010-11-22 03:04:43 +00007480 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7481 if (unlikely(map_length < submit_len + bvec->bv_len ||
7482 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7483 bvec->bv_offset) < bvec->bv_len)) {
7484 /*
7485 * inc the count before we submit the bio so
7486 * we know the end IO handler won't happen before
7487 * we inc the count. Otherwise, the dip might get freed
7488 * before we're done setting it up
7489 */
7490 atomic_inc(&dip->pending_bios);
7491 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7492 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007493 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007494 if (ret) {
7495 bio_put(bio);
7496 atomic_dec(&dip->pending_bios);
7497 goto out_err;
7498 }
7499
Miao Xiee65e1532010-11-22 03:04:43 +00007500 start_sector += submit_len >> 9;
7501 file_offset += submit_len;
7502
7503 submit_len = 0;
7504 nr_pages = 0;
7505
7506 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7507 start_sector, GFP_NOFS);
7508 if (!bio)
7509 goto out_err;
7510 bio->bi_private = dip;
7511 bio->bi_end_io = btrfs_end_dio_bio;
7512
Kent Overstreet4f024f32013-10-11 15:44:27 -07007513 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007514 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007515 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007516 &map_length, NULL, 0);
7517 if (ret) {
7518 bio_put(bio);
7519 goto out_err;
7520 }
7521 } else {
7522 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307523 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00007524 bvec++;
7525 }
7526 }
7527
Josef Bacik02f57c72011-04-06 14:25:44 -04007528submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007529 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007530 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007531 if (!ret)
7532 return 0;
7533
7534 bio_put(bio);
7535out_err:
7536 dip->errors = 1;
7537 /*
7538 * before atomic variable goto zero, we must
7539 * make sure dip->errors is perceived to be set.
7540 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007541 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007542 if (atomic_dec_and_test(&dip->pending_bios))
7543 bio_io_error(dip->orig_bio);
7544
7545 /* bio_end_io() will handle error, so we needn't return it */
7546 return 0;
7547}
7548
Chris Mason9be33952013-05-17 18:30:14 -04007549static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7550 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007551{
7552 struct btrfs_root *root = BTRFS_I(inode)->root;
7553 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007554 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007555 int skip_sum;
Miao Xiefacc8a22013-07-25 19:22:34 +08007556 int sum_len;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007557 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007558 int ret = 0;
Miao Xiefacc8a22013-07-25 19:22:34 +08007559 u16 csum_size;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007560
7561 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7562
Chris Mason9be33952013-05-17 18:30:14 -04007563 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007564 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007565 ret = -ENOMEM;
7566 goto free_ordered;
7567 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007568
Miao Xiefacc8a22013-07-25 19:22:34 +08007569 if (!skip_sum && !write) {
7570 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
Kent Overstreet4f024f32013-10-11 15:44:27 -07007571 sum_len = dio_bio->bi_iter.bi_size >>
7572 inode->i_sb->s_blocksize_bits;
Miao Xiefacc8a22013-07-25 19:22:34 +08007573 sum_len *= csum_size;
7574 } else {
7575 sum_len = 0;
7576 }
7577
7578 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007579 if (!dip) {
7580 ret = -ENOMEM;
7581 goto free_io_bio;
7582 }
7583
7584 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007585 dip->inode = inode;
7586 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007587 dip->bytes = dio_bio->bi_iter.bi_size;
7588 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04007589 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007590 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007591 dip->orig_bio = io_bio;
7592 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007593 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007594
7595 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007596 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007597 else
Chris Mason9be33952013-05-17 18:30:14 -04007598 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007599
Miao Xiee65e1532010-11-22 03:04:43 +00007600 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7601 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007602 return;
Chris Mason9be33952013-05-17 18:30:14 -04007603
7604free_io_bio:
7605 bio_put(io_bio);
7606
Josef Bacik4b46fce2010-05-23 11:00:55 -04007607free_ordered:
7608 /*
7609 * If this is a write, we need to clean up the reserved space and kill
7610 * the ordered extent.
7611 */
7612 if (write) {
7613 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007614 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007615 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7616 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7617 btrfs_free_reserved_extent(root, ordered->start,
Miao Xiee570fd22014-06-19 10:42:50 +08007618 ordered->disk_len, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007619 btrfs_put_ordered_extent(ordered);
7620 btrfs_put_ordered_extent(ordered);
7621 }
Chris Mason9be33952013-05-17 18:30:14 -04007622 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007623}
7624
Chris Mason5a5f79b2010-05-26 21:33:37 -04007625static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04007626 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04007627{
7628 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007629 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007630 unsigned blocksize_mask = root->sectorsize - 1;
7631 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007632
7633 if (offset & blocksize_mask)
7634 goto out;
7635
Al Viro28060d52014-03-22 05:15:17 -04007636 if (iov_iter_alignment(iter) & blocksize_mask)
7637 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007638
Al Viro28060d52014-03-22 05:15:17 -04007639 /* If this is a write we don't need to check anymore */
7640 if (rw & WRITE)
7641 return 0;
7642 /*
7643 * Check to make sure we don't have duplicate iov_base's in this
7644 * iovec, if so return EINVAL, otherwise we'll get csum errors
7645 * when reading back.
7646 */
7647 for (seg = 0; seg < iter->nr_segs; seg++) {
7648 for (i = seg + 1; i < iter->nr_segs; i++) {
7649 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00007650 goto out;
7651 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007652 }
7653 retval = 0;
7654out:
7655 return retval;
7656}
Josef Bacikeb838e72012-07-31 16:28:48 -04007657
Chris Mason16432982008-04-10 10:23:21 -04007658static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05007659 struct iov_iter *iter, loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04007660{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007661 struct file *file = iocb->ki_filp;
7662 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007663 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007664 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007665 bool wakeup = true;
7666 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007667 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007668
Al Viro28060d52014-03-22 05:15:17 -04007669 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007670 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007671
Miao Xie38851cc2013-02-08 07:04:11 +00007672 atomic_inc(&inode->i_dio_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007673 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00007674
Josef Bacik0e267c42013-07-02 10:38:02 -04007675 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08007676 * The generic stuff only does filemap_write_and_wait_range, which
7677 * isn't enough if we've written compressed pages to this area, so
7678 * we need to flush the dirty pages again to make absolutely sure
7679 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04007680 */
Al Viroa6cbcd42014-03-04 22:38:00 -05007681 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08007682 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7683 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08007684 filemap_fdatawrite_range(inode->i_mapping, offset,
7685 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04007686
Miao Xie09348562013-02-07 10:12:07 +00007687 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007688 /*
7689 * If the write DIO is beyond the EOF, we need update
7690 * the isize, but it is protected by i_mutex. So we can
7691 * not unlock the i_mutex at this case.
7692 */
7693 if (offset + count <= inode->i_size) {
7694 mutex_unlock(&inode->i_mutex);
7695 relock = true;
7696 }
Miao Xie09348562013-02-07 10:12:07 +00007697 ret = btrfs_delalloc_reserve_space(inode, count);
7698 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007699 goto out;
7700 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7701 &BTRFS_I(inode)->runtime_flags))) {
7702 inode_dio_done(inode);
7703 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7704 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007705 }
7706
7707 ret = __blockdev_direct_IO(rw, iocb, inode,
7708 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Al Viro31b14032014-03-05 01:33:16 -05007709 iter, offset, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007710 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007711 if (rw & WRITE) {
7712 if (ret < 0 && ret != -EIOCBQUEUED)
7713 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007714 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007715 btrfs_delalloc_release_space(inode,
7716 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007717 else
7718 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007719 }
Miao Xie38851cc2013-02-08 07:04:11 +00007720out:
Miao Xie2e60a512013-02-08 07:01:08 +00007721 if (wakeup)
7722 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007723 if (relock)
7724 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007725
7726 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007727}
7728
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007729#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7730
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007731static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7732 __u64 start, __u64 len)
7733{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007734 int ret;
7735
7736 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7737 if (ret)
7738 return ret;
7739
Chris Masonec29ed52011-02-23 16:23:20 -05007740 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007741}
7742
Chris Mason9ebefb182007-06-15 13:50:00 -04007743int btrfs_readpage(struct file *file, struct page *page)
7744{
Chris Masond1310b22008-01-24 16:13:08 -05007745 struct extent_io_tree *tree;
7746 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007747 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007748}
Chris Mason1832a6d2007-12-21 16:27:21 -05007749
Chris Mason39279cc2007-06-12 06:35:45 -04007750static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7751{
Chris Masond1310b22008-01-24 16:13:08 -05007752 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007753
7754
7755 if (current->flags & PF_MEMALLOC) {
7756 redirty_page_for_writepage(wbc, page);
7757 unlock_page(page);
7758 return 0;
7759 }
Chris Masond1310b22008-01-24 16:13:08 -05007760 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007761 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7762}
Chris Mason39279cc2007-06-12 06:35:45 -04007763
Eric Sandeen48a3b632013-04-25 20:41:01 +00007764static int btrfs_writepages(struct address_space *mapping,
7765 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007766{
Chris Masond1310b22008-01-24 16:13:08 -05007767 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007768
Chris Masond1310b22008-01-24 16:13:08 -05007769 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007770 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7771}
7772
Chris Mason3ab2fb52007-11-08 10:59:22 -05007773static int
7774btrfs_readpages(struct file *file, struct address_space *mapping,
7775 struct list_head *pages, unsigned nr_pages)
7776{
Chris Masond1310b22008-01-24 16:13:08 -05007777 struct extent_io_tree *tree;
7778 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007779 return extent_readpages(tree, mapping, pages, nr_pages,
7780 btrfs_get_extent);
7781}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007782static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007783{
Chris Masond1310b22008-01-24 16:13:08 -05007784 struct extent_io_tree *tree;
7785 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007786 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007787
Chris Masond1310b22008-01-24 16:13:08 -05007788 tree = &BTRFS_I(page->mapping->host)->io_tree;
7789 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007790 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007791 if (ret == 1) {
7792 ClearPagePrivate(page);
7793 set_page_private(page, 0);
7794 page_cache_release(page);
7795 }
7796 return ret;
7797}
Chris Mason39279cc2007-06-12 06:35:45 -04007798
Chris Masone6dcd2d2008-07-17 12:53:50 -04007799static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7800{
Chris Mason98509cf2008-09-11 15:51:43 -04007801 if (PageWriteback(page) || PageDirty(page))
7802 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007803 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007804}
7805
Lukas Czernerd47992f2013-05-21 23:17:23 -04007806static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7807 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007808{
Josef Bacik5fd02042012-05-02 14:00:54 -04007809 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007810 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007811 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007812 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007813 u64 page_start = page_offset(page);
7814 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007815 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04007816
Chris Mason8b62b722009-09-02 16:53:46 -04007817 /*
7818 * we have the page locked, so new writeback can't start,
7819 * and the dirty bit won't be cleared while we are here.
7820 *
7821 * Wait for IO on this page so that we can safely clear
7822 * the PagePrivate2 bit and do ordered accounting
7823 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007824 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007825
Josef Bacik5fd02042012-05-02 14:00:54 -04007826 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007827 if (offset) {
7828 btrfs_releasepage(page, GFP_NOFS);
7829 return;
7830 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007831
7832 if (!inode_evicting)
7833 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
7834 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007835 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007836 /*
7837 * IO on this page will never be started, so we need
7838 * to account for any ordered extents now
7839 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007840 if (!inode_evicting)
7841 clear_extent_bit(tree, page_start, page_end,
7842 EXTENT_DIRTY | EXTENT_DELALLOC |
7843 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7844 EXTENT_DEFRAG, 1, 0, &cached_state,
7845 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007846 /*
7847 * whoever cleared the private bit is responsible
7848 * for the finish_ordered_io
7849 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04007850 if (TestClearPagePrivate2(page)) {
7851 struct btrfs_ordered_inode_tree *tree;
7852 u64 new_len;
7853
7854 tree = &BTRFS_I(inode)->ordered_tree;
7855
7856 spin_lock_irq(&tree->lock);
7857 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
7858 new_len = page_start - ordered->file_offset;
7859 if (new_len < ordered->truncated_len)
7860 ordered->truncated_len = new_len;
7861 spin_unlock_irq(&tree->lock);
7862
7863 if (btrfs_dec_test_ordered_pending(inode, &ordered,
7864 page_start,
7865 PAGE_CACHE_SIZE, 1))
7866 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007867 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007868 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007869 if (!inode_evicting) {
7870 cached_state = NULL;
7871 lock_extent_bits(tree, page_start, page_end, 0,
7872 &cached_state);
7873 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007874 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007875
7876 if (!inode_evicting) {
7877 clear_extent_bit(tree, page_start, page_end,
7878 EXTENT_LOCKED | EXTENT_DIRTY |
7879 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
7880 EXTENT_DEFRAG, 1, 1,
7881 &cached_state, GFP_NOFS);
7882
7883 __btrfs_releasepage(page, GFP_NOFS);
7884 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007885
Chris Mason4a096752008-07-21 10:29:44 -04007886 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007887 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007888 ClearPagePrivate(page);
7889 set_page_private(page, 0);
7890 page_cache_release(page);
7891 }
Chris Mason39279cc2007-06-12 06:35:45 -04007892}
7893
Chris Mason9ebefb182007-06-15 13:50:00 -04007894/*
7895 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7896 * called from a page fault handler when a page is first dirtied. Hence we must
7897 * be careful to check for EOF conditions here. We set the page up correctly
7898 * for a written page which means we get ENOSPC checking when writing into
7899 * holes and correct delalloc and unwritten extent mapping on filesystems that
7900 * support these features.
7901 *
7902 * We are not allowed to take the i_mutex here so we have to play games to
7903 * protect against truncate races as the page could now be beyond EOF. Because
7904 * vmtruncate() writes the inode size before removing pages, once we have the
7905 * page lock we can determine safely if the page is beyond EOF. If it is not
7906 * beyond EOF, then the page is guaranteed safe against truncation until we
7907 * unlock the page.
7908 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007909int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007910{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007911 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007912 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007913 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007914 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7915 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007916 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007917 char *kaddr;
7918 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007919 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007920 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007921 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007922 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007923 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007924
Jan Karab2b5ef52012-06-12 16:20:45 +02007925 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007926 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007927 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007928 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007929 reserved = 1;
7930 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007931 if (ret) {
7932 if (ret == -ENOMEM)
7933 ret = VM_FAULT_OOM;
7934 else /* -ENOSPC, -EIO, etc */
7935 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007936 if (reserved)
7937 goto out;
7938 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007939 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007940
Nick Piggin56a76f82009-03-31 15:23:23 -07007941 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007942again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007943 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007944 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007945 page_start = page_offset(page);
7946 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007947
Chris Mason9ebefb182007-06-15 13:50:00 -04007948 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007949 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007950 /* page got truncated out from underneath us */
7951 goto out_unlock;
7952 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007953 wait_on_page_writeback(page);
7954
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007955 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007956 set_page_extent_mapped(page);
7957
Chris Masoneb84ae02008-07-17 13:53:27 -04007958 /*
7959 * we can't set the delalloc bits if there are pending ordered
7960 * extents. Drop our locks and wait for them to finish
7961 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007962 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7963 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007964 unlock_extent_cached(io_tree, page_start, page_end,
7965 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007966 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007967 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007968 btrfs_put_ordered_extent(ordered);
7969 goto again;
7970 }
7971
Josef Bacikfbf19082009-10-01 17:10:23 -04007972 /*
7973 * XXX - page_mkwrite gets called every time the page is dirtied, even
7974 * if it was already dirty, so for space accounting reasons we need to
7975 * clear any delalloc bits for the range we are fixing to save. There
7976 * is probably a better way to do this, but for now keep consistent with
7977 * prepare_pages in the normal write path.
7978 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007979 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007980 EXTENT_DIRTY | EXTENT_DELALLOC |
7981 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007982 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007983
Josef Bacik2ac55d42010-02-03 19:33:23 +00007984 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7985 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007986 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007987 unlock_extent_cached(io_tree, page_start, page_end,
7988 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007989 ret = VM_FAULT_SIGBUS;
7990 goto out_unlock;
7991 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007992 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007993
7994 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007995 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007996 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007997 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007998 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007999
Chris Masone6dcd2d2008-07-17 12:53:50 -04008000 if (zero_start != PAGE_CACHE_SIZE) {
8001 kaddr = kmap(page);
8002 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8003 flush_dcache_page(page);
8004 kunmap(page);
8005 }
Chris Mason247e7432008-07-17 12:53:51 -04008006 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008007 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008008 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008009
Chris Mason257c62e2009-10-13 13:21:08 -04008010 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8011 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008012 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008013
Josef Bacik2ac55d42010-02-03 19:33:23 +00008014 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008015
8016out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008017 if (!ret) {
8018 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008019 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008020 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008021 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008022out:
Josef Bacikec39e182012-01-12 19:10:12 -05008023 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008024out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008025 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008026 return ret;
8027}
8028
Josef Bacika41ad392011-01-31 15:30:16 -05008029static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008030{
8031 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008032 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008033 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008034 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008035 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008036 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008037 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008038
Josef Bacik0ef8b722013-10-25 16:13:35 -04008039 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8040 (u64)-1);
8041 if (ret)
8042 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008043
Josef Bacikfcb80c22011-05-03 10:40:22 -04008044 /*
8045 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8046 * 3 things going on here
8047 *
8048 * 1) We need to reserve space for our orphan item and the space to
8049 * delete our orphan item. Lord knows we don't want to have a dangling
8050 * orphan item because we didn't reserve space to remove it.
8051 *
8052 * 2) We need to reserve space to update our inode.
8053 *
8054 * 3) We need to have something to cache all the space that is going to
8055 * be free'd up by the truncate operation, but also have some slack
8056 * space reserved in case it uses space during the truncate (thank you
8057 * very much snapshotting).
8058 *
8059 * And we need these to all be seperate. The fact is we can use alot of
8060 * space doing the truncate, and we have no earthly idea how much space
8061 * we will use, so we need the truncate reservation to be seperate so it
8062 * doesn't end up using space reserved for updating the inode or
8063 * removing the orphan item. We also need to be able to stop the
8064 * transaction and start a new one, which means we need to be able to
8065 * update the inode several times, and we have no idea of knowing how
8066 * many times that will be, so we can't just reserve 1 item for the
8067 * entirety of the opration, so that has to be done seperately as well.
8068 * Then there is the orphan item, which does indeed need to be held on
8069 * to for the whole operation, and we need nobody to touch this reserved
8070 * space except the orphan code.
8071 *
8072 * So that leaves us with
8073 *
8074 * 1) root->orphan_block_rsv - for the orphan deletion.
8075 * 2) rsv - for the truncate reservation, which we will steal from the
8076 * transaction reservation.
8077 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8078 * updating the inode.
8079 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008080 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008081 if (!rsv)
8082 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008083 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008084 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008085
Josef Bacik907cbce2011-08-08 13:46:15 -04008086 /*
Josef Bacik07127182011-08-19 10:29:59 -04008087 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008088 * 1 for updating the inode.
8089 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008090 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008091 if (IS_ERR(trans)) {
8092 err = PTR_ERR(trans);
8093 goto out;
8094 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008095
Josef Bacik907cbce2011-08-08 13:46:15 -04008096 /* Migrate the slack space for the truncate to our reserve */
8097 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8098 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008099 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008100
Chris Mason5a3f23d2009-03-31 13:27:11 -04008101 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008102 * So if we truncate and then write and fsync we normally would just
8103 * write the extents that changed, which is a problem if we need to
8104 * first truncate that entire inode. So set this flag so we write out
8105 * all of the extents in the inode to the sync log so we're completely
8106 * safe.
8107 */
8108 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008109 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008110
Yan, Zheng80825102009-11-12 09:35:36 +00008111 while (1) {
8112 ret = btrfs_truncate_inode_items(trans, root, inode,
8113 inode->i_size,
8114 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008115 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05008116 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008117 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008118 }
Chris Mason39279cc2007-06-12 06:35:45 -04008119
Josef Bacikfcb80c22011-05-03 10:40:22 -04008120 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008121 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008122 if (ret) {
8123 err = ret;
8124 break;
8125 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008126
Yan, Zheng80825102009-11-12 09:35:36 +00008127 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008128 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008129
8130 trans = btrfs_start_transaction(root, 2);
8131 if (IS_ERR(trans)) {
8132 ret = err = PTR_ERR(trans);
8133 trans = NULL;
8134 break;
8135 }
8136
8137 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8138 rsv, min_size);
8139 BUG_ON(ret); /* shouldn't happen */
8140 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008141 }
8142
8143 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008144 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008145 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008146 if (ret)
8147 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008148 }
8149
Chris Mason917c16b2011-11-08 14:49:59 -05008150 if (trans) {
8151 trans->block_rsv = &root->fs_info->trans_block_rsv;
8152 ret = btrfs_update_inode(trans, root, inode);
8153 if (ret && !err)
8154 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008155
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008156 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008157 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008158 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008159
8160out:
8161 btrfs_free_block_rsv(root, rsv);
8162
Josef Bacik3893e332011-01-31 16:03:11 -05008163 if (ret && !err)
8164 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008165
Josef Bacik3893e332011-01-31 16:03:11 -05008166 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008167}
8168
Sven Wegener3b963622008-06-09 21:57:42 -04008169/*
Chris Masond352ac62008-09-29 15:18:18 -04008170 * create a new subvolume directory/inode (helper for the ioctl).
8171 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008172int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008173 struct btrfs_root *new_root,
8174 struct btrfs_root *parent_root,
8175 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008176{
Chris Mason39279cc2007-06-12 06:35:45 -04008177 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008178 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008179 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008180
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008181 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8182 new_dirid, new_dirid,
8183 S_IFDIR | (~current_umask() & S_IRWXUGO),
8184 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008185 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008186 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008187 inode->i_op = &btrfs_dir_inode_operations;
8188 inode->i_fop = &btrfs_dir_file_operations;
8189
Miklos Szeredibfe86842011-10-28 14:13:29 +02008190 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008191 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008192 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008193
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008194 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8195 if (err)
8196 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008197 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008198 new_root->root_key.objectid, err);
8199
Yan, Zheng76dda932009-09-21 16:00:26 -04008200 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008201
Yan, Zheng76dda932009-09-21 16:00:26 -04008202 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008203 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008204}
8205
Chris Mason39279cc2007-06-12 06:35:45 -04008206struct inode *btrfs_alloc_inode(struct super_block *sb)
8207{
8208 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008209 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008210
8211 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8212 if (!ei)
8213 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008214
8215 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008216 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008217 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008218 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008219 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008220 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008221 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008222 ei->disk_i_size = 0;
8223 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008224 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008225 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008226 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008227 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008228 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008229
Josef Bacik9e0baf62011-07-15 15:16:44 +00008230 spin_lock_init(&ei->lock);
8231 ei->outstanding_extents = 0;
8232 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008233
Josef Bacik72ac3c02012-05-23 14:13:11 -04008234 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008235 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008236
Miao Xie16cdcec2011-04-22 18:12:22 +08008237 ei->delayed_node = NULL;
8238
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008239 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008240 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008241 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8242 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008243 ei->io_tree.track_uptodate = 1;
8244 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008245 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008246 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008247 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008248 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008249 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008250 RB_CLEAR_NODE(&ei->rb_node);
8251
8252 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008253}
8254
Josef Bacikaaedb552013-10-11 14:44:09 -04008255#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8256void btrfs_test_destroy_inode(struct inode *inode)
8257{
8258 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8259 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8260}
8261#endif
8262
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008263static void btrfs_i_callback(struct rcu_head *head)
8264{
8265 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008266 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8267}
8268
Chris Mason39279cc2007-06-12 06:35:45 -04008269void btrfs_destroy_inode(struct inode *inode)
8270{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008271 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008272 struct btrfs_root *root = BTRFS_I(inode)->root;
8273
Al Virob3d9b7a2012-06-09 13:51:19 -04008274 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008275 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008276 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8277 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008278 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8279 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008280 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008281
Chris Mason5a3f23d2009-03-31 13:27:11 -04008282 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008283 * This can happen where we create an inode, but somebody else also
8284 * created the same inode and we need to destroy the one we already
8285 * created.
8286 */
8287 if (!root)
8288 goto free;
8289
Josef Bacik8a35d952012-05-23 14:26:42 -04008290 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8291 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008292 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008293 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008294 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008295 }
Josef Bacik7b128762008-07-24 12:17:14 -04008296
Chris Masond3977122009-01-05 21:25:51 -05008297 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008298 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8299 if (!ordered)
8300 break;
8301 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008302 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008303 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008304 btrfs_remove_ordered_extent(inode, ordered);
8305 btrfs_put_ordered_extent(ordered);
8306 btrfs_put_ordered_extent(ordered);
8307 }
8308 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008309 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008310 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008311free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008312 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008313}
8314
Al Viro45321ac2010-06-07 13:43:19 -04008315int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008316{
8317 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008318
Naohiro Aota6379ef92013-06-06 09:56:34 +00008319 if (root == NULL)
8320 return 1;
8321
Liu Bofa6ac872013-02-20 14:10:23 +00008322 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008323 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008324 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008325 else
Al Viro45321ac2010-06-07 13:43:19 -04008326 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008327}
8328
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008329static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008330{
8331 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8332
8333 inode_init_once(&ei->vfs_inode);
8334}
8335
8336void btrfs_destroy_cachep(void)
8337{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008338 /*
8339 * Make sure all delayed rcu free inodes are flushed before we
8340 * destroy cache.
8341 */
8342 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008343 if (btrfs_inode_cachep)
8344 kmem_cache_destroy(btrfs_inode_cachep);
8345 if (btrfs_trans_handle_cachep)
8346 kmem_cache_destroy(btrfs_trans_handle_cachep);
8347 if (btrfs_transaction_cachep)
8348 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008349 if (btrfs_path_cachep)
8350 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008351 if (btrfs_free_space_cachep)
8352 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008353 if (btrfs_delalloc_work_cachep)
8354 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008355}
8356
8357int btrfs_init_cachep(void)
8358{
David Sterba837e1972012-09-07 03:00:48 -06008359 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008360 sizeof(struct btrfs_inode), 0,
8361 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008362 if (!btrfs_inode_cachep)
8363 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008364
David Sterba837e1972012-09-07 03:00:48 -06008365 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008366 sizeof(struct btrfs_trans_handle), 0,
8367 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008368 if (!btrfs_trans_handle_cachep)
8369 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008370
David Sterba837e1972012-09-07 03:00:48 -06008371 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008372 sizeof(struct btrfs_transaction), 0,
8373 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008374 if (!btrfs_transaction_cachep)
8375 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008376
David Sterba837e1972012-09-07 03:00:48 -06008377 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008378 sizeof(struct btrfs_path), 0,
8379 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008380 if (!btrfs_path_cachep)
8381 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008382
David Sterba837e1972012-09-07 03:00:48 -06008383 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008384 sizeof(struct btrfs_free_space), 0,
8385 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8386 if (!btrfs_free_space_cachep)
8387 goto fail;
8388
Miao Xie8ccf6f192012-10-25 09:28:04 +00008389 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8390 sizeof(struct btrfs_delalloc_work), 0,
8391 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8392 NULL);
8393 if (!btrfs_delalloc_work_cachep)
8394 goto fail;
8395
Chris Mason39279cc2007-06-12 06:35:45 -04008396 return 0;
8397fail:
8398 btrfs_destroy_cachep();
8399 return -ENOMEM;
8400}
8401
8402static int btrfs_getattr(struct vfsmount *mnt,
8403 struct dentry *dentry, struct kstat *stat)
8404{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008405 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008406 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008407 u32 blocksize = inode->i_sb->s_blocksize;
8408
Chris Mason39279cc2007-06-12 06:35:45 -04008409 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008410 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008411 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008412
8413 spin_lock(&BTRFS_I(inode)->lock);
8414 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8415 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008416 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008417 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008418 return 0;
8419}
8420
Chris Masond3977122009-01-05 21:25:51 -05008421static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8422 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008423{
8424 struct btrfs_trans_handle *trans;
8425 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008426 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008427 struct inode *new_inode = new_dentry->d_inode;
8428 struct inode *old_inode = old_dentry->d_inode;
8429 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008430 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008431 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008432 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008433 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008434
Li Zefan33345d012011-04-20 10:31:50 +08008435 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008436 return -EPERM;
8437
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008438 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008439 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008440 return -EXDEV;
8441
Li Zefan33345d012011-04-20 10:31:50 +08008442 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8443 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008444 return -ENOTEMPTY;
8445
Chris Mason39279cc2007-06-12 06:35:45 -04008446 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008447 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008448 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008449
8450
8451 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008452 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008453 new_dentry->d_name.name,
8454 new_dentry->d_name.len);
8455
8456 if (ret) {
8457 if (ret == -EEXIST) {
8458 /* we shouldn't get
8459 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308460 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008461 return ret;
8462 }
8463 } else {
8464 /* maybe -EOVERFLOW */
8465 return ret;
8466 }
8467 }
8468 ret = 0;
8469
Chris Mason5a3f23d2009-03-31 13:27:11 -04008470 /*
Chris Mason8d875f92014-08-12 10:47:42 -07008471 * we're using rename to replace one file with another. Start IO on it
8472 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04008473 */
Chris Mason8d875f92014-08-12 10:47:42 -07008474 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04008475 filemap_flush(old_inode->i_mapping);
8476
Yan, Zheng76dda932009-09-21 16:00:26 -04008477 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008478 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008479 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008480 /*
8481 * We want to reserve the absolute worst case amount of items. So if
8482 * both inodes are subvols and we need to unlink them then that would
8483 * require 4 item modifications, but if they are both normal inodes it
8484 * would require 5 item modifications, so we'll assume their normal
8485 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8486 * should cover the worst case number of items we'll modify.
8487 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008488 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008489 if (IS_ERR(trans)) {
8490 ret = PTR_ERR(trans);
8491 goto out_notrans;
8492 }
Chris Mason5f39d392007-10-15 16:14:19 -04008493
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008494 if (dest != root)
8495 btrfs_record_root_in_trans(trans, dest);
8496
Yan, Zhenga5719522009-09-24 09:17:31 -04008497 ret = btrfs_set_inode_index(new_dir, &index);
8498 if (ret)
8499 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008500
Miao Xie67de1172013-12-26 13:07:06 +08008501 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08008502 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008503 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08008504 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008505 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008506 ret = btrfs_insert_inode_ref(trans, dest,
8507 new_dentry->d_name.name,
8508 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008509 old_ino,
8510 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008511 if (ret)
8512 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008513 /*
8514 * this is an ugly little race, but the rename is required
8515 * to make sure that if we crash, the inode is either at the
8516 * old name or the new one. pinning the log transaction lets
8517 * us make sure we don't allow a log commit to come in after
8518 * we unlink the name but before we add the new name back in.
8519 */
8520 btrfs_pin_log_trans(root);
8521 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04008522
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008523 inode_inc_iversion(old_dir);
8524 inode_inc_iversion(new_dir);
8525 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008526 old_dir->i_ctime = old_dir->i_mtime = ctime;
8527 new_dir->i_ctime = new_dir->i_mtime = ctime;
8528 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008529
Chris Mason12fcfd22009-03-24 10:24:20 -04008530 if (old_dentry->d_parent != new_dentry->d_parent)
8531 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8532
Li Zefan33345d012011-04-20 10:31:50 +08008533 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008534 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8535 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8536 old_dentry->d_name.name,
8537 old_dentry->d_name.len);
8538 } else {
Al Viro92986792011-03-04 17:14:37 +00008539 ret = __btrfs_unlink_inode(trans, root, old_dir,
8540 old_dentry->d_inode,
8541 old_dentry->d_name.name,
8542 old_dentry->d_name.len);
8543 if (!ret)
8544 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008545 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008546 if (ret) {
8547 btrfs_abort_transaction(trans, root, ret);
8548 goto out_fail;
8549 }
Chris Mason39279cc2007-06-12 06:35:45 -04008550
8551 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008552 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008553 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008554 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008555 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8556 root_objectid = BTRFS_I(new_inode)->location.objectid;
8557 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8558 root_objectid,
8559 new_dentry->d_name.name,
8560 new_dentry->d_name.len);
8561 BUG_ON(new_inode->i_nlink == 0);
8562 } else {
8563 ret = btrfs_unlink_inode(trans, dest, new_dir,
8564 new_dentry->d_inode,
8565 new_dentry->d_name.name,
8566 new_dentry->d_name.len);
8567 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008568 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008569 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008570 if (ret) {
8571 btrfs_abort_transaction(trans, root, ret);
8572 goto out_fail;
8573 }
Chris Mason39279cc2007-06-12 06:35:45 -04008574 }
Josef Bacikaec74772008-07-24 12:12:38 -04008575
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008576 ret = btrfs_add_link(trans, new_dir, old_inode,
8577 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008578 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008579 if (ret) {
8580 btrfs_abort_transaction(trans, root, ret);
8581 goto out_fail;
8582 }
Chris Mason39279cc2007-06-12 06:35:45 -04008583
Miao Xie67de1172013-12-26 13:07:06 +08008584 if (old_inode->i_nlink == 1)
8585 BTRFS_I(old_inode)->dir_index = index;
8586
Li Zefan33345d012011-04-20 10:31:50 +08008587 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008588 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008589 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008590 btrfs_end_log_trans(root);
8591 }
Chris Mason39279cc2007-06-12 06:35:45 -04008592out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008593 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008594out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008595 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008596 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008597
Chris Mason39279cc2007-06-12 06:35:45 -04008598 return ret;
8599}
8600
Miklos Szeredi80ace852014-07-23 15:15:32 +02008601static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
8602 struct inode *new_dir, struct dentry *new_dentry,
8603 unsigned int flags)
8604{
8605 if (flags & ~RENAME_NOREPLACE)
8606 return -EINVAL;
8607
8608 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
8609}
8610
Miao Xie8ccf6f192012-10-25 09:28:04 +00008611static void btrfs_run_delalloc_work(struct btrfs_work *work)
8612{
8613 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04008614 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008615
8616 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8617 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04008618 inode = delalloc_work->inode;
8619 if (delalloc_work->wait) {
8620 btrfs_wait_ordered_range(inode, 0, (u64)-1);
8621 } else {
8622 filemap_flush(inode->i_mapping);
8623 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8624 &BTRFS_I(inode)->runtime_flags))
8625 filemap_flush(inode->i_mapping);
8626 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008627
8628 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04008629 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008630 else
Josef Bacik9f23e282013-10-28 15:03:41 -04008631 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008632 complete(&delalloc_work->completion);
8633}
8634
8635struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8636 int wait, int delay_iput)
8637{
8638 struct btrfs_delalloc_work *work;
8639
8640 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8641 if (!work)
8642 return NULL;
8643
8644 init_completion(&work->completion);
8645 INIT_LIST_HEAD(&work->list);
8646 work->inode = inode;
8647 work->wait = wait;
8648 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08008649 WARN_ON_ONCE(!inode);
8650 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
8651 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008652
8653 return work;
8654}
8655
8656void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8657{
8658 wait_for_completion(&work->completion);
8659 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8660}
8661
Chris Masond352ac62008-09-29 15:18:18 -04008662/*
8663 * some fairly slow code that needs optimization. This walks the list
8664 * of all the inodes with pending delalloc and forces them to disk.
8665 */
Miao Xie6c255e62014-03-06 13:55:01 +08008666static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
8667 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04008668{
Chris Masonea8c2812008-08-04 23:17:27 -04008669 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008670 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008671 struct btrfs_delalloc_work *work, *next;
8672 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008673 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008674 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008675
Miao Xie8ccf6f192012-10-25 09:28:04 +00008676 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008677 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008678
Miao Xie573bfb72014-03-06 13:55:03 +08008679 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008680 spin_lock(&root->delalloc_lock);
8681 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008682 while (!list_empty(&splice)) {
8683 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008684 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008685
Miao Xieeb73c1b2013-05-15 07:48:22 +00008686 list_move_tail(&binode->delalloc_inodes,
8687 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008688 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008689 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008690 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008691 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008692 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008693 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008694
8695 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8696 if (unlikely(!work)) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04008697 if (delay_iput)
8698 btrfs_add_delayed_iput(inode);
8699 else
8700 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008701 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008702 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008703 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008704 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08008705 btrfs_queue_work(root->fs_info->flush_workers,
8706 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08008707 ret++;
8708 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008709 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008710 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008711 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008712 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008713 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee1d2008-09-29 11:19:10 -04008714
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008715out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008716 list_for_each_entry_safe(work, next, &works, list) {
8717 list_del_init(&work->list);
8718 btrfs_wait_and_free_delalloc_work(work);
8719 }
8720
Miao Xieeb73c1b2013-05-15 07:48:22 +00008721 if (!list_empty_careful(&splice)) {
8722 spin_lock(&root->delalloc_lock);
8723 list_splice_tail(&splice, &root->delalloc_inodes);
8724 spin_unlock(&root->delalloc_lock);
8725 }
Miao Xie573bfb72014-03-06 13:55:03 +08008726 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008727 return ret;
8728}
8729
8730int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8731{
8732 int ret;
8733
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008734 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008735 return -EROFS;
8736
Miao Xie6c255e62014-03-06 13:55:01 +08008737 ret = __start_delalloc_inodes(root, delay_iput, -1);
8738 if (ret > 0)
8739 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00008740 /*
8741 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee1d2008-09-29 11:19:10 -04008742 * we have to make sure the IO is actually started and that
8743 * ordered extents get created before we return
8744 */
8745 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008746 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008747 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee1d2008-09-29 11:19:10 -04008748 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008749 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8750 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee1d2008-09-29 11:19:10 -04008751 }
8752 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008753 return ret;
8754}
8755
Miao Xie6c255e62014-03-06 13:55:01 +08008756int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
8757 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008758{
8759 struct btrfs_root *root;
8760 struct list_head splice;
8761 int ret;
8762
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008763 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008764 return -EROFS;
8765
8766 INIT_LIST_HEAD(&splice);
8767
Miao Xie573bfb72014-03-06 13:55:03 +08008768 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008769 spin_lock(&fs_info->delalloc_root_lock);
8770 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08008771 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008772 root = list_first_entry(&splice, struct btrfs_root,
8773 delalloc_root);
8774 root = btrfs_grab_fs_root(root);
8775 BUG_ON(!root);
8776 list_move_tail(&root->delalloc_root,
8777 &fs_info->delalloc_roots);
8778 spin_unlock(&fs_info->delalloc_root_lock);
8779
Miao Xie6c255e62014-03-06 13:55:01 +08008780 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008781 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08008782 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008783 goto out;
8784
Miao Xie6c255e62014-03-06 13:55:01 +08008785 if (nr != -1) {
8786 nr -= ret;
8787 WARN_ON(nr < 0);
8788 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008789 spin_lock(&fs_info->delalloc_root_lock);
8790 }
8791 spin_unlock(&fs_info->delalloc_root_lock);
8792
Miao Xie6c255e62014-03-06 13:55:01 +08008793 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00008794 atomic_inc(&fs_info->async_submit_draining);
8795 while (atomic_read(&fs_info->nr_async_submits) ||
8796 atomic_read(&fs_info->async_delalloc_pages)) {
8797 wait_event(fs_info->async_submit_wait,
8798 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8799 atomic_read(&fs_info->async_delalloc_pages) == 0));
8800 }
8801 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008802out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008803 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008804 spin_lock(&fs_info->delalloc_root_lock);
8805 list_splice_tail(&splice, &fs_info->delalloc_roots);
8806 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008807 }
Miao Xie573bfb72014-03-06 13:55:03 +08008808 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008809 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008810}
8811
Chris Mason39279cc2007-06-12 06:35:45 -04008812static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8813 const char *symname)
8814{
8815 struct btrfs_trans_handle *trans;
8816 struct btrfs_root *root = BTRFS_I(dir)->root;
8817 struct btrfs_path *path;
8818 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008819 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008820 int err;
8821 int drop_inode = 0;
8822 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308823 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008824 int name_len;
8825 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008826 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008827 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008828 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008829
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008830 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04008831 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8832 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008833
Josef Bacik9ed74f22009-09-11 16:12:44 -04008834 /*
8835 * 2 items for inode item and ref
8836 * 2 items for dir items
8837 * 1 item for xattr if selinux is on
8838 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008839 trans = btrfs_start_transaction(root, 5);
8840 if (IS_ERR(trans))
8841 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008842
Li Zefan581bb052011-04-20 10:06:11 +08008843 err = btrfs_find_free_ino(root, &objectid);
8844 if (err)
8845 goto out_unlock;
8846
Josef Bacikaec74772008-07-24 12:12:38 -04008847 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008848 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008849 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008850 if (IS_ERR(inode)) {
8851 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008852 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008853 }
Chris Mason39279cc2007-06-12 06:35:45 -04008854
Casey Schauflerad19db72011-12-15 10:09:07 -05008855 /*
8856 * If the active LSM wants to access the inode during
8857 * d_instantiate it needs these. Smack checks to see
8858 * if the filesystem supports xattrs by looking at the
8859 * ops vector.
8860 */
8861 inode->i_fop = &btrfs_file_operations;
8862 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07008863 inode->i_mapping->a_ops = &btrfs_aops;
8864 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
8865 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
8866
8867 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
8868 if (err)
8869 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05008870
Josef Bacika1b075d2010-11-19 20:36:11 +00008871 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008872 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07008873 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008874
8875 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008876 if (!path) {
8877 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07008878 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07008879 }
Li Zefan33345d012011-04-20 10:31:50 +08008880 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008881 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02008882 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04008883 datasize = btrfs_file_extent_calc_inline_size(name_len);
8884 err = btrfs_insert_empty_item(trans, root, path, &key,
8885 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008886 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00008887 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07008888 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04008889 }
Chris Mason5f39d392007-10-15 16:14:19 -04008890 leaf = path->nodes[0];
8891 ei = btrfs_item_ptr(leaf, path->slots[0],
8892 struct btrfs_file_extent_item);
8893 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8894 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008895 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008896 btrfs_set_file_extent_encryption(leaf, ei, 0);
8897 btrfs_set_file_extent_compression(leaf, ei, 0);
8898 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8899 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8900
Chris Mason39279cc2007-06-12 06:35:45 -04008901 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008902 write_extent_buffer(leaf, symname, ptr, name_len);
8903 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008904 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008905
Chris Mason39279cc2007-06-12 06:35:45 -04008906 inode->i_op = &btrfs_symlink_inode_operations;
8907 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008908 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008909 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008910 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04008911 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07008912 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04008913 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07008914 goto out_unlock_inode;
8915 }
8916
8917 unlock_new_inode(inode);
8918 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008919
8920out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008921 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008922 if (drop_inode) {
8923 inode_dec_link_count(inode);
8924 iput(inode);
8925 }
Liu Bob53d3f52012-11-14 14:34:34 +00008926 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008927 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07008928
8929out_unlock_inode:
8930 drop_inode = 1;
8931 unlock_new_inode(inode);
8932 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04008933}
Chris Mason16432982008-04-10 10:23:21 -04008934
Josef Bacik0af3d002010-06-21 14:48:16 -04008935static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8936 u64 start, u64 num_bytes, u64 min_size,
8937 loff_t actual_len, u64 *alloc_hint,
8938 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008939{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008940 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8941 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008942 struct btrfs_root *root = BTRFS_I(inode)->root;
8943 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008944 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008945 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008946 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008947 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008948 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008949
Josef Bacik0af3d002010-06-21 14:48:16 -04008950 if (trans)
8951 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008952 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008953 if (own_trans) {
8954 trans = btrfs_start_transaction(root, 3);
8955 if (IS_ERR(trans)) {
8956 ret = PTR_ERR(trans);
8957 break;
8958 }
Yan Zhengd899e052008-10-30 14:25:28 -04008959 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008960
Chris Mason154ea282013-03-05 11:11:26 -05008961 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8962 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04008963 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08008964 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008965 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008966 if (own_trans)
8967 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008968 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008969 }
8970
Yan Zhengd899e052008-10-30 14:25:28 -04008971 ret = insert_reserved_file_extent(trans, inode,
8972 cur_offset, ins.objectid,
8973 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008974 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008975 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008976 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04008977 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08008978 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008979 btrfs_abort_transaction(trans, root, ret);
8980 if (own_trans)
8981 btrfs_end_transaction(trans, root);
8982 break;
8983 }
Chris Masona1ed8352009-09-11 12:27:37 -04008984 btrfs_drop_extent_cache(inode, cur_offset,
8985 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008986
Josef Bacik5dc562c2012-08-17 13:14:17 -04008987 em = alloc_extent_map();
8988 if (!em) {
8989 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8990 &BTRFS_I(inode)->runtime_flags);
8991 goto next;
8992 }
8993
8994 em->start = cur_offset;
8995 em->orig_start = cur_offset;
8996 em->len = ins.offset;
8997 em->block_start = ins.objectid;
8998 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008999 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009000 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009001 em->bdev = root->fs_info->fs_devices->latest_bdev;
9002 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9003 em->generation = trans->transid;
9004
9005 while (1) {
9006 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009007 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009008 write_unlock(&em_tree->lock);
9009 if (ret != -EEXIST)
9010 break;
9011 btrfs_drop_extent_cache(inode, cur_offset,
9012 cur_offset + ins.offset - 1,
9013 0);
9014 }
9015 free_extent_map(em);
9016next:
Yan Zhengd899e052008-10-30 14:25:28 -04009017 num_bytes -= ins.offset;
9018 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009019 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009020
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009021 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009022 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009023 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009024 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009025 (actual_len > inode->i_size) &&
9026 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009027 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009028 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009029 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009030 i_size = cur_offset;
9031 i_size_write(inode, i_size);
9032 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009033 }
9034
Yan Zhengd899e052008-10-30 14:25:28 -04009035 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009036
9037 if (ret) {
9038 btrfs_abort_transaction(trans, root, ret);
9039 if (own_trans)
9040 btrfs_end_transaction(trans, root);
9041 break;
9042 }
Yan Zhengd899e052008-10-30 14:25:28 -04009043
Josef Bacik0af3d002010-06-21 14:48:16 -04009044 if (own_trans)
9045 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009046 }
Yan Zhengd899e052008-10-30 14:25:28 -04009047 return ret;
9048}
9049
Josef Bacik0af3d002010-06-21 14:48:16 -04009050int btrfs_prealloc_file_range(struct inode *inode, int mode,
9051 u64 start, u64 num_bytes, u64 min_size,
9052 loff_t actual_len, u64 *alloc_hint)
9053{
9054 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9055 min_size, actual_len, alloc_hint,
9056 NULL);
9057}
9058
9059int btrfs_prealloc_file_range_trans(struct inode *inode,
9060 struct btrfs_trans_handle *trans, int mode,
9061 u64 start, u64 num_bytes, u64 min_size,
9062 loff_t actual_len, u64 *alloc_hint)
9063{
9064 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9065 min_size, actual_len, alloc_hint, trans);
9066}
9067
Chris Masone6dcd2d2008-07-17 12:53:50 -04009068static int btrfs_set_page_dirty(struct page *page)
9069{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009070 return __set_page_dirty_nobuffers(page);
9071}
9072
Al Viro10556cb2011-06-20 19:28:19 -04009073static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009074{
Li Zefanb83cc962010-12-20 16:04:08 +08009075 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009076 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009077
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009078 if (mask & MAY_WRITE &&
9079 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9080 if (btrfs_root_readonly(root))
9081 return -EROFS;
9082 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9083 return -EACCES;
9084 }
Al Viro2830ba72011-06-20 19:16:29 -04009085 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009086}
Chris Mason39279cc2007-06-12 06:35:45 -04009087
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009088static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9089{
9090 struct btrfs_trans_handle *trans;
9091 struct btrfs_root *root = BTRFS_I(dir)->root;
9092 struct inode *inode = NULL;
9093 u64 objectid;
9094 u64 index;
9095 int ret = 0;
9096
9097 /*
9098 * 5 units required for adding orphan entry
9099 */
9100 trans = btrfs_start_transaction(root, 5);
9101 if (IS_ERR(trans))
9102 return PTR_ERR(trans);
9103
9104 ret = btrfs_find_free_ino(root, &objectid);
9105 if (ret)
9106 goto out;
9107
9108 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9109 btrfs_ino(dir), objectid, mode, &index);
9110 if (IS_ERR(inode)) {
9111 ret = PTR_ERR(inode);
9112 inode = NULL;
9113 goto out;
9114 }
9115
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009116 inode->i_fop = &btrfs_file_operations;
9117 inode->i_op = &btrfs_file_inode_operations;
9118
9119 inode->i_mapping->a_ops = &btrfs_aops;
9120 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9121 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9122
Chris Masonb0d5d102014-09-08 13:08:51 -07009123 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9124 if (ret)
9125 goto out_inode;
9126
9127 ret = btrfs_update_inode(trans, root, inode);
9128 if (ret)
9129 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009130 ret = btrfs_orphan_add(trans, inode);
9131 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009132 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009133
Filipe Manana5762b5c2014-08-01 00:10:32 +01009134 /*
9135 * We set number of links to 0 in btrfs_new_inode(), and here we set
9136 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9137 * through:
9138 *
9139 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9140 */
9141 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009142 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009143 d_tmpfile(dentry, inode);
9144 mark_inode_dirty(inode);
9145
9146out:
9147 btrfs_end_transaction(trans, root);
9148 if (ret)
9149 iput(inode);
9150 btrfs_balance_delayed_items(root);
9151 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009152 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009153
9154out_inode:
9155 unlock_new_inode(inode);
9156 goto out;
9157
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009158}
9159
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009160static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009161 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009162 .lookup = btrfs_lookup,
9163 .create = btrfs_create,
9164 .unlink = btrfs_unlink,
9165 .link = btrfs_link,
9166 .mkdir = btrfs_mkdir,
9167 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +02009168 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -04009169 .symlink = btrfs_symlink,
9170 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009171 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009172 .setxattr = btrfs_setxattr,
9173 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009174 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009175 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009176 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009177 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009178 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009179 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009180 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009181};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009182static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009183 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009184 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009185 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009186 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009187 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009188};
Yan, Zheng76dda932009-09-21 16:00:26 -04009189
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009190static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009191 .llseek = generic_file_llseek,
9192 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009193 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009194 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009195#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009196 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009197#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009198 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009199 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009200};
9201
Chris Masond1310b22008-01-24 16:13:08 -05009202static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009203 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009204 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009205 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009206 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009207 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009208 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009209 .set_bit_hook = btrfs_set_bit_hook,
9210 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009211 .merge_extent_hook = btrfs_merge_extent_hook,
9212 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009213};
9214
Chris Mason35054392009-01-21 13:11:13 -05009215/*
9216 * btrfs doesn't support the bmap operation because swapfiles
9217 * use bmap to make a mapping of extents in the file. They assume
9218 * these extents won't change over the life of the file and they
9219 * use the bmap result to do IO directly to the drive.
9220 *
9221 * the btrfs bmap call would return logical addresses that aren't
9222 * suitable for IO and they also will change frequently as COW
9223 * operations happen. So, swapfile + btrfs == corruption.
9224 *
9225 * For now we're avoiding this by dropping bmap.
9226 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009227static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009228 .readpage = btrfs_readpage,
9229 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04009230 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009231 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009232 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009233 .invalidatepage = btrfs_invalidatepage,
9234 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009235 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009236 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009237};
9238
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009239static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009240 .readpage = btrfs_readpage,
9241 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009242 .invalidatepage = btrfs_invalidatepage,
9243 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009244};
9245
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009246static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009247 .getattr = btrfs_getattr,
9248 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009249 .setxattr = btrfs_setxattr,
9250 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009251 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009252 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009253 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009254 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009255 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009256 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009257 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009258};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009259static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009260 .getattr = btrfs_getattr,
9261 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009262 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009263 .setxattr = btrfs_setxattr,
9264 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009265 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009266 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009267 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009268 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009269 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04009270};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009271static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009272 .readlink = generic_readlink,
9273 .follow_link = page_follow_link_light,
9274 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009275 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009276 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009277 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009278 .setxattr = btrfs_setxattr,
9279 .getxattr = btrfs_getxattr,
9280 .listxattr = btrfs_listxattr,
9281 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009282 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009283};
Yan, Zheng76dda932009-09-21 16:00:26 -04009284
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009285const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009286 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009287 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009288};