blob: 09d8c5ee88695ce9e91de079bf0fa078b06b590e [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 Masonf03d9301f2009-02-04 09:31:06 -0500428 /*
429 * we don't want to send crud past the end of i_size through
430 * compression, that's just a waste of CPU time. So, if the
431 * end of the file is before the start of our current
432 * requested range of bytes, we bail out to the uncompressed
433 * cleanup code that can deal with all of this.
434 *
435 * It isn't really the fastest way to fix things, but this is a
436 * very uncommon corner.
437 */
438 if (actual_end <= start)
439 goto cleanup_and_bail_uncompressed;
440
Chris Masonc8b97812008-10-29 14:49:59 -0400441 total_compressed = actual_end - start;
442
443 /* we want to make sure that amount of ram required to uncompress
444 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500445 * of a compressed extent to 128k. This is a crucial number
446 * because it also controls how easily we can spread reads across
447 * cpus for decompression.
448 *
449 * We also want to make sure the amount of IO required to do
450 * a random read is reasonably small, so we limit the size of
451 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400452 */
453 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000454 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500455 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400456 total_in = 0;
457 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400458
Chris Mason771ed682008-11-06 22:02:51 -0500459 /*
460 * we do compression for mount -o compress and when the
461 * inode has not been flagged as nocompress. This flag can
462 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400463 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800464 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400465 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400466 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800467 if (!pages) {
468 /* just bail out to the uncompressed code */
469 goto cont;
470 }
Chris Mason179e29e2007-11-01 11:28:41 -0400471
Li Zefan261507a02010-12-17 14:21:50 +0800472 if (BTRFS_I(inode)->force_compress)
473 compress_type = BTRFS_I(inode)->force_compress;
474
Chris Mason4adaa612013-03-26 13:07:00 -0400475 /*
476 * we need to call clear_page_dirty_for_io on each
477 * page in the range. Otherwise applications with the file
478 * mmap'd can wander in and change the page contents while
479 * we are compressing them.
480 *
481 * If the compression fails for any reason, we set the pages
482 * dirty again later on.
483 */
484 extent_range_clear_dirty_for_io(inode, start, end);
485 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800486 ret = btrfs_compress_pages(compress_type,
487 inode->i_mapping, start,
488 total_compressed, pages,
489 nr_pages, &nr_pages_ret,
490 &total_in,
491 &total_compressed,
492 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400493
494 if (!ret) {
495 unsigned long offset = total_compressed &
496 (PAGE_CACHE_SIZE - 1);
497 struct page *page = pages[nr_pages_ret - 1];
498 char *kaddr;
499
500 /* zero the tail end of the last page, we might be
501 * sending it down to disk
502 */
503 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800504 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400505 memset(kaddr + offset, 0,
506 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800507 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400508 }
509 will_compress = 1;
510 }
511 }
Li Zefan560f7d72011-09-08 10:22:01 +0800512cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400513 if (start == 0) {
514 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500515 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400516 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500517 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400518 */
Josef Bacik00361582013-08-14 14:02:47 -0400519 ret = cow_file_range_inline(root, inode, start, end,
520 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400521 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500522 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400523 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000524 total_compressed,
525 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400526 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100527 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400528 unsigned long clear_flags = EXTENT_DELALLOC |
529 EXTENT_DEFRAG;
530 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
531
Chris Mason771ed682008-11-06 22:02:51 -0500532 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100533 * inline extent creation worked or returned error,
534 * we don't need to create any more async work items.
535 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500536 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400537 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400538 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400539 PAGE_CLEAR_DIRTY |
540 PAGE_SET_WRITEBACK |
541 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400542 goto free_pages_out;
543 }
544 }
545
546 if (will_compress) {
547 /*
548 * we aren't doing an inline extent round the compressed size
549 * up to a block size boundary so the allocator does sane
550 * things
551 */
Qu Wenruofda28322013-02-26 08:10:22 +0000552 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400553
554 /*
555 * one last check to make sure the compression is really a
556 * win, compare the page count read with the blocks on disk
557 */
Qu Wenruofda28322013-02-26 08:10:22 +0000558 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400559 if (total_compressed >= total_in) {
560 will_compress = 0;
561 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400562 num_bytes = total_in;
563 }
564 }
565 if (!will_compress && pages) {
566 /*
567 * the compression code ran but failed to make things smaller,
568 * free any pages it allocated and our page pointer array
569 */
570 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400571 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400572 page_cache_release(pages[i]);
573 }
574 kfree(pages);
575 pages = NULL;
576 total_compressed = 0;
577 nr_pages_ret = 0;
578
579 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500580 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
581 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500582 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500583 }
Chris Masonc8b97812008-10-29 14:49:59 -0400584 }
Chris Mason771ed682008-11-06 22:02:51 -0500585 if (will_compress) {
586 *num_added += 1;
587
588 /* the async work queues will take care of doing actual
589 * allocation on disk for these compressed pages,
590 * and will submit them to the elevator.
591 */
592 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800593 total_compressed, pages, nr_pages_ret,
594 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500595
Yan, Zheng24ae6362010-12-06 07:02:36 +0000596 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500597 start += num_bytes;
598 pages = NULL;
599 cond_resched();
600 goto again;
601 }
602 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500603cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500604 /*
605 * No compression, but we still need to write the pages in
606 * the file we've been given so far. redirty the locked
607 * page if it corresponds to our extent and set things up
608 * for the async work queue to run cow_file_range to do
609 * the normal delalloc dance
610 */
611 if (page_offset(locked_page) >= start &&
612 page_offset(locked_page) <= end) {
613 __set_page_dirty_nobuffers(locked_page);
614 /* unlocked later on in the async handlers */
615 }
Chris Mason4adaa612013-03-26 13:07:00 -0400616 if (redirty)
617 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800618 add_async_extent(async_cow, start, end - start + 1,
619 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500620 *num_added += 1;
621 }
622
623out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100624 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500625
626free_pages_out:
627 for (i = 0; i < nr_pages_ret; i++) {
628 WARN_ON(pages[i]->mapping);
629 page_cache_release(pages[i]);
630 }
Chris Masond3977122009-01-05 21:25:51 -0500631 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500632
633 goto out;
634}
635
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
Miao Xiedc380ae2014-09-12 18:43:55 +08002902static int __readpage_endio_check(struct inode *inode,
2903 struct btrfs_io_bio *io_bio,
2904 int icsum, struct page *page,
2905 int pgoff, u64 start, size_t len)
2906{
2907 char *kaddr;
2908 u32 csum_expected;
2909 u32 csum = ~(u32)0;
2910 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2911 DEFAULT_RATELIMIT_BURST);
2912
2913 csum_expected = *(((u32 *)io_bio->csum) + icsum);
2914
2915 kaddr = kmap_atomic(page);
2916 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
2917 btrfs_csum_final(csum, (char *)&csum);
2918 if (csum != csum_expected)
2919 goto zeroit;
2920
2921 kunmap_atomic(kaddr);
2922 return 0;
2923zeroit:
2924 if (__ratelimit(&_rs))
2925 btrfs_info(BTRFS_I(inode)->root->fs_info,
2926 "csum failed ino %llu off %llu csum %u expected csum %u",
2927 btrfs_ino(inode), start, csum, csum_expected);
2928 memset(kaddr + pgoff, 1, len);
2929 flush_dcache_page(page);
2930 kunmap_atomic(kaddr);
2931 if (csum_expected == 0)
2932 return 0;
2933 return -EIO;
2934}
2935
Chris Masond352ac62008-09-29 15:18:18 -04002936/*
Chris Masond352ac62008-09-29 15:18:18 -04002937 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002938 * if there's a match, we allow the bio to finish. If not, the code in
2939 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002940 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002941static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2942 u64 phy_offset, struct page *page,
2943 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002944{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002945 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002946 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002947 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002948 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002949
Chris Masond20f7042008-12-08 16:58:54 -05002950 if (PageChecked(page)) {
2951 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002952 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002953 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002954
2955 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002956 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002957
Yan Zheng17d217f2008-12-12 10:03:38 -05002958 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002959 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002960 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2961 GFP_NOFS);
2962 return 0;
2963 }
2964
Miao Xiefacc8a222013-07-25 19:22:34 +08002965 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08002966 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
2967 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002968}
Chris Masonb888db22007-08-27 16:49:44 -04002969
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002970struct delayed_iput {
2971 struct list_head list;
2972 struct inode *inode;
2973};
2974
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002975/* JDM: If this is fs-wide, why can't we add a pointer to
2976 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002977void btrfs_add_delayed_iput(struct inode *inode)
2978{
2979 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2980 struct delayed_iput *delayed;
2981
2982 if (atomic_add_unless(&inode->i_count, -1, 1))
2983 return;
2984
2985 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2986 delayed->inode = inode;
2987
2988 spin_lock(&fs_info->delayed_iput_lock);
2989 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2990 spin_unlock(&fs_info->delayed_iput_lock);
2991}
2992
2993void btrfs_run_delayed_iputs(struct btrfs_root *root)
2994{
2995 LIST_HEAD(list);
2996 struct btrfs_fs_info *fs_info = root->fs_info;
2997 struct delayed_iput *delayed;
2998 int empty;
2999
3000 spin_lock(&fs_info->delayed_iput_lock);
3001 empty = list_empty(&fs_info->delayed_iputs);
3002 spin_unlock(&fs_info->delayed_iput_lock);
3003 if (empty)
3004 return;
3005
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003006 spin_lock(&fs_info->delayed_iput_lock);
3007 list_splice_init(&fs_info->delayed_iputs, &list);
3008 spin_unlock(&fs_info->delayed_iput_lock);
3009
3010 while (!list_empty(&list)) {
3011 delayed = list_entry(list.next, struct delayed_iput, list);
3012 list_del(&delayed->list);
3013 iput(delayed->inode);
3014 kfree(delayed);
3015 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003016}
3017
Yan, Zhengd68fc572010-05-16 10:49:58 -04003018/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003019 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003020 * files in the subvolume, it removes orphan item and frees block_rsv
3021 * structure.
3022 */
3023void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3024 struct btrfs_root *root)
3025{
Josef Bacik90290e12011-12-02 15:44:12 -05003026 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003027 int ret;
3028
Josef Bacik8a35d952012-05-23 14:26:42 -04003029 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003030 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3031 return;
3032
Josef Bacik90290e12011-12-02 15:44:12 -05003033 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003034 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003035 spin_unlock(&root->orphan_lock);
3036 return;
3037 }
3038
3039 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3040 spin_unlock(&root->orphan_lock);
3041 return;
3042 }
3043
3044 block_rsv = root->orphan_block_rsv;
3045 root->orphan_block_rsv = NULL;
3046 spin_unlock(&root->orphan_lock);
3047
Miao Xie27cdeb72014-04-02 19:51:05 +08003048 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003049 btrfs_root_refs(&root->root_item) > 0) {
3050 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3051 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003052 if (ret)
3053 btrfs_abort_transaction(trans, root, ret);
3054 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003055 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3056 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003057 }
3058
Josef Bacik90290e12011-12-02 15:44:12 -05003059 if (block_rsv) {
3060 WARN_ON(block_rsv->size > 0);
3061 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003062 }
3063}
3064
3065/*
Josef Bacik7b128762008-07-24 12:17:14 -04003066 * This creates an orphan entry for the given inode in case something goes
3067 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003068 *
3069 * NOTE: caller of this function should reserve 5 units of metadata for
3070 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003071 */
3072int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3073{
3074 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003075 struct btrfs_block_rsv *block_rsv = NULL;
3076 int reserve = 0;
3077 int insert = 0;
3078 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003079
Yan, Zhengd68fc572010-05-16 10:49:58 -04003080 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003081 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003082 if (!block_rsv)
3083 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003084 }
3085
Yan, Zhengd68fc572010-05-16 10:49:58 -04003086 spin_lock(&root->orphan_lock);
3087 if (!root->orphan_block_rsv) {
3088 root->orphan_block_rsv = block_rsv;
3089 } else if (block_rsv) {
3090 btrfs_free_block_rsv(root, block_rsv);
3091 block_rsv = NULL;
3092 }
Josef Bacik7b128762008-07-24 12:17:14 -04003093
Josef Bacik8a35d952012-05-23 14:26:42 -04003094 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3095 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003096#if 0
3097 /*
3098 * For proper ENOSPC handling, we should do orphan
3099 * cleanup when mounting. But this introduces backward
3100 * compatibility issue.
3101 */
3102 if (!xchg(&root->orphan_item_inserted, 1))
3103 insert = 2;
3104 else
3105 insert = 1;
3106#endif
3107 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003108 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003109 }
Josef Bacik7b128762008-07-24 12:17:14 -04003110
Josef Bacik72ac3c02012-05-23 14:13:11 -04003111 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3112 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003113 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003114 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003115
Yan, Zhengd68fc572010-05-16 10:49:58 -04003116 /* grab metadata reservation from transaction handle */
3117 if (reserve) {
3118 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003119 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003120 }
3121
3122 /* insert an orphan item to track this unlinked/truncated file */
3123 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003124 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003125 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003126 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003127 if (reserve) {
3128 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3129 &BTRFS_I(inode)->runtime_flags);
3130 btrfs_orphan_release_metadata(inode);
3131 }
3132 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003133 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3134 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003135 btrfs_abort_transaction(trans, root, ret);
3136 return ret;
3137 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003138 }
3139 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003140 }
3141
3142 /* insert an orphan item to track subvolume contains orphan files */
3143 if (insert >= 2) {
3144 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3145 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003146 if (ret && ret != -EEXIST) {
3147 btrfs_abort_transaction(trans, root, ret);
3148 return ret;
3149 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003150 }
3151 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003152}
3153
3154/*
3155 * We have done the truncate/delete so we can go ahead and remove the orphan
3156 * item for this particular inode.
3157 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003158static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3159 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003160{
3161 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003162 int delete_item = 0;
3163 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003164 int ret = 0;
3165
Yan, Zhengd68fc572010-05-16 10:49:58 -04003166 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003167 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3168 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003169 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003170
Josef Bacik72ac3c02012-05-23 14:13:11 -04003171 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3172 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003173 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003174 spin_unlock(&root->orphan_lock);
3175
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003176 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003177 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003178 if (trans)
3179 ret = btrfs_del_orphan_item(trans, root,
3180 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003181 }
Josef Bacik7b128762008-07-24 12:17:14 -04003182
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003183 if (release_rsv)
3184 btrfs_orphan_release_metadata(inode);
3185
Josef Bacik4ef31a42013-08-13 14:10:08 -04003186 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003187}
3188
3189/*
3190 * this cleans up any orphans that may be left on the list from the last use
3191 * of this root.
3192 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003193int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003194{
3195 struct btrfs_path *path;
3196 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003197 struct btrfs_key key, found_key;
3198 struct btrfs_trans_handle *trans;
3199 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003200 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003201 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3202
Yan, Zhengd68fc572010-05-16 10:49:58 -04003203 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003204 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003205
3206 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003207 if (!path) {
3208 ret = -ENOMEM;
3209 goto out;
3210 }
Josef Bacik7b128762008-07-24 12:17:14 -04003211 path->reada = -1;
3212
3213 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003214 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003215 key.offset = (u64)-1;
3216
Josef Bacik7b128762008-07-24 12:17:14 -04003217 while (1) {
3218 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003219 if (ret < 0)
3220 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003221
3222 /*
3223 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003224 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003225 * find the key and see if we have stuff that matches
3226 */
3227 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003228 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003229 if (path->slots[0] == 0)
3230 break;
3231 path->slots[0]--;
3232 }
3233
3234 /* pull out the item */
3235 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003236 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3237
3238 /* make sure the item matches what we want */
3239 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3240 break;
David Sterba962a2982014-06-04 18:41:45 +02003241 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003242 break;
3243
3244 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003245 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003246
3247 /*
3248 * this is where we are basically btrfs_lookup, without the
3249 * crossing root thing. we store the inode number in the
3250 * offset of the orphan item.
3251 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003252
3253 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003254 btrfs_err(root->fs_info,
3255 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003256 ret = -EINVAL;
3257 goto out;
3258 }
3259
3260 last_objectid = found_key.offset;
3261
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003262 found_key.objectid = found_key.offset;
3263 found_key.type = BTRFS_INODE_ITEM_KEY;
3264 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003265 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303266 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003267 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003268 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003269
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003270 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3271 struct btrfs_root *dead_root;
3272 struct btrfs_fs_info *fs_info = root->fs_info;
3273 int is_dead_root = 0;
3274
3275 /*
3276 * this is an orphan in the tree root. Currently these
3277 * could come from 2 sources:
3278 * a) a snapshot deletion in progress
3279 * b) a free space cache inode
3280 * We need to distinguish those two, as the snapshot
3281 * orphan must not get deleted.
3282 * find_dead_roots already ran before us, so if this
3283 * is a snapshot deletion, we should find the root
3284 * in the dead_roots list
3285 */
3286 spin_lock(&fs_info->trans_lock);
3287 list_for_each_entry(dead_root, &fs_info->dead_roots,
3288 root_list) {
3289 if (dead_root->root_key.objectid ==
3290 found_key.objectid) {
3291 is_dead_root = 1;
3292 break;
3293 }
3294 }
3295 spin_unlock(&fs_info->trans_lock);
3296 if (is_dead_root) {
3297 /* prevent this orphan from being found again */
3298 key.offset = found_key.objectid - 1;
3299 continue;
3300 }
3301 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003302 /*
3303 * Inode is already gone but the orphan item is still there,
3304 * kill the orphan item.
3305 */
3306 if (ret == -ESTALE) {
3307 trans = btrfs_start_transaction(root, 1);
3308 if (IS_ERR(trans)) {
3309 ret = PTR_ERR(trans);
3310 goto out;
3311 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003312 btrfs_debug(root->fs_info, "auto deleting %Lu",
3313 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003314 ret = btrfs_del_orphan_item(trans, root,
3315 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003316 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003317 if (ret)
3318 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003319 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003320 }
Josef Bacik7b128762008-07-24 12:17:14 -04003321
Josef Bacik7b128762008-07-24 12:17:14 -04003322 /*
3323 * add this inode to the orphan list so btrfs_orphan_del does
3324 * the proper thing when we hit it
3325 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003326 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3327 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003328 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003329
Josef Bacik7b128762008-07-24 12:17:14 -04003330 /* if we have links, this was a truncate, lets do that */
3331 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303332 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003333 iput(inode);
3334 continue;
3335 }
Josef Bacik7b128762008-07-24 12:17:14 -04003336 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003337
3338 /* 1 for the orphan item deletion. */
3339 trans = btrfs_start_transaction(root, 1);
3340 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003341 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003342 ret = PTR_ERR(trans);
3343 goto out;
3344 }
3345 ret = btrfs_orphan_add(trans, inode);
3346 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003347 if (ret) {
3348 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003349 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003350 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003351
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003352 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003353 if (ret)
3354 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003355 } else {
3356 nr_unlink++;
3357 }
3358
3359 /* this will do delete_inode and everything for us */
3360 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003361 if (ret)
3362 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003363 }
Miao Xie3254c872011-11-10 20:45:05 -05003364 /* release the path since we're done with it */
3365 btrfs_release_path(path);
3366
Yan, Zhengd68fc572010-05-16 10:49:58 -04003367 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3368
3369 if (root->orphan_block_rsv)
3370 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3371 (u64)-1);
3372
Miao Xie27cdeb72014-04-02 19:51:05 +08003373 if (root->orphan_block_rsv ||
3374 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003375 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003376 if (!IS_ERR(trans))
3377 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003378 }
Josef Bacik7b128762008-07-24 12:17:14 -04003379
3380 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003381 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003382 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003383 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003384
3385out:
3386 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003387 btrfs_crit(root->fs_info,
3388 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003389 btrfs_free_path(path);
3390 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003391}
3392
Chris Masond352ac62008-09-29 15:18:18 -04003393/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003394 * very simple check to peek ahead in the leaf looking for xattrs. If we
3395 * don't find any xattrs, we know there can't be any acls.
3396 *
3397 * slot is the slot the inode is in, objectid is the objectid of the inode
3398 */
3399static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003400 int slot, u64 objectid,
3401 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003402{
3403 u32 nritems = btrfs_header_nritems(leaf);
3404 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003405 static u64 xattr_access = 0;
3406 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003407 int scanned = 0;
3408
Josef Bacikf23b5a52013-06-19 10:16:26 -04003409 if (!xattr_access) {
3410 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3411 strlen(POSIX_ACL_XATTR_ACCESS));
3412 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3413 strlen(POSIX_ACL_XATTR_DEFAULT));
3414 }
3415
Chris Mason46a53cc2009-04-27 11:47:50 -04003416 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003417 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003418 while (slot < nritems) {
3419 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3420
3421 /* we found a different objectid, there must not be acls */
3422 if (found_key.objectid != objectid)
3423 return 0;
3424
3425 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003426 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003427 if (*first_xattr_slot == -1)
3428 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003429 if (found_key.offset == xattr_access ||
3430 found_key.offset == xattr_default)
3431 return 1;
3432 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003433
3434 /*
3435 * we found a key greater than an xattr key, there can't
3436 * be any acls later on
3437 */
3438 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3439 return 0;
3440
3441 slot++;
3442 scanned++;
3443
3444 /*
3445 * it goes inode, inode backrefs, xattrs, extents,
3446 * so if there are a ton of hard links to an inode there can
3447 * be a lot of backrefs. Don't waste time searching too hard,
3448 * this is just an optimization
3449 */
3450 if (scanned >= 8)
3451 break;
3452 }
3453 /* we hit the end of the leaf before we found an xattr or
3454 * something larger than an xattr. We have to assume the inode
3455 * has acls
3456 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003457 if (*first_xattr_slot == -1)
3458 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003459 return 1;
3460}
3461
3462/*
Chris Masond352ac62008-09-29 15:18:18 -04003463 * read an inode from the btree into the in-memory inode
3464 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003465static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003466{
3467 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003468 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003469 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003470 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003471 struct btrfs_root *root = BTRFS_I(inode)->root;
3472 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003473 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003474 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003475 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003476 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003477 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003478 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003479
3480 ret = btrfs_fill_inode(inode, &rdev);
3481 if (!ret)
3482 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003483
3484 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003485 if (!path)
3486 goto make_bad;
3487
Chris Mason39279cc2007-06-12 06:35:45 -04003488 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003489
Chris Mason39279cc2007-06-12 06:35:45 -04003490 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003491 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003492 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003493
Chris Mason5f39d392007-10-15 16:14:19 -04003494 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003495
3496 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003497 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003498
Chris Mason5f39d392007-10-15 16:14:19 -04003499 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3500 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003501 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003502 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003503 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3504 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003505 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003506
3507 tspec = btrfs_inode_atime(inode_item);
3508 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3509 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3510
3511 tspec = btrfs_inode_mtime(inode_item);
3512 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3513 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3514
3515 tspec = btrfs_inode_ctime(inode_item);
3516 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3517 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3518
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003519 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003520 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003521 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3522
3523 /*
3524 * If we were modified in the current generation and evicted from memory
3525 * and then re-read we need to do a full sync since we don't have any
3526 * idea about which extents were modified before we were evicted from
3527 * cache.
3528 */
3529 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3530 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3531 &BTRFS_I(inode)->runtime_flags);
3532
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003533 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003534 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003535 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003536 rdev = btrfs_inode_rdev(leaf, inode_item);
3537
Josef Bacikaec74772008-07-24 12:12:38 -04003538 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003539 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003540
3541cache_index:
3542 path->slots[0]++;
3543 if (inode->i_nlink != 1 ||
3544 path->slots[0] >= btrfs_header_nritems(leaf))
3545 goto cache_acl;
3546
3547 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3548 if (location.objectid != btrfs_ino(inode))
3549 goto cache_acl;
3550
3551 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3552 if (location.type == BTRFS_INODE_REF_KEY) {
3553 struct btrfs_inode_ref *ref;
3554
3555 ref = (struct btrfs_inode_ref *)ptr;
3556 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3557 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3558 struct btrfs_inode_extref *extref;
3559
3560 extref = (struct btrfs_inode_extref *)ptr;
3561 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3562 extref);
3563 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003564cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003565 /*
3566 * try to precache a NULL acl entry for files that don't have
3567 * any xattrs or acls
3568 */
Li Zefan33345d012011-04-20 10:31:50 +08003569 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003570 btrfs_ino(inode), &first_xattr_slot);
3571 if (first_xattr_slot != -1) {
3572 path->slots[0] = first_xattr_slot;
3573 ret = btrfs_load_inode_props(inode, path);
3574 if (ret)
3575 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003576 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003577 btrfs_ino(inode),
3578 root->root_key.objectid, ret);
3579 }
3580 btrfs_free_path(path);
3581
Al Viro72c04902009-06-24 16:58:48 -04003582 if (!maybe_acls)
3583 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003584
Chris Mason39279cc2007-06-12 06:35:45 -04003585 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003586 case S_IFREG:
3587 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003588 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003589 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003590 inode->i_fop = &btrfs_file_operations;
3591 inode->i_op = &btrfs_file_inode_operations;
3592 break;
3593 case S_IFDIR:
3594 inode->i_fop = &btrfs_dir_file_operations;
3595 if (root == root->fs_info->tree_root)
3596 inode->i_op = &btrfs_dir_ro_inode_operations;
3597 else
3598 inode->i_op = &btrfs_dir_inode_operations;
3599 break;
3600 case S_IFLNK:
3601 inode->i_op = &btrfs_symlink_inode_operations;
3602 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003603 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003604 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003605 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003606 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003607 init_special_inode(inode, inode->i_mode, rdev);
3608 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003609 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003610
3611 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003612 return;
3613
3614make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003615 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003616 make_bad_inode(inode);
3617}
3618
Chris Masond352ac62008-09-29 15:18:18 -04003619/*
3620 * given a leaf and an inode, copy the inode fields into the leaf
3621 */
Chris Masone02119d2008-09-05 16:13:11 -04003622static void fill_inode_item(struct btrfs_trans_handle *trans,
3623 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003624 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003625 struct inode *inode)
3626{
Liu Bo51fab692012-12-27 09:01:21 +00003627 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003628
Liu Bo51fab692012-12-27 09:01:21 +00003629 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003630
Liu Bo51fab692012-12-27 09:01:21 +00003631 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3632 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3633 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3634 &token);
3635 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3636 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003637
Liu Bo51fab692012-12-27 09:01:21 +00003638 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3639 inode->i_atime.tv_sec, &token);
3640 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3641 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003642
Liu Bo51fab692012-12-27 09:01:21 +00003643 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3644 inode->i_mtime.tv_sec, &token);
3645 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3646 inode->i_mtime.tv_nsec, &token);
3647
3648 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3649 inode->i_ctime.tv_sec, &token);
3650 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3651 inode->i_ctime.tv_nsec, &token);
3652
3653 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3654 &token);
3655 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3656 &token);
3657 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3658 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3659 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3660 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3661 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003662}
3663
Chris Masond352ac62008-09-29 15:18:18 -04003664/*
3665 * copy everything in the in-memory inode into the btree.
3666 */
Chris Mason21151332011-11-10 20:39:08 -05003667static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003668 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003669{
3670 struct btrfs_inode_item *inode_item;
3671 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003672 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003673 int ret;
3674
3675 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003676 if (!path)
3677 return -ENOMEM;
3678
Chris Masonb9473432009-03-13 11:00:37 -04003679 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003680 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3681 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003682 if (ret) {
3683 if (ret > 0)
3684 ret = -ENOENT;
3685 goto failed;
3686 }
3687
Chris Mason5f39d392007-10-15 16:14:19 -04003688 leaf = path->nodes[0];
3689 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003690 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003691
Chris Masone02119d2008-09-05 16:13:11 -04003692 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003693 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003694 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003695 ret = 0;
3696failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003697 btrfs_free_path(path);
3698 return ret;
3699}
3700
Chris Masond352ac62008-09-29 15:18:18 -04003701/*
Chris Mason21151332011-11-10 20:39:08 -05003702 * copy everything in the in-memory inode into the btree.
3703 */
3704noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3705 struct btrfs_root *root, struct inode *inode)
3706{
3707 int ret;
3708
3709 /*
3710 * If the inode is a free space inode, we can deadlock during commit
3711 * if we put it into the delayed code.
3712 *
3713 * The data relocation inode should also be directly updated
3714 * without delay
3715 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003716 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003717 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003718 btrfs_update_root_times(trans, root);
3719
Chris Mason21151332011-11-10 20:39:08 -05003720 ret = btrfs_delayed_update_inode(trans, root, inode);
3721 if (!ret)
3722 btrfs_set_inode_last_trans(trans, inode);
3723 return ret;
3724 }
3725
3726 return btrfs_update_inode_item(trans, root, inode);
3727}
3728
Josef Bacikbe6aef62012-10-22 15:43:12 -04003729noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3730 struct btrfs_root *root,
3731 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003732{
3733 int ret;
3734
3735 ret = btrfs_update_inode(trans, root, inode);
3736 if (ret == -ENOSPC)
3737 return btrfs_update_inode_item(trans, root, inode);
3738 return ret;
3739}
3740
3741/*
Chris Masond352ac62008-09-29 15:18:18 -04003742 * unlink helper that gets used here in inode.c and in the tree logging
3743 * recovery code. It remove a link in a directory with a given name, and
3744 * also drops the back refs in the inode to the directory
3745 */
Al Viro92986792011-03-04 17:14:37 +00003746static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3747 struct btrfs_root *root,
3748 struct inode *dir, struct inode *inode,
3749 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003750{
3751 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003752 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003753 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003754 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003755 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003756 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003757 u64 ino = btrfs_ino(inode);
3758 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003759
3760 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003761 if (!path) {
3762 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003763 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003764 }
3765
Chris Masonb9473432009-03-13 11:00:37 -04003766 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003767 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003768 name, name_len, -1);
3769 if (IS_ERR(di)) {
3770 ret = PTR_ERR(di);
3771 goto err;
3772 }
3773 if (!di) {
3774 ret = -ENOENT;
3775 goto err;
3776 }
Chris Mason5f39d392007-10-15 16:14:19 -04003777 leaf = path->nodes[0];
3778 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003779 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003780 if (ret)
3781 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003782 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003783
Miao Xie67de1172013-12-26 13:07:06 +08003784 /*
3785 * If we don't have dir index, we have to get it by looking up
3786 * the inode ref, since we get the inode ref, remove it directly,
3787 * it is unnecessary to do delayed deletion.
3788 *
3789 * But if we have dir index, needn't search inode ref to get it.
3790 * Since the inode ref is close to the inode item, it is better
3791 * that we delay to delete it, and just do this deletion when
3792 * we update the inode item.
3793 */
3794 if (BTRFS_I(inode)->dir_index) {
3795 ret = btrfs_delayed_delete_inode_ref(inode);
3796 if (!ret) {
3797 index = BTRFS_I(inode)->dir_index;
3798 goto skip_backref;
3799 }
3800 }
3801
Li Zefan33345d012011-04-20 10:31:50 +08003802 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3803 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003804 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003805 btrfs_info(root->fs_info,
3806 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003807 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003808 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003809 goto err;
3810 }
Miao Xie67de1172013-12-26 13:07:06 +08003811skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003812 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003813 if (ret) {
3814 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003815 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003816 }
Chris Mason39279cc2007-06-12 06:35:45 -04003817
Chris Masone02119d2008-09-05 16:13:11 -04003818 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003819 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003820 if (ret != 0 && ret != -ENOENT) {
3821 btrfs_abort_transaction(trans, root, ret);
3822 goto err;
3823 }
Chris Masone02119d2008-09-05 16:13:11 -04003824
3825 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3826 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003827 if (ret == -ENOENT)
3828 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003829 else if (ret)
3830 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003831err:
3832 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003833 if (ret)
3834 goto out;
3835
3836 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003837 inode_inc_iversion(inode);
3838 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003839 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003840 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003841out:
Chris Mason39279cc2007-06-12 06:35:45 -04003842 return ret;
3843}
3844
Al Viro92986792011-03-04 17:14:37 +00003845int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3846 struct btrfs_root *root,
3847 struct inode *dir, struct inode *inode,
3848 const char *name, int name_len)
3849{
3850 int ret;
3851 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3852 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003853 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003854 ret = btrfs_update_inode(trans, root, inode);
3855 }
3856 return ret;
3857}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003858
3859/*
3860 * helper to start transaction for unlink and rmdir.
3861 *
Josef Bacikd52be812013-05-29 14:54:47 -04003862 * unlink and rmdir are special in btrfs, they do not always free space, so
3863 * if we cannot make our reservations the normal way try and see if there is
3864 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3865 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003866 */
Josef Bacikd52be812013-05-29 14:54:47 -04003867static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003868{
3869 struct btrfs_trans_handle *trans;
3870 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003871 int ret;
3872
Josef Bacike70bea52011-10-11 14:18:24 -04003873 /*
3874 * 1 for the possible orphan item
3875 * 1 for the dir item
3876 * 1 for the dir index
3877 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003878 * 1 for the inode
3879 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003880 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003881 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3882 return trans;
3883
Josef Bacikd52be812013-05-29 14:54:47 -04003884 if (PTR_ERR(trans) == -ENOSPC) {
3885 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003886
Josef Bacikd52be812013-05-29 14:54:47 -04003887 trans = btrfs_start_transaction(root, 0);
3888 if (IS_ERR(trans))
3889 return trans;
3890 ret = btrfs_cond_migrate_bytes(root->fs_info,
3891 &root->fs_info->trans_block_rsv,
3892 num_bytes, 5);
3893 if (ret) {
3894 btrfs_end_transaction(trans, root);
3895 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003896 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003897 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003898 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003899 }
Josef Bacikd52be812013-05-29 14:54:47 -04003900 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003901}
3902
Chris Mason39279cc2007-06-12 06:35:45 -04003903static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3904{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003905 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003906 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003907 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003908 int ret;
3909
Josef Bacikd52be812013-05-29 14:54:47 -04003910 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003911 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003912 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003913
Chris Mason12fcfd22009-03-24 10:24:20 -04003914 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3915
Chris Masone02119d2008-09-05 16:13:11 -04003916 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3917 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003918 if (ret)
3919 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003920
Yan, Zhenga22285a2010-05-16 10:48:46 -04003921 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003922 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003923 if (ret)
3924 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003925 }
Josef Bacik7b128762008-07-24 12:17:14 -04003926
Tsutomu Itohb5324022011-07-19 07:27:20 +00003927out:
Josef Bacikd52be812013-05-29 14:54:47 -04003928 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003929 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003930 return ret;
3931}
3932
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003933int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3934 struct btrfs_root *root,
3935 struct inode *dir, u64 objectid,
3936 const char *name, int name_len)
3937{
3938 struct btrfs_path *path;
3939 struct extent_buffer *leaf;
3940 struct btrfs_dir_item *di;
3941 struct btrfs_key key;
3942 u64 index;
3943 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003944 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003945
3946 path = btrfs_alloc_path();
3947 if (!path)
3948 return -ENOMEM;
3949
Li Zefan33345d012011-04-20 10:31:50 +08003950 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003951 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003952 if (IS_ERR_OR_NULL(di)) {
3953 if (!di)
3954 ret = -ENOENT;
3955 else
3956 ret = PTR_ERR(di);
3957 goto out;
3958 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003959
3960 leaf = path->nodes[0];
3961 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3962 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3963 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003964 if (ret) {
3965 btrfs_abort_transaction(trans, root, ret);
3966 goto out;
3967 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003968 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003969
3970 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3971 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003972 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003973 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003974 if (ret != -ENOENT) {
3975 btrfs_abort_transaction(trans, root, ret);
3976 goto out;
3977 }
Li Zefan33345d012011-04-20 10:31:50 +08003978 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003979 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003980 if (IS_ERR_OR_NULL(di)) {
3981 if (!di)
3982 ret = -ENOENT;
3983 else
3984 ret = PTR_ERR(di);
3985 btrfs_abort_transaction(trans, root, ret);
3986 goto out;
3987 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003988
3989 leaf = path->nodes[0];
3990 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003991 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003992 index = key.offset;
3993 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003994 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003995
Miao Xie16cdcec2011-04-22 18:12:22 +08003996 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003997 if (ret) {
3998 btrfs_abort_transaction(trans, root, ret);
3999 goto out;
4000 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004001
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004002 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004003 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004004 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06004005 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004006 if (ret)
4007 btrfs_abort_transaction(trans, root, ret);
4008out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004009 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004010 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004011}
4012
Chris Mason39279cc2007-06-12 06:35:45 -04004013static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4014{
4015 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004016 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004017 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004018 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004019
David Sterbab3ae2442012-09-13 16:04:34 -06004020 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004021 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004022 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4023 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004024
Josef Bacikd52be812013-05-29 14:54:47 -04004025 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004026 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004027 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004028
Li Zefan33345d012011-04-20 10:31:50 +08004029 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004030 err = btrfs_unlink_subvol(trans, root, dir,
4031 BTRFS_I(inode)->location.objectid,
4032 dentry->d_name.name,
4033 dentry->d_name.len);
4034 goto out;
4035 }
4036
Josef Bacik7b128762008-07-24 12:17:14 -04004037 err = btrfs_orphan_add(trans, inode);
4038 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004039 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004040
Chris Mason39279cc2007-06-12 06:35:45 -04004041 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004042 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4043 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004044 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004045 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004046out:
Josef Bacikd52be812013-05-29 14:54:47 -04004047 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004048 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004049
Chris Mason39279cc2007-06-12 06:35:45 -04004050 return err;
4051}
4052
Chris Mason323ac952008-10-01 19:05:46 -04004053/*
Chris Mason39279cc2007-06-12 06:35:45 -04004054 * this can truncate away extent items, csum items and directory items.
4055 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004056 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004057 *
4058 * csum items that cross the new i_size are truncated to the new size
4059 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004060 *
4061 * min_type is the minimum key type to truncate down to. If set to 0, this
4062 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004063 */
Yan, Zheng80825102009-11-12 09:35:36 +00004064int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4065 struct btrfs_root *root,
4066 struct inode *inode,
4067 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004068{
Chris Mason39279cc2007-06-12 06:35:45 -04004069 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004070 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004071 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004072 struct btrfs_key key;
4073 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004074 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004075 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004076 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004077 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004078 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00004079 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004080 int found_extent;
4081 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004082 int pending_del_nr = 0;
4083 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004084 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004085 int ret;
4086 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004087 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004088
4089 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004090
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004091 path = btrfs_alloc_path();
4092 if (!path)
4093 return -ENOMEM;
4094 path->reada = -1;
4095
Josef Bacik5dc562c2012-08-17 13:14:17 -04004096 /*
4097 * We want to drop from the next block forward in case this new size is
4098 * not block aligned since we will be keeping the last block of the
4099 * extent just the way it is.
4100 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004101 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4102 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004103 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4104 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004105
Miao Xie16cdcec2011-04-22 18:12:22 +08004106 /*
4107 * This function is also used to drop the items in the log tree before
4108 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4109 * it is used to drop the loged items. So we shouldn't kill the delayed
4110 * items.
4111 */
4112 if (min_type == 0 && root == BTRFS_I(inode)->root)
4113 btrfs_kill_delayed_inode_items(inode);
4114
Li Zefan33345d012011-04-20 10:31:50 +08004115 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004116 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004117 key.type = (u8)-1;
4118
Chris Mason85e21ba2008-01-29 15:11:36 -05004119search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004120 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004121 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004122 if (ret < 0) {
4123 err = ret;
4124 goto out;
4125 }
Chris Masond3977122009-01-05 21:25:51 -05004126
Chris Mason85e21ba2008-01-29 15:11:36 -05004127 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004128 /* there are no items in the tree for us to truncate, we're
4129 * done
4130 */
Yan, Zheng80825102009-11-12 09:35:36 +00004131 if (path->slots[0] == 0)
4132 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004133 path->slots[0]--;
4134 }
4135
Chris Masond3977122009-01-05 21:25:51 -05004136 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004137 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004138 leaf = path->nodes[0];
4139 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004140 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004141
Li Zefan33345d012011-04-20 10:31:50 +08004142 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004143 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004144
Chris Mason85e21ba2008-01-29 15:11:36 -05004145 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004146 break;
4147
Chris Mason5f39d392007-10-15 16:14:19 -04004148 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004149 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004150 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004151 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004152 extent_type = btrfs_file_extent_type(leaf, fi);
4153 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004154 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004155 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004156 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004157 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004158 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004159 }
Yan008630c2007-11-07 13:31:09 -05004160 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004161 }
Yan, Zheng80825102009-11-12 09:35:36 +00004162 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004163 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004164 } else {
4165 if (item_end < new_size)
4166 break;
4167 if (found_key.offset >= new_size)
4168 del_item = 1;
4169 else
4170 del_item = 0;
4171 }
Chris Mason39279cc2007-06-12 06:35:45 -04004172 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004173 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004174 if (found_type != BTRFS_EXTENT_DATA_KEY)
4175 goto delete;
4176
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004177 if (del_item)
4178 last_size = found_key.offset;
4179 else
4180 last_size = new_size;
4181
Chris Mason179e29e2007-11-01 11:28:41 -04004182 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004183 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004184 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004185 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004186 u64 orig_num_bytes =
4187 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004188 extent_num_bytes = ALIGN(new_size -
4189 found_key.offset,
4190 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004191 btrfs_set_file_extent_num_bytes(leaf, fi,
4192 extent_num_bytes);
4193 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004194 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004195 if (test_bit(BTRFS_ROOT_REF_COWS,
4196 &root->state) &&
4197 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004198 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004199 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004200 } else {
Chris Masondb945352007-10-15 16:15:53 -04004201 extent_num_bytes =
4202 btrfs_file_extent_disk_num_bytes(leaf,
4203 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004204 extent_offset = found_key.offset -
4205 btrfs_file_extent_offset(leaf, fi);
4206
Chris Mason39279cc2007-06-12 06:35:45 -04004207 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004208 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004209 if (extent_start != 0) {
4210 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004211 if (test_bit(BTRFS_ROOT_REF_COWS,
4212 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004213 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004214 }
4215 }
Chris Mason90692182008-02-08 13:49:28 -05004216 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004217 /*
4218 * we can't truncate inline items that have had
4219 * special encodings
4220 */
4221 if (!del_item &&
4222 btrfs_file_extent_compression(leaf, fi) == 0 &&
4223 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4224 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004225 u32 size = new_size - found_key.offset;
4226
Miao Xie27cdeb72014-04-02 19:51:05 +08004227 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004228 inode_sub_bytes(inode, item_end + 1 -
4229 new_size);
Chris Mason514ac8a2014-01-03 21:07:00 -08004230
4231 /*
4232 * update the ram bytes to properly reflect
4233 * the new size of our item
4234 */
4235 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004236 size =
4237 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004238 btrfs_truncate_item(root, path, size, 1);
Miao Xie27cdeb72014-04-02 19:51:05 +08004239 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4240 &root->state)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004241 inode_sub_bytes(inode, item_end + 1 -
4242 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004243 }
Chris Mason39279cc2007-06-12 06:35:45 -04004244 }
Chris Mason179e29e2007-11-01 11:28:41 -04004245delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004246 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004247 if (!pending_del_nr) {
4248 /* no pending yet, add ourselves */
4249 pending_del_slot = path->slots[0];
4250 pending_del_nr = 1;
4251 } else if (pending_del_nr &&
4252 path->slots[0] + 1 == pending_del_slot) {
4253 /* hop on the pending chunk */
4254 pending_del_nr++;
4255 pending_del_slot = path->slots[0];
4256 } else {
Chris Masond3977122009-01-05 21:25:51 -05004257 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004258 }
Chris Mason39279cc2007-06-12 06:35:45 -04004259 } else {
4260 break;
4261 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004262 if (found_extent &&
4263 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4264 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004265 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004266 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004267 extent_num_bytes, 0,
4268 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004269 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004270 BUG_ON(ret);
4271 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004272
Yan, Zheng80825102009-11-12 09:35:36 +00004273 if (found_type == BTRFS_INODE_ITEM_KEY)
4274 break;
4275
4276 if (path->slots[0] == 0 ||
4277 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004278 if (pending_del_nr) {
4279 ret = btrfs_del_items(trans, root, path,
4280 pending_del_slot,
4281 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004282 if (ret) {
4283 btrfs_abort_transaction(trans,
4284 root, ret);
4285 goto error;
4286 }
Yan, Zheng80825102009-11-12 09:35:36 +00004287 pending_del_nr = 0;
4288 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004289 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004290 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004291 } else {
4292 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004293 }
Chris Mason39279cc2007-06-12 06:35:45 -04004294 }
Yan, Zheng80825102009-11-12 09:35:36 +00004295out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004296 if (pending_del_nr) {
4297 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4298 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004299 if (ret)
4300 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004301 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004302error:
Filipe Mananadac57052014-08-29 20:54:26 +01004303 if (last_size != (u64)-1 &&
4304 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004305 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004306 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004307 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004308}
4309
Chris Masona52d9a82007-08-27 16:49:44 -04004310/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004311 * btrfs_truncate_page - read, zero a chunk and write a page
4312 * @inode - inode that we're zeroing
4313 * @from - the offset to start zeroing
4314 * @len - the length to zero, 0 to zero the entire range respective to the
4315 * offset
4316 * @front - zero up to the offset instead of from the offset on
4317 *
4318 * This will find the page for the "from" offset and cow the page and zero the
4319 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004320 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004321int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4322 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004323{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004324 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004325 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004326 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4327 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004328 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004329 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004330 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004331 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4332 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4333 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004334 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004335 int ret = 0;
4336 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004337 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004338
Josef Bacik2aaa6652012-08-29 14:27:18 -04004339 if ((offset & (blocksize - 1)) == 0 &&
4340 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004341 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004342 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004343 if (ret)
4344 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004345
Chris Mason211c17f2008-05-15 09:13:45 -04004346again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004347 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004348 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004349 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004350 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004351 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004352 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004353
4354 page_start = page_offset(page);
4355 page_end = page_start + PAGE_CACHE_SIZE - 1;
4356
Chris Masona52d9a82007-08-27 16:49:44 -04004357 if (!PageUptodate(page)) {
4358 ret = btrfs_readpage(NULL, page);
4359 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004360 if (page->mapping != mapping) {
4361 unlock_page(page);
4362 page_cache_release(page);
4363 goto again;
4364 }
Chris Masona52d9a82007-08-27 16:49:44 -04004365 if (!PageUptodate(page)) {
4366 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004367 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004368 }
4369 }
Chris Mason211c17f2008-05-15 09:13:45 -04004370 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004371
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004372 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004373 set_page_extent_mapped(page);
4374
4375 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4376 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004377 unlock_extent_cached(io_tree, page_start, page_end,
4378 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004379 unlock_page(page);
4380 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004381 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004382 btrfs_put_ordered_extent(ordered);
4383 goto again;
4384 }
4385
Josef Bacik2ac55d42010-02-03 19:33:23 +00004386 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004387 EXTENT_DIRTY | EXTENT_DELALLOC |
4388 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004389 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004390
Josef Bacik2ac55d42010-02-03 19:33:23 +00004391 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4392 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004393 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004394 unlock_extent_cached(io_tree, page_start, page_end,
4395 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004396 goto out_unlock;
4397 }
4398
Chris Masone6dcd2d2008-07-17 12:53:50 -04004399 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004400 if (!len)
4401 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004402 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004403 if (front)
4404 memset(kaddr, 0, offset);
4405 else
4406 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004407 flush_dcache_page(page);
4408 kunmap(page);
4409 }
Chris Mason247e7432008-07-17 12:53:51 -04004410 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004411 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004412 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4413 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004414
Chris Mason89642222008-07-24 09:41:53 -04004415out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004416 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004417 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004418 unlock_page(page);
4419 page_cache_release(page);
4420out:
4421 return ret;
4422}
4423
Josef Bacik16e75492013-10-22 12:18:51 -04004424static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4425 u64 offset, u64 len)
4426{
4427 struct btrfs_trans_handle *trans;
4428 int ret;
4429
4430 /*
4431 * Still need to make sure the inode looks like it's been updated so
4432 * that any holes get logged if we fsync.
4433 */
4434 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4435 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4436 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4437 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4438 return 0;
4439 }
4440
4441 /*
4442 * 1 - for the one we're dropping
4443 * 1 - for the one we're adding
4444 * 1 - for updating the inode.
4445 */
4446 trans = btrfs_start_transaction(root, 3);
4447 if (IS_ERR(trans))
4448 return PTR_ERR(trans);
4449
4450 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4451 if (ret) {
4452 btrfs_abort_transaction(trans, root, ret);
4453 btrfs_end_transaction(trans, root);
4454 return ret;
4455 }
4456
4457 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4458 0, 0, len, 0, len, 0, 0, 0);
4459 if (ret)
4460 btrfs_abort_transaction(trans, root, ret);
4461 else
4462 btrfs_update_inode(trans, root, inode);
4463 btrfs_end_transaction(trans, root);
4464 return ret;
4465}
4466
Josef Bacik695a0d02011-03-04 15:46:53 -05004467/*
4468 * This function puts in dummy file extents for the area we're creating a hole
4469 * for. So if we are truncating this file to a larger size we need to insert
4470 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4471 * the range between oldsize and size
4472 */
Josef Bacika41ad392011-01-31 15:30:16 -05004473int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004474{
Yan Zheng9036c102008-10-30 14:19:41 -04004475 struct btrfs_root *root = BTRFS_I(inode)->root;
4476 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004477 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004478 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004479 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004480 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4481 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004482 u64 last_byte;
4483 u64 cur_offset;
4484 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004485 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004486
Josef Bacika71754f2013-06-17 17:14:39 -04004487 /*
4488 * If our size started in the middle of a page we need to zero out the
4489 * rest of the page before we expand the i_size, otherwise we could
4490 * expose stale data.
4491 */
4492 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4493 if (err)
4494 return err;
4495
Yan Zheng9036c102008-10-30 14:19:41 -04004496 if (size <= hole_start)
4497 return 0;
4498
Yan Zheng9036c102008-10-30 14:19:41 -04004499 while (1) {
4500 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004501
Josef Bacik2ac55d42010-02-03 19:33:23 +00004502 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004503 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004504 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4505 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004506 if (!ordered)
4507 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004508 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4509 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004510 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004511 btrfs_put_ordered_extent(ordered);
4512 }
4513
Yan Zheng9036c102008-10-30 14:19:41 -04004514 cur_offset = hole_start;
4515 while (1) {
4516 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4517 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004518 if (IS_ERR(em)) {
4519 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004520 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004521 break;
4522 }
Yan Zheng9036c102008-10-30 14:19:41 -04004523 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004524 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004525 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004526 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004527 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004528
Josef Bacik16e75492013-10-22 12:18:51 -04004529 err = maybe_insert_hole(root, inode, cur_offset,
4530 hole_size);
4531 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004532 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004533 btrfs_drop_extent_cache(inode, cur_offset,
4534 cur_offset + hole_size - 1, 0);
4535 hole_em = alloc_extent_map();
4536 if (!hole_em) {
4537 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4538 &BTRFS_I(inode)->runtime_flags);
4539 goto next;
4540 }
4541 hole_em->start = cur_offset;
4542 hole_em->len = hole_size;
4543 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004544
Josef Bacik5dc562c2012-08-17 13:14:17 -04004545 hole_em->block_start = EXTENT_MAP_HOLE;
4546 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004547 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004548 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004549 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4550 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004551 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004552
4553 while (1) {
4554 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004555 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004556 write_unlock(&em_tree->lock);
4557 if (err != -EEXIST)
4558 break;
4559 btrfs_drop_extent_cache(inode, cur_offset,
4560 cur_offset +
4561 hole_size - 1, 0);
4562 }
4563 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004564 }
Josef Bacik16e75492013-10-22 12:18:51 -04004565next:
Yan Zheng9036c102008-10-30 14:19:41 -04004566 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004567 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004568 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004569 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004570 break;
4571 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004572 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004573 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4574 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004575 return err;
4576}
4577
Eric Sandeen3972f262013-01-12 02:57:22 +00004578static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004579{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004580 struct btrfs_root *root = BTRFS_I(inode)->root;
4581 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004582 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004583 loff_t newsize = attr->ia_size;
4584 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004585 int ret;
4586
Eric Sandeen3972f262013-01-12 02:57:22 +00004587 /*
4588 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4589 * special case where we need to update the times despite not having
4590 * these flags set. For all other operations the VFS set these flags
4591 * explicitly if it wants a timestamp update.
4592 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004593 if (newsize != oldsize) {
4594 inode_inc_iversion(inode);
4595 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4596 inode->i_ctime = inode->i_mtime =
4597 current_fs_time(inode->i_sb);
4598 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004599
Josef Bacika41ad392011-01-31 15:30:16 -05004600 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004601 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004602 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004603 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004604 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004605
Miao Xief4a2f4c2011-12-14 20:12:01 -05004606 trans = btrfs_start_transaction(root, 1);
4607 if (IS_ERR(trans))
4608 return PTR_ERR(trans);
4609
4610 i_size_write(inode, newsize);
4611 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4612 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004613 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004614 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004615
Josef Bacika41ad392011-01-31 15:30:16 -05004616 /*
4617 * We're truncating a file that used to have good data down to
4618 * zero. Make sure it gets into the ordered flush list so that
4619 * any new writes get down to disk quickly.
4620 */
4621 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004622 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4623 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004624
Josef Bacikf3fe8202013-01-07 17:03:21 -05004625 /*
4626 * 1 for the orphan item we're going to add
4627 * 1 for the orphan item deletion.
4628 */
4629 trans = btrfs_start_transaction(root, 2);
4630 if (IS_ERR(trans))
4631 return PTR_ERR(trans);
4632
4633 /*
4634 * We need to do this in case we fail at _any_ point during the
4635 * actual truncate. Once we do the truncate_setsize we could
4636 * invalidate pages which forces any outstanding ordered io to
4637 * be instantly completed which will give us extents that need
4638 * to be truncated. If we fail to get an orphan inode down we
4639 * could have left over extents that were never meant to live,
4640 * so we need to garuntee from this point on that everything
4641 * will be consistent.
4642 */
4643 ret = btrfs_orphan_add(trans, inode);
4644 btrfs_end_transaction(trans, root);
4645 if (ret)
4646 return ret;
4647
Josef Bacika41ad392011-01-31 15:30:16 -05004648 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4649 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004650
4651 /* Disable nonlocked read DIO to avoid the end less truncate */
4652 btrfs_inode_block_unlocked_dio(inode);
4653 inode_dio_wait(inode);
4654 btrfs_inode_resume_unlocked_dio(inode);
4655
Josef Bacika41ad392011-01-31 15:30:16 -05004656 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004657 if (ret && inode->i_nlink) {
4658 int err;
4659
4660 /*
4661 * failed to truncate, disk_i_size is only adjusted down
4662 * as we remove extents, so it should represent the true
4663 * size of the inode, so reset the in memory size and
4664 * delete our orphan entry.
4665 */
4666 trans = btrfs_join_transaction(root);
4667 if (IS_ERR(trans)) {
4668 btrfs_orphan_del(NULL, inode);
4669 return ret;
4670 }
4671 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4672 err = btrfs_orphan_del(trans, inode);
4673 if (err)
4674 btrfs_abort_transaction(trans, root, err);
4675 btrfs_end_transaction(trans, root);
4676 }
Yan, Zheng80825102009-11-12 09:35:36 +00004677 }
4678
Josef Bacika41ad392011-01-31 15:30:16 -05004679 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004680}
4681
Chris Mason39279cc2007-06-12 06:35:45 -04004682static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4683{
4684 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004685 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004686 int err;
4687
Li Zefanb83cc962010-12-20 16:04:08 +08004688 if (btrfs_root_readonly(root))
4689 return -EROFS;
4690
Chris Mason39279cc2007-06-12 06:35:45 -04004691 err = inode_change_ok(inode, attr);
4692 if (err)
4693 return err;
4694
Chris Mason5a3f23d2009-03-31 13:27:11 -04004695 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004696 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004697 if (err)
4698 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004699 }
Yan Zheng9036c102008-10-30 14:19:41 -04004700
Christoph Hellwig10257742010-06-04 11:30:02 +02004701 if (attr->ia_valid) {
4702 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004703 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004704 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004705
Josef Bacik22c44fe2011-11-30 10:45:38 -05004706 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004707 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004708 }
4709
Chris Mason39279cc2007-06-12 06:35:45 -04004710 return err;
4711}
Chris Mason61295eb2008-01-14 16:24:38 -05004712
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004713/*
4714 * While truncating the inode pages during eviction, we get the VFS calling
4715 * btrfs_invalidatepage() against each page of the inode. This is slow because
4716 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4717 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4718 * extent_state structures over and over, wasting lots of time.
4719 *
4720 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4721 * those expensive operations on a per page basis and do only the ordered io
4722 * finishing, while we release here the extent_map and extent_state structures,
4723 * without the excessive merging and splitting.
4724 */
4725static void evict_inode_truncate_pages(struct inode *inode)
4726{
4727 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4728 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4729 struct rb_node *node;
4730
4731 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004732 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004733
4734 write_lock(&map_tree->lock);
4735 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4736 struct extent_map *em;
4737
4738 node = rb_first(&map_tree->map);
4739 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004740 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4741 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004742 remove_extent_mapping(map_tree, em);
4743 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004744 if (need_resched()) {
4745 write_unlock(&map_tree->lock);
4746 cond_resched();
4747 write_lock(&map_tree->lock);
4748 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004749 }
4750 write_unlock(&map_tree->lock);
4751
4752 spin_lock(&io_tree->lock);
4753 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4754 struct extent_state *state;
4755 struct extent_state *cached_state = NULL;
4756
4757 node = rb_first(&io_tree->state);
4758 state = rb_entry(node, struct extent_state, rb_node);
4759 atomic_inc(&state->refs);
4760 spin_unlock(&io_tree->lock);
4761
4762 lock_extent_bits(io_tree, state->start, state->end,
4763 0, &cached_state);
4764 clear_extent_bit(io_tree, state->start, state->end,
4765 EXTENT_LOCKED | EXTENT_DIRTY |
4766 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4767 EXTENT_DEFRAG, 1, 1,
4768 &cached_state, GFP_NOFS);
4769 free_extent_state(state);
4770
Filipe Manana7064dd52014-08-08 02:47:05 +01004771 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004772 spin_lock(&io_tree->lock);
4773 }
4774 spin_unlock(&io_tree->lock);
4775}
4776
Al Virobd555972010-06-07 11:35:40 -04004777void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004778{
4779 struct btrfs_trans_handle *trans;
4780 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004781 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004782 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004783 int ret;
4784
liubo1abe9b82011-03-24 11:18:59 +00004785 trace_btrfs_inode_evict(inode);
4786
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004787 evict_inode_truncate_pages(inode);
4788
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004789 if (inode->i_nlink &&
4790 ((btrfs_root_refs(&root->root_item) != 0 &&
4791 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4792 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004793 goto no_delete;
4794
Chris Mason39279cc2007-06-12 06:35:45 -04004795 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004796 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004797 goto no_delete;
4798 }
Al Virobd555972010-06-07 11:35:40 -04004799 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004800 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004801
Yan, Zhengc71bf092009-11-12 09:34:40 +00004802 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004803 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004804 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004805 goto no_delete;
4806 }
4807
Yan, Zheng76dda932009-09-21 16:00:26 -04004808 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004809 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4810 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004811 goto no_delete;
4812 }
4813
Miao Xie0e8c36a2012-12-19 06:59:51 +00004814 ret = btrfs_commit_inode_delayed_inode(inode);
4815 if (ret) {
4816 btrfs_orphan_del(NULL, inode);
4817 goto no_delete;
4818 }
4819
Miao Xie66d8f3d2012-09-06 04:02:28 -06004820 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004821 if (!rsv) {
4822 btrfs_orphan_del(NULL, inode);
4823 goto no_delete;
4824 }
Josef Bacik4a338542011-08-29 11:01:31 -04004825 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004826 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004827 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004828
Chris Masondbe674a2008-07-17 12:54:05 -04004829 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004830
Josef Bacik4289a662011-08-05 13:22:24 -04004831 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004832 * This is a bit simpler than btrfs_truncate since we've already
4833 * reserved our space for our orphan item in the unlink, so we just
4834 * need to reserve some slack space in case we add bytes and update
4835 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004836 */
Yan, Zheng80825102009-11-12 09:35:36 +00004837 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004838 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4839 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004840
Josef Bacik726c35f2011-09-26 15:46:06 -04004841 /*
4842 * Try and steal from the global reserve since we will
4843 * likely not use this space anyway, we want to try as
4844 * hard as possible to get this to work.
4845 */
4846 if (ret)
4847 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4848
Yan, Zhengd68fc572010-05-16 10:49:58 -04004849 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004850 btrfs_warn(root->fs_info,
4851 "Could not get space for a delete, will truncate on mount %d",
4852 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004853 btrfs_orphan_del(NULL, inode);
4854 btrfs_free_block_rsv(root, rsv);
4855 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004856 }
4857
Miao Xie0e8c36a2012-12-19 06:59:51 +00004858 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004859 if (IS_ERR(trans)) {
4860 btrfs_orphan_del(NULL, inode);
4861 btrfs_free_block_rsv(root, rsv);
4862 goto no_delete;
4863 }
4864
4865 trans->block_rsv = rsv;
4866
Yan, Zhengd68fc572010-05-16 10:49:58 -04004867 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004868 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004869 break;
4870
Miao Xie8407aa42012-09-07 01:43:32 -06004871 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004872 btrfs_end_transaction(trans, root);
4873 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004874 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004875 }
4876
Josef Bacik4289a662011-08-05 13:22:24 -04004877 btrfs_free_block_rsv(root, rsv);
4878
Josef Bacik4ef31a42013-08-13 14:10:08 -04004879 /*
4880 * Errors here aren't a big deal, it just means we leave orphan items
4881 * in the tree. They will be cleaned up on the next mount.
4882 */
Yan, Zheng80825102009-11-12 09:35:36 +00004883 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004884 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004885 btrfs_orphan_del(trans, inode);
4886 } else {
4887 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004888 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004889
Josef Bacik4289a662011-08-05 13:22:24 -04004890 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004891 if (!(root == root->fs_info->tree_root ||
4892 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004893 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004894
Chris Mason54aa1f42007-06-22 14:16:25 -04004895 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004896 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004897no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004898 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004899 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004900 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004901}
4902
4903/*
4904 * this returns the key found in the dir entry in the location pointer.
4905 * If no dir entries were found, location->objectid is 0.
4906 */
4907static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4908 struct btrfs_key *location)
4909{
4910 const char *name = dentry->d_name.name;
4911 int namelen = dentry->d_name.len;
4912 struct btrfs_dir_item *di;
4913 struct btrfs_path *path;
4914 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004915 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004916
4917 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004918 if (!path)
4919 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004920
Li Zefan33345d012011-04-20 10:31:50 +08004921 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004922 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004923 if (IS_ERR(di))
4924 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004925
David Sterbac7040052011-04-19 18:00:01 +02004926 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004927 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004928
Chris Mason5f39d392007-10-15 16:14:19 -04004929 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004930out:
Chris Mason39279cc2007-06-12 06:35:45 -04004931 btrfs_free_path(path);
4932 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004933out_err:
4934 location->objectid = 0;
4935 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004936}
4937
4938/*
4939 * when we hit a tree root in a directory, the btrfs part of the inode
4940 * needs to be changed to reflect the root directory of the tree root. This
4941 * is kind of like crossing a mount point.
4942 */
4943static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004944 struct inode *dir,
4945 struct dentry *dentry,
4946 struct btrfs_key *location,
4947 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004948{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004949 struct btrfs_path *path;
4950 struct btrfs_root *new_root;
4951 struct btrfs_root_ref *ref;
4952 struct extent_buffer *leaf;
4953 int ret;
4954 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004955
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004956 path = btrfs_alloc_path();
4957 if (!path) {
4958 err = -ENOMEM;
4959 goto out;
4960 }
Chris Mason39279cc2007-06-12 06:35:45 -04004961
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004962 err = -ENOENT;
Kelley Nielsen75ac2dd2013-11-04 19:35:58 -08004963 ret = btrfs_find_item(root->fs_info->tree_root, path,
4964 BTRFS_I(dir)->root->root_key.objectid,
4965 location->objectid, BTRFS_ROOT_REF_KEY, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004966 if (ret) {
4967 if (ret < 0)
4968 err = ret;
4969 goto out;
4970 }
Chris Mason39279cc2007-06-12 06:35:45 -04004971
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004972 leaf = path->nodes[0];
4973 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004974 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004975 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4976 goto out;
4977
4978 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4979 (unsigned long)(ref + 1),
4980 dentry->d_name.len);
4981 if (ret)
4982 goto out;
4983
David Sterbab3b4aa72011-04-21 01:20:15 +02004984 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004985
4986 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4987 if (IS_ERR(new_root)) {
4988 err = PTR_ERR(new_root);
4989 goto out;
4990 }
4991
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004992 *sub_root = new_root;
4993 location->objectid = btrfs_root_dirid(&new_root->root_item);
4994 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004995 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004996 err = 0;
4997out:
4998 btrfs_free_path(path);
4999 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005000}
5001
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005002static void inode_tree_add(struct inode *inode)
5003{
5004 struct btrfs_root *root = BTRFS_I(inode)->root;
5005 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005006 struct rb_node **p;
5007 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005008 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005009 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005010
Al Viro1d3382cb2010-10-23 15:19:20 -04005011 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005012 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005013 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005014 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005015 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005016 while (*p) {
5017 parent = *p;
5018 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5019
Li Zefan33345d012011-04-20 10:31:50 +08005020 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005021 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005022 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005023 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005024 else {
5025 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005026 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005027 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005028 RB_CLEAR_NODE(parent);
5029 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005030 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005031 }
5032 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005033 rb_link_node(new, parent, p);
5034 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005035 spin_unlock(&root->inode_lock);
5036}
5037
5038static void inode_tree_del(struct inode *inode)
5039{
5040 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005041 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005042
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005043 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005044 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005045 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005046 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005047 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005048 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005049 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005050
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005051 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005052 synchronize_srcu(&root->fs_info->subvol_srcu);
5053 spin_lock(&root->inode_lock);
5054 empty = RB_EMPTY_ROOT(&root->inode_tree);
5055 spin_unlock(&root->inode_lock);
5056 if (empty)
5057 btrfs_add_dead_root(root);
5058 }
5059}
5060
Jeff Mahoney143bede2012-03-01 14:56:26 +01005061void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005062{
5063 struct rb_node *node;
5064 struct rb_node *prev;
5065 struct btrfs_inode *entry;
5066 struct inode *inode;
5067 u64 objectid = 0;
5068
Liu Bo7813b3d2014-02-10 17:37:25 +08005069 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5070 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005071
5072 spin_lock(&root->inode_lock);
5073again:
5074 node = root->inode_tree.rb_node;
5075 prev = NULL;
5076 while (node) {
5077 prev = node;
5078 entry = rb_entry(node, struct btrfs_inode, rb_node);
5079
Li Zefan33345d012011-04-20 10:31:50 +08005080 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005081 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005082 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005083 node = node->rb_right;
5084 else
5085 break;
5086 }
5087 if (!node) {
5088 while (prev) {
5089 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005090 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005091 node = prev;
5092 break;
5093 }
5094 prev = rb_next(prev);
5095 }
5096 }
5097 while (node) {
5098 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005099 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005100 inode = igrab(&entry->vfs_inode);
5101 if (inode) {
5102 spin_unlock(&root->inode_lock);
5103 if (atomic_read(&inode->i_count) > 1)
5104 d_prune_aliases(inode);
5105 /*
Al Viro45321ac2010-06-07 13:43:19 -04005106 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005107 * the inode cache when its usage count
5108 * hits zero.
5109 */
5110 iput(inode);
5111 cond_resched();
5112 spin_lock(&root->inode_lock);
5113 goto again;
5114 }
5115
5116 if (cond_resched_lock(&root->inode_lock))
5117 goto again;
5118
5119 node = rb_next(node);
5120 }
5121 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005122}
5123
Chris Masone02119d2008-09-05 16:13:11 -04005124static int btrfs_init_locked_inode(struct inode *inode, void *p)
5125{
5126 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005127 inode->i_ino = args->location->objectid;
5128 memcpy(&BTRFS_I(inode)->location, args->location,
5129 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005130 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005131 return 0;
5132}
5133
5134static int btrfs_find_actor(struct inode *inode, void *opaque)
5135{
5136 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005137 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005138 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005139}
5140
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005141static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005142 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005143 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005144{
5145 struct inode *inode;
5146 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005147 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005148
Chris Mason90d3e592014-01-09 17:28:00 -08005149 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005150 args.root = root;
5151
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005152 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005153 btrfs_init_locked_inode,
5154 (void *)&args);
5155 return inode;
5156}
5157
Balaji Rao1a54ef82008-07-21 02:01:04 +05305158/* Get an inode object given its location and corresponding root.
5159 * Returns in *is_new if the inode was read from disk
5160 */
5161struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005162 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305163{
5164 struct inode *inode;
5165
Chris Mason90d3e592014-01-09 17:28:00 -08005166 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305167 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005168 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305169
5170 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305171 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005172 if (!is_bad_inode(inode)) {
5173 inode_tree_add(inode);
5174 unlock_new_inode(inode);
5175 if (new)
5176 *new = 1;
5177 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005178 unlock_new_inode(inode);
5179 iput(inode);
5180 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005181 }
5182 }
5183
Balaji Rao1a54ef82008-07-21 02:01:04 +05305184 return inode;
5185}
5186
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005187static struct inode *new_simple_dir(struct super_block *s,
5188 struct btrfs_key *key,
5189 struct btrfs_root *root)
5190{
5191 struct inode *inode = new_inode(s);
5192
5193 if (!inode)
5194 return ERR_PTR(-ENOMEM);
5195
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005196 BTRFS_I(inode)->root = root;
5197 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005198 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005199
5200 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005201 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005202 inode->i_fop = &simple_dir_operations;
5203 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5204 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5205
5206 return inode;
5207}
5208
Chris Mason3de45862008-11-17 21:02:50 -05005209struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005210{
Chris Masond3977122009-01-05 21:25:51 -05005211 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005212 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005213 struct btrfs_root *sub_root = root;
5214 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005215 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005216 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005217
5218 if (dentry->d_name.len > BTRFS_NAME_LEN)
5219 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005220
Jeff Layton39e3c952012-11-28 11:30:53 -05005221 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005222 if (ret < 0)
5223 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005224
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005225 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005226 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005227
5228 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005229 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005230 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005231 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005232
5233 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5234
Yan, Zheng76dda932009-09-21 16:00:26 -04005235 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005236 ret = fixup_tree_root_location(root, dir, dentry,
5237 &location, &sub_root);
5238 if (ret < 0) {
5239 if (ret != -ENOENT)
5240 inode = ERR_PTR(ret);
5241 else
5242 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5243 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005244 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005245 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005246 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5247
Julia Lawall34d19ba2011-01-24 19:55:19 +00005248 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005249 down_read(&root->fs_info->cleanup_work_sem);
5250 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005251 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005252 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005253 if (ret) {
5254 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005255 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005256 }
Filipe Manana9c3b3062014-07-31 14:41:07 +01005257 /*
5258 * If orphan cleanup did remove any orphans, it means the tree
5259 * was modified and therefore the commit root is not the same as
5260 * the current root anymore. This is a problem, because send
5261 * uses the commit root and therefore can see inode items that
5262 * don't exist in the current root anymore, and for example make
5263 * calls to btrfs_iget, which will do tree lookups based on the
5264 * current root and not on the commit root. Those lookups will
5265 * fail, returning a -ESTALE error, and making send fail with
5266 * that error. So make sure a send does not see any orphans we
5267 * have just removed, and that it will see the same inodes
5268 * regardless of whether a transaction commit happened before
5269 * it started (meaning that the commit root will be the same as
5270 * the current root) or not.
5271 */
5272 if (sub_root->node != sub_root->commit_root) {
5273 u64 sub_flags = btrfs_root_flags(&sub_root->root_item);
5274
5275 if (sub_flags & BTRFS_ROOT_SUBVOL_RDONLY) {
5276 struct extent_buffer *eb;
5277
5278 /*
5279 * Assert we can't have races between dentry
5280 * lookup called through the snapshot creation
5281 * ioctl and the VFS.
5282 */
5283 ASSERT(mutex_is_locked(&dir->i_mutex));
5284
5285 down_write(&root->fs_info->commit_root_sem);
5286 eb = sub_root->commit_root;
5287 sub_root->commit_root =
5288 btrfs_root_node(sub_root);
5289 up_write(&root->fs_info->commit_root_sem);
5290 free_extent_buffer(eb);
5291 }
5292 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005293 }
5294
Chris Mason3de45862008-11-17 21:02:50 -05005295 return inode;
5296}
5297
Nick Pigginfe15ce42011-01-07 17:49:23 +11005298static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005299{
5300 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005301 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005302
Li Zefan848cce02012-02-21 17:04:28 +08005303 if (!inode && !IS_ROOT(dentry))
5304 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005305
Li Zefan848cce02012-02-21 17:04:28 +08005306 if (inode) {
5307 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005308 if (btrfs_root_refs(&root->root_item) == 0)
5309 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005310
5311 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5312 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005313 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005314 return 0;
5315}
5316
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005317static void btrfs_dentry_release(struct dentry *dentry)
5318{
Daeseok Youn944a4512014-04-14 15:37:02 +09005319 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005320}
5321
Chris Mason3de45862008-11-17 21:02:50 -05005322static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005323 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005324{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005325 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005326
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005327 inode = btrfs_lookup_dentry(dir, dentry);
5328 if (IS_ERR(inode)) {
5329 if (PTR_ERR(inode) == -ENOENT)
5330 inode = NULL;
5331 else
5332 return ERR_CAST(inode);
5333 }
5334
Josef Bacik3a0dfa62014-02-14 13:43:48 -05005335 return d_materialise_unique(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005336}
5337
Miao Xie16cdcec2011-04-22 18:12:22 +08005338unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005339 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5340};
5341
Al Viro9cdda8d2013-05-22 16:48:09 -04005342static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005343{
Al Viro9cdda8d2013-05-22 16:48:09 -04005344 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005345 struct btrfs_root *root = BTRFS_I(inode)->root;
5346 struct btrfs_item *item;
5347 struct btrfs_dir_item *di;
5348 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005349 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005350 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005351 struct list_head ins_list;
5352 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005353 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005354 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005355 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005356 unsigned char d_type;
5357 int over = 0;
5358 u32 di_cur;
5359 u32 di_total;
5360 u32 di_len;
5361 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005362 char tmp_name[32];
5363 char *name_ptr;
5364 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005365 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005366
5367 /* FIXME, use a real flag for deciding about the key type */
5368 if (root->fs_info->tree_root == root)
5369 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005370
Al Viro9cdda8d2013-05-22 16:48:09 -04005371 if (!dir_emit_dots(file, ctx))
5372 return 0;
5373
David Woodhouse49593bf2008-08-17 17:08:36 +01005374 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005375 if (!path)
5376 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005377
Josef Bacik026fd312011-05-13 10:32:11 -04005378 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005379
Miao Xie16cdcec2011-04-22 18:12:22 +08005380 if (key_type == BTRFS_DIR_INDEX_KEY) {
5381 INIT_LIST_HEAD(&ins_list);
5382 INIT_LIST_HEAD(&del_list);
5383 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5384 }
5385
David Sterba962a2982014-06-04 18:41:45 +02005386 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005387 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005388 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005389
Chris Mason39279cc2007-06-12 06:35:45 -04005390 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5391 if (ret < 0)
5392 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005393
5394 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005395 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005396 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005397 if (slot >= btrfs_header_nritems(leaf)) {
5398 ret = btrfs_next_leaf(root, path);
5399 if (ret < 0)
5400 goto err;
5401 else if (ret > 0)
5402 break;
5403 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005404 }
Chris Mason3de45862008-11-17 21:02:50 -05005405
Ross Kirkdd3cc162013-09-16 15:58:09 +01005406 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005407 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5408
5409 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005410 break;
David Sterba962a2982014-06-04 18:41:45 +02005411 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005412 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005413 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005414 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005415 if (key_type == BTRFS_DIR_INDEX_KEY &&
5416 btrfs_should_delete_dir_index(&del_list,
5417 found_key.offset))
5418 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005419
Al Viro9cdda8d2013-05-22 16:48:09 -04005420 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005421 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005422
Chris Mason39279cc2007-06-12 06:35:45 -04005423 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5424 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005425 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005426
5427 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005428 struct btrfs_key location;
5429
Josef Bacik22a94d42011-03-16 16:47:17 -04005430 if (verify_dir_item(root, leaf, di))
5431 break;
5432
Chris Mason5f39d392007-10-15 16:14:19 -04005433 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005434 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005435 name_ptr = tmp_name;
5436 } else {
5437 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005438 if (!name_ptr) {
5439 ret = -ENOMEM;
5440 goto err;
5441 }
Chris Mason5f39d392007-10-15 16:14:19 -04005442 }
5443 read_extent_buffer(leaf, name_ptr,
5444 (unsigned long)(di + 1), name_len);
5445
5446 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5447 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005448
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005449
Chris Mason3de45862008-11-17 21:02:50 -05005450 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005451 * skip it.
5452 *
5453 * In contrast to old kernels, we insert the snapshot's
5454 * dir item and dir index after it has been created, so
5455 * we won't find a reference to our own snapshot. We
5456 * still keep the following code for backward
5457 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005458 */
5459 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5460 location.objectid == root->root_key.objectid) {
5461 over = 0;
5462 goto skip;
5463 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005464 over = !dir_emit(ctx, name_ptr, name_len,
5465 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005466
Chris Mason3de45862008-11-17 21:02:50 -05005467skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005468 if (name_ptr != tmp_name)
5469 kfree(name_ptr);
5470
Chris Mason39279cc2007-06-12 06:35:45 -04005471 if (over)
5472 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005473 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005474 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005475 di_cur += di_len;
5476 di = (struct btrfs_dir_item *)((char *)di + di_len);
5477 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005478next:
5479 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005480 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005481
Miao Xie16cdcec2011-04-22 18:12:22 +08005482 if (key_type == BTRFS_DIR_INDEX_KEY) {
5483 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005484 ctx->pos++;
5485 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005486 if (ret)
5487 goto nopos;
5488 }
5489
David Woodhouse49593bf2008-08-17 17:08:36 +01005490 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005491 ctx->pos++;
5492
5493 /*
5494 * Stop new entries from being returned after we return the last
5495 * entry.
5496 *
5497 * New directory entries are assigned a strictly increasing
5498 * offset. This means that new entries created during readdir
5499 * are *guaranteed* to be seen in the future by that readdir.
5500 * This has broken buggy programs which operate on names as
5501 * they're returned by readdir. Until we re-use freed offsets
5502 * we have this hack to stop new entries from being returned
5503 * under the assumption that they'll never reach this huge
5504 * offset.
5505 *
5506 * This is being careful not to overflow 32bit loff_t unless the
5507 * last entry requires it because doing so has broken 32bit apps
5508 * in the past.
5509 */
5510 if (key_type == BTRFS_DIR_INDEX_KEY) {
5511 if (ctx->pos >= INT_MAX)
5512 ctx->pos = LLONG_MAX;
5513 else
5514 ctx->pos = INT_MAX;
5515 }
Chris Mason39279cc2007-06-12 06:35:45 -04005516nopos:
5517 ret = 0;
5518err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005519 if (key_type == BTRFS_DIR_INDEX_KEY)
5520 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005521 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005522 return ret;
5523}
5524
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005525int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005526{
5527 struct btrfs_root *root = BTRFS_I(inode)->root;
5528 struct btrfs_trans_handle *trans;
5529 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005530 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005531
Josef Bacik72ac3c02012-05-23 14:13:11 -04005532 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005533 return 0;
5534
Liu Bo83eea1f2012-07-10 05:28:39 -06005535 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005536 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005537
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005538 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005539 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005540 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005541 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005542 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005543 if (IS_ERR(trans))
5544 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005545 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005546 }
5547 return ret;
5548}
5549
5550/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005551 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005552 * inode changes. But, it is most likely to find the inode in cache.
5553 * FIXME, needs more benchmarking...there are no reasons other than performance
5554 * to keep or drop this code.
5555 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005556static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005557{
5558 struct btrfs_root *root = BTRFS_I(inode)->root;
5559 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005560 int ret;
5561
Josef Bacik72ac3c02012-05-23 14:13:11 -04005562 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005563 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005564
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005565 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005566 if (IS_ERR(trans))
5567 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005568
5569 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005570 if (ret && ret == -ENOSPC) {
5571 /* whoops, lets try again with the full transaction */
5572 btrfs_end_transaction(trans, root);
5573 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005574 if (IS_ERR(trans))
5575 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005576
Chris Mason94b60442010-05-26 11:02:00 -04005577 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005578 }
Chris Mason39279cc2007-06-12 06:35:45 -04005579 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005580 if (BTRFS_I(inode)->delayed_node)
5581 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005582
5583 return ret;
5584}
5585
5586/*
5587 * This is a copy of file_update_time. We need this so we can return error on
5588 * ENOSPC for updating the inode in the case of file write and mmap writes.
5589 */
Josef Bacike41f9412012-03-26 09:46:47 -04005590static int btrfs_update_time(struct inode *inode, struct timespec *now,
5591 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005592{
Alexander Block2bc5565282012-06-15 09:49:33 +02005593 struct btrfs_root *root = BTRFS_I(inode)->root;
5594
5595 if (btrfs_root_readonly(root))
5596 return -EROFS;
5597
Josef Bacike41f9412012-03-26 09:46:47 -04005598 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005599 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005600 if (flags & S_CTIME)
5601 inode->i_ctime = *now;
5602 if (flags & S_MTIME)
5603 inode->i_mtime = *now;
5604 if (flags & S_ATIME)
5605 inode->i_atime = *now;
5606 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005607}
5608
Chris Masond352ac62008-09-29 15:18:18 -04005609/*
5610 * find the highest existing sequence number in a directory
5611 * and then set the in-memory index_cnt variable to reflect
5612 * free sequence numbers
5613 */
Josef Bacikaec74772008-07-24 12:12:38 -04005614static int btrfs_set_inode_index_count(struct inode *inode)
5615{
5616 struct btrfs_root *root = BTRFS_I(inode)->root;
5617 struct btrfs_key key, found_key;
5618 struct btrfs_path *path;
5619 struct extent_buffer *leaf;
5620 int ret;
5621
Li Zefan33345d012011-04-20 10:31:50 +08005622 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005623 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005624 key.offset = (u64)-1;
5625
5626 path = btrfs_alloc_path();
5627 if (!path)
5628 return -ENOMEM;
5629
5630 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5631 if (ret < 0)
5632 goto out;
5633 /* FIXME: we should be able to handle this */
5634 if (ret == 0)
5635 goto out;
5636 ret = 0;
5637
5638 /*
5639 * MAGIC NUMBER EXPLANATION:
5640 * since we search a directory based on f_pos we have to start at 2
5641 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5642 * else has to start at 2
5643 */
5644 if (path->slots[0] == 0) {
5645 BTRFS_I(inode)->index_cnt = 2;
5646 goto out;
5647 }
5648
5649 path->slots[0]--;
5650
5651 leaf = path->nodes[0];
5652 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5653
Li Zefan33345d012011-04-20 10:31:50 +08005654 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005655 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04005656 BTRFS_I(inode)->index_cnt = 2;
5657 goto out;
5658 }
5659
5660 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5661out:
5662 btrfs_free_path(path);
5663 return ret;
5664}
5665
Chris Masond352ac62008-09-29 15:18:18 -04005666/*
5667 * helper to find a free sequence number in a given directory. This current
5668 * code is very simple, later versions will do smarter things in the btree
5669 */
Chris Mason3de45862008-11-17 21:02:50 -05005670int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005671{
5672 int ret = 0;
5673
5674 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005675 ret = btrfs_inode_delayed_dir_index_count(dir);
5676 if (ret) {
5677 ret = btrfs_set_inode_index_count(dir);
5678 if (ret)
5679 return ret;
5680 }
Josef Bacikaec74772008-07-24 12:12:38 -04005681 }
5682
Chris Mason00e4e6b2008-08-05 11:18:09 -04005683 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005684 BTRFS_I(dir)->index_cnt++;
5685
5686 return ret;
5687}
5688
Chris Masonb0d5d102014-09-08 13:08:51 -07005689static int btrfs_insert_inode_locked(struct inode *inode)
5690{
5691 struct btrfs_iget_args args;
5692 args.location = &BTRFS_I(inode)->location;
5693 args.root = BTRFS_I(inode)->root;
5694
5695 return insert_inode_locked4(inode,
5696 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5697 btrfs_find_actor, &args);
5698}
5699
Chris Mason39279cc2007-06-12 06:35:45 -04005700static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5701 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005702 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005703 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005704 u64 ref_objectid, u64 objectid,
5705 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005706{
5707 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005708 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005709 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005710 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005711 struct btrfs_inode_ref *ref;
5712 struct btrfs_key key[2];
5713 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005714 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005715 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005716 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005717
Chris Mason5f39d392007-10-15 16:14:19 -04005718 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005719 if (!path)
5720 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005721
Chris Mason39279cc2007-06-12 06:35:45 -04005722 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005723 if (!inode) {
5724 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005725 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005726 }
Chris Mason39279cc2007-06-12 06:35:45 -04005727
Li Zefan581bb052011-04-20 10:06:11 +08005728 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005729 * O_TMPFILE, set link count to 0, so that after this point,
5730 * we fill in an inode item with the correct link count.
5731 */
5732 if (!name)
5733 set_nlink(inode, 0);
5734
5735 /*
Li Zefan581bb052011-04-20 10:06:11 +08005736 * we have to initialize this early, so we can reclaim the inode
5737 * number if we fail afterwards in this function.
5738 */
5739 inode->i_ino = objectid;
5740
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005741 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005742 trace_btrfs_inode_request(dir);
5743
Chris Mason3de45862008-11-17 21:02:50 -05005744 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005745 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005746 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005747 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005748 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005749 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005750 } else if (dir) {
5751 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005752 }
5753 /*
5754 * index_cnt is ignored for everything but a dir,
5755 * btrfs_get_inode_index_count has an explanation for the magic
5756 * number
5757 */
5758 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005759 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005760 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005761 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005762 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005763
Josef Bacik5dc562c2012-08-17 13:14:17 -04005764 /*
5765 * We could have gotten an inode number from somebody who was fsynced
5766 * and then removed in this same transaction, so let's just set full
5767 * sync since it will be a full sync anyway and this will blow away the
5768 * old info in the log.
5769 */
5770 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5771
Chris Mason9c583092008-01-29 15:15:18 -05005772 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005773 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005774 key[0].offset = 0;
5775
Chris Mason9c583092008-01-29 15:15:18 -05005776 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005777
5778 if (name) {
5779 /*
5780 * Start new inodes with an inode_ref. This is slightly more
5781 * efficient for small numbers of hard links since they will
5782 * be packed into one item. Extended refs will kick in if we
5783 * add more hard links than can fit in the ref item.
5784 */
5785 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005786 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005787 key[1].offset = ref_objectid;
5788
5789 sizes[1] = name_len + sizeof(*ref);
5790 }
Chris Mason9c583092008-01-29 15:15:18 -05005791
Chris Masonb0d5d102014-09-08 13:08:51 -07005792 location = &BTRFS_I(inode)->location;
5793 location->objectid = objectid;
5794 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02005795 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07005796
5797 ret = btrfs_insert_inode_locked(inode);
5798 if (ret < 0)
5799 goto fail;
5800
Chris Masonb9473432009-03-13 11:00:37 -04005801 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005802 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005803 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07005804 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04005805
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005806 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005807 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005808 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005809 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5810 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005811 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5812 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005813 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005814
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005815 if (name) {
5816 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5817 struct btrfs_inode_ref);
5818 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5819 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5820 ptr = (unsigned long)(ref + 1);
5821 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5822 }
Chris Mason9c583092008-01-29 15:15:18 -05005823
Chris Mason5f39d392007-10-15 16:14:19 -04005824 btrfs_mark_buffer_dirty(path->nodes[0]);
5825 btrfs_free_path(path);
5826
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005827 btrfs_inherit_iflags(inode, dir);
5828
Al Viro569254b2011-07-24 17:08:40 -04005829 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005830 if (btrfs_test_opt(root, NODATASUM))
5831 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005832 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005833 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5834 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005835 }
5836
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005837 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005838
5839 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005840 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005841
Alexander Block8ea05e32012-07-25 17:35:53 +02005842 btrfs_update_root_times(trans, root);
5843
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005844 ret = btrfs_inode_inherit_props(trans, inode, dir);
5845 if (ret)
5846 btrfs_err(root->fs_info,
5847 "error inheriting props for ino %llu (root %llu): %d",
5848 btrfs_ino(inode), root->root_key.objectid, ret);
5849
Chris Mason39279cc2007-06-12 06:35:45 -04005850 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07005851
5852fail_unlock:
5853 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005854fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005855 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04005856 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005857 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005858 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005859 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005860}
5861
5862static inline u8 btrfs_inode_type(struct inode *inode)
5863{
5864 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5865}
5866
Chris Masond352ac62008-09-29 15:18:18 -04005867/*
5868 * utility function to add 'inode' into 'parent_inode' with
5869 * a give name and a given sequence number.
5870 * if 'add_backref' is true, also insert a backref from the
5871 * inode to the parent directory.
5872 */
Chris Masone02119d2008-09-05 16:13:11 -04005873int btrfs_add_link(struct btrfs_trans_handle *trans,
5874 struct inode *parent_inode, struct inode *inode,
5875 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005876{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005877 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005878 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005879 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005880 u64 ino = btrfs_ino(inode);
5881 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005882
Li Zefan33345d012011-04-20 10:31:50 +08005883 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005884 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5885 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005886 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02005887 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005888 key.offset = 0;
5889 }
Chris Mason39279cc2007-06-12 06:35:45 -04005890
Li Zefan33345d012011-04-20 10:31:50 +08005891 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005892 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5893 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005894 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005895 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005896 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5897 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005898 }
5899
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005900 /* Nothing to clean up yet */
5901 if (ret)
5902 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005903
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005904 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5905 parent_inode, &key,
5906 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005907 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005908 goto fail_dir_item;
5909 else if (ret) {
5910 btrfs_abort_transaction(trans, root, ret);
5911 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005912 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005913
5914 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5915 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005916 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005917 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5918 ret = btrfs_update_inode(trans, root, parent_inode);
5919 if (ret)
5920 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005921 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005922
5923fail_dir_item:
5924 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5925 u64 local_index;
5926 int err;
5927 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5928 key.objectid, root->root_key.objectid,
5929 parent_ino, &local_index, name, name_len);
5930
5931 } else if (add_backref) {
5932 u64 local_index;
5933 int err;
5934
5935 err = btrfs_del_inode_ref(trans, root, name, name_len,
5936 ino, parent_ino, &local_index);
5937 }
5938 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005939}
5940
5941static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005942 struct inode *dir, struct dentry *dentry,
5943 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005944{
Josef Bacika1b075d2010-11-19 20:36:11 +00005945 int err = btrfs_add_link(trans, dir, inode,
5946 dentry->d_name.name, dentry->d_name.len,
5947 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005948 if (err > 0)
5949 err = -EEXIST;
5950 return err;
5951}
5952
Josef Bacik618e21d2007-07-11 10:18:17 -04005953static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005954 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005955{
5956 struct btrfs_trans_handle *trans;
5957 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005958 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005959 int err;
5960 int drop_inode = 0;
5961 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005962 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005963
5964 if (!new_valid_dev(rdev))
5965 return -EINVAL;
5966
Josef Bacik9ed74f22009-09-11 16:12:44 -04005967 /*
5968 * 2 for inode item and ref
5969 * 2 for dir items
5970 * 1 for xattr if selinux is on
5971 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005972 trans = btrfs_start_transaction(root, 5);
5973 if (IS_ERR(trans))
5974 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005975
Li Zefan581bb052011-04-20 10:06:11 +08005976 err = btrfs_find_free_ino(root, &objectid);
5977 if (err)
5978 goto out_unlock;
5979
Josef Bacikaec74772008-07-24 12:12:38 -04005980 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005981 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005982 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005983 if (IS_ERR(inode)) {
5984 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005985 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005986 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005987
Casey Schauflerad19db72011-12-15 10:09:07 -05005988 /*
5989 * If the active LSM wants to access the inode during
5990 * d_instantiate it needs these. Smack checks to see
5991 * if the filesystem supports xattrs by looking at the
5992 * ops vector.
5993 */
Casey Schauflerad19db72011-12-15 10:09:07 -05005994 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07005995 init_special_inode(inode, inode->i_mode, rdev);
5996
5997 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04005998 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07005999 goto out_unlock_inode;
6000
6001 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6002 if (err) {
6003 goto out_unlock_inode;
6004 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006005 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006006 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006007 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006008 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006009
Josef Bacik618e21d2007-07-11 10:18:17 -04006010out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006011 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006012 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006013 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006014 if (drop_inode) {
6015 inode_dec_link_count(inode);
6016 iput(inode);
6017 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006018 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006019
6020out_unlock_inode:
6021 drop_inode = 1;
6022 unlock_new_inode(inode);
6023 goto out_unlock;
6024
Josef Bacik618e21d2007-07-11 10:18:17 -04006025}
6026
Chris Mason39279cc2007-06-12 06:35:45 -04006027static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006028 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006029{
6030 struct btrfs_trans_handle *trans;
6031 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006032 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006033 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006034 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006035 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006036 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006037
Josef Bacik9ed74f22009-09-11 16:12:44 -04006038 /*
6039 * 2 for inode item and ref
6040 * 2 for dir items
6041 * 1 for xattr if selinux is on
6042 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006043 trans = btrfs_start_transaction(root, 5);
6044 if (IS_ERR(trans))
6045 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006046
Li Zefan581bb052011-04-20 10:06:11 +08006047 err = btrfs_find_free_ino(root, &objectid);
6048 if (err)
6049 goto out_unlock;
6050
Josef Bacikaec74772008-07-24 12:12:38 -04006051 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006052 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006053 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006054 if (IS_ERR(inode)) {
6055 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006056 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006057 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006058 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006059 /*
6060 * If the active LSM wants to access the inode during
6061 * d_instantiate it needs these. Smack checks to see
6062 * if the filesystem supports xattrs by looking at the
6063 * ops vector.
6064 */
6065 inode->i_fop = &btrfs_file_operations;
6066 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006067 inode->i_mapping->a_ops = &btrfs_aops;
6068 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
6069
6070 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6071 if (err)
6072 goto out_unlock_inode;
6073
6074 err = btrfs_update_inode(trans, root, inode);
6075 if (err)
6076 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006077
Josef Bacika1b075d2010-11-19 20:36:11 +00006078 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006079 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006080 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006081
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006082 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006083 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006084 d_instantiate(dentry, inode);
6085
Chris Mason39279cc2007-06-12 06:35:45 -04006086out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006087 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006088 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006089 inode_dec_link_count(inode);
6090 iput(inode);
6091 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006092 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006093 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006094 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006095
6096out_unlock_inode:
6097 unlock_new_inode(inode);
6098 goto out_unlock;
6099
Chris Mason39279cc2007-06-12 06:35:45 -04006100}
6101
6102static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6103 struct dentry *dentry)
6104{
6105 struct btrfs_trans_handle *trans;
6106 struct btrfs_root *root = BTRFS_I(dir)->root;
6107 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006108 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006109 int err;
6110 int drop_inode = 0;
6111
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006112 /* do not allow sys_link's with other subvols of the same device */
6113 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006114 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006115
Mark Fashehf1863732012-08-08 11:32:27 -07006116 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006117 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006118
Chris Mason3de45862008-11-17 21:02:50 -05006119 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006120 if (err)
6121 goto fail;
6122
Yan, Zhenga22285a2010-05-16 10:48:46 -04006123 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006124 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006125 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006126 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006127 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006128 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006129 if (IS_ERR(trans)) {
6130 err = PTR_ERR(trans);
6131 goto fail;
6132 }
Chris Mason5f39d392007-10-15 16:14:19 -04006133
Miao Xie67de1172013-12-26 13:07:06 +08006134 /* There are several dir indexes for this inode, clear the cache. */
6135 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006136 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006137 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006138 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006139 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006140 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006141
Josef Bacika1b075d2010-11-19 20:36:11 +00006142 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006143
Yan, Zhenga5719522009-09-24 09:17:31 -04006144 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006145 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006146 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006147 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006148 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006149 if (err)
6150 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006151 if (inode->i_nlink == 1) {
6152 /*
6153 * If new hard link count is 1, it's a file created
6154 * with open(2) O_TMPFILE flag.
6155 */
6156 err = btrfs_orphan_del(trans, inode);
6157 if (err)
6158 goto fail;
6159 }
Al Viro08c422c2011-12-23 07:58:13 -05006160 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006161 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006162 }
Chris Mason39279cc2007-06-12 06:35:45 -04006163
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006164 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006165 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006166fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006167 if (drop_inode) {
6168 inode_dec_link_count(inode);
6169 iput(inode);
6170 }
Liu Bob53d3f52012-11-14 14:34:34 +00006171 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006172 return err;
6173}
6174
Al Viro18bb1db2011-07-26 01:41:39 -04006175static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006176{
Chris Masonb9d86662008-05-02 16:13:49 -04006177 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006178 struct btrfs_trans_handle *trans;
6179 struct btrfs_root *root = BTRFS_I(dir)->root;
6180 int err = 0;
6181 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006182 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006183 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006184
Josef Bacik9ed74f22009-09-11 16:12:44 -04006185 /*
6186 * 2 items for inode and ref
6187 * 2 items for dir items
6188 * 1 for xattr if selinux is on
6189 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006190 trans = btrfs_start_transaction(root, 5);
6191 if (IS_ERR(trans))
6192 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006193
Li Zefan581bb052011-04-20 10:06:11 +08006194 err = btrfs_find_free_ino(root, &objectid);
6195 if (err)
6196 goto out_fail;
6197
Josef Bacikaec74772008-07-24 12:12:38 -04006198 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006199 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006200 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006201 if (IS_ERR(inode)) {
6202 err = PTR_ERR(inode);
6203 goto out_fail;
6204 }
Chris Mason5f39d392007-10-15 16:14:19 -04006205
Chris Mason39279cc2007-06-12 06:35:45 -04006206 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006207 /* these must be set before we unlock the inode */
6208 inode->i_op = &btrfs_dir_inode_operations;
6209 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006210
Eric Paris2a7dba32011-02-01 11:05:39 -05006211 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006212 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006213 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006214
Chris Masondbe674a2008-07-17 12:54:05 -04006215 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006216 err = btrfs_update_inode(trans, root, inode);
6217 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006218 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006219
Josef Bacika1b075d2010-11-19 20:36:11 +00006220 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6221 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006222 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006223 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006224
Chris Mason39279cc2007-06-12 06:35:45 -04006225 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006226 /*
6227 * mkdir is special. We're unlocking after we call d_instantiate
6228 * to avoid a race with nfsd calling d_instantiate.
6229 */
6230 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006231 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006232
6233out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006234 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006235 if (drop_on_err)
6236 iput(inode);
Josef Bacikc581afc2014-02-06 16:06:06 -05006237 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006238 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006239 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006240
6241out_fail_inode:
6242 unlock_new_inode(inode);
6243 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006244}
6245
Chris Masond352ac62008-09-29 15:18:18 -04006246/* helper for btfs_get_extent. Given an existing extent in the tree,
6247 * and an extent that you want to insert, deal with overlap and insert
6248 * the new extent into the tree.
6249 */
Chris Mason3b951512008-04-17 11:29:12 -04006250static int merge_extent_mapping(struct extent_map_tree *em_tree,
6251 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006252 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006253 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006254{
6255 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006256
Chris Masone6dcd2d2008-07-17 12:53:50 -04006257 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
6258 start_diff = map_start - em->start;
6259 em->start = map_start;
Qu Wenruo51f395a2014-08-08 13:06:20 +08006260 em->len = existing->start - em->start;
Chris Masonc8b97812008-10-29 14:49:59 -04006261 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6262 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006263 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006264 em->block_len -= start_diff;
6265 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006266 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006267}
6268
Chris Masonc8b97812008-10-29 14:49:59 -04006269static noinline int uncompress_inline(struct btrfs_path *path,
6270 struct inode *inode, struct page *page,
6271 size_t pg_offset, u64 extent_offset,
6272 struct btrfs_file_extent_item *item)
6273{
6274 int ret;
6275 struct extent_buffer *leaf = path->nodes[0];
6276 char *tmp;
6277 size_t max_size;
6278 unsigned long inline_size;
6279 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006280 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006281
6282 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006283 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006284 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6285 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006286 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006287 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006288 if (!tmp)
6289 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006290 ptr = btrfs_file_extent_inline_start(item);
6291
6292 read_extent_buffer(leaf, tmp, ptr, inline_size);
6293
Chris Mason5b050f02008-11-11 09:34:41 -05006294 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006295 ret = btrfs_decompress(compress_type, tmp, page,
6296 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006297 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006298 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006299}
6300
Chris Masond352ac62008-09-29 15:18:18 -04006301/*
6302 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006303 * the ugly parts come from merging extents from the disk with the in-ram
6304 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006305 * where the in-ram extents might be locked pending data=ordered completion.
6306 *
6307 * This also copies inline extents directly into the page.
6308 */
Chris Masond3977122009-01-05 21:25:51 -05006309
Chris Masona52d9a82007-08-27 16:49:44 -04006310struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006311 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006312 int create)
6313{
6314 int ret;
6315 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006316 u64 extent_start = 0;
6317 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006318 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006319 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006320 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006321 struct btrfs_root *root = BTRFS_I(inode)->root;
6322 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006323 struct extent_buffer *leaf;
6324 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006325 struct extent_map *em = NULL;
6326 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006327 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006328 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006329 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006330
Chris Masona52d9a82007-08-27 16:49:44 -04006331again:
Chris Mason890871b2009-09-02 16:24:52 -04006332 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006333 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006334 if (em)
6335 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006336 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006337
Chris Masona52d9a82007-08-27 16:49:44 -04006338 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006339 if (em->start > start || em->start + em->len <= start)
6340 free_extent_map(em);
6341 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006342 free_extent_map(em);
6343 else
6344 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006345 }
David Sterba172ddd62011-04-21 00:48:27 +02006346 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006347 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006348 err = -ENOMEM;
6349 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006350 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006351 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006352 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006353 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006354 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006355 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006356
6357 if (!path) {
6358 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006359 if (!path) {
6360 err = -ENOMEM;
6361 goto out;
6362 }
6363 /*
6364 * Chances are we'll be called again, so go ahead and do
6365 * readahead
6366 */
6367 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006368 }
6369
Chris Mason179e29e2007-11-01 11:28:41 -04006370 ret = btrfs_lookup_file_extent(trans, root, path,
6371 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006372 if (ret < 0) {
6373 err = ret;
6374 goto out;
6375 }
6376
6377 if (ret != 0) {
6378 if (path->slots[0] == 0)
6379 goto not_found;
6380 path->slots[0]--;
6381 }
6382
Chris Mason5f39d392007-10-15 16:14:19 -04006383 leaf = path->nodes[0];
6384 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006385 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006386 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006387 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006388 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006389 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006390 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006391 /*
6392 * If we backup past the first extent we want to move forward
6393 * and see if there is an extent in front of us, otherwise we'll
6394 * say there is a hole for our whole search range which can
6395 * cause problems.
6396 */
6397 extent_end = start;
6398 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006399 }
6400
Chris Mason5f39d392007-10-15 16:14:19 -04006401 found_type = btrfs_file_extent_type(leaf, item);
6402 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006403 if (found_type == BTRFS_FILE_EXTENT_REG ||
6404 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006405 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006406 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006407 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6408 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006409 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006410 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006411 }
Josef Bacik25a50342013-10-14 12:08:38 -04006412next:
Yan Zheng9036c102008-10-30 14:19:41 -04006413 if (start >= extent_end) {
6414 path->slots[0]++;
6415 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6416 ret = btrfs_next_leaf(root, path);
6417 if (ret < 0) {
6418 err = ret;
6419 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006420 }
Yan Zheng9036c102008-10-30 14:19:41 -04006421 if (ret > 0)
6422 goto not_found;
6423 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006424 }
Yan Zheng9036c102008-10-30 14:19:41 -04006425 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6426 if (found_key.objectid != objectid ||
6427 found_key.type != BTRFS_EXTENT_DATA_KEY)
6428 goto not_found;
6429 if (start + len <= found_key.offset)
6430 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006431 if (start > found_key.offset)
6432 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006433 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006434 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006435 em->len = found_key.offset - start;
6436 goto not_found_em;
6437 }
6438
Filipe Manana7ffbb592014-06-09 03:48:05 +01006439 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6440
Yan Zhengd899e052008-10-30 14:25:28 -04006441 if (found_type == BTRFS_FILE_EXTENT_REG ||
6442 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006443 goto insert;
6444 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006445 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006446 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006447 size_t size;
6448 size_t extent_offset;
6449 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006450
Filipe Manana7ffbb592014-06-09 03:48:05 +01006451 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006452 goto out;
Yan689f9342007-10-29 11:41:07 -04006453
Chris Mason514ac8a2014-01-03 21:07:00 -08006454 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006455 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006456 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006457 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006458 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006459 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006460 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006461 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006462 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006463 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006464 if (btrfs_file_extent_compression(leaf, item) !=
6465 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006466 ret = uncompress_inline(path, inode, page,
6467 pg_offset,
6468 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006469 if (ret) {
6470 err = ret;
6471 goto out;
6472 }
Chris Masonc8b97812008-10-29 14:49:59 -04006473 } else {
6474 map = kmap(page);
6475 read_extent_buffer(leaf, map + pg_offset, ptr,
6476 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006477 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6478 memset(map + pg_offset + copy_size, 0,
6479 PAGE_CACHE_SIZE - pg_offset -
6480 copy_size);
6481 }
Chris Masonc8b97812008-10-29 14:49:59 -04006482 kunmap(page);
6483 }
Chris Mason179e29e2007-11-01 11:28:41 -04006484 flush_dcache_page(page);
6485 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006486 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006487 if (!trans) {
6488 kunmap(page);
6489 free_extent_map(em);
6490 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006491
David Sterbab3b4aa72011-04-21 01:20:15 +02006492 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006493 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006494
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006495 if (IS_ERR(trans))
6496 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006497 goto again;
6498 }
Chris Masonc8b97812008-10-29 14:49:59 -04006499 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006500 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006501 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006502 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006503 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006504 }
Chris Masond1310b22008-01-24 16:13:08 -05006505 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006506 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006507 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006508 }
6509not_found:
6510 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006511 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006512 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006513not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006514 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006515 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006516insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006517 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006518 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006519 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006520 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006521 err = -EIO;
6522 goto out;
6523 }
Chris Masond1310b22008-01-24 16:13:08 -05006524
6525 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006526 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006527 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006528 /* it is possible that someone inserted the extent into the tree
6529 * while we had the lock dropped. It is also possible that
6530 * an overlapping map exists in the tree
6531 */
Chris Masona52d9a82007-08-27 16:49:44 -04006532 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006533 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006534
6535 ret = 0;
6536
Chris Mason3b951512008-04-17 11:29:12 -04006537 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006538 if (existing && (existing->start > start ||
6539 existing->start + existing->len <= start)) {
6540 free_extent_map(existing);
6541 existing = NULL;
6542 }
Chris Mason3b951512008-04-17 11:29:12 -04006543 if (!existing) {
6544 existing = lookup_extent_mapping(em_tree, em->start,
6545 em->len);
6546 if (existing) {
6547 err = merge_extent_mapping(em_tree, existing,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006548 em, start);
Chris Mason3b951512008-04-17 11:29:12 -04006549 free_extent_map(existing);
6550 if (err) {
6551 free_extent_map(em);
6552 em = NULL;
6553 }
6554 } else {
6555 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006556 free_extent_map(em);
6557 em = NULL;
6558 }
6559 } else {
6560 free_extent_map(em);
6561 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006562 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006563 }
Chris Masona52d9a82007-08-27 16:49:44 -04006564 }
Chris Mason890871b2009-09-02 16:24:52 -04006565 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006566out:
liubo1abe9b82011-03-24 11:18:59 +00006567
Steven Rostedt4cd85872013-11-14 22:57:29 -05006568 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006569
Chris Masonf4219502008-07-22 11:18:09 -04006570 if (path)
6571 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006572 if (trans) {
6573 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006574 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006575 err = ret;
6576 }
Chris Masona52d9a82007-08-27 16:49:44 -04006577 if (err) {
6578 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006579 return ERR_PTR(err);
6580 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006581 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006582 return em;
6583}
6584
Chris Masonec29ed52011-02-23 16:23:20 -05006585struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6586 size_t pg_offset, u64 start, u64 len,
6587 int create)
6588{
6589 struct extent_map *em;
6590 struct extent_map *hole_em = NULL;
6591 u64 range_start = start;
6592 u64 end;
6593 u64 found;
6594 u64 found_end;
6595 int err = 0;
6596
6597 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6598 if (IS_ERR(em))
6599 return em;
6600 if (em) {
6601 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006602 * if our em maps to
6603 * - a hole or
6604 * - a pre-alloc extent,
6605 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006606 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006607 if (em->block_start != EXTENT_MAP_HOLE &&
6608 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006609 return em;
6610 else
6611 hole_em = em;
6612 }
6613
6614 /* check to see if we've wrapped (len == -1 or similar) */
6615 end = start + len;
6616 if (end < start)
6617 end = (u64)-1;
6618 else
6619 end -= 1;
6620
6621 em = NULL;
6622
6623 /* ok, we didn't find anything, lets look for delalloc */
6624 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6625 end, len, EXTENT_DELALLOC, 1);
6626 found_end = range_start + found;
6627 if (found_end < range_start)
6628 found_end = (u64)-1;
6629
6630 /*
6631 * we didn't find anything useful, return
6632 * the original results from get_extent()
6633 */
6634 if (range_start > end || found_end <= start) {
6635 em = hole_em;
6636 hole_em = NULL;
6637 goto out;
6638 }
6639
6640 /* adjust the range_start to make sure it doesn't
6641 * go backwards from the start they passed in
6642 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306643 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006644 found = found_end - range_start;
6645
6646 if (found > 0) {
6647 u64 hole_start = start;
6648 u64 hole_len = len;
6649
David Sterba172ddd62011-04-21 00:48:27 +02006650 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006651 if (!em) {
6652 err = -ENOMEM;
6653 goto out;
6654 }
6655 /*
6656 * when btrfs_get_extent can't find anything it
6657 * returns one huge hole
6658 *
6659 * make sure what it found really fits our range, and
6660 * adjust to make sure it is based on the start from
6661 * the caller
6662 */
6663 if (hole_em) {
6664 u64 calc_end = extent_map_end(hole_em);
6665
6666 if (calc_end <= start || (hole_em->start > end)) {
6667 free_extent_map(hole_em);
6668 hole_em = NULL;
6669 } else {
6670 hole_start = max(hole_em->start, start);
6671 hole_len = calc_end - hole_start;
6672 }
6673 }
6674 em->bdev = NULL;
6675 if (hole_em && range_start > hole_start) {
6676 /* our hole starts before our delalloc, so we
6677 * have to return just the parts of the hole
6678 * that go until the delalloc starts
6679 */
6680 em->len = min(hole_len,
6681 range_start - hole_start);
6682 em->start = hole_start;
6683 em->orig_start = hole_start;
6684 /*
6685 * don't adjust block start at all,
6686 * it is fixed at EXTENT_MAP_HOLE
6687 */
6688 em->block_start = hole_em->block_start;
6689 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006690 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6691 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006692 } else {
6693 em->start = range_start;
6694 em->len = found;
6695 em->orig_start = range_start;
6696 em->block_start = EXTENT_MAP_DELALLOC;
6697 em->block_len = found;
6698 }
6699 } else if (hole_em) {
6700 return hole_em;
6701 }
6702out:
6703
6704 free_extent_map(hole_em);
6705 if (err) {
6706 free_extent_map(em);
6707 return ERR_PTR(err);
6708 }
6709 return em;
6710}
6711
Josef Bacik4b46fce2010-05-23 11:00:55 -04006712static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6713 u64 start, u64 len)
6714{
6715 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006716 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006717 struct btrfs_key ins;
6718 u64 alloc_hint;
6719 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006720
Josef Bacik4b46fce2010-05-23 11:00:55 -04006721 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006722 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08006723 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006724 if (ret)
6725 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006726
Josef Bacik70c8a912012-10-11 16:54:30 -04006727 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006728 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006729 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08006730 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006731 return em;
6732 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006733
6734 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6735 ins.offset, ins.offset, 0);
6736 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08006737 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006738 free_extent_map(em);
6739 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006740 }
Josef Bacik00361582013-08-14 14:02:47 -04006741
Josef Bacik4b46fce2010-05-23 11:00:55 -04006742 return em;
6743}
6744
Chris Mason46bfbb52010-05-26 11:04:10 -04006745/*
6746 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6747 * block must be cow'd
6748 */
Josef Bacik00361582013-08-14 14:02:47 -04006749noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006750 u64 *orig_start, u64 *orig_block_len,
6751 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006752{
Josef Bacik00361582013-08-14 14:02:47 -04006753 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006754 struct btrfs_path *path;
6755 int ret;
6756 struct extent_buffer *leaf;
6757 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006758 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006759 struct btrfs_file_extent_item *fi;
6760 struct btrfs_key key;
6761 u64 disk_bytenr;
6762 u64 backref_offset;
6763 u64 extent_end;
6764 u64 num_bytes;
6765 int slot;
6766 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006767 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006768
Chris Mason46bfbb52010-05-26 11:04:10 -04006769 path = btrfs_alloc_path();
6770 if (!path)
6771 return -ENOMEM;
6772
Josef Bacik00361582013-08-14 14:02:47 -04006773 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006774 offset, 0);
6775 if (ret < 0)
6776 goto out;
6777
6778 slot = path->slots[0];
6779 if (ret == 1) {
6780 if (slot == 0) {
6781 /* can't find the item, must cow */
6782 ret = 0;
6783 goto out;
6784 }
6785 slot--;
6786 }
6787 ret = 0;
6788 leaf = path->nodes[0];
6789 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006790 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006791 key.type != BTRFS_EXTENT_DATA_KEY) {
6792 /* not our file or wrong item type, must cow */
6793 goto out;
6794 }
6795
6796 if (key.offset > offset) {
6797 /* Wrong offset, must cow */
6798 goto out;
6799 }
6800
6801 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6802 found_type = btrfs_file_extent_type(leaf, fi);
6803 if (found_type != BTRFS_FILE_EXTENT_REG &&
6804 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6805 /* not a regular extent, must cow */
6806 goto out;
6807 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006808
6809 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6810 goto out;
6811
Miao Xiee77751a2013-12-27 21:11:50 +08006812 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6813 if (extent_end <= offset)
6814 goto out;
6815
Chris Mason46bfbb52010-05-26 11:04:10 -04006816 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006817 if (disk_bytenr == 0)
6818 goto out;
6819
6820 if (btrfs_file_extent_compression(leaf, fi) ||
6821 btrfs_file_extent_encryption(leaf, fi) ||
6822 btrfs_file_extent_other_encoding(leaf, fi))
6823 goto out;
6824
Chris Mason46bfbb52010-05-26 11:04:10 -04006825 backref_offset = btrfs_file_extent_offset(leaf, fi);
6826
Josef Bacik7ee9e442013-06-21 16:37:03 -04006827 if (orig_start) {
6828 *orig_start = key.offset - backref_offset;
6829 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6830 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6831 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006832
Chris Mason46bfbb52010-05-26 11:04:10 -04006833 if (btrfs_extent_readonly(root, disk_bytenr))
6834 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006835
6836 num_bytes = min(offset + *len, extent_end) - offset;
6837 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6838 u64 range_end;
6839
6840 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
6841 ret = test_range_bit(io_tree, offset, range_end,
6842 EXTENT_DELALLOC, 0, NULL);
6843 if (ret) {
6844 ret = -EAGAIN;
6845 goto out;
6846 }
6847 }
6848
Josef Bacik1bda19e2013-10-18 12:10:36 -04006849 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006850
6851 /*
6852 * look for other files referencing this extent, if we
6853 * find any we must cow
6854 */
Josef Bacik00361582013-08-14 14:02:47 -04006855 trans = btrfs_join_transaction(root);
6856 if (IS_ERR(trans)) {
6857 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006858 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006859 }
6860
6861 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6862 key.offset - backref_offset, disk_bytenr);
6863 btrfs_end_transaction(trans, root);
6864 if (ret) {
6865 ret = 0;
6866 goto out;
6867 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006868
6869 /*
6870 * adjust disk_bytenr and num_bytes to cover just the bytes
6871 * in this extent we are about to write. If there
6872 * are any csums in that range we have to cow in order
6873 * to keep the csums correct
6874 */
6875 disk_bytenr += backref_offset;
6876 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006877 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6878 goto out;
6879 /*
6880 * all of the above have passed, it is safe to overwrite this extent
6881 * without cow
6882 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006883 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006884 ret = 1;
6885out:
6886 btrfs_free_path(path);
6887 return ret;
6888}
6889
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006890bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
6891{
6892 struct radix_tree_root *root = &inode->i_mapping->page_tree;
6893 int found = false;
6894 void **pagep = NULL;
6895 struct page *page = NULL;
6896 int start_idx;
6897 int end_idx;
6898
6899 start_idx = start >> PAGE_CACHE_SHIFT;
6900
6901 /*
6902 * end is the last byte in the last page. end == start is legal
6903 */
6904 end_idx = end >> PAGE_CACHE_SHIFT;
6905
6906 rcu_read_lock();
6907
6908 /* Most of the code in this while loop is lifted from
6909 * find_get_page. It's been modified to begin searching from a
6910 * page and return just the first page found in that range. If the
6911 * found idx is less than or equal to the end idx then we know that
6912 * a page exists. If no pages are found or if those pages are
6913 * outside of the range then we're fine (yay!) */
6914 while (page == NULL &&
6915 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
6916 page = radix_tree_deref_slot(pagep);
6917 if (unlikely(!page))
6918 break;
6919
6920 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01006921 if (radix_tree_deref_retry(page)) {
6922 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006923 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01006924 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006925 /*
6926 * Otherwise, shmem/tmpfs must be storing a swap entry
6927 * here as an exceptional entry: so return it without
6928 * attempting to raise page count.
6929 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01006930 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006931 break; /* TODO: Is this relevant for this use case? */
6932 }
6933
Filipe Manana91405152014-06-05 13:22:24 +01006934 if (!page_cache_get_speculative(page)) {
6935 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006936 continue;
Filipe Manana91405152014-06-05 13:22:24 +01006937 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006938
6939 /*
6940 * Has the page moved?
6941 * This is part of the lockless pagecache protocol. See
6942 * include/linux/pagemap.h for details.
6943 */
6944 if (unlikely(page != *pagep)) {
6945 page_cache_release(page);
6946 page = NULL;
6947 }
6948 }
6949
6950 if (page) {
6951 if (page->index <= end_idx)
6952 found = true;
6953 page_cache_release(page);
6954 }
6955
6956 rcu_read_unlock();
6957 return found;
6958}
6959
Josef Bacikeb838e72012-07-31 16:28:48 -04006960static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6961 struct extent_state **cached_state, int writing)
6962{
6963 struct btrfs_ordered_extent *ordered;
6964 int ret = 0;
6965
6966 while (1) {
6967 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6968 0, cached_state);
6969 /*
6970 * We're concerned with the entire range that we're going to be
6971 * doing DIO to, so we need to make sure theres no ordered
6972 * extents in this range.
6973 */
6974 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6975 lockend - lockstart + 1);
6976
6977 /*
6978 * We need to make sure there are no buffered pages in this
6979 * range either, we could have raced between the invalidate in
6980 * generic_file_direct_write and locking the extent. The
6981 * invalidate needs to happen so that reads after a write do not
6982 * get stale data.
6983 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006984 if (!ordered &&
6985 (!writing ||
6986 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04006987 break;
6988
6989 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6990 cached_state, GFP_NOFS);
6991
6992 if (ordered) {
6993 btrfs_start_ordered_extent(inode, ordered, 1);
6994 btrfs_put_ordered_extent(ordered);
6995 } else {
6996 /* Screw you mmap */
6997 ret = filemap_write_and_wait_range(inode->i_mapping,
6998 lockstart,
6999 lockend);
7000 if (ret)
7001 break;
7002
7003 /*
7004 * If we found a page that couldn't be invalidated just
7005 * fall back to buffered.
7006 */
7007 ret = invalidate_inode_pages2_range(inode->i_mapping,
7008 lockstart >> PAGE_CACHE_SHIFT,
7009 lockend >> PAGE_CACHE_SHIFT);
7010 if (ret)
7011 break;
7012 }
7013
7014 cond_resched();
7015 }
7016
7017 return ret;
7018}
7019
Josef Bacik69ffb542012-09-11 15:40:07 -04007020static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7021 u64 len, u64 orig_start,
7022 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007023 u64 orig_block_len, u64 ram_bytes,
7024 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007025{
7026 struct extent_map_tree *em_tree;
7027 struct extent_map *em;
7028 struct btrfs_root *root = BTRFS_I(inode)->root;
7029 int ret;
7030
7031 em_tree = &BTRFS_I(inode)->extent_tree;
7032 em = alloc_extent_map();
7033 if (!em)
7034 return ERR_PTR(-ENOMEM);
7035
7036 em->start = start;
7037 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007038 em->mod_start = start;
7039 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007040 em->len = len;
7041 em->block_len = block_len;
7042 em->block_start = block_start;
7043 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007044 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007045 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007046 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007047 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7048 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007049 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007050
7051 do {
7052 btrfs_drop_extent_cache(inode, em->start,
7053 em->start + em->len - 1, 0);
7054 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007055 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007056 write_unlock(&em_tree->lock);
7057 } while (ret == -EEXIST);
7058
7059 if (ret) {
7060 free_extent_map(em);
7061 return ERR_PTR(ret);
7062 }
7063
7064 return em;
7065}
7066
7067
Josef Bacik4b46fce2010-05-23 11:00:55 -04007068static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7069 struct buffer_head *bh_result, int create)
7070{
7071 struct extent_map *em;
7072 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007073 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007074 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007075 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007076 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007077 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007078 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007079
Miao Xie172a5042013-02-21 02:48:22 -07007080 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04007081 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007082 else
Josef Bacikc3298612012-08-03 16:49:19 -04007083 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007084
Josef Bacikc3298612012-08-03 16:49:19 -04007085 lockstart = start;
7086 lockend = start + len - 1;
7087
Josef Bacikeb838e72012-07-31 16:28:48 -04007088 /*
7089 * If this errors out it's because we couldn't invalidate pagecache for
7090 * this range and we need to fallback to buffered.
7091 */
7092 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7093 return -ENOTBLK;
7094
Josef Bacik4b46fce2010-05-23 11:00:55 -04007095 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007096 if (IS_ERR(em)) {
7097 ret = PTR_ERR(em);
7098 goto unlock_err;
7099 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007100
7101 /*
7102 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7103 * io. INLINE is special, and we could probably kludge it in here, but
7104 * it's still buffered so for safety lets just fall back to the generic
7105 * buffered path.
7106 *
7107 * For COMPRESSED we _have_ to read the entire extent in so we can
7108 * decompress it, so there will be buffering required no matter what we
7109 * do, so go ahead and fallback to buffered.
7110 *
7111 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7112 * to buffered IO. Don't blame me, this is the price we pay for using
7113 * the generic code.
7114 */
7115 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7116 em->block_start == EXTENT_MAP_INLINE) {
7117 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007118 ret = -ENOTBLK;
7119 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007120 }
7121
7122 /* Just a good old fashioned hole, return */
7123 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7124 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7125 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007126 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007127 }
7128
7129 /*
7130 * We don't allocate a new extent in the following cases
7131 *
7132 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7133 * existing extent.
7134 * 2) The extent is marked as PREALLOC. We're good to go here and can
7135 * just use the extent.
7136 *
7137 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007138 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007139 len = min(len, em->len - (start - em->start));
7140 lockstart = start + len;
7141 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007142 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007143
7144 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7145 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7146 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007147 int type;
7148 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04007149 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007150
7151 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7152 type = BTRFS_ORDERED_PREALLOC;
7153 else
7154 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007155 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007156 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007157
Josef Bacik00361582013-08-14 14:02:47 -04007158 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007159 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007160 if (type == BTRFS_ORDERED_PREALLOC) {
7161 free_extent_map(em);
7162 em = create_pinned_em(inode, start, len,
7163 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007164 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007165 orig_block_len,
7166 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007167 if (IS_ERR(em)) {
7168 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007169 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007170 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007171 }
7172
Chris Mason46bfbb52010-05-26 11:04:10 -04007173 ret = btrfs_add_ordered_extent_dio(inode, start,
7174 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007175 if (ret) {
7176 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007177 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007178 }
7179 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007180 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007181 }
Josef Bacik00361582013-08-14 14:02:47 -04007182
Chris Mason46bfbb52010-05-26 11:04:10 -04007183 /*
7184 * this will cow the extent, reset the len in case we changed
7185 * it above
7186 */
7187 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007188 free_extent_map(em);
7189 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007190 if (IS_ERR(em)) {
7191 ret = PTR_ERR(em);
7192 goto unlock_err;
7193 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007194 len = min(len, em->len - (start - em->start));
7195unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007196 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7197 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007198 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007199 bh_result->b_bdev = em->bdev;
7200 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007201 if (create) {
7202 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7203 set_buffer_new(bh_result);
7204
7205 /*
7206 * Need to update the i_size under the extent lock so buffered
7207 * readers will get the updated i_size when we unlock.
7208 */
7209 if (start + len > i_size_read(inode))
7210 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007211
Miao Xie172a5042013-02-21 02:48:22 -07007212 spin_lock(&BTRFS_I(inode)->lock);
7213 BTRFS_I(inode)->outstanding_extents++;
7214 spin_unlock(&BTRFS_I(inode)->lock);
7215
Miao Xie09348562013-02-07 10:12:07 +00007216 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7217 lockstart + len - 1, EXTENT_DELALLOC, NULL,
7218 &cached_state, GFP_NOFS);
7219 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04007220 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007221
Josef Bacikeb838e72012-07-31 16:28:48 -04007222 /*
7223 * In the case of write we need to clear and unlock the entire range,
7224 * in the case of read we need to unlock only the end area that we
7225 * aren't using if there is any left over space.
7226 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007227 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007228 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7229 lockend, unlock_bits, 1, 0,
7230 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007231 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007232 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007233 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007234
Josef Bacik4b46fce2010-05-23 11:00:55 -04007235 free_extent_map(em);
7236
7237 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007238
7239unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007240 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7241 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
7242 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007243}
7244
Miao Xiec1dc0892014-09-12 18:43:56 +08007245static int btrfs_subio_endio_read(struct inode *inode,
7246 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007247{
Kent Overstreet2c30c712013-11-07 12:20:26 -08007248 struct bio_vec *bvec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007249 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007250 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007251 int ret;
7252 int err = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007253
Miao Xiec1dc0892014-09-12 18:43:56 +08007254 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
7255 return 0;
Miao Xiedc380ae2014-09-12 18:43:55 +08007256
Miao Xiec1dc0892014-09-12 18:43:56 +08007257 start = io_bio->logical;
7258 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Miao Xiedc380ae2014-09-12 18:43:55 +08007259 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7260 0, start, bvec->bv_len);
7261 if (ret)
7262 err = -EIO;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007263 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007264 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007265
7266 return err;
7267}
7268
7269static void btrfs_endio_direct_read(struct bio *bio, int err)
7270{
7271 struct btrfs_dio_private *dip = bio->bi_private;
7272 struct inode *inode = dip->inode;
7273 struct bio *dio_bio;
7274 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7275
7276 if (!err && (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED))
7277 err = btrfs_subio_endio_read(inode, io_bio);
7278
Josef Bacik4b46fce2010-05-23 11:00:55 -04007279 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007280 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007281 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007282
Josef Bacik4b46fce2010-05-23 11:00:55 -04007283 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007284
7285 /* If we had a csum failure make sure to clear the uptodate flag */
7286 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007287 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7288 dio_end_io(dio_bio, err);
Miao Xie23ea8e52014-09-12 18:43:54 +08007289
7290 if (io_bio->end_io)
7291 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04007292 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007293}
7294
7295static void btrfs_endio_direct_write(struct bio *bio, int err)
7296{
7297 struct btrfs_dio_private *dip = bio->bi_private;
7298 struct inode *inode = dip->inode;
7299 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007300 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007301 u64 ordered_offset = dip->logical_offset;
7302 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007303 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007304 int ret;
7305
7306 if (err)
7307 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007308again:
7309 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7310 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007311 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007312 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007313 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007314
Liu Bo9e0af232014-08-15 23:36:53 +08007315 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7316 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007317 btrfs_queue_work(root->fs_info->endio_write_workers,
7318 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007319out_test:
7320 /*
7321 * our bio might span multiple ordered extents. If we haven't
7322 * completed the accounting for the whole dio, go back and try again
7323 */
7324 if (ordered_offset < dip->logical_offset + dip->bytes) {
7325 ordered_bytes = dip->logical_offset + dip->bytes -
7326 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007327 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007328 goto again;
7329 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007330out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007331 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007332
Josef Bacik4b46fce2010-05-23 11:00:55 -04007333 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007334
7335 /* If we had an error make sure to clear the uptodate flag */
7336 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007337 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7338 dio_end_io(dio_bio, err);
7339 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007340}
7341
Chris Masoneaf25d92010-05-25 09:48:28 -04007342static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7343 struct bio *bio, int mirror_num,
7344 unsigned long bio_flags, u64 offset)
7345{
7346 int ret;
7347 struct btrfs_root *root = BTRFS_I(inode)->root;
7348 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007349 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007350 return 0;
7351}
7352
Miao Xiee65e1532010-11-22 03:04:43 +00007353static void btrfs_end_dio_bio(struct bio *bio, int err)
7354{
7355 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiec1dc0892014-09-12 18:43:56 +08007356 int ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007357
7358 if (err) {
Frank Holtonefe120a2013-12-20 11:37:06 -05007359 btrfs_err(BTRFS_I(dip->inode)->root->fs_info,
7360 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007361 btrfs_ino(dip->inode), bio->bi_rw,
Kent Overstreet4f024f32013-10-11 15:44:27 -07007362 (unsigned long long)bio->bi_iter.bi_sector,
7363 bio->bi_iter.bi_size, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007364 } else if (dip->subio_endio) {
7365 ret = dip->subio_endio(dip->inode, btrfs_io_bio(bio));
7366 if (ret)
7367 err = ret;
7368 }
7369
7370 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00007371 dip->errors = 1;
7372
7373 /*
7374 * before atomic variable goto zero, we must make sure
7375 * dip->errors is perceived to be set.
7376 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007377 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007378 }
7379
7380 /* if there are more bios still pending for this dio, just exit */
7381 if (!atomic_dec_and_test(&dip->pending_bios))
7382 goto out;
7383
Chris Mason9be33952013-05-17 18:30:14 -04007384 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007385 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007386 } else {
7387 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007388 bio_endio(dip->orig_bio, 0);
7389 }
7390out:
7391 bio_put(bio);
7392}
7393
7394static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7395 u64 first_sector, gfp_t gfp_flags)
7396{
7397 int nr_vecs = bio_get_nr_vecs(bdev);
7398 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7399}
7400
Miao Xiec1dc0892014-09-12 18:43:56 +08007401static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
7402 struct inode *inode,
7403 struct btrfs_dio_private *dip,
7404 struct bio *bio,
7405 u64 file_offset)
7406{
7407 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7408 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
7409 int ret;
7410
7411 /*
7412 * We load all the csum data we need when we submit
7413 * the first bio to reduce the csum tree search and
7414 * contention.
7415 */
7416 if (dip->logical_offset == file_offset) {
7417 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
7418 file_offset);
7419 if (ret)
7420 return ret;
7421 }
7422
7423 if (bio == dip->orig_bio)
7424 return 0;
7425
7426 file_offset -= dip->logical_offset;
7427 file_offset >>= inode->i_sb->s_blocksize_bits;
7428 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
7429
7430 return 0;
7431}
7432
Miao Xiee65e1532010-11-22 03:04:43 +00007433static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7434 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007435 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007436{
Miao Xiefacc8a222013-07-25 19:22:34 +08007437 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007438 int write = rw & REQ_WRITE;
7439 struct btrfs_root *root = BTRFS_I(inode)->root;
7440 int ret;
7441
Josef Bacikb812ce22012-11-16 13:56:32 -05007442 if (async_submit)
7443 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7444
Miao Xiee65e1532010-11-22 03:04:43 +00007445 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007446
7447 if (!write) {
7448 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7449 if (ret)
7450 goto err;
7451 }
Miao Xiee65e1532010-11-22 03:04:43 +00007452
Josef Bacik1ae39932011-04-06 14:41:34 -04007453 if (skip_sum)
7454 goto map;
7455
7456 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007457 ret = btrfs_wq_submit_bio(root->fs_info,
7458 inode, rw, bio, 0, 0,
7459 file_offset,
7460 __btrfs_submit_bio_start_direct_io,
7461 __btrfs_submit_bio_done);
7462 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007463 } else if (write) {
7464 /*
7465 * If we aren't doing async submit, calculate the csum of the
7466 * bio now.
7467 */
7468 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7469 if (ret)
7470 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007471 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08007472 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
7473 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007474 if (ret)
7475 goto err;
7476 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007477map:
7478 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007479err:
7480 bio_put(bio);
7481 return ret;
7482}
7483
7484static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7485 int skip_sum)
7486{
7487 struct inode *inode = dip->inode;
7488 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007489 struct bio *bio;
7490 struct bio *orig_bio = dip->orig_bio;
7491 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007492 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00007493 u64 file_offset = dip->logical_offset;
7494 u64 submit_len = 0;
7495 u64 map_length;
7496 int nr_pages = 0;
Miao Xie23ea8e52014-09-12 18:43:54 +08007497 int ret;
Josef Bacik1ae39932011-04-06 14:41:34 -04007498 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007499
Kent Overstreet4f024f32013-10-11 15:44:27 -07007500 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007501 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007502 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08007503 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00007504 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08007505
Kent Overstreet4f024f32013-10-11 15:44:27 -07007506 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04007507 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007508 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04007509 goto submit;
7510 }
7511
David Woodhouse53b381b2013-01-29 18:40:14 -05007512 /* async crcs make it difficult to collect full stripe writes. */
7513 if (btrfs_get_alloc_profile(root, 1) &
7514 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7515 async_submit = 0;
7516 else
7517 async_submit = 1;
7518
Josef Bacik02f57c72011-04-06 14:25:44 -04007519 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7520 if (!bio)
7521 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08007522
Josef Bacik02f57c72011-04-06 14:25:44 -04007523 bio->bi_private = dip;
7524 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007525 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04007526 atomic_inc(&dip->pending_bios);
7527
Miao Xiee65e1532010-11-22 03:04:43 +00007528 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7529 if (unlikely(map_length < submit_len + bvec->bv_len ||
7530 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7531 bvec->bv_offset) < bvec->bv_len)) {
7532 /*
7533 * inc the count before we submit the bio so
7534 * we know the end IO handler won't happen before
7535 * we inc the count. Otherwise, the dip might get freed
7536 * before we're done setting it up
7537 */
7538 atomic_inc(&dip->pending_bios);
7539 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7540 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007541 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007542 if (ret) {
7543 bio_put(bio);
7544 atomic_dec(&dip->pending_bios);
7545 goto out_err;
7546 }
7547
Miao Xiee65e1532010-11-22 03:04:43 +00007548 start_sector += submit_len >> 9;
7549 file_offset += submit_len;
7550
7551 submit_len = 0;
7552 nr_pages = 0;
7553
7554 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7555 start_sector, GFP_NOFS);
7556 if (!bio)
7557 goto out_err;
7558 bio->bi_private = dip;
7559 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007560 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007561
Kent Overstreet4f024f32013-10-11 15:44:27 -07007562 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007563 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007564 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007565 &map_length, NULL, 0);
7566 if (ret) {
7567 bio_put(bio);
7568 goto out_err;
7569 }
7570 } else {
7571 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307572 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00007573 bvec++;
7574 }
7575 }
7576
Josef Bacik02f57c72011-04-06 14:25:44 -04007577submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007578 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007579 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007580 if (!ret)
7581 return 0;
7582
7583 bio_put(bio);
7584out_err:
7585 dip->errors = 1;
7586 /*
7587 * before atomic variable goto zero, we must
7588 * make sure dip->errors is perceived to be set.
7589 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007590 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007591 if (atomic_dec_and_test(&dip->pending_bios))
7592 bio_io_error(dip->orig_bio);
7593
7594 /* bio_end_io() will handle error, so we needn't return it */
7595 return 0;
7596}
7597
Chris Mason9be33952013-05-17 18:30:14 -04007598static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7599 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007600{
7601 struct btrfs_root *root = BTRFS_I(inode)->root;
7602 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007603 struct bio *io_bio;
Miao Xie23ea8e52014-09-12 18:43:54 +08007604 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007605 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007606 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007607 int ret = 0;
7608
7609 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7610
Chris Mason9be33952013-05-17 18:30:14 -04007611 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007612 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007613 ret = -ENOMEM;
7614 goto free_ordered;
7615 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007616
Miao Xiec1dc0892014-09-12 18:43:56 +08007617 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007618 if (!dip) {
7619 ret = -ENOMEM;
7620 goto free_io_bio;
7621 }
7622
7623 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007624 dip->inode = inode;
7625 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007626 dip->bytes = dio_bio->bi_iter.bi_size;
7627 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04007628 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04007629 dip->orig_bio = io_bio;
7630 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007631 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08007632 btrfs_bio = btrfs_io_bio(io_bio);
7633 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007634
Miao Xiec1dc0892014-09-12 18:43:56 +08007635 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04007636 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08007637 } else {
Chris Mason9be33952013-05-17 18:30:14 -04007638 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08007639 dip->subio_endio = btrfs_subio_endio_read;
7640 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007641
Miao Xiee65e1532010-11-22 03:04:43 +00007642 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7643 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007644 return;
Chris Mason9be33952013-05-17 18:30:14 -04007645
Miao Xie23ea8e52014-09-12 18:43:54 +08007646 if (btrfs_bio->end_io)
7647 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04007648free_io_bio:
7649 bio_put(io_bio);
7650
Josef Bacik4b46fce2010-05-23 11:00:55 -04007651free_ordered:
7652 /*
7653 * If this is a write, we need to clean up the reserved space and kill
7654 * the ordered extent.
7655 */
7656 if (write) {
7657 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007658 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007659 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7660 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7661 btrfs_free_reserved_extent(root, ordered->start,
Miao Xiee570fd22014-06-19 10:42:50 +08007662 ordered->disk_len, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007663 btrfs_put_ordered_extent(ordered);
7664 btrfs_put_ordered_extent(ordered);
7665 }
Chris Mason9be33952013-05-17 18:30:14 -04007666 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007667}
7668
Chris Mason5a5f79b2010-05-26 21:33:37 -04007669static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04007670 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04007671{
7672 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007673 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007674 unsigned blocksize_mask = root->sectorsize - 1;
7675 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007676
7677 if (offset & blocksize_mask)
7678 goto out;
7679
Al Viro28060d52014-03-22 05:15:17 -04007680 if (iov_iter_alignment(iter) & blocksize_mask)
7681 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007682
Al Viro28060d52014-03-22 05:15:17 -04007683 /* If this is a write we don't need to check anymore */
7684 if (rw & WRITE)
7685 return 0;
7686 /*
7687 * Check to make sure we don't have duplicate iov_base's in this
7688 * iovec, if so return EINVAL, otherwise we'll get csum errors
7689 * when reading back.
7690 */
7691 for (seg = 0; seg < iter->nr_segs; seg++) {
7692 for (i = seg + 1; i < iter->nr_segs; i++) {
7693 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00007694 goto out;
7695 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007696 }
7697 retval = 0;
7698out:
7699 return retval;
7700}
Josef Bacikeb838e72012-07-31 16:28:48 -04007701
Chris Mason16432982008-04-10 10:23:21 -04007702static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05007703 struct iov_iter *iter, loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04007704{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007705 struct file *file = iocb->ki_filp;
7706 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007707 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007708 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007709 bool wakeup = true;
7710 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007711 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007712
Al Viro28060d52014-03-22 05:15:17 -04007713 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007714 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007715
Miao Xie38851cc2013-02-08 07:04:11 +00007716 atomic_inc(&inode->i_dio_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007717 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00007718
Josef Bacik0e267c42013-07-02 10:38:02 -04007719 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08007720 * The generic stuff only does filemap_write_and_wait_range, which
7721 * isn't enough if we've written compressed pages to this area, so
7722 * we need to flush the dirty pages again to make absolutely sure
7723 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04007724 */
Al Viroa6cbcd42014-03-04 22:38:00 -05007725 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08007726 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7727 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08007728 filemap_fdatawrite_range(inode->i_mapping, offset,
7729 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04007730
Miao Xie09348562013-02-07 10:12:07 +00007731 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007732 /*
7733 * If the write DIO is beyond the EOF, we need update
7734 * the isize, but it is protected by i_mutex. So we can
7735 * not unlock the i_mutex at this case.
7736 */
7737 if (offset + count <= inode->i_size) {
7738 mutex_unlock(&inode->i_mutex);
7739 relock = true;
7740 }
Miao Xie09348562013-02-07 10:12:07 +00007741 ret = btrfs_delalloc_reserve_space(inode, count);
7742 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007743 goto out;
7744 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7745 &BTRFS_I(inode)->runtime_flags))) {
7746 inode_dio_done(inode);
7747 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7748 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007749 }
7750
7751 ret = __blockdev_direct_IO(rw, iocb, inode,
7752 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Al Viro31b14032014-03-05 01:33:16 -05007753 iter, offset, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007754 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007755 if (rw & WRITE) {
7756 if (ret < 0 && ret != -EIOCBQUEUED)
7757 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007758 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007759 btrfs_delalloc_release_space(inode,
7760 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007761 else
7762 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007763 }
Miao Xie38851cc2013-02-08 07:04:11 +00007764out:
Miao Xie2e60a512013-02-08 07:01:08 +00007765 if (wakeup)
7766 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007767 if (relock)
7768 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007769
7770 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007771}
7772
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007773#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7774
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007775static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7776 __u64 start, __u64 len)
7777{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007778 int ret;
7779
7780 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7781 if (ret)
7782 return ret;
7783
Chris Masonec29ed52011-02-23 16:23:20 -05007784 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007785}
7786
Chris Mason9ebefb182007-06-15 13:50:00 -04007787int btrfs_readpage(struct file *file, struct page *page)
7788{
Chris Masond1310b22008-01-24 16:13:08 -05007789 struct extent_io_tree *tree;
7790 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007791 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007792}
Chris Mason1832a6d2007-12-21 16:27:21 -05007793
Chris Mason39279cc2007-06-12 06:35:45 -04007794static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7795{
Chris Masond1310b22008-01-24 16:13:08 -05007796 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007797
7798
7799 if (current->flags & PF_MEMALLOC) {
7800 redirty_page_for_writepage(wbc, page);
7801 unlock_page(page);
7802 return 0;
7803 }
Chris Masond1310b22008-01-24 16:13:08 -05007804 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007805 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7806}
Chris Mason39279cc2007-06-12 06:35:45 -04007807
Eric Sandeen48a3b632013-04-25 20:41:01 +00007808static int btrfs_writepages(struct address_space *mapping,
7809 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007810{
Chris Masond1310b22008-01-24 16:13:08 -05007811 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007812
Chris Masond1310b22008-01-24 16:13:08 -05007813 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007814 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7815}
7816
Chris Mason3ab2fb52007-11-08 10:59:22 -05007817static int
7818btrfs_readpages(struct file *file, struct address_space *mapping,
7819 struct list_head *pages, unsigned nr_pages)
7820{
Chris Masond1310b22008-01-24 16:13:08 -05007821 struct extent_io_tree *tree;
7822 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007823 return extent_readpages(tree, mapping, pages, nr_pages,
7824 btrfs_get_extent);
7825}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007826static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007827{
Chris Masond1310b22008-01-24 16:13:08 -05007828 struct extent_io_tree *tree;
7829 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007830 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007831
Chris Masond1310b22008-01-24 16:13:08 -05007832 tree = &BTRFS_I(page->mapping->host)->io_tree;
7833 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007834 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007835 if (ret == 1) {
7836 ClearPagePrivate(page);
7837 set_page_private(page, 0);
7838 page_cache_release(page);
7839 }
7840 return ret;
7841}
Chris Mason39279cc2007-06-12 06:35:45 -04007842
Chris Masone6dcd2d2008-07-17 12:53:50 -04007843static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7844{
Chris Mason98509cf2008-09-11 15:51:43 -04007845 if (PageWriteback(page) || PageDirty(page))
7846 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007847 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007848}
7849
Lukas Czernerd47992f2013-05-21 23:17:23 -04007850static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7851 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007852{
Josef Bacik5fd02042012-05-02 14:00:54 -04007853 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007854 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007855 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007856 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007857 u64 page_start = page_offset(page);
7858 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007859 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04007860
Chris Mason8b62b722009-09-02 16:53:46 -04007861 /*
7862 * we have the page locked, so new writeback can't start,
7863 * and the dirty bit won't be cleared while we are here.
7864 *
7865 * Wait for IO on this page so that we can safely clear
7866 * the PagePrivate2 bit and do ordered accounting
7867 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007868 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007869
Josef Bacik5fd02042012-05-02 14:00:54 -04007870 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007871 if (offset) {
7872 btrfs_releasepage(page, GFP_NOFS);
7873 return;
7874 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007875
7876 if (!inode_evicting)
7877 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
7878 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007879 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007880 /*
7881 * IO on this page will never be started, so we need
7882 * to account for any ordered extents now
7883 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007884 if (!inode_evicting)
7885 clear_extent_bit(tree, page_start, page_end,
7886 EXTENT_DIRTY | EXTENT_DELALLOC |
7887 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7888 EXTENT_DEFRAG, 1, 0, &cached_state,
7889 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007890 /*
7891 * whoever cleared the private bit is responsible
7892 * for the finish_ordered_io
7893 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04007894 if (TestClearPagePrivate2(page)) {
7895 struct btrfs_ordered_inode_tree *tree;
7896 u64 new_len;
7897
7898 tree = &BTRFS_I(inode)->ordered_tree;
7899
7900 spin_lock_irq(&tree->lock);
7901 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
7902 new_len = page_start - ordered->file_offset;
7903 if (new_len < ordered->truncated_len)
7904 ordered->truncated_len = new_len;
7905 spin_unlock_irq(&tree->lock);
7906
7907 if (btrfs_dec_test_ordered_pending(inode, &ordered,
7908 page_start,
7909 PAGE_CACHE_SIZE, 1))
7910 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007911 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007912 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007913 if (!inode_evicting) {
7914 cached_state = NULL;
7915 lock_extent_bits(tree, page_start, page_end, 0,
7916 &cached_state);
7917 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007918 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007919
7920 if (!inode_evicting) {
7921 clear_extent_bit(tree, page_start, page_end,
7922 EXTENT_LOCKED | EXTENT_DIRTY |
7923 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
7924 EXTENT_DEFRAG, 1, 1,
7925 &cached_state, GFP_NOFS);
7926
7927 __btrfs_releasepage(page, GFP_NOFS);
7928 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007929
Chris Mason4a096752008-07-21 10:29:44 -04007930 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007931 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007932 ClearPagePrivate(page);
7933 set_page_private(page, 0);
7934 page_cache_release(page);
7935 }
Chris Mason39279cc2007-06-12 06:35:45 -04007936}
7937
Chris Mason9ebefb182007-06-15 13:50:00 -04007938/*
7939 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7940 * called from a page fault handler when a page is first dirtied. Hence we must
7941 * be careful to check for EOF conditions here. We set the page up correctly
7942 * for a written page which means we get ENOSPC checking when writing into
7943 * holes and correct delalloc and unwritten extent mapping on filesystems that
7944 * support these features.
7945 *
7946 * We are not allowed to take the i_mutex here so we have to play games to
7947 * protect against truncate races as the page could now be beyond EOF. Because
7948 * vmtruncate() writes the inode size before removing pages, once we have the
7949 * page lock we can determine safely if the page is beyond EOF. If it is not
7950 * beyond EOF, then the page is guaranteed safe against truncation until we
7951 * unlock the page.
7952 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007953int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007954{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007955 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007956 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007957 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007958 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7959 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007960 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007961 char *kaddr;
7962 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007963 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007964 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007965 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007966 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007967 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007968
Jan Karab2b5ef52012-06-12 16:20:45 +02007969 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007970 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007971 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007972 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007973 reserved = 1;
7974 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007975 if (ret) {
7976 if (ret == -ENOMEM)
7977 ret = VM_FAULT_OOM;
7978 else /* -ENOSPC, -EIO, etc */
7979 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007980 if (reserved)
7981 goto out;
7982 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007983 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007984
Nick Piggin56a76f82009-03-31 15:23:23 -07007985 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007986again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007987 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007988 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007989 page_start = page_offset(page);
7990 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007991
Chris Mason9ebefb182007-06-15 13:50:00 -04007992 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007993 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007994 /* page got truncated out from underneath us */
7995 goto out_unlock;
7996 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007997 wait_on_page_writeback(page);
7998
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007999 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008000 set_page_extent_mapped(page);
8001
Chris Masoneb84ae02008-07-17 13:53:27 -04008002 /*
8003 * we can't set the delalloc bits if there are pending ordered
8004 * extents. Drop our locks and wait for them to finish
8005 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008006 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8007 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008008 unlock_extent_cached(io_tree, page_start, page_end,
8009 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008010 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008011 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008012 btrfs_put_ordered_extent(ordered);
8013 goto again;
8014 }
8015
Josef Bacikfbf19082009-10-01 17:10:23 -04008016 /*
8017 * XXX - page_mkwrite gets called every time the page is dirtied, even
8018 * if it was already dirty, so for space accounting reasons we need to
8019 * clear any delalloc bits for the range we are fixing to save. There
8020 * is probably a better way to do this, but for now keep consistent with
8021 * prepare_pages in the normal write path.
8022 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00008023 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008024 EXTENT_DIRTY | EXTENT_DELALLOC |
8025 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00008026 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04008027
Josef Bacik2ac55d42010-02-03 19:33:23 +00008028 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
8029 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008030 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008031 unlock_extent_cached(io_tree, page_start, page_end,
8032 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008033 ret = VM_FAULT_SIGBUS;
8034 goto out_unlock;
8035 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008036 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008037
8038 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04008039 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008040 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008041 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008042 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008043
Chris Masone6dcd2d2008-07-17 12:53:50 -04008044 if (zero_start != PAGE_CACHE_SIZE) {
8045 kaddr = kmap(page);
8046 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8047 flush_dcache_page(page);
8048 kunmap(page);
8049 }
Chris Mason247e7432008-07-17 12:53:51 -04008050 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008051 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008052 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008053
Chris Mason257c62e2009-10-13 13:21:08 -04008054 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8055 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008056 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008057
Josef Bacik2ac55d42010-02-03 19:33:23 +00008058 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008059
8060out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008061 if (!ret) {
8062 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008063 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008064 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008065 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008066out:
Josef Bacikec39e182012-01-12 19:10:12 -05008067 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008068out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008069 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008070 return ret;
8071}
8072
Josef Bacika41ad392011-01-31 15:30:16 -05008073static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008074{
8075 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008076 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008077 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008078 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008079 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008080 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008081 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008082
Josef Bacik0ef8b722013-10-25 16:13:35 -04008083 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8084 (u64)-1);
8085 if (ret)
8086 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008087
Josef Bacikfcb80c22011-05-03 10:40:22 -04008088 /*
8089 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8090 * 3 things going on here
8091 *
8092 * 1) We need to reserve space for our orphan item and the space to
8093 * delete our orphan item. Lord knows we don't want to have a dangling
8094 * orphan item because we didn't reserve space to remove it.
8095 *
8096 * 2) We need to reserve space to update our inode.
8097 *
8098 * 3) We need to have something to cache all the space that is going to
8099 * be free'd up by the truncate operation, but also have some slack
8100 * space reserved in case it uses space during the truncate (thank you
8101 * very much snapshotting).
8102 *
8103 * And we need these to all be seperate. The fact is we can use alot of
8104 * space doing the truncate, and we have no earthly idea how much space
8105 * we will use, so we need the truncate reservation to be seperate so it
8106 * doesn't end up using space reserved for updating the inode or
8107 * removing the orphan item. We also need to be able to stop the
8108 * transaction and start a new one, which means we need to be able to
8109 * update the inode several times, and we have no idea of knowing how
8110 * many times that will be, so we can't just reserve 1 item for the
8111 * entirety of the opration, so that has to be done seperately as well.
8112 * Then there is the orphan item, which does indeed need to be held on
8113 * to for the whole operation, and we need nobody to touch this reserved
8114 * space except the orphan code.
8115 *
8116 * So that leaves us with
8117 *
8118 * 1) root->orphan_block_rsv - for the orphan deletion.
8119 * 2) rsv - for the truncate reservation, which we will steal from the
8120 * transaction reservation.
8121 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8122 * updating the inode.
8123 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008124 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008125 if (!rsv)
8126 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008127 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008128 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008129
Josef Bacik907cbce2011-08-08 13:46:15 -04008130 /*
Josef Bacik07127182011-08-19 10:29:59 -04008131 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008132 * 1 for updating the inode.
8133 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008134 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008135 if (IS_ERR(trans)) {
8136 err = PTR_ERR(trans);
8137 goto out;
8138 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008139
Josef Bacik907cbce2011-08-08 13:46:15 -04008140 /* Migrate the slack space for the truncate to our reserve */
8141 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8142 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008143 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008144
Chris Mason5a3f23d2009-03-31 13:27:11 -04008145 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008146 * So if we truncate and then write and fsync we normally would just
8147 * write the extents that changed, which is a problem if we need to
8148 * first truncate that entire inode. So set this flag so we write out
8149 * all of the extents in the inode to the sync log so we're completely
8150 * safe.
8151 */
8152 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008153 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008154
Yan, Zheng80825102009-11-12 09:35:36 +00008155 while (1) {
8156 ret = btrfs_truncate_inode_items(trans, root, inode,
8157 inode->i_size,
8158 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008159 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05008160 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008161 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008162 }
Chris Mason39279cc2007-06-12 06:35:45 -04008163
Josef Bacikfcb80c22011-05-03 10:40:22 -04008164 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008165 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008166 if (ret) {
8167 err = ret;
8168 break;
8169 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008170
Yan, Zheng80825102009-11-12 09:35:36 +00008171 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008172 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008173
8174 trans = btrfs_start_transaction(root, 2);
8175 if (IS_ERR(trans)) {
8176 ret = err = PTR_ERR(trans);
8177 trans = NULL;
8178 break;
8179 }
8180
8181 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8182 rsv, min_size);
8183 BUG_ON(ret); /* shouldn't happen */
8184 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008185 }
8186
8187 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008188 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008189 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008190 if (ret)
8191 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008192 }
8193
Chris Mason917c16b2011-11-08 14:49:59 -05008194 if (trans) {
8195 trans->block_rsv = &root->fs_info->trans_block_rsv;
8196 ret = btrfs_update_inode(trans, root, inode);
8197 if (ret && !err)
8198 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008199
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008200 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008201 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008202 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008203
8204out:
8205 btrfs_free_block_rsv(root, rsv);
8206
Josef Bacik3893e332011-01-31 16:03:11 -05008207 if (ret && !err)
8208 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008209
Josef Bacik3893e332011-01-31 16:03:11 -05008210 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008211}
8212
Sven Wegener3b963622008-06-09 21:57:42 -04008213/*
Chris Masond352ac62008-09-29 15:18:18 -04008214 * create a new subvolume directory/inode (helper for the ioctl).
8215 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008216int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008217 struct btrfs_root *new_root,
8218 struct btrfs_root *parent_root,
8219 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008220{
Chris Mason39279cc2007-06-12 06:35:45 -04008221 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008222 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008223 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008224
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008225 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8226 new_dirid, new_dirid,
8227 S_IFDIR | (~current_umask() & S_IRWXUGO),
8228 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008229 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008230 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008231 inode->i_op = &btrfs_dir_inode_operations;
8232 inode->i_fop = &btrfs_dir_file_operations;
8233
Miklos Szeredibfe86842011-10-28 14:13:29 +02008234 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008235 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008236 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008237
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008238 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8239 if (err)
8240 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008241 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008242 new_root->root_key.objectid, err);
8243
Yan, Zheng76dda932009-09-21 16:00:26 -04008244 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008245
Yan, Zheng76dda932009-09-21 16:00:26 -04008246 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008247 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008248}
8249
Chris Mason39279cc2007-06-12 06:35:45 -04008250struct inode *btrfs_alloc_inode(struct super_block *sb)
8251{
8252 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008253 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008254
8255 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8256 if (!ei)
8257 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008258
8259 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008260 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008261 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008262 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008263 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008264 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008265 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008266 ei->disk_i_size = 0;
8267 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008268 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008269 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008270 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008271 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008272 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008273
Josef Bacik9e0baf62011-07-15 15:16:44 +00008274 spin_lock_init(&ei->lock);
8275 ei->outstanding_extents = 0;
8276 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008277
Josef Bacik72ac3c02012-05-23 14:13:11 -04008278 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008279 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008280
Miao Xie16cdcec2011-04-22 18:12:22 +08008281 ei->delayed_node = NULL;
8282
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008283 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008284 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008285 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8286 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008287 ei->io_tree.track_uptodate = 1;
8288 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008289 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008290 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008291 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008292 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008293 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008294 RB_CLEAR_NODE(&ei->rb_node);
8295
8296 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008297}
8298
Josef Bacikaaedb552013-10-11 14:44:09 -04008299#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8300void btrfs_test_destroy_inode(struct inode *inode)
8301{
8302 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8303 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8304}
8305#endif
8306
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008307static void btrfs_i_callback(struct rcu_head *head)
8308{
8309 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008310 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8311}
8312
Chris Mason39279cc2007-06-12 06:35:45 -04008313void btrfs_destroy_inode(struct inode *inode)
8314{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008315 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008316 struct btrfs_root *root = BTRFS_I(inode)->root;
8317
Al Virob3d9b7a2012-06-09 13:51:19 -04008318 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008319 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008320 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8321 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008322 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8323 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008324 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008325
Chris Mason5a3f23d2009-03-31 13:27:11 -04008326 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008327 * This can happen where we create an inode, but somebody else also
8328 * created the same inode and we need to destroy the one we already
8329 * created.
8330 */
8331 if (!root)
8332 goto free;
8333
Josef Bacik8a35d952012-05-23 14:26:42 -04008334 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8335 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008336 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008337 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008338 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008339 }
Josef Bacik7b128762008-07-24 12:17:14 -04008340
Chris Masond3977122009-01-05 21:25:51 -05008341 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008342 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8343 if (!ordered)
8344 break;
8345 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008346 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008347 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008348 btrfs_remove_ordered_extent(inode, ordered);
8349 btrfs_put_ordered_extent(ordered);
8350 btrfs_put_ordered_extent(ordered);
8351 }
8352 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008353 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008354 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008355free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008356 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008357}
8358
Al Viro45321ac2010-06-07 13:43:19 -04008359int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008360{
8361 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008362
Naohiro Aota6379ef92013-06-06 09:56:34 +00008363 if (root == NULL)
8364 return 1;
8365
Liu Bofa6ac872013-02-20 14:10:23 +00008366 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008367 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008368 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008369 else
Al Viro45321ac2010-06-07 13:43:19 -04008370 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008371}
8372
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008373static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008374{
8375 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8376
8377 inode_init_once(&ei->vfs_inode);
8378}
8379
8380void btrfs_destroy_cachep(void)
8381{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008382 /*
8383 * Make sure all delayed rcu free inodes are flushed before we
8384 * destroy cache.
8385 */
8386 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008387 if (btrfs_inode_cachep)
8388 kmem_cache_destroy(btrfs_inode_cachep);
8389 if (btrfs_trans_handle_cachep)
8390 kmem_cache_destroy(btrfs_trans_handle_cachep);
8391 if (btrfs_transaction_cachep)
8392 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008393 if (btrfs_path_cachep)
8394 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008395 if (btrfs_free_space_cachep)
8396 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008397 if (btrfs_delalloc_work_cachep)
8398 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008399}
8400
8401int btrfs_init_cachep(void)
8402{
David Sterba837e1972012-09-07 03:00:48 -06008403 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008404 sizeof(struct btrfs_inode), 0,
8405 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008406 if (!btrfs_inode_cachep)
8407 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008408
David Sterba837e1972012-09-07 03:00:48 -06008409 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008410 sizeof(struct btrfs_trans_handle), 0,
8411 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008412 if (!btrfs_trans_handle_cachep)
8413 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008414
David Sterba837e1972012-09-07 03:00:48 -06008415 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008416 sizeof(struct btrfs_transaction), 0,
8417 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008418 if (!btrfs_transaction_cachep)
8419 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008420
David Sterba837e1972012-09-07 03:00:48 -06008421 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008422 sizeof(struct btrfs_path), 0,
8423 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008424 if (!btrfs_path_cachep)
8425 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008426
David Sterba837e1972012-09-07 03:00:48 -06008427 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008428 sizeof(struct btrfs_free_space), 0,
8429 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8430 if (!btrfs_free_space_cachep)
8431 goto fail;
8432
Miao Xie8ccf6f192012-10-25 09:28:04 +00008433 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8434 sizeof(struct btrfs_delalloc_work), 0,
8435 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8436 NULL);
8437 if (!btrfs_delalloc_work_cachep)
8438 goto fail;
8439
Chris Mason39279cc2007-06-12 06:35:45 -04008440 return 0;
8441fail:
8442 btrfs_destroy_cachep();
8443 return -ENOMEM;
8444}
8445
8446static int btrfs_getattr(struct vfsmount *mnt,
8447 struct dentry *dentry, struct kstat *stat)
8448{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008449 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008450 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008451 u32 blocksize = inode->i_sb->s_blocksize;
8452
Chris Mason39279cc2007-06-12 06:35:45 -04008453 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008454 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008455 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008456
8457 spin_lock(&BTRFS_I(inode)->lock);
8458 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8459 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008460 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008461 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008462 return 0;
8463}
8464
Chris Masond3977122009-01-05 21:25:51 -05008465static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8466 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008467{
8468 struct btrfs_trans_handle *trans;
8469 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008470 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008471 struct inode *new_inode = new_dentry->d_inode;
8472 struct inode *old_inode = old_dentry->d_inode;
8473 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008474 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008475 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008476 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008477 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008478
Li Zefan33345d012011-04-20 10:31:50 +08008479 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008480 return -EPERM;
8481
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008482 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008483 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008484 return -EXDEV;
8485
Li Zefan33345d012011-04-20 10:31:50 +08008486 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8487 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008488 return -ENOTEMPTY;
8489
Chris Mason39279cc2007-06-12 06:35:45 -04008490 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008491 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008492 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008493
8494
8495 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008496 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008497 new_dentry->d_name.name,
8498 new_dentry->d_name.len);
8499
8500 if (ret) {
8501 if (ret == -EEXIST) {
8502 /* we shouldn't get
8503 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308504 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008505 return ret;
8506 }
8507 } else {
8508 /* maybe -EOVERFLOW */
8509 return ret;
8510 }
8511 }
8512 ret = 0;
8513
Chris Mason5a3f23d2009-03-31 13:27:11 -04008514 /*
Chris Mason8d875f92014-08-12 10:47:42 -07008515 * we're using rename to replace one file with another. Start IO on it
8516 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04008517 */
Chris Mason8d875f92014-08-12 10:47:42 -07008518 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04008519 filemap_flush(old_inode->i_mapping);
8520
Yan, Zheng76dda932009-09-21 16:00:26 -04008521 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008522 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008523 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008524 /*
8525 * We want to reserve the absolute worst case amount of items. So if
8526 * both inodes are subvols and we need to unlink them then that would
8527 * require 4 item modifications, but if they are both normal inodes it
8528 * would require 5 item modifications, so we'll assume their normal
8529 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8530 * should cover the worst case number of items we'll modify.
8531 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008532 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008533 if (IS_ERR(trans)) {
8534 ret = PTR_ERR(trans);
8535 goto out_notrans;
8536 }
Chris Mason5f39d392007-10-15 16:14:19 -04008537
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008538 if (dest != root)
8539 btrfs_record_root_in_trans(trans, dest);
8540
Yan, Zhenga5719522009-09-24 09:17:31 -04008541 ret = btrfs_set_inode_index(new_dir, &index);
8542 if (ret)
8543 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008544
Miao Xie67de1172013-12-26 13:07:06 +08008545 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08008546 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008547 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08008548 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008549 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008550 ret = btrfs_insert_inode_ref(trans, dest,
8551 new_dentry->d_name.name,
8552 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008553 old_ino,
8554 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008555 if (ret)
8556 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008557 /*
8558 * this is an ugly little race, but the rename is required
8559 * to make sure that if we crash, the inode is either at the
8560 * old name or the new one. pinning the log transaction lets
8561 * us make sure we don't allow a log commit to come in after
8562 * we unlink the name but before we add the new name back in.
8563 */
8564 btrfs_pin_log_trans(root);
8565 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04008566
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008567 inode_inc_iversion(old_dir);
8568 inode_inc_iversion(new_dir);
8569 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008570 old_dir->i_ctime = old_dir->i_mtime = ctime;
8571 new_dir->i_ctime = new_dir->i_mtime = ctime;
8572 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008573
Chris Mason12fcfd22009-03-24 10:24:20 -04008574 if (old_dentry->d_parent != new_dentry->d_parent)
8575 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8576
Li Zefan33345d012011-04-20 10:31:50 +08008577 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008578 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8579 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8580 old_dentry->d_name.name,
8581 old_dentry->d_name.len);
8582 } else {
Al Viro92986792011-03-04 17:14:37 +00008583 ret = __btrfs_unlink_inode(trans, root, old_dir,
8584 old_dentry->d_inode,
8585 old_dentry->d_name.name,
8586 old_dentry->d_name.len);
8587 if (!ret)
8588 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008589 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008590 if (ret) {
8591 btrfs_abort_transaction(trans, root, ret);
8592 goto out_fail;
8593 }
Chris Mason39279cc2007-06-12 06:35:45 -04008594
8595 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008596 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008597 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008598 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008599 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8600 root_objectid = BTRFS_I(new_inode)->location.objectid;
8601 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8602 root_objectid,
8603 new_dentry->d_name.name,
8604 new_dentry->d_name.len);
8605 BUG_ON(new_inode->i_nlink == 0);
8606 } else {
8607 ret = btrfs_unlink_inode(trans, dest, new_dir,
8608 new_dentry->d_inode,
8609 new_dentry->d_name.name,
8610 new_dentry->d_name.len);
8611 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008612 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008613 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008614 if (ret) {
8615 btrfs_abort_transaction(trans, root, ret);
8616 goto out_fail;
8617 }
Chris Mason39279cc2007-06-12 06:35:45 -04008618 }
Josef Bacikaec74772008-07-24 12:12:38 -04008619
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008620 ret = btrfs_add_link(trans, new_dir, old_inode,
8621 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008622 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008623 if (ret) {
8624 btrfs_abort_transaction(trans, root, ret);
8625 goto out_fail;
8626 }
Chris Mason39279cc2007-06-12 06:35:45 -04008627
Miao Xie67de1172013-12-26 13:07:06 +08008628 if (old_inode->i_nlink == 1)
8629 BTRFS_I(old_inode)->dir_index = index;
8630
Li Zefan33345d012011-04-20 10:31:50 +08008631 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008632 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008633 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008634 btrfs_end_log_trans(root);
8635 }
Chris Mason39279cc2007-06-12 06:35:45 -04008636out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008637 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008638out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008639 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008640 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008641
Chris Mason39279cc2007-06-12 06:35:45 -04008642 return ret;
8643}
8644
Miklos Szeredi80ace852014-07-23 15:15:32 +02008645static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
8646 struct inode *new_dir, struct dentry *new_dentry,
8647 unsigned int flags)
8648{
8649 if (flags & ~RENAME_NOREPLACE)
8650 return -EINVAL;
8651
8652 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
8653}
8654
Miao Xie8ccf6f192012-10-25 09:28:04 +00008655static void btrfs_run_delalloc_work(struct btrfs_work *work)
8656{
8657 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04008658 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008659
8660 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8661 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04008662 inode = delalloc_work->inode;
8663 if (delalloc_work->wait) {
8664 btrfs_wait_ordered_range(inode, 0, (u64)-1);
8665 } else {
8666 filemap_flush(inode->i_mapping);
8667 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8668 &BTRFS_I(inode)->runtime_flags))
8669 filemap_flush(inode->i_mapping);
8670 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008671
8672 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04008673 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008674 else
Josef Bacik9f23e282013-10-28 15:03:41 -04008675 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008676 complete(&delalloc_work->completion);
8677}
8678
8679struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8680 int wait, int delay_iput)
8681{
8682 struct btrfs_delalloc_work *work;
8683
8684 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8685 if (!work)
8686 return NULL;
8687
8688 init_completion(&work->completion);
8689 INIT_LIST_HEAD(&work->list);
8690 work->inode = inode;
8691 work->wait = wait;
8692 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08008693 WARN_ON_ONCE(!inode);
8694 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
8695 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008696
8697 return work;
8698}
8699
8700void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8701{
8702 wait_for_completion(&work->completion);
8703 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8704}
8705
Chris Masond352ac62008-09-29 15:18:18 -04008706/*
8707 * some fairly slow code that needs optimization. This walks the list
8708 * of all the inodes with pending delalloc and forces them to disk.
8709 */
Miao Xie6c255e62014-03-06 13:55:01 +08008710static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
8711 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04008712{
Chris Masonea8c2812008-08-04 23:17:27 -04008713 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008714 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008715 struct btrfs_delalloc_work *work, *next;
8716 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008717 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008718 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008719
Miao Xie8ccf6f192012-10-25 09:28:04 +00008720 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008721 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008722
Miao Xie573bfb72014-03-06 13:55:03 +08008723 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008724 spin_lock(&root->delalloc_lock);
8725 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008726 while (!list_empty(&splice)) {
8727 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008728 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008729
Miao Xieeb73c1b2013-05-15 07:48:22 +00008730 list_move_tail(&binode->delalloc_inodes,
8731 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008732 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008733 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008734 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008735 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008736 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008737 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008738
8739 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8740 if (unlikely(!work)) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04008741 if (delay_iput)
8742 btrfs_add_delayed_iput(inode);
8743 else
8744 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008745 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008746 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008747 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008748 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08008749 btrfs_queue_work(root->fs_info->flush_workers,
8750 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08008751 ret++;
8752 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008753 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008754 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008755 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008756 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008757 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008758
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008759out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008760 list_for_each_entry_safe(work, next, &works, list) {
8761 list_del_init(&work->list);
8762 btrfs_wait_and_free_delalloc_work(work);
8763 }
8764
Miao Xieeb73c1b2013-05-15 07:48:22 +00008765 if (!list_empty_careful(&splice)) {
8766 spin_lock(&root->delalloc_lock);
8767 list_splice_tail(&splice, &root->delalloc_inodes);
8768 spin_unlock(&root->delalloc_lock);
8769 }
Miao Xie573bfb72014-03-06 13:55:03 +08008770 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008771 return ret;
8772}
8773
8774int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8775{
8776 int ret;
8777
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008778 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008779 return -EROFS;
8780
Miao Xie6c255e62014-03-06 13:55:01 +08008781 ret = __start_delalloc_inodes(root, delay_iput, -1);
8782 if (ret > 0)
8783 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00008784 /*
8785 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008786 * we have to make sure the IO is actually started and that
8787 * ordered extents get created before we return
8788 */
8789 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008790 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008791 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008792 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008793 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8794 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008795 }
8796 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008797 return ret;
8798}
8799
Miao Xie6c255e62014-03-06 13:55:01 +08008800int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
8801 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008802{
8803 struct btrfs_root *root;
8804 struct list_head splice;
8805 int ret;
8806
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008807 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008808 return -EROFS;
8809
8810 INIT_LIST_HEAD(&splice);
8811
Miao Xie573bfb72014-03-06 13:55:03 +08008812 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008813 spin_lock(&fs_info->delalloc_root_lock);
8814 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08008815 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008816 root = list_first_entry(&splice, struct btrfs_root,
8817 delalloc_root);
8818 root = btrfs_grab_fs_root(root);
8819 BUG_ON(!root);
8820 list_move_tail(&root->delalloc_root,
8821 &fs_info->delalloc_roots);
8822 spin_unlock(&fs_info->delalloc_root_lock);
8823
Miao Xie6c255e62014-03-06 13:55:01 +08008824 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008825 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08008826 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008827 goto out;
8828
Miao Xie6c255e62014-03-06 13:55:01 +08008829 if (nr != -1) {
8830 nr -= ret;
8831 WARN_ON(nr < 0);
8832 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008833 spin_lock(&fs_info->delalloc_root_lock);
8834 }
8835 spin_unlock(&fs_info->delalloc_root_lock);
8836
Miao Xie6c255e62014-03-06 13:55:01 +08008837 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00008838 atomic_inc(&fs_info->async_submit_draining);
8839 while (atomic_read(&fs_info->nr_async_submits) ||
8840 atomic_read(&fs_info->async_delalloc_pages)) {
8841 wait_event(fs_info->async_submit_wait,
8842 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8843 atomic_read(&fs_info->async_delalloc_pages) == 0));
8844 }
8845 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008846out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008847 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008848 spin_lock(&fs_info->delalloc_root_lock);
8849 list_splice_tail(&splice, &fs_info->delalloc_roots);
8850 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008851 }
Miao Xie573bfb72014-03-06 13:55:03 +08008852 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008853 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008854}
8855
Chris Mason39279cc2007-06-12 06:35:45 -04008856static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8857 const char *symname)
8858{
8859 struct btrfs_trans_handle *trans;
8860 struct btrfs_root *root = BTRFS_I(dir)->root;
8861 struct btrfs_path *path;
8862 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008863 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008864 int err;
8865 int drop_inode = 0;
8866 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308867 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008868 int name_len;
8869 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008870 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008871 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008872 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008873
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008874 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04008875 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8876 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008877
Josef Bacik9ed74f22009-09-11 16:12:44 -04008878 /*
8879 * 2 items for inode item and ref
8880 * 2 items for dir items
8881 * 1 item for xattr if selinux is on
8882 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008883 trans = btrfs_start_transaction(root, 5);
8884 if (IS_ERR(trans))
8885 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008886
Li Zefan581bb052011-04-20 10:06:11 +08008887 err = btrfs_find_free_ino(root, &objectid);
8888 if (err)
8889 goto out_unlock;
8890
Josef Bacikaec74772008-07-24 12:12:38 -04008891 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008892 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008893 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008894 if (IS_ERR(inode)) {
8895 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008896 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008897 }
Chris Mason39279cc2007-06-12 06:35:45 -04008898
Casey Schauflerad19db72011-12-15 10:09:07 -05008899 /*
8900 * If the active LSM wants to access the inode during
8901 * d_instantiate it needs these. Smack checks to see
8902 * if the filesystem supports xattrs by looking at the
8903 * ops vector.
8904 */
8905 inode->i_fop = &btrfs_file_operations;
8906 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07008907 inode->i_mapping->a_ops = &btrfs_aops;
8908 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
8909 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
8910
8911 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
8912 if (err)
8913 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05008914
Josef Bacika1b075d2010-11-19 20:36:11 +00008915 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008916 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07008917 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008918
8919 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008920 if (!path) {
8921 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07008922 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07008923 }
Li Zefan33345d012011-04-20 10:31:50 +08008924 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008925 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02008926 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04008927 datasize = btrfs_file_extent_calc_inline_size(name_len);
8928 err = btrfs_insert_empty_item(trans, root, path, &key,
8929 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008930 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00008931 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07008932 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04008933 }
Chris Mason5f39d392007-10-15 16:14:19 -04008934 leaf = path->nodes[0];
8935 ei = btrfs_item_ptr(leaf, path->slots[0],
8936 struct btrfs_file_extent_item);
8937 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8938 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008939 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008940 btrfs_set_file_extent_encryption(leaf, ei, 0);
8941 btrfs_set_file_extent_compression(leaf, ei, 0);
8942 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8943 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8944
Chris Mason39279cc2007-06-12 06:35:45 -04008945 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008946 write_extent_buffer(leaf, symname, ptr, name_len);
8947 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008948 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008949
Chris Mason39279cc2007-06-12 06:35:45 -04008950 inode->i_op = &btrfs_symlink_inode_operations;
8951 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008952 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008953 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008954 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04008955 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07008956 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04008957 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07008958 goto out_unlock_inode;
8959 }
8960
8961 unlock_new_inode(inode);
8962 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008963
8964out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008965 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008966 if (drop_inode) {
8967 inode_dec_link_count(inode);
8968 iput(inode);
8969 }
Liu Bob53d3f52012-11-14 14:34:34 +00008970 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008971 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07008972
8973out_unlock_inode:
8974 drop_inode = 1;
8975 unlock_new_inode(inode);
8976 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04008977}
Chris Mason16432982008-04-10 10:23:21 -04008978
Josef Bacik0af3d002010-06-21 14:48:16 -04008979static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8980 u64 start, u64 num_bytes, u64 min_size,
8981 loff_t actual_len, u64 *alloc_hint,
8982 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008983{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008984 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8985 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008986 struct btrfs_root *root = BTRFS_I(inode)->root;
8987 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008988 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008989 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008990 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008991 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008992 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008993
Josef Bacik0af3d002010-06-21 14:48:16 -04008994 if (trans)
8995 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008996 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008997 if (own_trans) {
8998 trans = btrfs_start_transaction(root, 3);
8999 if (IS_ERR(trans)) {
9000 ret = PTR_ERR(trans);
9001 break;
9002 }
Yan Zhengd899e052008-10-30 14:25:28 -04009003 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009004
Chris Mason154ea282013-03-05 11:11:26 -05009005 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
9006 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04009007 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08009008 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009009 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009010 if (own_trans)
9011 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009012 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009013 }
9014
Yan Zhengd899e052008-10-30 14:25:28 -04009015 ret = insert_reserved_file_extent(trans, inode,
9016 cur_offset, ins.objectid,
9017 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009018 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009019 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009020 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04009021 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009022 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009023 btrfs_abort_transaction(trans, root, ret);
9024 if (own_trans)
9025 btrfs_end_transaction(trans, root);
9026 break;
9027 }
Chris Masona1ed8352009-09-11 12:27:37 -04009028 btrfs_drop_extent_cache(inode, cur_offset,
9029 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009030
Josef Bacik5dc562c2012-08-17 13:14:17 -04009031 em = alloc_extent_map();
9032 if (!em) {
9033 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9034 &BTRFS_I(inode)->runtime_flags);
9035 goto next;
9036 }
9037
9038 em->start = cur_offset;
9039 em->orig_start = cur_offset;
9040 em->len = ins.offset;
9041 em->block_start = ins.objectid;
9042 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009043 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009044 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009045 em->bdev = root->fs_info->fs_devices->latest_bdev;
9046 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9047 em->generation = trans->transid;
9048
9049 while (1) {
9050 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009051 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009052 write_unlock(&em_tree->lock);
9053 if (ret != -EEXIST)
9054 break;
9055 btrfs_drop_extent_cache(inode, cur_offset,
9056 cur_offset + ins.offset - 1,
9057 0);
9058 }
9059 free_extent_map(em);
9060next:
Yan Zhengd899e052008-10-30 14:25:28 -04009061 num_bytes -= ins.offset;
9062 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009063 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009064
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009065 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009066 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009067 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009068 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009069 (actual_len > inode->i_size) &&
9070 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009071 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009072 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009073 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009074 i_size = cur_offset;
9075 i_size_write(inode, i_size);
9076 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009077 }
9078
Yan Zhengd899e052008-10-30 14:25:28 -04009079 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009080
9081 if (ret) {
9082 btrfs_abort_transaction(trans, root, ret);
9083 if (own_trans)
9084 btrfs_end_transaction(trans, root);
9085 break;
9086 }
Yan Zhengd899e052008-10-30 14:25:28 -04009087
Josef Bacik0af3d002010-06-21 14:48:16 -04009088 if (own_trans)
9089 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009090 }
Yan Zhengd899e052008-10-30 14:25:28 -04009091 return ret;
9092}
9093
Josef Bacik0af3d002010-06-21 14:48:16 -04009094int btrfs_prealloc_file_range(struct inode *inode, int mode,
9095 u64 start, u64 num_bytes, u64 min_size,
9096 loff_t actual_len, u64 *alloc_hint)
9097{
9098 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9099 min_size, actual_len, alloc_hint,
9100 NULL);
9101}
9102
9103int btrfs_prealloc_file_range_trans(struct inode *inode,
9104 struct btrfs_trans_handle *trans, int mode,
9105 u64 start, u64 num_bytes, u64 min_size,
9106 loff_t actual_len, u64 *alloc_hint)
9107{
9108 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9109 min_size, actual_len, alloc_hint, trans);
9110}
9111
Chris Masone6dcd2d2008-07-17 12:53:50 -04009112static int btrfs_set_page_dirty(struct page *page)
9113{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009114 return __set_page_dirty_nobuffers(page);
9115}
9116
Al Viro10556cb2011-06-20 19:28:19 -04009117static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009118{
Li Zefanb83cc962010-12-20 16:04:08 +08009119 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009120 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009121
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009122 if (mask & MAY_WRITE &&
9123 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9124 if (btrfs_root_readonly(root))
9125 return -EROFS;
9126 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9127 return -EACCES;
9128 }
Al Viro2830ba72011-06-20 19:16:29 -04009129 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009130}
Chris Mason39279cc2007-06-12 06:35:45 -04009131
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009132static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9133{
9134 struct btrfs_trans_handle *trans;
9135 struct btrfs_root *root = BTRFS_I(dir)->root;
9136 struct inode *inode = NULL;
9137 u64 objectid;
9138 u64 index;
9139 int ret = 0;
9140
9141 /*
9142 * 5 units required for adding orphan entry
9143 */
9144 trans = btrfs_start_transaction(root, 5);
9145 if (IS_ERR(trans))
9146 return PTR_ERR(trans);
9147
9148 ret = btrfs_find_free_ino(root, &objectid);
9149 if (ret)
9150 goto out;
9151
9152 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9153 btrfs_ino(dir), objectid, mode, &index);
9154 if (IS_ERR(inode)) {
9155 ret = PTR_ERR(inode);
9156 inode = NULL;
9157 goto out;
9158 }
9159
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009160 inode->i_fop = &btrfs_file_operations;
9161 inode->i_op = &btrfs_file_inode_operations;
9162
9163 inode->i_mapping->a_ops = &btrfs_aops;
9164 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9165 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9166
Chris Masonb0d5d102014-09-08 13:08:51 -07009167 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9168 if (ret)
9169 goto out_inode;
9170
9171 ret = btrfs_update_inode(trans, root, inode);
9172 if (ret)
9173 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009174 ret = btrfs_orphan_add(trans, inode);
9175 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009176 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009177
Filipe Manana5762b5c2014-08-01 00:10:32 +01009178 /*
9179 * We set number of links to 0 in btrfs_new_inode(), and here we set
9180 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9181 * through:
9182 *
9183 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9184 */
9185 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009186 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009187 d_tmpfile(dentry, inode);
9188 mark_inode_dirty(inode);
9189
9190out:
9191 btrfs_end_transaction(trans, root);
9192 if (ret)
9193 iput(inode);
9194 btrfs_balance_delayed_items(root);
9195 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009196 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009197
9198out_inode:
9199 unlock_new_inode(inode);
9200 goto out;
9201
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009202}
9203
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009204static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009205 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009206 .lookup = btrfs_lookup,
9207 .create = btrfs_create,
9208 .unlink = btrfs_unlink,
9209 .link = btrfs_link,
9210 .mkdir = btrfs_mkdir,
9211 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +02009212 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -04009213 .symlink = btrfs_symlink,
9214 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009215 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009216 .setxattr = btrfs_setxattr,
9217 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009218 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009219 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009220 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009221 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009222 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009223 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009224 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009225};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009226static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009227 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009228 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009229 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009230 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009231 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009232};
Yan, Zheng76dda932009-09-21 16:00:26 -04009233
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009234static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009235 .llseek = generic_file_llseek,
9236 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009237 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009238 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009239#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009240 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009241#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009242 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009243 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009244};
9245
Chris Masond1310b22008-01-24 16:13:08 -05009246static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009247 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009248 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009249 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009250 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009251 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009252 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009253 .set_bit_hook = btrfs_set_bit_hook,
9254 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009255 .merge_extent_hook = btrfs_merge_extent_hook,
9256 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009257};
9258
Chris Mason35054392009-01-21 13:11:13 -05009259/*
9260 * btrfs doesn't support the bmap operation because swapfiles
9261 * use bmap to make a mapping of extents in the file. They assume
9262 * these extents won't change over the life of the file and they
9263 * use the bmap result to do IO directly to the drive.
9264 *
9265 * the btrfs bmap call would return logical addresses that aren't
9266 * suitable for IO and they also will change frequently as COW
9267 * operations happen. So, swapfile + btrfs == corruption.
9268 *
9269 * For now we're avoiding this by dropping bmap.
9270 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009271static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009272 .readpage = btrfs_readpage,
9273 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04009274 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009275 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009276 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009277 .invalidatepage = btrfs_invalidatepage,
9278 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009279 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009280 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009281};
9282
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009283static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009284 .readpage = btrfs_readpage,
9285 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009286 .invalidatepage = btrfs_invalidatepage,
9287 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009288};
9289
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009290static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009291 .getattr = btrfs_getattr,
9292 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009293 .setxattr = btrfs_setxattr,
9294 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009295 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009296 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009297 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009298 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009299 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009300 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009301 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009302};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009303static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009304 .getattr = btrfs_getattr,
9305 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009306 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009307 .setxattr = btrfs_setxattr,
9308 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009309 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009310 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009311 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009312 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009313 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04009314};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009315static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009316 .readlink = generic_readlink,
9317 .follow_link = page_follow_link_light,
9318 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009319 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009320 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009321 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009322 .setxattr = btrfs_setxattr,
9323 .getxattr = btrfs_getxattr,
9324 .listxattr = btrfs_listxattr,
9325 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009326 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009327};
Yan, Zheng76dda932009-09-21 16:00:26 -04009328
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009329const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009330 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009331 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009332};