blob: 48a2886842b591e839ee4a030f62a3d2263f0817 [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 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400252 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400253 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
254 (!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;
Chris Mason7f366cf2009-03-12 20:12:45 -04001491 struct btrfs_root *root = BTRFS_I(inode)->root;
Wang Shilong47059d92014-07-03 18:22:07 +08001492 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001493
Wang Shilong47059d92014-07-03 18:22:07 +08001494 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001495 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001496 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001497 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001498 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001499 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001500 } else if (!btrfs_test_opt(root, COMPRESS) &&
1501 !(BTRFS_I(inode)->force_compress) &&
1502 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001503 ret = cow_file_range(inode, locked_page, start, end,
1504 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001505 } else {
1506 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1507 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001508 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001509 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001510 }
Chris Masonb888db22007-08-27 16:49:44 -04001511 return ret;
1512}
1513
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001514static void btrfs_split_extent_hook(struct inode *inode,
1515 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001516{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001517 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001518 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001519 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001520
Josef Bacik9e0baf62011-07-15 15:16:44 +00001521 spin_lock(&BTRFS_I(inode)->lock);
1522 BTRFS_I(inode)->outstanding_extents++;
1523 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001524}
1525
1526/*
1527 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1528 * extents so we can keep track of new extents that are just merged onto old
1529 * extents, such as when we are doing sequential writes, so we can properly
1530 * account for the metadata space we'll need.
1531 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001532static void btrfs_merge_extent_hook(struct inode *inode,
1533 struct extent_state *new,
1534 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001535{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001536 /* not delalloc, ignore it */
1537 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001538 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001539
Josef Bacik9e0baf62011-07-15 15:16:44 +00001540 spin_lock(&BTRFS_I(inode)->lock);
1541 BTRFS_I(inode)->outstanding_extents--;
1542 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001543}
1544
Miao Xieeb73c1b2013-05-15 07:48:22 +00001545static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1546 struct inode *inode)
1547{
1548 spin_lock(&root->delalloc_lock);
1549 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1550 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1551 &root->delalloc_inodes);
1552 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1553 &BTRFS_I(inode)->runtime_flags);
1554 root->nr_delalloc_inodes++;
1555 if (root->nr_delalloc_inodes == 1) {
1556 spin_lock(&root->fs_info->delalloc_root_lock);
1557 BUG_ON(!list_empty(&root->delalloc_root));
1558 list_add_tail(&root->delalloc_root,
1559 &root->fs_info->delalloc_roots);
1560 spin_unlock(&root->fs_info->delalloc_root_lock);
1561 }
1562 }
1563 spin_unlock(&root->delalloc_lock);
1564}
1565
1566static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1567 struct inode *inode)
1568{
1569 spin_lock(&root->delalloc_lock);
1570 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1571 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1572 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1573 &BTRFS_I(inode)->runtime_flags);
1574 root->nr_delalloc_inodes--;
1575 if (!root->nr_delalloc_inodes) {
1576 spin_lock(&root->fs_info->delalloc_root_lock);
1577 BUG_ON(list_empty(&root->delalloc_root));
1578 list_del_init(&root->delalloc_root);
1579 spin_unlock(&root->fs_info->delalloc_root_lock);
1580 }
1581 }
1582 spin_unlock(&root->delalloc_lock);
1583}
1584
Chris Masond352ac62008-09-29 15:18:18 -04001585/*
1586 * extent_io.c set_bit_hook, used to track delayed allocation
1587 * bytes in this file, and to maintain the list of inodes that
1588 * have pending delalloc work to be done.
1589 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001590static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001591 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001592{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001593
Wang Shilong47059d92014-07-03 18:22:07 +08001594 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1595 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001596 /*
1597 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001598 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001599 * bit, which is only set or cleared with irqs on
1600 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001601 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001602 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001603 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001604 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001605
Josef Bacik9e0baf62011-07-15 15:16:44 +00001606 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001607 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001608 } else {
1609 spin_lock(&BTRFS_I(inode)->lock);
1610 BTRFS_I(inode)->outstanding_extents++;
1611 spin_unlock(&BTRFS_I(inode)->lock);
1612 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001613
Miao Xie963d6782013-01-29 10:10:51 +00001614 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1615 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001616 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001617 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001618 if (*bits & EXTENT_DEFRAG)
1619 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001620 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001621 &BTRFS_I(inode)->runtime_flags))
1622 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001623 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001624 }
Chris Mason291d6732008-01-29 15:55:23 -05001625}
1626
Chris Masond352ac62008-09-29 15:18:18 -04001627/*
1628 * extent_io.c clear_bit_hook, see set_bit_hook for why
1629 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001630static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001631 struct extent_state *state,
1632 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001633{
Wang Shilong47059d92014-07-03 18:22:07 +08001634 u64 len = state->end + 1 - state->start;
1635
1636 spin_lock(&BTRFS_I(inode)->lock);
1637 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1638 BTRFS_I(inode)->defrag_bytes -= len;
1639 spin_unlock(&BTRFS_I(inode)->lock);
1640
Chris Mason75eff682008-12-15 15:54:40 -05001641 /*
1642 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001643 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001644 * bit, which is only set or cleared with irqs on
1645 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001646 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001647 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001648 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001649
Josef Bacik9e0baf62011-07-15 15:16:44 +00001650 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001651 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001652 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1653 spin_lock(&BTRFS_I(inode)->lock);
1654 BTRFS_I(inode)->outstanding_extents--;
1655 spin_unlock(&BTRFS_I(inode)->lock);
1656 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001657
Josef Bacikb6d08f02013-09-27 14:57:43 -04001658 /*
1659 * We don't reserve metadata space for space cache inodes so we
1660 * don't need to call dellalloc_release_metadata if there is an
1661 * error.
1662 */
1663 if (*bits & EXTENT_DO_ACCOUNTING &&
1664 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001665 btrfs_delalloc_release_metadata(inode, len);
1666
Josef Bacik0cb59c92010-07-02 12:14:14 -04001667 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001668 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001669 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001670
Miao Xie963d6782013-01-29 10:10:51 +00001671 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1672 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001673 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001674 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001675 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001676 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001677 &BTRFS_I(inode)->runtime_flags))
1678 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001679 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001680 }
Chris Mason291d6732008-01-29 15:55:23 -05001681}
1682
Chris Masond352ac62008-09-29 15:18:18 -04001683/*
1684 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1685 * we don't create bios that span stripes or chunks
1686 */
David Woodhouse64a16702009-07-15 23:29:37 +01001687int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001688 size_t size, struct bio *bio,
1689 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001690{
1691 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001692 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001693 u64 length = 0;
1694 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001695 int ret;
1696
Chris Mason771ed682008-11-06 22:02:51 -05001697 if (bio_flags & EXTENT_BIO_COMPRESSED)
1698 return 0;
1699
Kent Overstreet4f024f32013-10-11 15:44:27 -07001700 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001701 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001702 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001703 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001704 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001705 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001706 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001707 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001708 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001709}
1710
Chris Masond352ac62008-09-29 15:18:18 -04001711/*
1712 * in order to insert checksums into the metadata in large chunks,
1713 * we wait until bio submission time. All the pages in the bio are
1714 * checksummed and sums are attached onto the ordered extent record.
1715 *
1716 * At IO completion time the cums attached on the ordered extent record
1717 * are inserted into the btree
1718 */
Chris Masond3977122009-01-05 21:25:51 -05001719static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1720 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001721 unsigned long bio_flags,
1722 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001723{
Chris Mason065631f2008-02-20 12:07:25 -05001724 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001725 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001726
Chris Masond20f7042008-12-08 16:58:54 -05001727 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001728 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001729 return 0;
1730}
Chris Masone0156402008-04-16 11:15:20 -04001731
Chris Mason4a69a412008-11-06 22:03:00 -05001732/*
1733 * in order to insert checksums into the metadata in large chunks,
1734 * we wait until bio submission time. All the pages in the bio are
1735 * checksummed and sums are attached onto the ordered extent record.
1736 *
1737 * At IO completion time the cums attached on the ordered extent record
1738 * are inserted into the btree
1739 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001740static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001741 int mirror_num, unsigned long bio_flags,
1742 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001743{
1744 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001745 int ret;
1746
1747 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1748 if (ret)
1749 bio_endio(bio, ret);
1750 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001751}
1752
Chris Masond352ac62008-09-29 15:18:18 -04001753/*
Chris Masoncad321a2008-12-17 14:51:42 -05001754 * extent_io.c submission hook. This does the right thing for csum calculation
1755 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001756 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001757static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001758 int mirror_num, unsigned long bio_flags,
1759 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001760{
1761 struct btrfs_root *root = BTRFS_I(inode)->root;
1762 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001763 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001764 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001765 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001766
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001767 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001768
Liu Bo83eea1f2012-07-10 05:28:39 -06001769 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001770 metadata = 2;
1771
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001772 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001773 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1774 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001775 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001776
Chris Masond20f7042008-12-08 16:58:54 -05001777 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001778 ret = btrfs_submit_compressed_read(inode, bio,
1779 mirror_num,
1780 bio_flags);
1781 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001782 } else if (!skip_sum) {
1783 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1784 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001785 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001786 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001787 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001788 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001789 /* csum items have already been cloned */
1790 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1791 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001792 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001793 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001794 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001795 bio_flags, bio_offset,
1796 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001797 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001798 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001799 } else if (!skip_sum) {
1800 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1801 if (ret)
1802 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001803 }
1804
Chris Mason0b86a832008-03-24 15:01:56 -04001805mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001806 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1807
1808out:
1809 if (ret < 0)
1810 bio_endio(bio, ret);
1811 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001812}
Chris Mason6885f302008-02-20 16:11:05 -05001813
Chris Masond352ac62008-09-29 15:18:18 -04001814/*
1815 * given a list of ordered sums record them in the inode. This happens
1816 * at IO completion time based on sums calculated at bio submission time.
1817 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001818static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001819 struct inode *inode, u64 file_offset,
1820 struct list_head *list)
1821{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001822 struct btrfs_ordered_sum *sum;
1823
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001824 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001825 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001826 btrfs_csum_file_blocks(trans,
1827 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001828 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001829 }
1830 return 0;
1831}
1832
Josef Bacik2ac55d42010-02-03 19:33:23 +00001833int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1834 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001835{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001836 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001837 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001838 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001839}
1840
Chris Masond352ac62008-09-29 15:18:18 -04001841/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001842struct btrfs_writepage_fixup {
1843 struct page *page;
1844 struct btrfs_work work;
1845};
1846
Christoph Hellwigb2950862008-12-02 09:54:17 -05001847static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001848{
1849 struct btrfs_writepage_fixup *fixup;
1850 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001851 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001852 struct page *page;
1853 struct inode *inode;
1854 u64 page_start;
1855 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001856 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001857
1858 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1859 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001860again:
Chris Mason247e7432008-07-17 12:53:51 -04001861 lock_page(page);
1862 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1863 ClearPageChecked(page);
1864 goto out_page;
1865 }
1866
1867 inode = page->mapping->host;
1868 page_start = page_offset(page);
1869 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1870
Josef Bacik2ac55d42010-02-03 19:33:23 +00001871 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001872 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001873
1874 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001875 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001876 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001877
1878 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1879 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001880 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1881 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001882 unlock_page(page);
1883 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001884 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001885 goto again;
1886 }
Chris Mason247e7432008-07-17 12:53:51 -04001887
Jeff Mahoney87826df2012-02-15 16:23:57 +01001888 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1889 if (ret) {
1890 mapping_set_error(page->mapping, ret);
1891 end_extent_writepage(page, ret, page_start, page_end);
1892 ClearPageChecked(page);
1893 goto out;
1894 }
1895
Josef Bacik2ac55d42010-02-03 19:33:23 +00001896 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001897 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001898 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001899out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001900 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1901 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001902out_page:
1903 unlock_page(page);
1904 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001905 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001906}
1907
1908/*
1909 * There are a few paths in the higher layers of the kernel that directly
1910 * set the page dirty bit without asking the filesystem if it is a
1911 * good idea. This causes problems because we want to make sure COW
1912 * properly happens and the data=ordered rules are followed.
1913 *
Chris Masonc8b97812008-10-29 14:49:59 -04001914 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001915 * hasn't been properly setup for IO. We kick off an async process
1916 * to fix it up. The async helper will wait for ordered extents, set
1917 * the delalloc bit and make it safe to write the page.
1918 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001919static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001920{
1921 struct inode *inode = page->mapping->host;
1922 struct btrfs_writepage_fixup *fixup;
1923 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001924
Chris Mason8b62b722009-09-02 16:53:46 -04001925 /* this page is properly in the ordered list */
1926 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001927 return 0;
1928
1929 if (PageChecked(page))
1930 return -EAGAIN;
1931
1932 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1933 if (!fixup)
1934 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001935
Chris Mason247e7432008-07-17 12:53:51 -04001936 SetPageChecked(page);
1937 page_cache_get(page);
Liu Bo9e0af232014-08-15 23:36:53 +08001938 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
1939 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04001940 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08001941 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001942 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001943}
1944
Yan Zhengd899e052008-10-30 14:25:28 -04001945static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1946 struct inode *inode, u64 file_pos,
1947 u64 disk_bytenr, u64 disk_num_bytes,
1948 u64 num_bytes, u64 ram_bytes,
1949 u8 compression, u8 encryption,
1950 u16 other_encoding, int extent_type)
1951{
1952 struct btrfs_root *root = BTRFS_I(inode)->root;
1953 struct btrfs_file_extent_item *fi;
1954 struct btrfs_path *path;
1955 struct extent_buffer *leaf;
1956 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001957 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04001958 int ret;
1959
1960 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001961 if (!path)
1962 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001963
Chris Masona1ed8352009-09-11 12:27:37 -04001964 /*
1965 * we may be replacing one extent in the tree with another.
1966 * The new extent is pinned in the extent map, and we don't want
1967 * to drop it from the cache until it is completely in the btree.
1968 *
1969 * So, tell btrfs_drop_extents to leave this extent in the cache.
1970 * the caller is expected to unpin it and allow it to be merged
1971 * with the others.
1972 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001973 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
1974 file_pos + num_bytes, NULL, 0,
1975 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001976 if (ret)
1977 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001978
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001979 if (!extent_inserted) {
1980 ins.objectid = btrfs_ino(inode);
1981 ins.offset = file_pos;
1982 ins.type = BTRFS_EXTENT_DATA_KEY;
1983
1984 path->leave_spinning = 1;
1985 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1986 sizeof(*fi));
1987 if (ret)
1988 goto out;
1989 }
Yan Zhengd899e052008-10-30 14:25:28 -04001990 leaf = path->nodes[0];
1991 fi = btrfs_item_ptr(leaf, path->slots[0],
1992 struct btrfs_file_extent_item);
1993 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1994 btrfs_set_file_extent_type(leaf, fi, extent_type);
1995 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1996 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1997 btrfs_set_file_extent_offset(leaf, fi, 0);
1998 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1999 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2000 btrfs_set_file_extent_compression(leaf, fi, compression);
2001 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2002 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002003
Yan Zhengd899e052008-10-30 14:25:28 -04002004 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002005 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002006
2007 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002008
2009 ins.objectid = disk_bytenr;
2010 ins.offset = disk_num_bytes;
2011 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002012 ret = btrfs_alloc_reserved_file_extent(trans, root,
2013 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002014 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002015out:
Yan Zhengd899e052008-10-30 14:25:28 -04002016 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002017
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002018 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002019}
2020
Liu Bo38c227d2013-01-29 03:18:40 +00002021/* snapshot-aware defrag */
2022struct sa_defrag_extent_backref {
2023 struct rb_node node;
2024 struct old_sa_defrag_extent *old;
2025 u64 root_id;
2026 u64 inum;
2027 u64 file_pos;
2028 u64 extent_offset;
2029 u64 num_bytes;
2030 u64 generation;
2031};
2032
2033struct old_sa_defrag_extent {
2034 struct list_head list;
2035 struct new_sa_defrag_extent *new;
2036
2037 u64 extent_offset;
2038 u64 bytenr;
2039 u64 offset;
2040 u64 len;
2041 int count;
2042};
2043
2044struct new_sa_defrag_extent {
2045 struct rb_root root;
2046 struct list_head head;
2047 struct btrfs_path *path;
2048 struct inode *inode;
2049 u64 file_pos;
2050 u64 len;
2051 u64 bytenr;
2052 u64 disk_len;
2053 u8 compress_type;
2054};
2055
2056static int backref_comp(struct sa_defrag_extent_backref *b1,
2057 struct sa_defrag_extent_backref *b2)
2058{
2059 if (b1->root_id < b2->root_id)
2060 return -1;
2061 else if (b1->root_id > b2->root_id)
2062 return 1;
2063
2064 if (b1->inum < b2->inum)
2065 return -1;
2066 else if (b1->inum > b2->inum)
2067 return 1;
2068
2069 if (b1->file_pos < b2->file_pos)
2070 return -1;
2071 else if (b1->file_pos > b2->file_pos)
2072 return 1;
2073
2074 /*
2075 * [------------------------------] ===> (a range of space)
2076 * |<--->| |<---->| =============> (fs/file tree A)
2077 * |<---------------------------->| ===> (fs/file tree B)
2078 *
2079 * A range of space can refer to two file extents in one tree while
2080 * refer to only one file extent in another tree.
2081 *
2082 * So we may process a disk offset more than one time(two extents in A)
2083 * and locate at the same extent(one extent in B), then insert two same
2084 * backrefs(both refer to the extent in B).
2085 */
2086 return 0;
2087}
2088
2089static void backref_insert(struct rb_root *root,
2090 struct sa_defrag_extent_backref *backref)
2091{
2092 struct rb_node **p = &root->rb_node;
2093 struct rb_node *parent = NULL;
2094 struct sa_defrag_extent_backref *entry;
2095 int ret;
2096
2097 while (*p) {
2098 parent = *p;
2099 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2100
2101 ret = backref_comp(backref, entry);
2102 if (ret < 0)
2103 p = &(*p)->rb_left;
2104 else
2105 p = &(*p)->rb_right;
2106 }
2107
2108 rb_link_node(&backref->node, parent, p);
2109 rb_insert_color(&backref->node, root);
2110}
2111
2112/*
2113 * Note the backref might has changed, and in this case we just return 0.
2114 */
2115static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2116 void *ctx)
2117{
2118 struct btrfs_file_extent_item *extent;
2119 struct btrfs_fs_info *fs_info;
2120 struct old_sa_defrag_extent *old = ctx;
2121 struct new_sa_defrag_extent *new = old->new;
2122 struct btrfs_path *path = new->path;
2123 struct btrfs_key key;
2124 struct btrfs_root *root;
2125 struct sa_defrag_extent_backref *backref;
2126 struct extent_buffer *leaf;
2127 struct inode *inode = new->inode;
2128 int slot;
2129 int ret;
2130 u64 extent_offset;
2131 u64 num_bytes;
2132
2133 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2134 inum == btrfs_ino(inode))
2135 return 0;
2136
2137 key.objectid = root_id;
2138 key.type = BTRFS_ROOT_ITEM_KEY;
2139 key.offset = (u64)-1;
2140
2141 fs_info = BTRFS_I(inode)->root->fs_info;
2142 root = btrfs_read_fs_root_no_name(fs_info, &key);
2143 if (IS_ERR(root)) {
2144 if (PTR_ERR(root) == -ENOENT)
2145 return 0;
2146 WARN_ON(1);
2147 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2148 inum, offset, root_id);
2149 return PTR_ERR(root);
2150 }
2151
2152 key.objectid = inum;
2153 key.type = BTRFS_EXTENT_DATA_KEY;
2154 if (offset > (u64)-1 << 32)
2155 key.offset = 0;
2156 else
2157 key.offset = offset;
2158
2159 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302160 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002161 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002162 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002163
2164 while (1) {
2165 cond_resched();
2166
2167 leaf = path->nodes[0];
2168 slot = path->slots[0];
2169
2170 if (slot >= btrfs_header_nritems(leaf)) {
2171 ret = btrfs_next_leaf(root, path);
2172 if (ret < 0) {
2173 goto out;
2174 } else if (ret > 0) {
2175 ret = 0;
2176 goto out;
2177 }
2178 continue;
2179 }
2180
2181 path->slots[0]++;
2182
2183 btrfs_item_key_to_cpu(leaf, &key, slot);
2184
2185 if (key.objectid > inum)
2186 goto out;
2187
2188 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2189 continue;
2190
2191 extent = btrfs_item_ptr(leaf, slot,
2192 struct btrfs_file_extent_item);
2193
2194 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2195 continue;
2196
Liu Boe68afa42013-07-01 22:13:26 +08002197 /*
2198 * 'offset' refers to the exact key.offset,
2199 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2200 * (key.offset - extent_offset).
2201 */
2202 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002203 continue;
2204
Liu Boe68afa42013-07-01 22:13:26 +08002205 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002206 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002207
Liu Bo38c227d2013-01-29 03:18:40 +00002208 if (extent_offset >= old->extent_offset + old->offset +
2209 old->len || extent_offset + num_bytes <=
2210 old->extent_offset + old->offset)
2211 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002212 break;
2213 }
2214
2215 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2216 if (!backref) {
2217 ret = -ENOENT;
2218 goto out;
2219 }
2220
2221 backref->root_id = root_id;
2222 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002223 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002224 backref->num_bytes = num_bytes;
2225 backref->extent_offset = extent_offset;
2226 backref->generation = btrfs_file_extent_generation(leaf, extent);
2227 backref->old = old;
2228 backref_insert(&new->root, backref);
2229 old->count++;
2230out:
2231 btrfs_release_path(path);
2232 WARN_ON(ret);
2233 return ret;
2234}
2235
2236static noinline bool record_extent_backrefs(struct btrfs_path *path,
2237 struct new_sa_defrag_extent *new)
2238{
2239 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2240 struct old_sa_defrag_extent *old, *tmp;
2241 int ret;
2242
2243 new->path = path;
2244
2245 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002246 ret = iterate_inodes_from_logical(old->bytenr +
2247 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002248 path, record_one_backref,
2249 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002250 if (ret < 0 && ret != -ENOENT)
2251 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002252
2253 /* no backref to be processed for this extent */
2254 if (!old->count) {
2255 list_del(&old->list);
2256 kfree(old);
2257 }
2258 }
2259
2260 if (list_empty(&new->head))
2261 return false;
2262
2263 return true;
2264}
2265
2266static int relink_is_mergable(struct extent_buffer *leaf,
2267 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002268 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002269{
Liu Bo116e0022013-08-02 16:30:40 +08002270 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002271 return 0;
2272
2273 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2274 return 0;
2275
Liu Bo116e0022013-08-02 16:30:40 +08002276 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2277 return 0;
2278
2279 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002280 btrfs_file_extent_other_encoding(leaf, fi))
2281 return 0;
2282
2283 return 1;
2284}
2285
2286/*
2287 * Note the backref might has changed, and in this case we just return 0.
2288 */
2289static noinline int relink_extent_backref(struct btrfs_path *path,
2290 struct sa_defrag_extent_backref *prev,
2291 struct sa_defrag_extent_backref *backref)
2292{
2293 struct btrfs_file_extent_item *extent;
2294 struct btrfs_file_extent_item *item;
2295 struct btrfs_ordered_extent *ordered;
2296 struct btrfs_trans_handle *trans;
2297 struct btrfs_fs_info *fs_info;
2298 struct btrfs_root *root;
2299 struct btrfs_key key;
2300 struct extent_buffer *leaf;
2301 struct old_sa_defrag_extent *old = backref->old;
2302 struct new_sa_defrag_extent *new = old->new;
2303 struct inode *src_inode = new->inode;
2304 struct inode *inode;
2305 struct extent_state *cached = NULL;
2306 int ret = 0;
2307 u64 start;
2308 u64 len;
2309 u64 lock_start;
2310 u64 lock_end;
2311 bool merge = false;
2312 int index;
2313
2314 if (prev && prev->root_id == backref->root_id &&
2315 prev->inum == backref->inum &&
2316 prev->file_pos + prev->num_bytes == backref->file_pos)
2317 merge = true;
2318
2319 /* step 1: get root */
2320 key.objectid = backref->root_id;
2321 key.type = BTRFS_ROOT_ITEM_KEY;
2322 key.offset = (u64)-1;
2323
2324 fs_info = BTRFS_I(src_inode)->root->fs_info;
2325 index = srcu_read_lock(&fs_info->subvol_srcu);
2326
2327 root = btrfs_read_fs_root_no_name(fs_info, &key);
2328 if (IS_ERR(root)) {
2329 srcu_read_unlock(&fs_info->subvol_srcu, index);
2330 if (PTR_ERR(root) == -ENOENT)
2331 return 0;
2332 return PTR_ERR(root);
2333 }
Liu Bo38c227d2013-01-29 03:18:40 +00002334
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002335 if (btrfs_root_readonly(root)) {
2336 srcu_read_unlock(&fs_info->subvol_srcu, index);
2337 return 0;
2338 }
2339
Liu Bo38c227d2013-01-29 03:18:40 +00002340 /* step 2: get inode */
2341 key.objectid = backref->inum;
2342 key.type = BTRFS_INODE_ITEM_KEY;
2343 key.offset = 0;
2344
2345 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2346 if (IS_ERR(inode)) {
2347 srcu_read_unlock(&fs_info->subvol_srcu, index);
2348 return 0;
2349 }
2350
2351 srcu_read_unlock(&fs_info->subvol_srcu, index);
2352
2353 /* step 3: relink backref */
2354 lock_start = backref->file_pos;
2355 lock_end = backref->file_pos + backref->num_bytes - 1;
2356 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2357 0, &cached);
2358
2359 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2360 if (ordered) {
2361 btrfs_put_ordered_extent(ordered);
2362 goto out_unlock;
2363 }
2364
2365 trans = btrfs_join_transaction(root);
2366 if (IS_ERR(trans)) {
2367 ret = PTR_ERR(trans);
2368 goto out_unlock;
2369 }
2370
2371 key.objectid = backref->inum;
2372 key.type = BTRFS_EXTENT_DATA_KEY;
2373 key.offset = backref->file_pos;
2374
2375 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2376 if (ret < 0) {
2377 goto out_free_path;
2378 } else if (ret > 0) {
2379 ret = 0;
2380 goto out_free_path;
2381 }
2382
2383 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2384 struct btrfs_file_extent_item);
2385
2386 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2387 backref->generation)
2388 goto out_free_path;
2389
2390 btrfs_release_path(path);
2391
2392 start = backref->file_pos;
2393 if (backref->extent_offset < old->extent_offset + old->offset)
2394 start += old->extent_offset + old->offset -
2395 backref->extent_offset;
2396
2397 len = min(backref->extent_offset + backref->num_bytes,
2398 old->extent_offset + old->offset + old->len);
2399 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2400
2401 ret = btrfs_drop_extents(trans, root, inode, start,
2402 start + len, 1);
2403 if (ret)
2404 goto out_free_path;
2405again:
2406 key.objectid = btrfs_ino(inode);
2407 key.type = BTRFS_EXTENT_DATA_KEY;
2408 key.offset = start;
2409
Liu Boa09a0a72013-03-11 09:20:58 +00002410 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002411 if (merge) {
2412 struct btrfs_file_extent_item *fi;
2413 u64 extent_len;
2414 struct btrfs_key found_key;
2415
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002416 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002417 if (ret < 0)
2418 goto out_free_path;
2419
2420 path->slots[0]--;
2421 leaf = path->nodes[0];
2422 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2423
2424 fi = btrfs_item_ptr(leaf, path->slots[0],
2425 struct btrfs_file_extent_item);
2426 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2427
Liu Bo116e0022013-08-02 16:30:40 +08002428 if (extent_len + found_key.offset == start &&
2429 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002430 btrfs_set_file_extent_num_bytes(leaf, fi,
2431 extent_len + len);
2432 btrfs_mark_buffer_dirty(leaf);
2433 inode_add_bytes(inode, len);
2434
2435 ret = 1;
2436 goto out_free_path;
2437 } else {
2438 merge = false;
2439 btrfs_release_path(path);
2440 goto again;
2441 }
2442 }
2443
2444 ret = btrfs_insert_empty_item(trans, root, path, &key,
2445 sizeof(*extent));
2446 if (ret) {
2447 btrfs_abort_transaction(trans, root, ret);
2448 goto out_free_path;
2449 }
2450
2451 leaf = path->nodes[0];
2452 item = btrfs_item_ptr(leaf, path->slots[0],
2453 struct btrfs_file_extent_item);
2454 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2455 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2456 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2457 btrfs_set_file_extent_num_bytes(leaf, item, len);
2458 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2459 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2460 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2461 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2462 btrfs_set_file_extent_encryption(leaf, item, 0);
2463 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2464
2465 btrfs_mark_buffer_dirty(leaf);
2466 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002467 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002468
2469 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2470 new->disk_len, 0,
2471 backref->root_id, backref->inum,
2472 new->file_pos, 0); /* start - extent_offset */
2473 if (ret) {
2474 btrfs_abort_transaction(trans, root, ret);
2475 goto out_free_path;
2476 }
2477
2478 ret = 1;
2479out_free_path:
2480 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002481 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002482 btrfs_end_transaction(trans, root);
2483out_unlock:
2484 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2485 &cached, GFP_NOFS);
2486 iput(inode);
2487 return ret;
2488}
2489
Liu Bo6f519562013-10-29 10:45:05 +08002490static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2491{
2492 struct old_sa_defrag_extent *old, *tmp;
2493
2494 if (!new)
2495 return;
2496
2497 list_for_each_entry_safe(old, tmp, &new->head, list) {
2498 list_del(&old->list);
2499 kfree(old);
2500 }
2501 kfree(new);
2502}
2503
Liu Bo38c227d2013-01-29 03:18:40 +00002504static void relink_file_extents(struct new_sa_defrag_extent *new)
2505{
2506 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002507 struct sa_defrag_extent_backref *backref;
2508 struct sa_defrag_extent_backref *prev = NULL;
2509 struct inode *inode;
2510 struct btrfs_root *root;
2511 struct rb_node *node;
2512 int ret;
2513
2514 inode = new->inode;
2515 root = BTRFS_I(inode)->root;
2516
2517 path = btrfs_alloc_path();
2518 if (!path)
2519 return;
2520
2521 if (!record_extent_backrefs(path, new)) {
2522 btrfs_free_path(path);
2523 goto out;
2524 }
2525 btrfs_release_path(path);
2526
2527 while (1) {
2528 node = rb_first(&new->root);
2529 if (!node)
2530 break;
2531 rb_erase(node, &new->root);
2532
2533 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2534
2535 ret = relink_extent_backref(path, prev, backref);
2536 WARN_ON(ret < 0);
2537
2538 kfree(prev);
2539
2540 if (ret == 1)
2541 prev = backref;
2542 else
2543 prev = NULL;
2544 cond_resched();
2545 }
2546 kfree(prev);
2547
2548 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002549out:
Liu Bo6f519562013-10-29 10:45:05 +08002550 free_sa_defrag_extent(new);
2551
Liu Bo38c227d2013-01-29 03:18:40 +00002552 atomic_dec(&root->fs_info->defrag_running);
2553 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002554}
2555
2556static struct new_sa_defrag_extent *
2557record_old_file_extents(struct inode *inode,
2558 struct btrfs_ordered_extent *ordered)
2559{
2560 struct btrfs_root *root = BTRFS_I(inode)->root;
2561 struct btrfs_path *path;
2562 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002563 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002564 struct new_sa_defrag_extent *new;
2565 int ret;
2566
2567 new = kmalloc(sizeof(*new), GFP_NOFS);
2568 if (!new)
2569 return NULL;
2570
2571 new->inode = inode;
2572 new->file_pos = ordered->file_offset;
2573 new->len = ordered->len;
2574 new->bytenr = ordered->start;
2575 new->disk_len = ordered->disk_len;
2576 new->compress_type = ordered->compress_type;
2577 new->root = RB_ROOT;
2578 INIT_LIST_HEAD(&new->head);
2579
2580 path = btrfs_alloc_path();
2581 if (!path)
2582 goto out_kfree;
2583
2584 key.objectid = btrfs_ino(inode);
2585 key.type = BTRFS_EXTENT_DATA_KEY;
2586 key.offset = new->file_pos;
2587
2588 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2589 if (ret < 0)
2590 goto out_free_path;
2591 if (ret > 0 && path->slots[0] > 0)
2592 path->slots[0]--;
2593
2594 /* find out all the old extents for the file range */
2595 while (1) {
2596 struct btrfs_file_extent_item *extent;
2597 struct extent_buffer *l;
2598 int slot;
2599 u64 num_bytes;
2600 u64 offset;
2601 u64 end;
2602 u64 disk_bytenr;
2603 u64 extent_offset;
2604
2605 l = path->nodes[0];
2606 slot = path->slots[0];
2607
2608 if (slot >= btrfs_header_nritems(l)) {
2609 ret = btrfs_next_leaf(root, path);
2610 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002611 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002612 else if (ret > 0)
2613 break;
2614 continue;
2615 }
2616
2617 btrfs_item_key_to_cpu(l, &key, slot);
2618
2619 if (key.objectid != btrfs_ino(inode))
2620 break;
2621 if (key.type != BTRFS_EXTENT_DATA_KEY)
2622 break;
2623 if (key.offset >= new->file_pos + new->len)
2624 break;
2625
2626 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2627
2628 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2629 if (key.offset + num_bytes < new->file_pos)
2630 goto next;
2631
2632 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2633 if (!disk_bytenr)
2634 goto next;
2635
2636 extent_offset = btrfs_file_extent_offset(l, extent);
2637
2638 old = kmalloc(sizeof(*old), GFP_NOFS);
2639 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002640 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002641
2642 offset = max(new->file_pos, key.offset);
2643 end = min(new->file_pos + new->len, key.offset + num_bytes);
2644
2645 old->bytenr = disk_bytenr;
2646 old->extent_offset = extent_offset;
2647 old->offset = offset - key.offset;
2648 old->len = end - offset;
2649 old->new = new;
2650 old->count = 0;
2651 list_add_tail(&old->list, &new->head);
2652next:
2653 path->slots[0]++;
2654 cond_resched();
2655 }
2656
2657 btrfs_free_path(path);
2658 atomic_inc(&root->fs_info->defrag_running);
2659
2660 return new;
2661
Liu Bo38c227d2013-01-29 03:18:40 +00002662out_free_path:
2663 btrfs_free_path(path);
2664out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002665 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002666 return NULL;
2667}
2668
Miao Xiee570fd22014-06-19 10:42:50 +08002669static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2670 u64 start, u64 len)
2671{
2672 struct btrfs_block_group_cache *cache;
2673
2674 cache = btrfs_lookup_block_group(root->fs_info, start);
2675 ASSERT(cache);
2676
2677 spin_lock(&cache->lock);
2678 cache->delalloc_bytes -= len;
2679 spin_unlock(&cache->lock);
2680
2681 btrfs_put_block_group(cache);
2682}
2683
Chris Masond352ac62008-09-29 15:18:18 -04002684/* as ordered data IO finishes, this gets called so we can finish
2685 * an ordered extent if the range of bytes in the file it covers are
2686 * fully written.
2687 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002688static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002689{
Josef Bacik5fd02042012-05-02 14:00:54 -04002690 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002691 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002692 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002693 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002694 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002695 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002696 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002697 int ret = 0;
2698 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002699 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002700 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002701
Liu Bo83eea1f2012-07-10 05:28:39 -06002702 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002703
Josef Bacik5fd02042012-05-02 14:00:54 -04002704 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2705 ret = -EIO;
2706 goto out;
2707 }
2708
Josef Bacik77cef2e2013-08-29 13:57:21 -04002709 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2710 truncated = true;
2711 logical_len = ordered_extent->truncated_len;
2712 /* Truncated the entire extent, don't bother adding */
2713 if (!logical_len)
2714 goto out;
2715 }
2716
Yan, Zhengc2167752009-11-12 09:34:21 +00002717 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002718 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002719 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2720 if (nolock)
2721 trans = btrfs_join_transaction_nolock(root);
2722 else
2723 trans = btrfs_join_transaction(root);
2724 if (IS_ERR(trans)) {
2725 ret = PTR_ERR(trans);
2726 trans = NULL;
2727 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002728 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002729 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2730 ret = btrfs_update_inode_fallback(trans, root, inode);
2731 if (ret) /* -ENOMEM or corruption */
2732 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002733 goto out;
2734 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002735
Josef Bacik2ac55d42010-02-03 19:33:23 +00002736 lock_extent_bits(io_tree, ordered_extent->file_offset,
2737 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002738 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002739
Liu Bo38c227d2013-01-29 03:18:40 +00002740 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2741 ordered_extent->file_offset + ordered_extent->len - 1,
2742 EXTENT_DEFRAG, 1, cached_state);
2743 if (ret) {
2744 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002745 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002746 /* the inode is shared */
2747 new = record_old_file_extents(inode, ordered_extent);
2748
2749 clear_extent_bit(io_tree, ordered_extent->file_offset,
2750 ordered_extent->file_offset + ordered_extent->len - 1,
2751 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2752 }
2753
Josef Bacik0cb59c92010-07-02 12:14:14 -04002754 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002755 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002756 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002757 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002758 if (IS_ERR(trans)) {
2759 ret = PTR_ERR(trans);
2760 trans = NULL;
2761 goto out_unlock;
2762 }
Chris Masona79b7d42014-05-22 16:18:52 -07002763
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002764 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002765
Chris Masonc8b97812008-10-29 14:49:59 -04002766 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002767 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002768 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002769 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002770 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002771 ordered_extent->file_offset,
2772 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002773 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002774 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002775 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002776 ret = insert_reserved_file_extent(trans, inode,
2777 ordered_extent->file_offset,
2778 ordered_extent->start,
2779 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002780 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002781 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002782 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002783 if (!ret)
2784 btrfs_release_delalloc_bytes(root,
2785 ordered_extent->start,
2786 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002787 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002788 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2789 ordered_extent->file_offset, ordered_extent->len,
2790 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002791 if (ret < 0) {
2792 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002793 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002794 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002795
Chris Masone6dcd2d2008-07-17 12:53:50 -04002796 add_pending_csums(trans, inode, ordered_extent->file_offset,
2797 &ordered_extent->list);
2798
Josef Bacik6c760c02012-11-09 10:53:21 -05002799 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2800 ret = btrfs_update_inode_fallback(trans, root, inode);
2801 if (ret) { /* -ENOMEM or corruption */
2802 btrfs_abort_transaction(trans, root, ret);
2803 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002804 }
2805 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002806out_unlock:
2807 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2808 ordered_extent->file_offset +
2809 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002810out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002811 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002812 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002813 if (trans)
2814 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002815
Josef Bacik77cef2e2013-08-29 13:57:21 -04002816 if (ret || truncated) {
2817 u64 start, end;
2818
2819 if (truncated)
2820 start = ordered_extent->file_offset + logical_len;
2821 else
2822 start = ordered_extent->file_offset;
2823 end = ordered_extent->file_offset + ordered_extent->len - 1;
2824 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2825
2826 /* Drop the cache for the part of the extent we didn't write. */
2827 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002828
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002829 /*
2830 * If the ordered extent had an IOERR or something else went
2831 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002832 * back to the allocator. We only free the extent in the
2833 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002834 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002835 if ((ret || !logical_len) &&
2836 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002837 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2838 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08002839 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002840 }
2841
2842
Josef Bacik5fd02042012-05-02 14:00:54 -04002843 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002844 * This needs to be done to make sure anybody waiting knows we are done
2845 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002846 */
2847 btrfs_remove_ordered_extent(inode, ordered_extent);
2848
Liu Bo38c227d2013-01-29 03:18:40 +00002849 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08002850 if (new) {
2851 if (ret) {
2852 free_sa_defrag_extent(new);
2853 atomic_dec(&root->fs_info->defrag_running);
2854 } else {
2855 relink_file_extents(new);
2856 }
2857 }
Liu Bo38c227d2013-01-29 03:18:40 +00002858
Chris Masone6dcd2d2008-07-17 12:53:50 -04002859 /* once for us */
2860 btrfs_put_ordered_extent(ordered_extent);
2861 /* once for the tree */
2862 btrfs_put_ordered_extent(ordered_extent);
2863
Josef Bacik5fd02042012-05-02 14:00:54 -04002864 return ret;
2865}
2866
2867static void finish_ordered_fn(struct btrfs_work *work)
2868{
2869 struct btrfs_ordered_extent *ordered_extent;
2870 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2871 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002872}
2873
Christoph Hellwigb2950862008-12-02 09:54:17 -05002874static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002875 struct extent_state *state, int uptodate)
2876{
Josef Bacik5fd02042012-05-02 14:00:54 -04002877 struct inode *inode = page->mapping->host;
2878 struct btrfs_root *root = BTRFS_I(inode)->root;
2879 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002880 struct btrfs_workqueue *wq;
2881 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04002882
liubo1abe9b82011-03-24 11:18:59 +00002883 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2884
Chris Mason8b62b722009-09-02 16:53:46 -04002885 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002886 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2887 end - start + 1, uptodate))
2888 return 0;
2889
Liu Bo9e0af232014-08-15 23:36:53 +08002890 if (btrfs_is_free_space_inode(inode)) {
2891 wq = root->fs_info->endio_freespace_worker;
2892 func = btrfs_freespace_write_helper;
2893 } else {
2894 wq = root->fs_info->endio_write_workers;
2895 func = btrfs_endio_write_helper;
2896 }
Josef Bacik5fd02042012-05-02 14:00:54 -04002897
Liu Bo9e0af232014-08-15 23:36:53 +08002898 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
2899 NULL);
2900 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04002901
2902 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002903}
2904
Chris Masond352ac62008-09-29 15:18:18 -04002905/*
Chris Masond352ac62008-09-29 15:18:18 -04002906 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002907 * if there's a match, we allow the bio to finish. If not, the code in
2908 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002909 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002910static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2911 u64 phy_offset, struct page *page,
2912 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002913{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002914 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002915 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002916 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002917 char *kaddr;
Chris Masonff79f812007-10-15 16:22:25 -04002918 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiefacc8a222013-07-25 19:22:34 +08002919 u32 csum_expected;
Chris Masonff79f812007-10-15 16:22:25 -04002920 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002921 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2922 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002923
Chris Masond20f7042008-12-08 16:58:54 -05002924 if (PageChecked(page)) {
2925 ClearPageChecked(page);
2926 goto good;
2927 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002928
2929 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002930 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002931
Yan Zheng17d217f2008-12-12 10:03:38 -05002932 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002933 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002934 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2935 GFP_NOFS);
2936 return 0;
2937 }
2938
Miao Xiefacc8a222013-07-25 19:22:34 +08002939 phy_offset >>= inode->i_sb->s_blocksize_bits;
2940 csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
Chris Masond3977122009-01-05 21:25:51 -05002941
Miao Xiefacc8a222013-07-25 19:22:34 +08002942 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00002943 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002944 btrfs_csum_final(csum, (char *)&csum);
Miao Xiefacc8a222013-07-25 19:22:34 +08002945 if (csum != csum_expected)
Chris Mason07157aa2007-08-30 08:50:51 -04002946 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002947
Cong Wang7ac687d2011-11-25 23:14:28 +08002948 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002949good:
Chris Mason07157aa2007-08-30 08:50:51 -04002950 return 0;
2951
2952zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002953 if (__ratelimit(&_rs))
Miao Xiefacc8a222013-07-25 19:22:34 +08002954 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002955 btrfs_ino(page->mapping->host), start, csum, csum_expected);
Chris Masondb945352007-10-15 16:15:53 -04002956 memset(kaddr + offset, 1, end - start + 1);
2957 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002958 kunmap_atomic(kaddr);
Miao Xiefacc8a222013-07-25 19:22:34 +08002959 if (csum_expected == 0)
Chris Mason3b951512008-04-17 11:29:12 -04002960 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002961 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002962}
Chris Masonb888db22007-08-27 16:49:44 -04002963
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002964struct delayed_iput {
2965 struct list_head list;
2966 struct inode *inode;
2967};
2968
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002969/* JDM: If this is fs-wide, why can't we add a pointer to
2970 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002971void btrfs_add_delayed_iput(struct inode *inode)
2972{
2973 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2974 struct delayed_iput *delayed;
2975
2976 if (atomic_add_unless(&inode->i_count, -1, 1))
2977 return;
2978
2979 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2980 delayed->inode = inode;
2981
2982 spin_lock(&fs_info->delayed_iput_lock);
2983 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2984 spin_unlock(&fs_info->delayed_iput_lock);
2985}
2986
2987void btrfs_run_delayed_iputs(struct btrfs_root *root)
2988{
2989 LIST_HEAD(list);
2990 struct btrfs_fs_info *fs_info = root->fs_info;
2991 struct delayed_iput *delayed;
2992 int empty;
2993
2994 spin_lock(&fs_info->delayed_iput_lock);
2995 empty = list_empty(&fs_info->delayed_iputs);
2996 spin_unlock(&fs_info->delayed_iput_lock);
2997 if (empty)
2998 return;
2999
Yan, Zheng24bbcf042009-11-12 09:36:34 +00003000 spin_lock(&fs_info->delayed_iput_lock);
3001 list_splice_init(&fs_info->delayed_iputs, &list);
3002 spin_unlock(&fs_info->delayed_iput_lock);
3003
3004 while (!list_empty(&list)) {
3005 delayed = list_entry(list.next, struct delayed_iput, list);
3006 list_del(&delayed->list);
3007 iput(delayed->inode);
3008 kfree(delayed);
3009 }
Yan, Zheng24bbcf042009-11-12 09:36:34 +00003010}
3011
Yan, Zhengd68fc572010-05-16 10:49:58 -04003012/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003013 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003014 * files in the subvolume, it removes orphan item and frees block_rsv
3015 * structure.
3016 */
3017void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3018 struct btrfs_root *root)
3019{
Josef Bacik90290e12011-12-02 15:44:12 -05003020 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003021 int ret;
3022
Josef Bacik8a35d952012-05-23 14:26:42 -04003023 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003024 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3025 return;
3026
Josef Bacik90290e12011-12-02 15:44:12 -05003027 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003028 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003029 spin_unlock(&root->orphan_lock);
3030 return;
3031 }
3032
3033 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3034 spin_unlock(&root->orphan_lock);
3035 return;
3036 }
3037
3038 block_rsv = root->orphan_block_rsv;
3039 root->orphan_block_rsv = NULL;
3040 spin_unlock(&root->orphan_lock);
3041
Miao Xie27cdeb72014-04-02 19:51:05 +08003042 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003043 btrfs_root_refs(&root->root_item) > 0) {
3044 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3045 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003046 if (ret)
3047 btrfs_abort_transaction(trans, root, ret);
3048 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003049 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3050 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003051 }
3052
Josef Bacik90290e12011-12-02 15:44:12 -05003053 if (block_rsv) {
3054 WARN_ON(block_rsv->size > 0);
3055 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003056 }
3057}
3058
3059/*
Josef Bacik7b128762008-07-24 12:17:14 -04003060 * This creates an orphan entry for the given inode in case something goes
3061 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003062 *
3063 * NOTE: caller of this function should reserve 5 units of metadata for
3064 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003065 */
3066int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3067{
3068 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003069 struct btrfs_block_rsv *block_rsv = NULL;
3070 int reserve = 0;
3071 int insert = 0;
3072 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003073
Yan, Zhengd68fc572010-05-16 10:49:58 -04003074 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003075 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003076 if (!block_rsv)
3077 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003078 }
3079
Yan, Zhengd68fc572010-05-16 10:49:58 -04003080 spin_lock(&root->orphan_lock);
3081 if (!root->orphan_block_rsv) {
3082 root->orphan_block_rsv = block_rsv;
3083 } else if (block_rsv) {
3084 btrfs_free_block_rsv(root, block_rsv);
3085 block_rsv = NULL;
3086 }
Josef Bacik7b128762008-07-24 12:17:14 -04003087
Josef Bacik8a35d952012-05-23 14:26:42 -04003088 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3089 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003090#if 0
3091 /*
3092 * For proper ENOSPC handling, we should do orphan
3093 * cleanup when mounting. But this introduces backward
3094 * compatibility issue.
3095 */
3096 if (!xchg(&root->orphan_item_inserted, 1))
3097 insert = 2;
3098 else
3099 insert = 1;
3100#endif
3101 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003102 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003103 }
Josef Bacik7b128762008-07-24 12:17:14 -04003104
Josef Bacik72ac3c02012-05-23 14:13:11 -04003105 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3106 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003107 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003108 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003109
Yan, Zhengd68fc572010-05-16 10:49:58 -04003110 /* grab metadata reservation from transaction handle */
3111 if (reserve) {
3112 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003113 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003114 }
3115
3116 /* insert an orphan item to track this unlinked/truncated file */
3117 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003118 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003119 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003120 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003121 if (reserve) {
3122 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3123 &BTRFS_I(inode)->runtime_flags);
3124 btrfs_orphan_release_metadata(inode);
3125 }
3126 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003127 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3128 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003129 btrfs_abort_transaction(trans, root, ret);
3130 return ret;
3131 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003132 }
3133 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003134 }
3135
3136 /* insert an orphan item to track subvolume contains orphan files */
3137 if (insert >= 2) {
3138 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3139 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003140 if (ret && ret != -EEXIST) {
3141 btrfs_abort_transaction(trans, root, ret);
3142 return ret;
3143 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003144 }
3145 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003146}
3147
3148/*
3149 * We have done the truncate/delete so we can go ahead and remove the orphan
3150 * item for this particular inode.
3151 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003152static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3153 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003154{
3155 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003156 int delete_item = 0;
3157 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003158 int ret = 0;
3159
Yan, Zhengd68fc572010-05-16 10:49:58 -04003160 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003161 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3162 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003163 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003164
Josef Bacik72ac3c02012-05-23 14:13:11 -04003165 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3166 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003167 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003168 spin_unlock(&root->orphan_lock);
3169
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003170 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003171 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003172 if (trans)
3173 ret = btrfs_del_orphan_item(trans, root,
3174 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003175 }
Josef Bacik7b128762008-07-24 12:17:14 -04003176
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003177 if (release_rsv)
3178 btrfs_orphan_release_metadata(inode);
3179
Josef Bacik4ef31a42013-08-13 14:10:08 -04003180 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003181}
3182
3183/*
3184 * this cleans up any orphans that may be left on the list from the last use
3185 * of this root.
3186 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003187int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003188{
3189 struct btrfs_path *path;
3190 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003191 struct btrfs_key key, found_key;
3192 struct btrfs_trans_handle *trans;
3193 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003194 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003195 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3196
Yan, Zhengd68fc572010-05-16 10:49:58 -04003197 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003198 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003199
3200 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003201 if (!path) {
3202 ret = -ENOMEM;
3203 goto out;
3204 }
Josef Bacik7b128762008-07-24 12:17:14 -04003205 path->reada = -1;
3206
3207 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003208 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003209 key.offset = (u64)-1;
3210
Josef Bacik7b128762008-07-24 12:17:14 -04003211 while (1) {
3212 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003213 if (ret < 0)
3214 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003215
3216 /*
3217 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003218 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003219 * find the key and see if we have stuff that matches
3220 */
3221 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003222 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003223 if (path->slots[0] == 0)
3224 break;
3225 path->slots[0]--;
3226 }
3227
3228 /* pull out the item */
3229 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003230 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3231
3232 /* make sure the item matches what we want */
3233 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3234 break;
David Sterba962a2982014-06-04 18:41:45 +02003235 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003236 break;
3237
3238 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003239 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003240
3241 /*
3242 * this is where we are basically btrfs_lookup, without the
3243 * crossing root thing. we store the inode number in the
3244 * offset of the orphan item.
3245 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003246
3247 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003248 btrfs_err(root->fs_info,
3249 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003250 ret = -EINVAL;
3251 goto out;
3252 }
3253
3254 last_objectid = found_key.offset;
3255
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003256 found_key.objectid = found_key.offset;
3257 found_key.type = BTRFS_INODE_ITEM_KEY;
3258 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003259 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303260 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003261 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003262 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003263
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003264 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3265 struct btrfs_root *dead_root;
3266 struct btrfs_fs_info *fs_info = root->fs_info;
3267 int is_dead_root = 0;
3268
3269 /*
3270 * this is an orphan in the tree root. Currently these
3271 * could come from 2 sources:
3272 * a) a snapshot deletion in progress
3273 * b) a free space cache inode
3274 * We need to distinguish those two, as the snapshot
3275 * orphan must not get deleted.
3276 * find_dead_roots already ran before us, so if this
3277 * is a snapshot deletion, we should find the root
3278 * in the dead_roots list
3279 */
3280 spin_lock(&fs_info->trans_lock);
3281 list_for_each_entry(dead_root, &fs_info->dead_roots,
3282 root_list) {
3283 if (dead_root->root_key.objectid ==
3284 found_key.objectid) {
3285 is_dead_root = 1;
3286 break;
3287 }
3288 }
3289 spin_unlock(&fs_info->trans_lock);
3290 if (is_dead_root) {
3291 /* prevent this orphan from being found again */
3292 key.offset = found_key.objectid - 1;
3293 continue;
3294 }
3295 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003296 /*
3297 * Inode is already gone but the orphan item is still there,
3298 * kill the orphan item.
3299 */
3300 if (ret == -ESTALE) {
3301 trans = btrfs_start_transaction(root, 1);
3302 if (IS_ERR(trans)) {
3303 ret = PTR_ERR(trans);
3304 goto out;
3305 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003306 btrfs_debug(root->fs_info, "auto deleting %Lu",
3307 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003308 ret = btrfs_del_orphan_item(trans, root,
3309 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003310 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003311 if (ret)
3312 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003313 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003314 }
Josef Bacik7b128762008-07-24 12:17:14 -04003315
Josef Bacik7b128762008-07-24 12:17:14 -04003316 /*
3317 * add this inode to the orphan list so btrfs_orphan_del does
3318 * the proper thing when we hit it
3319 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003320 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3321 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003322 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003323
Josef Bacik7b128762008-07-24 12:17:14 -04003324 /* if we have links, this was a truncate, lets do that */
3325 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303326 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003327 iput(inode);
3328 continue;
3329 }
Josef Bacik7b128762008-07-24 12:17:14 -04003330 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003331
3332 /* 1 for the orphan item deletion. */
3333 trans = btrfs_start_transaction(root, 1);
3334 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003335 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003336 ret = PTR_ERR(trans);
3337 goto out;
3338 }
3339 ret = btrfs_orphan_add(trans, inode);
3340 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003341 if (ret) {
3342 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003343 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003344 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003345
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003346 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003347 if (ret)
3348 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003349 } else {
3350 nr_unlink++;
3351 }
3352
3353 /* this will do delete_inode and everything for us */
3354 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003355 if (ret)
3356 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003357 }
Miao Xie3254c872011-11-10 20:45:05 -05003358 /* release the path since we're done with it */
3359 btrfs_release_path(path);
3360
Yan, Zhengd68fc572010-05-16 10:49:58 -04003361 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3362
3363 if (root->orphan_block_rsv)
3364 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3365 (u64)-1);
3366
Miao Xie27cdeb72014-04-02 19:51:05 +08003367 if (root->orphan_block_rsv ||
3368 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003369 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003370 if (!IS_ERR(trans))
3371 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003372 }
Josef Bacik7b128762008-07-24 12:17:14 -04003373
3374 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003375 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003376 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003377 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003378
3379out:
3380 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003381 btrfs_crit(root->fs_info,
3382 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003383 btrfs_free_path(path);
3384 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003385}
3386
Chris Masond352ac62008-09-29 15:18:18 -04003387/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003388 * very simple check to peek ahead in the leaf looking for xattrs. If we
3389 * don't find any xattrs, we know there can't be any acls.
3390 *
3391 * slot is the slot the inode is in, objectid is the objectid of the inode
3392 */
3393static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003394 int slot, u64 objectid,
3395 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003396{
3397 u32 nritems = btrfs_header_nritems(leaf);
3398 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003399 static u64 xattr_access = 0;
3400 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003401 int scanned = 0;
3402
Josef Bacikf23b5a52013-06-19 10:16:26 -04003403 if (!xattr_access) {
3404 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3405 strlen(POSIX_ACL_XATTR_ACCESS));
3406 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3407 strlen(POSIX_ACL_XATTR_DEFAULT));
3408 }
3409
Chris Mason46a53cc2009-04-27 11:47:50 -04003410 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003411 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003412 while (slot < nritems) {
3413 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3414
3415 /* we found a different objectid, there must not be acls */
3416 if (found_key.objectid != objectid)
3417 return 0;
3418
3419 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003420 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003421 if (*first_xattr_slot == -1)
3422 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003423 if (found_key.offset == xattr_access ||
3424 found_key.offset == xattr_default)
3425 return 1;
3426 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003427
3428 /*
3429 * we found a key greater than an xattr key, there can't
3430 * be any acls later on
3431 */
3432 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3433 return 0;
3434
3435 slot++;
3436 scanned++;
3437
3438 /*
3439 * it goes inode, inode backrefs, xattrs, extents,
3440 * so if there are a ton of hard links to an inode there can
3441 * be a lot of backrefs. Don't waste time searching too hard,
3442 * this is just an optimization
3443 */
3444 if (scanned >= 8)
3445 break;
3446 }
3447 /* we hit the end of the leaf before we found an xattr or
3448 * something larger than an xattr. We have to assume the inode
3449 * has acls
3450 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003451 if (*first_xattr_slot == -1)
3452 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003453 return 1;
3454}
3455
3456/*
Chris Masond352ac62008-09-29 15:18:18 -04003457 * read an inode from the btree into the in-memory inode
3458 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003459static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003460{
3461 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003462 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003463 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003464 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003465 struct btrfs_root *root = BTRFS_I(inode)->root;
3466 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003467 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003468 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003469 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003470 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003471 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003472 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003473
3474 ret = btrfs_fill_inode(inode, &rdev);
3475 if (!ret)
3476 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003477
3478 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003479 if (!path)
3480 goto make_bad;
3481
Chris Mason39279cc2007-06-12 06:35:45 -04003482 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003483
Chris Mason39279cc2007-06-12 06:35:45 -04003484 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003485 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003486 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003487
Chris Mason5f39d392007-10-15 16:14:19 -04003488 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003489
3490 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003491 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003492
Chris Mason5f39d392007-10-15 16:14:19 -04003493 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3494 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003495 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003496 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003497 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3498 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003499 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003500
3501 tspec = btrfs_inode_atime(inode_item);
3502 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3503 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3504
3505 tspec = btrfs_inode_mtime(inode_item);
3506 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3507 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3508
3509 tspec = btrfs_inode_ctime(inode_item);
3510 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3511 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3512
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003513 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003514 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003515 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3516
3517 /*
3518 * If we were modified in the current generation and evicted from memory
3519 * and then re-read we need to do a full sync since we don't have any
3520 * idea about which extents were modified before we were evicted from
3521 * cache.
3522 */
3523 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3524 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3525 &BTRFS_I(inode)->runtime_flags);
3526
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003527 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003528 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003529 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003530 rdev = btrfs_inode_rdev(leaf, inode_item);
3531
Josef Bacikaec74772008-07-24 12:12:38 -04003532 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003533 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003534
3535cache_index:
3536 path->slots[0]++;
3537 if (inode->i_nlink != 1 ||
3538 path->slots[0] >= btrfs_header_nritems(leaf))
3539 goto cache_acl;
3540
3541 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3542 if (location.objectid != btrfs_ino(inode))
3543 goto cache_acl;
3544
3545 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3546 if (location.type == BTRFS_INODE_REF_KEY) {
3547 struct btrfs_inode_ref *ref;
3548
3549 ref = (struct btrfs_inode_ref *)ptr;
3550 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3551 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3552 struct btrfs_inode_extref *extref;
3553
3554 extref = (struct btrfs_inode_extref *)ptr;
3555 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3556 extref);
3557 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003558cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003559 /*
3560 * try to precache a NULL acl entry for files that don't have
3561 * any xattrs or acls
3562 */
Li Zefan33345d012011-04-20 10:31:50 +08003563 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003564 btrfs_ino(inode), &first_xattr_slot);
3565 if (first_xattr_slot != -1) {
3566 path->slots[0] = first_xattr_slot;
3567 ret = btrfs_load_inode_props(inode, path);
3568 if (ret)
3569 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003570 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003571 btrfs_ino(inode),
3572 root->root_key.objectid, ret);
3573 }
3574 btrfs_free_path(path);
3575
Al Viro72c04902009-06-24 16:58:48 -04003576 if (!maybe_acls)
3577 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003578
Chris Mason39279cc2007-06-12 06:35:45 -04003579 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003580 case S_IFREG:
3581 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003582 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003583 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003584 inode->i_fop = &btrfs_file_operations;
3585 inode->i_op = &btrfs_file_inode_operations;
3586 break;
3587 case S_IFDIR:
3588 inode->i_fop = &btrfs_dir_file_operations;
3589 if (root == root->fs_info->tree_root)
3590 inode->i_op = &btrfs_dir_ro_inode_operations;
3591 else
3592 inode->i_op = &btrfs_dir_inode_operations;
3593 break;
3594 case S_IFLNK:
3595 inode->i_op = &btrfs_symlink_inode_operations;
3596 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003597 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003598 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003599 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003600 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003601 init_special_inode(inode, inode->i_mode, rdev);
3602 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003603 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003604
3605 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003606 return;
3607
3608make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003609 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003610 make_bad_inode(inode);
3611}
3612
Chris Masond352ac62008-09-29 15:18:18 -04003613/*
3614 * given a leaf and an inode, copy the inode fields into the leaf
3615 */
Chris Masone02119d2008-09-05 16:13:11 -04003616static void fill_inode_item(struct btrfs_trans_handle *trans,
3617 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003618 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003619 struct inode *inode)
3620{
Liu Bo51fab692012-12-27 09:01:21 +00003621 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003622
Liu Bo51fab692012-12-27 09:01:21 +00003623 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003624
Liu Bo51fab692012-12-27 09:01:21 +00003625 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3626 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3627 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3628 &token);
3629 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3630 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003631
Liu Bo51fab692012-12-27 09:01:21 +00003632 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3633 inode->i_atime.tv_sec, &token);
3634 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3635 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003636
Liu Bo51fab692012-12-27 09:01:21 +00003637 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3638 inode->i_mtime.tv_sec, &token);
3639 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3640 inode->i_mtime.tv_nsec, &token);
3641
3642 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3643 inode->i_ctime.tv_sec, &token);
3644 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3645 inode->i_ctime.tv_nsec, &token);
3646
3647 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3648 &token);
3649 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3650 &token);
3651 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3652 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3653 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3654 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3655 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003656}
3657
Chris Masond352ac62008-09-29 15:18:18 -04003658/*
3659 * copy everything in the in-memory inode into the btree.
3660 */
Chris Mason21151332011-11-10 20:39:08 -05003661static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003662 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003663{
3664 struct btrfs_inode_item *inode_item;
3665 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003666 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003667 int ret;
3668
3669 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003670 if (!path)
3671 return -ENOMEM;
3672
Chris Masonb9473432009-03-13 11:00:37 -04003673 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003674 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3675 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003676 if (ret) {
3677 if (ret > 0)
3678 ret = -ENOENT;
3679 goto failed;
3680 }
3681
Chris Mason5f39d392007-10-15 16:14:19 -04003682 leaf = path->nodes[0];
3683 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003684 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003685
Chris Masone02119d2008-09-05 16:13:11 -04003686 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003687 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003688 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003689 ret = 0;
3690failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003691 btrfs_free_path(path);
3692 return ret;
3693}
3694
Chris Masond352ac62008-09-29 15:18:18 -04003695/*
Chris Mason21151332011-11-10 20:39:08 -05003696 * copy everything in the in-memory inode into the btree.
3697 */
3698noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3699 struct btrfs_root *root, struct inode *inode)
3700{
3701 int ret;
3702
3703 /*
3704 * If the inode is a free space inode, we can deadlock during commit
3705 * if we put it into the delayed code.
3706 *
3707 * The data relocation inode should also be directly updated
3708 * without delay
3709 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003710 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003711 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003712 btrfs_update_root_times(trans, root);
3713
Chris Mason21151332011-11-10 20:39:08 -05003714 ret = btrfs_delayed_update_inode(trans, root, inode);
3715 if (!ret)
3716 btrfs_set_inode_last_trans(trans, inode);
3717 return ret;
3718 }
3719
3720 return btrfs_update_inode_item(trans, root, inode);
3721}
3722
Josef Bacikbe6aef62012-10-22 15:43:12 -04003723noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3724 struct btrfs_root *root,
3725 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003726{
3727 int ret;
3728
3729 ret = btrfs_update_inode(trans, root, inode);
3730 if (ret == -ENOSPC)
3731 return btrfs_update_inode_item(trans, root, inode);
3732 return ret;
3733}
3734
3735/*
Chris Masond352ac62008-09-29 15:18:18 -04003736 * unlink helper that gets used here in inode.c and in the tree logging
3737 * recovery code. It remove a link in a directory with a given name, and
3738 * also drops the back refs in the inode to the directory
3739 */
Al Viro92986792011-03-04 17:14:37 +00003740static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3741 struct btrfs_root *root,
3742 struct inode *dir, struct inode *inode,
3743 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003744{
3745 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003746 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003747 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003748 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003749 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003750 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003751 u64 ino = btrfs_ino(inode);
3752 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003753
3754 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003755 if (!path) {
3756 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003757 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003758 }
3759
Chris Masonb9473432009-03-13 11:00:37 -04003760 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003761 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003762 name, name_len, -1);
3763 if (IS_ERR(di)) {
3764 ret = PTR_ERR(di);
3765 goto err;
3766 }
3767 if (!di) {
3768 ret = -ENOENT;
3769 goto err;
3770 }
Chris Mason5f39d392007-10-15 16:14:19 -04003771 leaf = path->nodes[0];
3772 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003773 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003774 if (ret)
3775 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003776 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003777
Miao Xie67de1172013-12-26 13:07:06 +08003778 /*
3779 * If we don't have dir index, we have to get it by looking up
3780 * the inode ref, since we get the inode ref, remove it directly,
3781 * it is unnecessary to do delayed deletion.
3782 *
3783 * But if we have dir index, needn't search inode ref to get it.
3784 * Since the inode ref is close to the inode item, it is better
3785 * that we delay to delete it, and just do this deletion when
3786 * we update the inode item.
3787 */
3788 if (BTRFS_I(inode)->dir_index) {
3789 ret = btrfs_delayed_delete_inode_ref(inode);
3790 if (!ret) {
3791 index = BTRFS_I(inode)->dir_index;
3792 goto skip_backref;
3793 }
3794 }
3795
Li Zefan33345d012011-04-20 10:31:50 +08003796 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3797 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003798 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003799 btrfs_info(root->fs_info,
3800 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003801 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003802 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003803 goto err;
3804 }
Miao Xie67de1172013-12-26 13:07:06 +08003805skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08003806 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003807 if (ret) {
3808 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003809 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003810 }
Chris Mason39279cc2007-06-12 06:35:45 -04003811
Chris Masone02119d2008-09-05 16:13:11 -04003812 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003813 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003814 if (ret != 0 && ret != -ENOENT) {
3815 btrfs_abort_transaction(trans, root, ret);
3816 goto err;
3817 }
Chris Masone02119d2008-09-05 16:13:11 -04003818
3819 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3820 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003821 if (ret == -ENOENT)
3822 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003823 else if (ret)
3824 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003825err:
3826 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003827 if (ret)
3828 goto out;
3829
3830 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003831 inode_inc_iversion(inode);
3832 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003833 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003834 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003835out:
Chris Mason39279cc2007-06-12 06:35:45 -04003836 return ret;
3837}
3838
Al Viro92986792011-03-04 17:14:37 +00003839int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3840 struct btrfs_root *root,
3841 struct inode *dir, struct inode *inode,
3842 const char *name, int name_len)
3843{
3844 int ret;
3845 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3846 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07003847 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00003848 ret = btrfs_update_inode(trans, root, inode);
3849 }
3850 return ret;
3851}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003852
3853/*
3854 * helper to start transaction for unlink and rmdir.
3855 *
Josef Bacikd52be812013-05-29 14:54:47 -04003856 * unlink and rmdir are special in btrfs, they do not always free space, so
3857 * if we cannot make our reservations the normal way try and see if there is
3858 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3859 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003860 */
Josef Bacikd52be812013-05-29 14:54:47 -04003861static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003862{
3863 struct btrfs_trans_handle *trans;
3864 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003865 int ret;
3866
Josef Bacike70bea52011-10-11 14:18:24 -04003867 /*
3868 * 1 for the possible orphan item
3869 * 1 for the dir item
3870 * 1 for the dir index
3871 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003872 * 1 for the inode
3873 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003874 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003875 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3876 return trans;
3877
Josef Bacikd52be812013-05-29 14:54:47 -04003878 if (PTR_ERR(trans) == -ENOSPC) {
3879 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003880
Josef Bacikd52be812013-05-29 14:54:47 -04003881 trans = btrfs_start_transaction(root, 0);
3882 if (IS_ERR(trans))
3883 return trans;
3884 ret = btrfs_cond_migrate_bytes(root->fs_info,
3885 &root->fs_info->trans_block_rsv,
3886 num_bytes, 5);
3887 if (ret) {
3888 btrfs_end_transaction(trans, root);
3889 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003890 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003891 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003892 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003893 }
Josef Bacikd52be812013-05-29 14:54:47 -04003894 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003895}
3896
Chris Mason39279cc2007-06-12 06:35:45 -04003897static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3898{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003899 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003900 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003901 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003902 int ret;
3903
Josef Bacikd52be812013-05-29 14:54:47 -04003904 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003905 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003906 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003907
Chris Mason12fcfd22009-03-24 10:24:20 -04003908 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3909
Chris Masone02119d2008-09-05 16:13:11 -04003910 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3911 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003912 if (ret)
3913 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003914
Yan, Zhenga22285a2010-05-16 10:48:46 -04003915 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003916 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003917 if (ret)
3918 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003919 }
Josef Bacik7b128762008-07-24 12:17:14 -04003920
Tsutomu Itohb5324022011-07-19 07:27:20 +00003921out:
Josef Bacikd52be812013-05-29 14:54:47 -04003922 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003923 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003924 return ret;
3925}
3926
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003927int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3928 struct btrfs_root *root,
3929 struct inode *dir, u64 objectid,
3930 const char *name, int name_len)
3931{
3932 struct btrfs_path *path;
3933 struct extent_buffer *leaf;
3934 struct btrfs_dir_item *di;
3935 struct btrfs_key key;
3936 u64 index;
3937 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003938 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003939
3940 path = btrfs_alloc_path();
3941 if (!path)
3942 return -ENOMEM;
3943
Li Zefan33345d012011-04-20 10:31:50 +08003944 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003945 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003946 if (IS_ERR_OR_NULL(di)) {
3947 if (!di)
3948 ret = -ENOENT;
3949 else
3950 ret = PTR_ERR(di);
3951 goto out;
3952 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003953
3954 leaf = path->nodes[0];
3955 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3956 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3957 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003958 if (ret) {
3959 btrfs_abort_transaction(trans, root, ret);
3960 goto out;
3961 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003962 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003963
3964 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3965 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003966 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003967 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003968 if (ret != -ENOENT) {
3969 btrfs_abort_transaction(trans, root, ret);
3970 goto out;
3971 }
Li Zefan33345d012011-04-20 10:31:50 +08003972 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003973 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003974 if (IS_ERR_OR_NULL(di)) {
3975 if (!di)
3976 ret = -ENOENT;
3977 else
3978 ret = PTR_ERR(di);
3979 btrfs_abort_transaction(trans, root, ret);
3980 goto out;
3981 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003982
3983 leaf = path->nodes[0];
3984 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003985 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003986 index = key.offset;
3987 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003988 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003989
Miao Xie16cdcec2011-04-22 18:12:22 +08003990 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003991 if (ret) {
3992 btrfs_abort_transaction(trans, root, ret);
3993 goto out;
3994 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003995
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003996 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003997 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003998 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003999 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004000 if (ret)
4001 btrfs_abort_transaction(trans, root, ret);
4002out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004003 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004004 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004005}
4006
Chris Mason39279cc2007-06-12 06:35:45 -04004007static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4008{
4009 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004010 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004011 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004012 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004013
David Sterbab3ae2442012-09-13 16:04:34 -06004014 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004015 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004016 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4017 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004018
Josef Bacikd52be812013-05-29 14:54:47 -04004019 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004020 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004021 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004022
Li Zefan33345d012011-04-20 10:31:50 +08004023 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004024 err = btrfs_unlink_subvol(trans, root, dir,
4025 BTRFS_I(inode)->location.objectid,
4026 dentry->d_name.name,
4027 dentry->d_name.len);
4028 goto out;
4029 }
4030
Josef Bacik7b128762008-07-24 12:17:14 -04004031 err = btrfs_orphan_add(trans, inode);
4032 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004033 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004034
Chris Mason39279cc2007-06-12 06:35:45 -04004035 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04004036 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
4037 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004038 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004039 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004040out:
Josef Bacikd52be812013-05-29 14:54:47 -04004041 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004042 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004043
Chris Mason39279cc2007-06-12 06:35:45 -04004044 return err;
4045}
4046
Chris Mason323ac952008-10-01 19:05:46 -04004047/*
Chris Mason39279cc2007-06-12 06:35:45 -04004048 * this can truncate away extent items, csum items and directory items.
4049 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004050 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004051 *
4052 * csum items that cross the new i_size are truncated to the new size
4053 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004054 *
4055 * min_type is the minimum key type to truncate down to. If set to 0, this
4056 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004057 */
Yan, Zheng80825102009-11-12 09:35:36 +00004058int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4059 struct btrfs_root *root,
4060 struct inode *inode,
4061 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004062{
Chris Mason39279cc2007-06-12 06:35:45 -04004063 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004064 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004065 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004066 struct btrfs_key key;
4067 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004068 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004069 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004070 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004071 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004072 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00004073 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004074 int found_extent;
4075 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004076 int pending_del_nr = 0;
4077 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004078 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004079 int ret;
4080 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004081 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004082
4083 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004084
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004085 path = btrfs_alloc_path();
4086 if (!path)
4087 return -ENOMEM;
4088 path->reada = -1;
4089
Josef Bacik5dc562c2012-08-17 13:14:17 -04004090 /*
4091 * We want to drop from the next block forward in case this new size is
4092 * not block aligned since we will be keeping the last block of the
4093 * extent just the way it is.
4094 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004095 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4096 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004097 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4098 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004099
Miao Xie16cdcec2011-04-22 18:12:22 +08004100 /*
4101 * This function is also used to drop the items in the log tree before
4102 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4103 * it is used to drop the loged items. So we shouldn't kill the delayed
4104 * items.
4105 */
4106 if (min_type == 0 && root == BTRFS_I(inode)->root)
4107 btrfs_kill_delayed_inode_items(inode);
4108
Li Zefan33345d012011-04-20 10:31:50 +08004109 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004110 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004111 key.type = (u8)-1;
4112
Chris Mason85e21ba2008-01-29 15:11:36 -05004113search_again:
Chris Masonb9473432009-03-13 11:00:37 -04004114 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004115 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004116 if (ret < 0) {
4117 err = ret;
4118 goto out;
4119 }
Chris Masond3977122009-01-05 21:25:51 -05004120
Chris Mason85e21ba2008-01-29 15:11:36 -05004121 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004122 /* there are no items in the tree for us to truncate, we're
4123 * done
4124 */
Yan, Zheng80825102009-11-12 09:35:36 +00004125 if (path->slots[0] == 0)
4126 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004127 path->slots[0]--;
4128 }
4129
Chris Masond3977122009-01-05 21:25:51 -05004130 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004131 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004132 leaf = path->nodes[0];
4133 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004134 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004135
Li Zefan33345d012011-04-20 10:31:50 +08004136 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004137 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004138
Chris Mason85e21ba2008-01-29 15:11:36 -05004139 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004140 break;
4141
Chris Mason5f39d392007-10-15 16:14:19 -04004142 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004143 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004144 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004145 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004146 extent_type = btrfs_file_extent_type(leaf, fi);
4147 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004148 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004149 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004150 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004151 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004152 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004153 }
Yan008630c2007-11-07 13:31:09 -05004154 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004155 }
Yan, Zheng80825102009-11-12 09:35:36 +00004156 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004157 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004158 } else {
4159 if (item_end < new_size)
4160 break;
4161 if (found_key.offset >= new_size)
4162 del_item = 1;
4163 else
4164 del_item = 0;
4165 }
Chris Mason39279cc2007-06-12 06:35:45 -04004166 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004167 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004168 if (found_type != BTRFS_EXTENT_DATA_KEY)
4169 goto delete;
4170
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004171 if (del_item)
4172 last_size = found_key.offset;
4173 else
4174 last_size = new_size;
4175
Chris Mason179e29e2007-11-01 11:28:41 -04004176 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004177 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004178 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004179 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004180 u64 orig_num_bytes =
4181 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004182 extent_num_bytes = ALIGN(new_size -
4183 found_key.offset,
4184 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004185 btrfs_set_file_extent_num_bytes(leaf, fi,
4186 extent_num_bytes);
4187 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004188 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004189 if (test_bit(BTRFS_ROOT_REF_COWS,
4190 &root->state) &&
4191 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004192 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004193 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004194 } else {
Chris Masondb945352007-10-15 16:15:53 -04004195 extent_num_bytes =
4196 btrfs_file_extent_disk_num_bytes(leaf,
4197 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004198 extent_offset = found_key.offset -
4199 btrfs_file_extent_offset(leaf, fi);
4200
Chris Mason39279cc2007-06-12 06:35:45 -04004201 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004202 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004203 if (extent_start != 0) {
4204 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004205 if (test_bit(BTRFS_ROOT_REF_COWS,
4206 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004207 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004208 }
4209 }
Chris Mason90692182008-02-08 13:49:28 -05004210 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004211 /*
4212 * we can't truncate inline items that have had
4213 * special encodings
4214 */
4215 if (!del_item &&
4216 btrfs_file_extent_compression(leaf, fi) == 0 &&
4217 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4218 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004219 u32 size = new_size - found_key.offset;
4220
Miao Xie27cdeb72014-04-02 19:51:05 +08004221 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004222 inode_sub_bytes(inode, item_end + 1 -
4223 new_size);
Chris Mason514ac8a2014-01-03 21:07:00 -08004224
4225 /*
4226 * update the ram bytes to properly reflect
4227 * the new size of our item
4228 */
4229 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
Chris Masone02119d2008-09-05 16:13:11 -04004230 size =
4231 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004232 btrfs_truncate_item(root, path, size, 1);
Miao Xie27cdeb72014-04-02 19:51:05 +08004233 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4234 &root->state)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004235 inode_sub_bytes(inode, item_end + 1 -
4236 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004237 }
Chris Mason39279cc2007-06-12 06:35:45 -04004238 }
Chris Mason179e29e2007-11-01 11:28:41 -04004239delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004240 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004241 if (!pending_del_nr) {
4242 /* no pending yet, add ourselves */
4243 pending_del_slot = path->slots[0];
4244 pending_del_nr = 1;
4245 } else if (pending_del_nr &&
4246 path->slots[0] + 1 == pending_del_slot) {
4247 /* hop on the pending chunk */
4248 pending_del_nr++;
4249 pending_del_slot = path->slots[0];
4250 } else {
Chris Masond3977122009-01-05 21:25:51 -05004251 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004252 }
Chris Mason39279cc2007-06-12 06:35:45 -04004253 } else {
4254 break;
4255 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004256 if (found_extent &&
4257 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4258 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004259 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004260 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004261 extent_num_bytes, 0,
4262 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004263 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004264 BUG_ON(ret);
4265 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004266
Yan, Zheng80825102009-11-12 09:35:36 +00004267 if (found_type == BTRFS_INODE_ITEM_KEY)
4268 break;
4269
4270 if (path->slots[0] == 0 ||
4271 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004272 if (pending_del_nr) {
4273 ret = btrfs_del_items(trans, root, path,
4274 pending_del_slot,
4275 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004276 if (ret) {
4277 btrfs_abort_transaction(trans,
4278 root, ret);
4279 goto error;
4280 }
Yan, Zheng80825102009-11-12 09:35:36 +00004281 pending_del_nr = 0;
4282 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004283 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004284 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004285 } else {
4286 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004287 }
Chris Mason39279cc2007-06-12 06:35:45 -04004288 }
Yan, Zheng80825102009-11-12 09:35:36 +00004289out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004290 if (pending_del_nr) {
4291 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4292 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004293 if (ret)
4294 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004295 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004296error:
Filipe Mananadac57052014-08-29 20:54:26 +01004297 if (last_size != (u64)-1 &&
4298 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004299 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004300 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004301 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004302}
4303
Chris Masona52d9a82007-08-27 16:49:44 -04004304/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004305 * btrfs_truncate_page - read, zero a chunk and write a page
4306 * @inode - inode that we're zeroing
4307 * @from - the offset to start zeroing
4308 * @len - the length to zero, 0 to zero the entire range respective to the
4309 * offset
4310 * @front - zero up to the offset instead of from the offset on
4311 *
4312 * This will find the page for the "from" offset and cow the page and zero the
4313 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004314 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004315int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4316 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004317{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004318 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004319 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004320 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4321 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004322 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004323 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004324 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004325 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4326 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4327 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004328 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004329 int ret = 0;
4330 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004331 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004332
Josef Bacik2aaa6652012-08-29 14:27:18 -04004333 if ((offset & (blocksize - 1)) == 0 &&
4334 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004335 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004336 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004337 if (ret)
4338 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004339
Chris Mason211c17f2008-05-15 09:13:45 -04004340again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004341 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004342 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004343 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004344 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004345 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004346 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004347
4348 page_start = page_offset(page);
4349 page_end = page_start + PAGE_CACHE_SIZE - 1;
4350
Chris Masona52d9a82007-08-27 16:49:44 -04004351 if (!PageUptodate(page)) {
4352 ret = btrfs_readpage(NULL, page);
4353 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004354 if (page->mapping != mapping) {
4355 unlock_page(page);
4356 page_cache_release(page);
4357 goto again;
4358 }
Chris Masona52d9a82007-08-27 16:49:44 -04004359 if (!PageUptodate(page)) {
4360 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004361 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004362 }
4363 }
Chris Mason211c17f2008-05-15 09:13:45 -04004364 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004365
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004366 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004367 set_page_extent_mapped(page);
4368
4369 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4370 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004371 unlock_extent_cached(io_tree, page_start, page_end,
4372 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004373 unlock_page(page);
4374 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004375 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004376 btrfs_put_ordered_extent(ordered);
4377 goto again;
4378 }
4379
Josef Bacik2ac55d42010-02-03 19:33:23 +00004380 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004381 EXTENT_DIRTY | EXTENT_DELALLOC |
4382 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004383 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004384
Josef Bacik2ac55d42010-02-03 19:33:23 +00004385 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4386 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004387 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004388 unlock_extent_cached(io_tree, page_start, page_end,
4389 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004390 goto out_unlock;
4391 }
4392
Chris Masone6dcd2d2008-07-17 12:53:50 -04004393 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004394 if (!len)
4395 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004396 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004397 if (front)
4398 memset(kaddr, 0, offset);
4399 else
4400 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004401 flush_dcache_page(page);
4402 kunmap(page);
4403 }
Chris Mason247e7432008-07-17 12:53:51 -04004404 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004405 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004406 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4407 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004408
Chris Mason89642222008-07-24 09:41:53 -04004409out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004410 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004411 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004412 unlock_page(page);
4413 page_cache_release(page);
4414out:
4415 return ret;
4416}
4417
Josef Bacik16e75492013-10-22 12:18:51 -04004418static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4419 u64 offset, u64 len)
4420{
4421 struct btrfs_trans_handle *trans;
4422 int ret;
4423
4424 /*
4425 * Still need to make sure the inode looks like it's been updated so
4426 * that any holes get logged if we fsync.
4427 */
4428 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4429 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4430 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4431 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4432 return 0;
4433 }
4434
4435 /*
4436 * 1 - for the one we're dropping
4437 * 1 - for the one we're adding
4438 * 1 - for updating the inode.
4439 */
4440 trans = btrfs_start_transaction(root, 3);
4441 if (IS_ERR(trans))
4442 return PTR_ERR(trans);
4443
4444 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4445 if (ret) {
4446 btrfs_abort_transaction(trans, root, ret);
4447 btrfs_end_transaction(trans, root);
4448 return ret;
4449 }
4450
4451 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4452 0, 0, len, 0, len, 0, 0, 0);
4453 if (ret)
4454 btrfs_abort_transaction(trans, root, ret);
4455 else
4456 btrfs_update_inode(trans, root, inode);
4457 btrfs_end_transaction(trans, root);
4458 return ret;
4459}
4460
Josef Bacik695a0d02011-03-04 15:46:53 -05004461/*
4462 * This function puts in dummy file extents for the area we're creating a hole
4463 * for. So if we are truncating this file to a larger size we need to insert
4464 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4465 * the range between oldsize and size
4466 */
Josef Bacika41ad392011-01-31 15:30:16 -05004467int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004468{
Yan Zheng9036c102008-10-30 14:19:41 -04004469 struct btrfs_root *root = BTRFS_I(inode)->root;
4470 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004471 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004472 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004473 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004474 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4475 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004476 u64 last_byte;
4477 u64 cur_offset;
4478 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004479 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004480
Josef Bacika71754f2013-06-17 17:14:39 -04004481 /*
4482 * If our size started in the middle of a page we need to zero out the
4483 * rest of the page before we expand the i_size, otherwise we could
4484 * expose stale data.
4485 */
4486 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4487 if (err)
4488 return err;
4489
Yan Zheng9036c102008-10-30 14:19:41 -04004490 if (size <= hole_start)
4491 return 0;
4492
Yan Zheng9036c102008-10-30 14:19:41 -04004493 while (1) {
4494 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004495
Josef Bacik2ac55d42010-02-03 19:33:23 +00004496 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004497 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004498 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4499 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004500 if (!ordered)
4501 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004502 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4503 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004504 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004505 btrfs_put_ordered_extent(ordered);
4506 }
4507
Yan Zheng9036c102008-10-30 14:19:41 -04004508 cur_offset = hole_start;
4509 while (1) {
4510 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4511 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004512 if (IS_ERR(em)) {
4513 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004514 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004515 break;
4516 }
Yan Zheng9036c102008-10-30 14:19:41 -04004517 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004518 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004519 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004520 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004521 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004522
Josef Bacik16e75492013-10-22 12:18:51 -04004523 err = maybe_insert_hole(root, inode, cur_offset,
4524 hole_size);
4525 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004526 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004527 btrfs_drop_extent_cache(inode, cur_offset,
4528 cur_offset + hole_size - 1, 0);
4529 hole_em = alloc_extent_map();
4530 if (!hole_em) {
4531 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4532 &BTRFS_I(inode)->runtime_flags);
4533 goto next;
4534 }
4535 hole_em->start = cur_offset;
4536 hole_em->len = hole_size;
4537 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004538
Josef Bacik5dc562c2012-08-17 13:14:17 -04004539 hole_em->block_start = EXTENT_MAP_HOLE;
4540 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004541 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004542 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004543 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4544 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004545 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004546
4547 while (1) {
4548 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004549 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004550 write_unlock(&em_tree->lock);
4551 if (err != -EEXIST)
4552 break;
4553 btrfs_drop_extent_cache(inode, cur_offset,
4554 cur_offset +
4555 hole_size - 1, 0);
4556 }
4557 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004558 }
Josef Bacik16e75492013-10-22 12:18:51 -04004559next:
Yan Zheng9036c102008-10-30 14:19:41 -04004560 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004561 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004562 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004563 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004564 break;
4565 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004566 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004567 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4568 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004569 return err;
4570}
4571
Eric Sandeen3972f262013-01-12 02:57:22 +00004572static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004573{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004574 struct btrfs_root *root = BTRFS_I(inode)->root;
4575 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004576 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004577 loff_t newsize = attr->ia_size;
4578 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004579 int ret;
4580
Eric Sandeen3972f262013-01-12 02:57:22 +00004581 /*
4582 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4583 * special case where we need to update the times despite not having
4584 * these flags set. For all other operations the VFS set these flags
4585 * explicitly if it wants a timestamp update.
4586 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004587 if (newsize != oldsize) {
4588 inode_inc_iversion(inode);
4589 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4590 inode->i_ctime = inode->i_mtime =
4591 current_fs_time(inode->i_sb);
4592 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004593
Josef Bacika41ad392011-01-31 15:30:16 -05004594 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004595 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004596 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004597 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004598 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004599
Miao Xief4a2f4c2011-12-14 20:12:01 -05004600 trans = btrfs_start_transaction(root, 1);
4601 if (IS_ERR(trans))
4602 return PTR_ERR(trans);
4603
4604 i_size_write(inode, newsize);
4605 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4606 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004607 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004608 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004609
Josef Bacika41ad392011-01-31 15:30:16 -05004610 /*
4611 * We're truncating a file that used to have good data down to
4612 * zero. Make sure it gets into the ordered flush list so that
4613 * any new writes get down to disk quickly.
4614 */
4615 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004616 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4617 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004618
Josef Bacikf3fe8202013-01-07 17:03:21 -05004619 /*
4620 * 1 for the orphan item we're going to add
4621 * 1 for the orphan item deletion.
4622 */
4623 trans = btrfs_start_transaction(root, 2);
4624 if (IS_ERR(trans))
4625 return PTR_ERR(trans);
4626
4627 /*
4628 * We need to do this in case we fail at _any_ point during the
4629 * actual truncate. Once we do the truncate_setsize we could
4630 * invalidate pages which forces any outstanding ordered io to
4631 * be instantly completed which will give us extents that need
4632 * to be truncated. If we fail to get an orphan inode down we
4633 * could have left over extents that were never meant to live,
4634 * so we need to garuntee from this point on that everything
4635 * will be consistent.
4636 */
4637 ret = btrfs_orphan_add(trans, inode);
4638 btrfs_end_transaction(trans, root);
4639 if (ret)
4640 return ret;
4641
Josef Bacika41ad392011-01-31 15:30:16 -05004642 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4643 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004644
4645 /* Disable nonlocked read DIO to avoid the end less truncate */
4646 btrfs_inode_block_unlocked_dio(inode);
4647 inode_dio_wait(inode);
4648 btrfs_inode_resume_unlocked_dio(inode);
4649
Josef Bacika41ad392011-01-31 15:30:16 -05004650 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004651 if (ret && inode->i_nlink) {
4652 int err;
4653
4654 /*
4655 * failed to truncate, disk_i_size is only adjusted down
4656 * as we remove extents, so it should represent the true
4657 * size of the inode, so reset the in memory size and
4658 * delete our orphan entry.
4659 */
4660 trans = btrfs_join_transaction(root);
4661 if (IS_ERR(trans)) {
4662 btrfs_orphan_del(NULL, inode);
4663 return ret;
4664 }
4665 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4666 err = btrfs_orphan_del(trans, inode);
4667 if (err)
4668 btrfs_abort_transaction(trans, root, err);
4669 btrfs_end_transaction(trans, root);
4670 }
Yan, Zheng80825102009-11-12 09:35:36 +00004671 }
4672
Josef Bacika41ad392011-01-31 15:30:16 -05004673 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004674}
4675
Chris Mason39279cc2007-06-12 06:35:45 -04004676static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4677{
4678 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004679 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004680 int err;
4681
Li Zefanb83cc962010-12-20 16:04:08 +08004682 if (btrfs_root_readonly(root))
4683 return -EROFS;
4684
Chris Mason39279cc2007-06-12 06:35:45 -04004685 err = inode_change_ok(inode, attr);
4686 if (err)
4687 return err;
4688
Chris Mason5a3f23d2009-03-31 13:27:11 -04004689 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004690 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004691 if (err)
4692 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004693 }
Yan Zheng9036c102008-10-30 14:19:41 -04004694
Christoph Hellwig10257742010-06-04 11:30:02 +02004695 if (attr->ia_valid) {
4696 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004697 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004698 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004699
Josef Bacik22c44fe2011-11-30 10:45:38 -05004700 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004701 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004702 }
4703
Chris Mason39279cc2007-06-12 06:35:45 -04004704 return err;
4705}
Chris Mason61295eb2008-01-14 16:24:38 -05004706
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004707/*
4708 * While truncating the inode pages during eviction, we get the VFS calling
4709 * btrfs_invalidatepage() against each page of the inode. This is slow because
4710 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4711 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4712 * extent_state structures over and over, wasting lots of time.
4713 *
4714 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4715 * those expensive operations on a per page basis and do only the ordered io
4716 * finishing, while we release here the extent_map and extent_state structures,
4717 * without the excessive merging and splitting.
4718 */
4719static void evict_inode_truncate_pages(struct inode *inode)
4720{
4721 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4722 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4723 struct rb_node *node;
4724
4725 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004726 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004727
4728 write_lock(&map_tree->lock);
4729 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4730 struct extent_map *em;
4731
4732 node = rb_first(&map_tree->map);
4733 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004734 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4735 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004736 remove_extent_mapping(map_tree, em);
4737 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004738 if (need_resched()) {
4739 write_unlock(&map_tree->lock);
4740 cond_resched();
4741 write_lock(&map_tree->lock);
4742 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004743 }
4744 write_unlock(&map_tree->lock);
4745
4746 spin_lock(&io_tree->lock);
4747 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4748 struct extent_state *state;
4749 struct extent_state *cached_state = NULL;
4750
4751 node = rb_first(&io_tree->state);
4752 state = rb_entry(node, struct extent_state, rb_node);
4753 atomic_inc(&state->refs);
4754 spin_unlock(&io_tree->lock);
4755
4756 lock_extent_bits(io_tree, state->start, state->end,
4757 0, &cached_state);
4758 clear_extent_bit(io_tree, state->start, state->end,
4759 EXTENT_LOCKED | EXTENT_DIRTY |
4760 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4761 EXTENT_DEFRAG, 1, 1,
4762 &cached_state, GFP_NOFS);
4763 free_extent_state(state);
4764
Filipe Manana7064dd52014-08-08 02:47:05 +01004765 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004766 spin_lock(&io_tree->lock);
4767 }
4768 spin_unlock(&io_tree->lock);
4769}
4770
Al Virobd555972010-06-07 11:35:40 -04004771void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004772{
4773 struct btrfs_trans_handle *trans;
4774 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004775 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004776 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004777 int ret;
4778
liubo1abe9b82011-03-24 11:18:59 +00004779 trace_btrfs_inode_evict(inode);
4780
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004781 evict_inode_truncate_pages(inode);
4782
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004783 if (inode->i_nlink &&
4784 ((btrfs_root_refs(&root->root_item) != 0 &&
4785 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4786 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004787 goto no_delete;
4788
Chris Mason39279cc2007-06-12 06:35:45 -04004789 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004790 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004791 goto no_delete;
4792 }
Al Virobd555972010-06-07 11:35:40 -04004793 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004794 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004795
Yan, Zhengc71bf092009-11-12 09:34:40 +00004796 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004797 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004798 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004799 goto no_delete;
4800 }
4801
Yan, Zheng76dda932009-09-21 16:00:26 -04004802 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004803 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4804 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004805 goto no_delete;
4806 }
4807
Miao Xie0e8c36a2012-12-19 06:59:51 +00004808 ret = btrfs_commit_inode_delayed_inode(inode);
4809 if (ret) {
4810 btrfs_orphan_del(NULL, inode);
4811 goto no_delete;
4812 }
4813
Miao Xie66d8f3d2012-09-06 04:02:28 -06004814 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004815 if (!rsv) {
4816 btrfs_orphan_del(NULL, inode);
4817 goto no_delete;
4818 }
Josef Bacik4a338542011-08-29 11:01:31 -04004819 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004820 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004821 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004822
Chris Masondbe674a2008-07-17 12:54:05 -04004823 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004824
Josef Bacik4289a662011-08-05 13:22:24 -04004825 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004826 * This is a bit simpler than btrfs_truncate since we've already
4827 * reserved our space for our orphan item in the unlink, so we just
4828 * need to reserve some slack space in case we add bytes and update
4829 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004830 */
Yan, Zheng80825102009-11-12 09:35:36 +00004831 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004832 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4833 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004834
Josef Bacik726c35f2011-09-26 15:46:06 -04004835 /*
4836 * Try and steal from the global reserve since we will
4837 * likely not use this space anyway, we want to try as
4838 * hard as possible to get this to work.
4839 */
4840 if (ret)
4841 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4842
Yan, Zhengd68fc572010-05-16 10:49:58 -04004843 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004844 btrfs_warn(root->fs_info,
4845 "Could not get space for a delete, will truncate on mount %d",
4846 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004847 btrfs_orphan_del(NULL, inode);
4848 btrfs_free_block_rsv(root, rsv);
4849 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004850 }
4851
Miao Xie0e8c36a2012-12-19 06:59:51 +00004852 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004853 if (IS_ERR(trans)) {
4854 btrfs_orphan_del(NULL, inode);
4855 btrfs_free_block_rsv(root, rsv);
4856 goto no_delete;
4857 }
4858
4859 trans->block_rsv = rsv;
4860
Yan, Zhengd68fc572010-05-16 10:49:58 -04004861 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004862 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004863 break;
4864
Miao Xie8407aa42012-09-07 01:43:32 -06004865 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004866 btrfs_end_transaction(trans, root);
4867 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004868 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004869 }
4870
Josef Bacik4289a662011-08-05 13:22:24 -04004871 btrfs_free_block_rsv(root, rsv);
4872
Josef Bacik4ef31a42013-08-13 14:10:08 -04004873 /*
4874 * Errors here aren't a big deal, it just means we leave orphan items
4875 * in the tree. They will be cleaned up on the next mount.
4876 */
Yan, Zheng80825102009-11-12 09:35:36 +00004877 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004878 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004879 btrfs_orphan_del(trans, inode);
4880 } else {
4881 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004882 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004883
Josef Bacik4289a662011-08-05 13:22:24 -04004884 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004885 if (!(root == root->fs_info->tree_root ||
4886 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004887 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004888
Chris Mason54aa1f42007-06-22 14:16:25 -04004889 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004890 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004891no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004892 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004893 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004894 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004895}
4896
4897/*
4898 * this returns the key found in the dir entry in the location pointer.
4899 * If no dir entries were found, location->objectid is 0.
4900 */
4901static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4902 struct btrfs_key *location)
4903{
4904 const char *name = dentry->d_name.name;
4905 int namelen = dentry->d_name.len;
4906 struct btrfs_dir_item *di;
4907 struct btrfs_path *path;
4908 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004909 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004910
4911 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004912 if (!path)
4913 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004914
Li Zefan33345d012011-04-20 10:31:50 +08004915 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004916 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004917 if (IS_ERR(di))
4918 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004919
David Sterbac7040052011-04-19 18:00:01 +02004920 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004921 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004922
Chris Mason5f39d392007-10-15 16:14:19 -04004923 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004924out:
Chris Mason39279cc2007-06-12 06:35:45 -04004925 btrfs_free_path(path);
4926 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004927out_err:
4928 location->objectid = 0;
4929 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004930}
4931
4932/*
4933 * when we hit a tree root in a directory, the btrfs part of the inode
4934 * needs to be changed to reflect the root directory of the tree root. This
4935 * is kind of like crossing a mount point.
4936 */
4937static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004938 struct inode *dir,
4939 struct dentry *dentry,
4940 struct btrfs_key *location,
4941 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004942{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004943 struct btrfs_path *path;
4944 struct btrfs_root *new_root;
4945 struct btrfs_root_ref *ref;
4946 struct extent_buffer *leaf;
4947 int ret;
4948 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004949
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004950 path = btrfs_alloc_path();
4951 if (!path) {
4952 err = -ENOMEM;
4953 goto out;
4954 }
Chris Mason39279cc2007-06-12 06:35:45 -04004955
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004956 err = -ENOENT;
Kelley Nielsen75ac2dd2013-11-04 19:35:58 -08004957 ret = btrfs_find_item(root->fs_info->tree_root, path,
4958 BTRFS_I(dir)->root->root_key.objectid,
4959 location->objectid, BTRFS_ROOT_REF_KEY, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004960 if (ret) {
4961 if (ret < 0)
4962 err = ret;
4963 goto out;
4964 }
Chris Mason39279cc2007-06-12 06:35:45 -04004965
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004966 leaf = path->nodes[0];
4967 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004968 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004969 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4970 goto out;
4971
4972 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4973 (unsigned long)(ref + 1),
4974 dentry->d_name.len);
4975 if (ret)
4976 goto out;
4977
David Sterbab3b4aa72011-04-21 01:20:15 +02004978 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004979
4980 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4981 if (IS_ERR(new_root)) {
4982 err = PTR_ERR(new_root);
4983 goto out;
4984 }
4985
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004986 *sub_root = new_root;
4987 location->objectid = btrfs_root_dirid(&new_root->root_item);
4988 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004989 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004990 err = 0;
4991out:
4992 btrfs_free_path(path);
4993 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004994}
4995
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004996static void inode_tree_add(struct inode *inode)
4997{
4998 struct btrfs_root *root = BTRFS_I(inode)->root;
4999 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005000 struct rb_node **p;
5001 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005002 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005003 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005004
Al Viro1d3382cb2010-10-23 15:19:20 -04005005 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005006 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005007 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005008 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005009 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005010 while (*p) {
5011 parent = *p;
5012 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5013
Li Zefan33345d012011-04-20 10:31:50 +08005014 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005015 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005016 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005017 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005018 else {
5019 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005020 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005021 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005022 RB_CLEAR_NODE(parent);
5023 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005024 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005025 }
5026 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005027 rb_link_node(new, parent, p);
5028 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005029 spin_unlock(&root->inode_lock);
5030}
5031
5032static void inode_tree_del(struct inode *inode)
5033{
5034 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005035 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005036
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005037 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005038 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005039 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005040 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005041 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005042 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005043 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005044
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005045 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005046 synchronize_srcu(&root->fs_info->subvol_srcu);
5047 spin_lock(&root->inode_lock);
5048 empty = RB_EMPTY_ROOT(&root->inode_tree);
5049 spin_unlock(&root->inode_lock);
5050 if (empty)
5051 btrfs_add_dead_root(root);
5052 }
5053}
5054
Jeff Mahoney143bede2012-03-01 14:56:26 +01005055void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005056{
5057 struct rb_node *node;
5058 struct rb_node *prev;
5059 struct btrfs_inode *entry;
5060 struct inode *inode;
5061 u64 objectid = 0;
5062
Liu Bo7813b3d2014-02-10 17:37:25 +08005063 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5064 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005065
5066 spin_lock(&root->inode_lock);
5067again:
5068 node = root->inode_tree.rb_node;
5069 prev = NULL;
5070 while (node) {
5071 prev = node;
5072 entry = rb_entry(node, struct btrfs_inode, rb_node);
5073
Li Zefan33345d012011-04-20 10:31:50 +08005074 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005075 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005076 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005077 node = node->rb_right;
5078 else
5079 break;
5080 }
5081 if (!node) {
5082 while (prev) {
5083 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005084 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005085 node = prev;
5086 break;
5087 }
5088 prev = rb_next(prev);
5089 }
5090 }
5091 while (node) {
5092 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005093 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005094 inode = igrab(&entry->vfs_inode);
5095 if (inode) {
5096 spin_unlock(&root->inode_lock);
5097 if (atomic_read(&inode->i_count) > 1)
5098 d_prune_aliases(inode);
5099 /*
Al Viro45321ac2010-06-07 13:43:19 -04005100 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005101 * the inode cache when its usage count
5102 * hits zero.
5103 */
5104 iput(inode);
5105 cond_resched();
5106 spin_lock(&root->inode_lock);
5107 goto again;
5108 }
5109
5110 if (cond_resched_lock(&root->inode_lock))
5111 goto again;
5112
5113 node = rb_next(node);
5114 }
5115 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005116}
5117
Chris Masone02119d2008-09-05 16:13:11 -04005118static int btrfs_init_locked_inode(struct inode *inode, void *p)
5119{
5120 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005121 inode->i_ino = args->location->objectid;
5122 memcpy(&BTRFS_I(inode)->location, args->location,
5123 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005124 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005125 return 0;
5126}
5127
5128static int btrfs_find_actor(struct inode *inode, void *opaque)
5129{
5130 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005131 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005132 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005133}
5134
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005135static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005136 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005137 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005138{
5139 struct inode *inode;
5140 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005141 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005142
Chris Mason90d3e592014-01-09 17:28:00 -08005143 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005144 args.root = root;
5145
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005146 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005147 btrfs_init_locked_inode,
5148 (void *)&args);
5149 return inode;
5150}
5151
Balaji Rao1a54ef82008-07-21 02:01:04 +05305152/* Get an inode object given its location and corresponding root.
5153 * Returns in *is_new if the inode was read from disk
5154 */
5155struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005156 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305157{
5158 struct inode *inode;
5159
Chris Mason90d3e592014-01-09 17:28:00 -08005160 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305161 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005162 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305163
5164 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305165 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005166 if (!is_bad_inode(inode)) {
5167 inode_tree_add(inode);
5168 unlock_new_inode(inode);
5169 if (new)
5170 *new = 1;
5171 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005172 unlock_new_inode(inode);
5173 iput(inode);
5174 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005175 }
5176 }
5177
Balaji Rao1a54ef82008-07-21 02:01:04 +05305178 return inode;
5179}
5180
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005181static struct inode *new_simple_dir(struct super_block *s,
5182 struct btrfs_key *key,
5183 struct btrfs_root *root)
5184{
5185 struct inode *inode = new_inode(s);
5186
5187 if (!inode)
5188 return ERR_PTR(-ENOMEM);
5189
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005190 BTRFS_I(inode)->root = root;
5191 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005192 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005193
5194 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005195 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005196 inode->i_fop = &simple_dir_operations;
5197 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5198 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5199
5200 return inode;
5201}
5202
Chris Mason3de45862008-11-17 21:02:50 -05005203struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005204{
Chris Masond3977122009-01-05 21:25:51 -05005205 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005206 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005207 struct btrfs_root *sub_root = root;
5208 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005209 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005210 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005211
5212 if (dentry->d_name.len > BTRFS_NAME_LEN)
5213 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005214
Jeff Layton39e3c952012-11-28 11:30:53 -05005215 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005216 if (ret < 0)
5217 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005218
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005219 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005220 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005221
5222 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005223 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005224 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005225 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005226
5227 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5228
Yan, Zheng76dda932009-09-21 16:00:26 -04005229 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005230 ret = fixup_tree_root_location(root, dir, dentry,
5231 &location, &sub_root);
5232 if (ret < 0) {
5233 if (ret != -ENOENT)
5234 inode = ERR_PTR(ret);
5235 else
5236 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5237 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005238 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005239 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005240 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5241
Julia Lawall34d19ba2011-01-24 19:55:19 +00005242 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005243 down_read(&root->fs_info->cleanup_work_sem);
5244 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005245 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005246 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005247 if (ret) {
5248 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005249 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005250 }
Filipe Manana9c3b3062014-07-31 14:41:07 +01005251 /*
5252 * If orphan cleanup did remove any orphans, it means the tree
5253 * was modified and therefore the commit root is not the same as
5254 * the current root anymore. This is a problem, because send
5255 * uses the commit root and therefore can see inode items that
5256 * don't exist in the current root anymore, and for example make
5257 * calls to btrfs_iget, which will do tree lookups based on the
5258 * current root and not on the commit root. Those lookups will
5259 * fail, returning a -ESTALE error, and making send fail with
5260 * that error. So make sure a send does not see any orphans we
5261 * have just removed, and that it will see the same inodes
5262 * regardless of whether a transaction commit happened before
5263 * it started (meaning that the commit root will be the same as
5264 * the current root) or not.
5265 */
5266 if (sub_root->node != sub_root->commit_root) {
5267 u64 sub_flags = btrfs_root_flags(&sub_root->root_item);
5268
5269 if (sub_flags & BTRFS_ROOT_SUBVOL_RDONLY) {
5270 struct extent_buffer *eb;
5271
5272 /*
5273 * Assert we can't have races between dentry
5274 * lookup called through the snapshot creation
5275 * ioctl and the VFS.
5276 */
5277 ASSERT(mutex_is_locked(&dir->i_mutex));
5278
5279 down_write(&root->fs_info->commit_root_sem);
5280 eb = sub_root->commit_root;
5281 sub_root->commit_root =
5282 btrfs_root_node(sub_root);
5283 up_write(&root->fs_info->commit_root_sem);
5284 free_extent_buffer(eb);
5285 }
5286 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005287 }
5288
Chris Mason3de45862008-11-17 21:02:50 -05005289 return inode;
5290}
5291
Nick Pigginfe15ce42011-01-07 17:49:23 +11005292static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005293{
5294 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08005295 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005296
Li Zefan848cce02012-02-21 17:04:28 +08005297 if (!inode && !IS_ROOT(dentry))
5298 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005299
Li Zefan848cce02012-02-21 17:04:28 +08005300 if (inode) {
5301 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005302 if (btrfs_root_refs(&root->root_item) == 0)
5303 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005304
5305 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5306 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005307 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005308 return 0;
5309}
5310
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005311static void btrfs_dentry_release(struct dentry *dentry)
5312{
Daeseok Youn944a4512014-04-14 15:37:02 +09005313 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005314}
5315
Chris Mason3de45862008-11-17 21:02:50 -05005316static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005317 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005318{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005319 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005320
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005321 inode = btrfs_lookup_dentry(dir, dentry);
5322 if (IS_ERR(inode)) {
5323 if (PTR_ERR(inode) == -ENOENT)
5324 inode = NULL;
5325 else
5326 return ERR_CAST(inode);
5327 }
5328
Josef Bacik3a0dfa62014-02-14 13:43:48 -05005329 return d_materialise_unique(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005330}
5331
Miao Xie16cdcec2011-04-22 18:12:22 +08005332unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005333 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5334};
5335
Al Viro9cdda8d2013-05-22 16:48:09 -04005336static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005337{
Al Viro9cdda8d2013-05-22 16:48:09 -04005338 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005339 struct btrfs_root *root = BTRFS_I(inode)->root;
5340 struct btrfs_item *item;
5341 struct btrfs_dir_item *di;
5342 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005343 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005344 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005345 struct list_head ins_list;
5346 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005347 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005348 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005349 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005350 unsigned char d_type;
5351 int over = 0;
5352 u32 di_cur;
5353 u32 di_total;
5354 u32 di_len;
5355 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005356 char tmp_name[32];
5357 char *name_ptr;
5358 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005359 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005360
5361 /* FIXME, use a real flag for deciding about the key type */
5362 if (root->fs_info->tree_root == root)
5363 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005364
Al Viro9cdda8d2013-05-22 16:48:09 -04005365 if (!dir_emit_dots(file, ctx))
5366 return 0;
5367
David Woodhouse49593bf2008-08-17 17:08:36 +01005368 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005369 if (!path)
5370 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005371
Josef Bacik026fd312011-05-13 10:32:11 -04005372 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005373
Miao Xie16cdcec2011-04-22 18:12:22 +08005374 if (key_type == BTRFS_DIR_INDEX_KEY) {
5375 INIT_LIST_HEAD(&ins_list);
5376 INIT_LIST_HEAD(&del_list);
5377 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5378 }
5379
David Sterba962a2982014-06-04 18:41:45 +02005380 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005381 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005382 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005383
Chris Mason39279cc2007-06-12 06:35:45 -04005384 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5385 if (ret < 0)
5386 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005387
5388 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005389 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005390 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005391 if (slot >= btrfs_header_nritems(leaf)) {
5392 ret = btrfs_next_leaf(root, path);
5393 if (ret < 0)
5394 goto err;
5395 else if (ret > 0)
5396 break;
5397 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005398 }
Chris Mason3de45862008-11-17 21:02:50 -05005399
Ross Kirkdd3cc162013-09-16 15:58:09 +01005400 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005401 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5402
5403 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005404 break;
David Sterba962a2982014-06-04 18:41:45 +02005405 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005406 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005407 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005408 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005409 if (key_type == BTRFS_DIR_INDEX_KEY &&
5410 btrfs_should_delete_dir_index(&del_list,
5411 found_key.offset))
5412 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005413
Al Viro9cdda8d2013-05-22 16:48:09 -04005414 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005415 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005416
Chris Mason39279cc2007-06-12 06:35:45 -04005417 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5418 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005419 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005420
5421 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005422 struct btrfs_key location;
5423
Josef Bacik22a94d42011-03-16 16:47:17 -04005424 if (verify_dir_item(root, leaf, di))
5425 break;
5426
Chris Mason5f39d392007-10-15 16:14:19 -04005427 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005428 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005429 name_ptr = tmp_name;
5430 } else {
5431 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005432 if (!name_ptr) {
5433 ret = -ENOMEM;
5434 goto err;
5435 }
Chris Mason5f39d392007-10-15 16:14:19 -04005436 }
5437 read_extent_buffer(leaf, name_ptr,
5438 (unsigned long)(di + 1), name_len);
5439
5440 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5441 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005442
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005443
Chris Mason3de45862008-11-17 21:02:50 -05005444 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005445 * skip it.
5446 *
5447 * In contrast to old kernels, we insert the snapshot's
5448 * dir item and dir index after it has been created, so
5449 * we won't find a reference to our own snapshot. We
5450 * still keep the following code for backward
5451 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005452 */
5453 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5454 location.objectid == root->root_key.objectid) {
5455 over = 0;
5456 goto skip;
5457 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005458 over = !dir_emit(ctx, name_ptr, name_len,
5459 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005460
Chris Mason3de45862008-11-17 21:02:50 -05005461skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005462 if (name_ptr != tmp_name)
5463 kfree(name_ptr);
5464
Chris Mason39279cc2007-06-12 06:35:45 -04005465 if (over)
5466 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005467 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005468 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005469 di_cur += di_len;
5470 di = (struct btrfs_dir_item *)((char *)di + di_len);
5471 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005472next:
5473 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005474 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005475
Miao Xie16cdcec2011-04-22 18:12:22 +08005476 if (key_type == BTRFS_DIR_INDEX_KEY) {
5477 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005478 ctx->pos++;
5479 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005480 if (ret)
5481 goto nopos;
5482 }
5483
David Woodhouse49593bf2008-08-17 17:08:36 +01005484 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005485 ctx->pos++;
5486
5487 /*
5488 * Stop new entries from being returned after we return the last
5489 * entry.
5490 *
5491 * New directory entries are assigned a strictly increasing
5492 * offset. This means that new entries created during readdir
5493 * are *guaranteed* to be seen in the future by that readdir.
5494 * This has broken buggy programs which operate on names as
5495 * they're returned by readdir. Until we re-use freed offsets
5496 * we have this hack to stop new entries from being returned
5497 * under the assumption that they'll never reach this huge
5498 * offset.
5499 *
5500 * This is being careful not to overflow 32bit loff_t unless the
5501 * last entry requires it because doing so has broken 32bit apps
5502 * in the past.
5503 */
5504 if (key_type == BTRFS_DIR_INDEX_KEY) {
5505 if (ctx->pos >= INT_MAX)
5506 ctx->pos = LLONG_MAX;
5507 else
5508 ctx->pos = INT_MAX;
5509 }
Chris Mason39279cc2007-06-12 06:35:45 -04005510nopos:
5511 ret = 0;
5512err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005513 if (key_type == BTRFS_DIR_INDEX_KEY)
5514 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005515 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005516 return ret;
5517}
5518
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005519int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005520{
5521 struct btrfs_root *root = BTRFS_I(inode)->root;
5522 struct btrfs_trans_handle *trans;
5523 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005524 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005525
Josef Bacik72ac3c02012-05-23 14:13:11 -04005526 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005527 return 0;
5528
Liu Bo83eea1f2012-07-10 05:28:39 -06005529 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005530 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005531
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005532 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005533 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005534 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005535 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005536 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005537 if (IS_ERR(trans))
5538 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005539 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005540 }
5541 return ret;
5542}
5543
5544/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005545 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005546 * inode changes. But, it is most likely to find the inode in cache.
5547 * FIXME, needs more benchmarking...there are no reasons other than performance
5548 * to keep or drop this code.
5549 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005550static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005551{
5552 struct btrfs_root *root = BTRFS_I(inode)->root;
5553 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005554 int ret;
5555
Josef Bacik72ac3c02012-05-23 14:13:11 -04005556 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005557 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005558
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005559 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005560 if (IS_ERR(trans))
5561 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005562
5563 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005564 if (ret && ret == -ENOSPC) {
5565 /* whoops, lets try again with the full transaction */
5566 btrfs_end_transaction(trans, root);
5567 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005568 if (IS_ERR(trans))
5569 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005570
Chris Mason94b60442010-05-26 11:02:00 -04005571 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005572 }
Chris Mason39279cc2007-06-12 06:35:45 -04005573 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005574 if (BTRFS_I(inode)->delayed_node)
5575 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005576
5577 return ret;
5578}
5579
5580/*
5581 * This is a copy of file_update_time. We need this so we can return error on
5582 * ENOSPC for updating the inode in the case of file write and mmap writes.
5583 */
Josef Bacike41f9412012-03-26 09:46:47 -04005584static int btrfs_update_time(struct inode *inode, struct timespec *now,
5585 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005586{
Alexander Block2bc5565282012-06-15 09:49:33 +02005587 struct btrfs_root *root = BTRFS_I(inode)->root;
5588
5589 if (btrfs_root_readonly(root))
5590 return -EROFS;
5591
Josef Bacike41f9412012-03-26 09:46:47 -04005592 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005593 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005594 if (flags & S_CTIME)
5595 inode->i_ctime = *now;
5596 if (flags & S_MTIME)
5597 inode->i_mtime = *now;
5598 if (flags & S_ATIME)
5599 inode->i_atime = *now;
5600 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005601}
5602
Chris Masond352ac62008-09-29 15:18:18 -04005603/*
5604 * find the highest existing sequence number in a directory
5605 * and then set the in-memory index_cnt variable to reflect
5606 * free sequence numbers
5607 */
Josef Bacikaec74772008-07-24 12:12:38 -04005608static int btrfs_set_inode_index_count(struct inode *inode)
5609{
5610 struct btrfs_root *root = BTRFS_I(inode)->root;
5611 struct btrfs_key key, found_key;
5612 struct btrfs_path *path;
5613 struct extent_buffer *leaf;
5614 int ret;
5615
Li Zefan33345d012011-04-20 10:31:50 +08005616 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005617 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005618 key.offset = (u64)-1;
5619
5620 path = btrfs_alloc_path();
5621 if (!path)
5622 return -ENOMEM;
5623
5624 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5625 if (ret < 0)
5626 goto out;
5627 /* FIXME: we should be able to handle this */
5628 if (ret == 0)
5629 goto out;
5630 ret = 0;
5631
5632 /*
5633 * MAGIC NUMBER EXPLANATION:
5634 * since we search a directory based on f_pos we have to start at 2
5635 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5636 * else has to start at 2
5637 */
5638 if (path->slots[0] == 0) {
5639 BTRFS_I(inode)->index_cnt = 2;
5640 goto out;
5641 }
5642
5643 path->slots[0]--;
5644
5645 leaf = path->nodes[0];
5646 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5647
Li Zefan33345d012011-04-20 10:31:50 +08005648 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005649 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04005650 BTRFS_I(inode)->index_cnt = 2;
5651 goto out;
5652 }
5653
5654 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5655out:
5656 btrfs_free_path(path);
5657 return ret;
5658}
5659
Chris Masond352ac62008-09-29 15:18:18 -04005660/*
5661 * helper to find a free sequence number in a given directory. This current
5662 * code is very simple, later versions will do smarter things in the btree
5663 */
Chris Mason3de45862008-11-17 21:02:50 -05005664int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005665{
5666 int ret = 0;
5667
5668 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005669 ret = btrfs_inode_delayed_dir_index_count(dir);
5670 if (ret) {
5671 ret = btrfs_set_inode_index_count(dir);
5672 if (ret)
5673 return ret;
5674 }
Josef Bacikaec74772008-07-24 12:12:38 -04005675 }
5676
Chris Mason00e4e6b2008-08-05 11:18:09 -04005677 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005678 BTRFS_I(dir)->index_cnt++;
5679
5680 return ret;
5681}
5682
Chris Masonb0d5d102014-09-08 13:08:51 -07005683static int btrfs_insert_inode_locked(struct inode *inode)
5684{
5685 struct btrfs_iget_args args;
5686 args.location = &BTRFS_I(inode)->location;
5687 args.root = BTRFS_I(inode)->root;
5688
5689 return insert_inode_locked4(inode,
5690 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5691 btrfs_find_actor, &args);
5692}
5693
Chris Mason39279cc2007-06-12 06:35:45 -04005694static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5695 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005696 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005697 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005698 u64 ref_objectid, u64 objectid,
5699 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005700{
5701 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005702 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005703 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005704 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005705 struct btrfs_inode_ref *ref;
5706 struct btrfs_key key[2];
5707 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005708 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005709 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005710 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005711
Chris Mason5f39d392007-10-15 16:14:19 -04005712 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005713 if (!path)
5714 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005715
Chris Mason39279cc2007-06-12 06:35:45 -04005716 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005717 if (!inode) {
5718 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005719 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005720 }
Chris Mason39279cc2007-06-12 06:35:45 -04005721
Li Zefan581bb052011-04-20 10:06:11 +08005722 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005723 * O_TMPFILE, set link count to 0, so that after this point,
5724 * we fill in an inode item with the correct link count.
5725 */
5726 if (!name)
5727 set_nlink(inode, 0);
5728
5729 /*
Li Zefan581bb052011-04-20 10:06:11 +08005730 * we have to initialize this early, so we can reclaim the inode
5731 * number if we fail afterwards in this function.
5732 */
5733 inode->i_ino = objectid;
5734
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005735 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005736 trace_btrfs_inode_request(dir);
5737
Chris Mason3de45862008-11-17 21:02:50 -05005738 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005739 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005740 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005741 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005742 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005743 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005744 } else if (dir) {
5745 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005746 }
5747 /*
5748 * index_cnt is ignored for everything but a dir,
5749 * btrfs_get_inode_index_count has an explanation for the magic
5750 * number
5751 */
5752 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005753 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005754 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005755 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005756 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005757
Josef Bacik5dc562c2012-08-17 13:14:17 -04005758 /*
5759 * We could have gotten an inode number from somebody who was fsynced
5760 * and then removed in this same transaction, so let's just set full
5761 * sync since it will be a full sync anyway and this will blow away the
5762 * old info in the log.
5763 */
5764 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5765
Chris Mason9c583092008-01-29 15:15:18 -05005766 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005767 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005768 key[0].offset = 0;
5769
Chris Mason9c583092008-01-29 15:15:18 -05005770 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005771
5772 if (name) {
5773 /*
5774 * Start new inodes with an inode_ref. This is slightly more
5775 * efficient for small numbers of hard links since they will
5776 * be packed into one item. Extended refs will kick in if we
5777 * add more hard links than can fit in the ref item.
5778 */
5779 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005780 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005781 key[1].offset = ref_objectid;
5782
5783 sizes[1] = name_len + sizeof(*ref);
5784 }
Chris Mason9c583092008-01-29 15:15:18 -05005785
Chris Masonb0d5d102014-09-08 13:08:51 -07005786 location = &BTRFS_I(inode)->location;
5787 location->objectid = objectid;
5788 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02005789 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07005790
5791 ret = btrfs_insert_inode_locked(inode);
5792 if (ret < 0)
5793 goto fail;
5794
Chris Masonb9473432009-03-13 11:00:37 -04005795 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005796 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005797 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07005798 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04005799
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005800 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005801 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005802 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005803 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5804 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005805 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5806 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005807 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005808
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005809 if (name) {
5810 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5811 struct btrfs_inode_ref);
5812 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5813 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5814 ptr = (unsigned long)(ref + 1);
5815 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5816 }
Chris Mason9c583092008-01-29 15:15:18 -05005817
Chris Mason5f39d392007-10-15 16:14:19 -04005818 btrfs_mark_buffer_dirty(path->nodes[0]);
5819 btrfs_free_path(path);
5820
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005821 btrfs_inherit_iflags(inode, dir);
5822
Al Viro569254b2011-07-24 17:08:40 -04005823 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005824 if (btrfs_test_opt(root, NODATASUM))
5825 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005826 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005827 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5828 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005829 }
5830
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005831 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005832
5833 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005834 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005835
Alexander Block8ea05e32012-07-25 17:35:53 +02005836 btrfs_update_root_times(trans, root);
5837
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005838 ret = btrfs_inode_inherit_props(trans, inode, dir);
5839 if (ret)
5840 btrfs_err(root->fs_info,
5841 "error inheriting props for ino %llu (root %llu): %d",
5842 btrfs_ino(inode), root->root_key.objectid, ret);
5843
Chris Mason39279cc2007-06-12 06:35:45 -04005844 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07005845
5846fail_unlock:
5847 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005848fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005849 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04005850 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005851 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005852 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005853 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005854}
5855
5856static inline u8 btrfs_inode_type(struct inode *inode)
5857{
5858 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5859}
5860
Chris Masond352ac62008-09-29 15:18:18 -04005861/*
5862 * utility function to add 'inode' into 'parent_inode' with
5863 * a give name and a given sequence number.
5864 * if 'add_backref' is true, also insert a backref from the
5865 * inode to the parent directory.
5866 */
Chris Masone02119d2008-09-05 16:13:11 -04005867int btrfs_add_link(struct btrfs_trans_handle *trans,
5868 struct inode *parent_inode, struct inode *inode,
5869 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005870{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005871 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005872 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005873 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005874 u64 ino = btrfs_ino(inode);
5875 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005876
Li Zefan33345d012011-04-20 10:31:50 +08005877 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005878 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5879 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005880 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02005881 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005882 key.offset = 0;
5883 }
Chris Mason39279cc2007-06-12 06:35:45 -04005884
Li Zefan33345d012011-04-20 10:31:50 +08005885 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005886 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5887 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005888 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005889 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005890 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5891 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005892 }
5893
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005894 /* Nothing to clean up yet */
5895 if (ret)
5896 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005897
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005898 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5899 parent_inode, &key,
5900 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005901 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005902 goto fail_dir_item;
5903 else if (ret) {
5904 btrfs_abort_transaction(trans, root, ret);
5905 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005906 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005907
5908 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5909 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005910 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005911 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5912 ret = btrfs_update_inode(trans, root, parent_inode);
5913 if (ret)
5914 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005915 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005916
5917fail_dir_item:
5918 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5919 u64 local_index;
5920 int err;
5921 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5922 key.objectid, root->root_key.objectid,
5923 parent_ino, &local_index, name, name_len);
5924
5925 } else if (add_backref) {
5926 u64 local_index;
5927 int err;
5928
5929 err = btrfs_del_inode_ref(trans, root, name, name_len,
5930 ino, parent_ino, &local_index);
5931 }
5932 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005933}
5934
5935static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005936 struct inode *dir, struct dentry *dentry,
5937 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005938{
Josef Bacika1b075d2010-11-19 20:36:11 +00005939 int err = btrfs_add_link(trans, dir, inode,
5940 dentry->d_name.name, dentry->d_name.len,
5941 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005942 if (err > 0)
5943 err = -EEXIST;
5944 return err;
5945}
5946
Josef Bacik618e21d2007-07-11 10:18:17 -04005947static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005948 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005949{
5950 struct btrfs_trans_handle *trans;
5951 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005952 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005953 int err;
5954 int drop_inode = 0;
5955 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005956 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005957
5958 if (!new_valid_dev(rdev))
5959 return -EINVAL;
5960
Josef Bacik9ed74f22009-09-11 16:12:44 -04005961 /*
5962 * 2 for inode item and ref
5963 * 2 for dir items
5964 * 1 for xattr if selinux is on
5965 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005966 trans = btrfs_start_transaction(root, 5);
5967 if (IS_ERR(trans))
5968 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005969
Li Zefan581bb052011-04-20 10:06:11 +08005970 err = btrfs_find_free_ino(root, &objectid);
5971 if (err)
5972 goto out_unlock;
5973
Josef Bacikaec74772008-07-24 12:12:38 -04005974 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005975 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005976 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005977 if (IS_ERR(inode)) {
5978 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005979 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005980 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005981
Casey Schauflerad19db72011-12-15 10:09:07 -05005982 /*
5983 * If the active LSM wants to access the inode during
5984 * d_instantiate it needs these. Smack checks to see
5985 * if the filesystem supports xattrs by looking at the
5986 * ops vector.
5987 */
Casey Schauflerad19db72011-12-15 10:09:07 -05005988 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07005989 init_special_inode(inode, inode->i_mode, rdev);
5990
5991 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04005992 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07005993 goto out_unlock_inode;
5994
5995 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
5996 if (err) {
5997 goto out_unlock_inode;
5998 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04005999 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006000 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006001 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006002 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006003
Josef Bacik618e21d2007-07-11 10:18:17 -04006004out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006005 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006006 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006007 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006008 if (drop_inode) {
6009 inode_dec_link_count(inode);
6010 iput(inode);
6011 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006012 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006013
6014out_unlock_inode:
6015 drop_inode = 1;
6016 unlock_new_inode(inode);
6017 goto out_unlock;
6018
Josef Bacik618e21d2007-07-11 10:18:17 -04006019}
6020
Chris Mason39279cc2007-06-12 06:35:45 -04006021static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006022 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006023{
6024 struct btrfs_trans_handle *trans;
6025 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006026 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006027 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006028 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006029 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006030 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006031
Josef Bacik9ed74f22009-09-11 16:12:44 -04006032 /*
6033 * 2 for inode item and ref
6034 * 2 for dir items
6035 * 1 for xattr if selinux is on
6036 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006037 trans = btrfs_start_transaction(root, 5);
6038 if (IS_ERR(trans))
6039 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006040
Li Zefan581bb052011-04-20 10:06:11 +08006041 err = btrfs_find_free_ino(root, &objectid);
6042 if (err)
6043 goto out_unlock;
6044
Josef Bacikaec74772008-07-24 12:12:38 -04006045 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006046 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006047 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006048 if (IS_ERR(inode)) {
6049 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006050 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006051 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006052 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006053 /*
6054 * If the active LSM wants to access the inode during
6055 * d_instantiate it needs these. Smack checks to see
6056 * if the filesystem supports xattrs by looking at the
6057 * ops vector.
6058 */
6059 inode->i_fop = &btrfs_file_operations;
6060 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006061 inode->i_mapping->a_ops = &btrfs_aops;
6062 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
6063
6064 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6065 if (err)
6066 goto out_unlock_inode;
6067
6068 err = btrfs_update_inode(trans, root, inode);
6069 if (err)
6070 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006071
Josef Bacika1b075d2010-11-19 20:36:11 +00006072 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006073 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006074 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006075
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006076 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006077 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006078 d_instantiate(dentry, inode);
6079
Chris Mason39279cc2007-06-12 06:35:45 -04006080out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006081 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006082 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006083 inode_dec_link_count(inode);
6084 iput(inode);
6085 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006086 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006087 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006088 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006089
6090out_unlock_inode:
6091 unlock_new_inode(inode);
6092 goto out_unlock;
6093
Chris Mason39279cc2007-06-12 06:35:45 -04006094}
6095
6096static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6097 struct dentry *dentry)
6098{
6099 struct btrfs_trans_handle *trans;
6100 struct btrfs_root *root = BTRFS_I(dir)->root;
6101 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006102 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006103 int err;
6104 int drop_inode = 0;
6105
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006106 /* do not allow sys_link's with other subvols of the same device */
6107 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006108 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006109
Mark Fashehf1863732012-08-08 11:32:27 -07006110 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006111 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006112
Chris Mason3de45862008-11-17 21:02:50 -05006113 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006114 if (err)
6115 goto fail;
6116
Yan, Zhenga22285a2010-05-16 10:48:46 -04006117 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006118 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006119 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006120 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006121 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006122 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006123 if (IS_ERR(trans)) {
6124 err = PTR_ERR(trans);
6125 goto fail;
6126 }
Chris Mason5f39d392007-10-15 16:14:19 -04006127
Miao Xie67de1172013-12-26 13:07:06 +08006128 /* There are several dir indexes for this inode, clear the cache. */
6129 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006130 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006131 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08006132 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04006133 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006134 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006135
Josef Bacika1b075d2010-11-19 20:36:11 +00006136 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006137
Yan, Zhenga5719522009-09-24 09:17:31 -04006138 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006139 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006140 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006141 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006142 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006143 if (err)
6144 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006145 if (inode->i_nlink == 1) {
6146 /*
6147 * If new hard link count is 1, it's a file created
6148 * with open(2) O_TMPFILE flag.
6149 */
6150 err = btrfs_orphan_del(trans, inode);
6151 if (err)
6152 goto fail;
6153 }
Al Viro08c422c2011-12-23 07:58:13 -05006154 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006155 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006156 }
Chris Mason39279cc2007-06-12 06:35:45 -04006157
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006158 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006159 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006160fail:
Chris Mason39279cc2007-06-12 06:35:45 -04006161 if (drop_inode) {
6162 inode_dec_link_count(inode);
6163 iput(inode);
6164 }
Liu Bob53d3f52012-11-14 14:34:34 +00006165 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006166 return err;
6167}
6168
Al Viro18bb1db2011-07-26 01:41:39 -04006169static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006170{
Chris Masonb9d86662008-05-02 16:13:49 -04006171 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006172 struct btrfs_trans_handle *trans;
6173 struct btrfs_root *root = BTRFS_I(dir)->root;
6174 int err = 0;
6175 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006176 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006177 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006178
Josef Bacik9ed74f22009-09-11 16:12:44 -04006179 /*
6180 * 2 items for inode and ref
6181 * 2 items for dir items
6182 * 1 for xattr if selinux is on
6183 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006184 trans = btrfs_start_transaction(root, 5);
6185 if (IS_ERR(trans))
6186 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006187
Li Zefan581bb052011-04-20 10:06:11 +08006188 err = btrfs_find_free_ino(root, &objectid);
6189 if (err)
6190 goto out_fail;
6191
Josef Bacikaec74772008-07-24 12:12:38 -04006192 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006193 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006194 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006195 if (IS_ERR(inode)) {
6196 err = PTR_ERR(inode);
6197 goto out_fail;
6198 }
Chris Mason5f39d392007-10-15 16:14:19 -04006199
Chris Mason39279cc2007-06-12 06:35:45 -04006200 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006201 /* these must be set before we unlock the inode */
6202 inode->i_op = &btrfs_dir_inode_operations;
6203 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006204
Eric Paris2a7dba32011-02-01 11:05:39 -05006205 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006206 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006207 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006208
Chris Masondbe674a2008-07-17 12:54:05 -04006209 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006210 err = btrfs_update_inode(trans, root, inode);
6211 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006212 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006213
Josef Bacika1b075d2010-11-19 20:36:11 +00006214 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6215 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006216 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006217 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006218
Chris Mason39279cc2007-06-12 06:35:45 -04006219 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006220 /*
6221 * mkdir is special. We're unlocking after we call d_instantiate
6222 * to avoid a race with nfsd calling d_instantiate.
6223 */
6224 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006225 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006226
6227out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006228 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006229 if (drop_on_err)
6230 iput(inode);
Josef Bacikc581afc2014-02-06 16:06:06 -05006231 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006232 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006233 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006234
6235out_fail_inode:
6236 unlock_new_inode(inode);
6237 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006238}
6239
Chris Masond352ac62008-09-29 15:18:18 -04006240/* helper for btfs_get_extent. Given an existing extent in the tree,
6241 * and an extent that you want to insert, deal with overlap and insert
6242 * the new extent into the tree.
6243 */
Chris Mason3b951512008-04-17 11:29:12 -04006244static int merge_extent_mapping(struct extent_map_tree *em_tree,
6245 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006246 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006247 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006248{
6249 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006250
Chris Masone6dcd2d2008-07-17 12:53:50 -04006251 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
6252 start_diff = map_start - em->start;
6253 em->start = map_start;
Qu Wenruo51f395a2014-08-08 13:06:20 +08006254 em->len = existing->start - em->start;
Chris Masonc8b97812008-10-29 14:49:59 -04006255 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6256 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006257 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006258 em->block_len -= start_diff;
6259 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006260 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006261}
6262
Chris Masonc8b97812008-10-29 14:49:59 -04006263static noinline int uncompress_inline(struct btrfs_path *path,
6264 struct inode *inode, struct page *page,
6265 size_t pg_offset, u64 extent_offset,
6266 struct btrfs_file_extent_item *item)
6267{
6268 int ret;
6269 struct extent_buffer *leaf = path->nodes[0];
6270 char *tmp;
6271 size_t max_size;
6272 unsigned long inline_size;
6273 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006274 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006275
6276 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006277 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006278 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6279 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006280 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006281 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006282 if (!tmp)
6283 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006284 ptr = btrfs_file_extent_inline_start(item);
6285
6286 read_extent_buffer(leaf, tmp, ptr, inline_size);
6287
Chris Mason5b050f02008-11-11 09:34:41 -05006288 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006289 ret = btrfs_decompress(compress_type, tmp, page,
6290 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006291 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006292 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006293}
6294
Chris Masond352ac62008-09-29 15:18:18 -04006295/*
6296 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006297 * the ugly parts come from merging extents from the disk with the in-ram
6298 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006299 * where the in-ram extents might be locked pending data=ordered completion.
6300 *
6301 * This also copies inline extents directly into the page.
6302 */
Chris Masond3977122009-01-05 21:25:51 -05006303
Chris Masona52d9a82007-08-27 16:49:44 -04006304struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006305 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006306 int create)
6307{
6308 int ret;
6309 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006310 u64 extent_start = 0;
6311 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006312 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006313 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006314 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006315 struct btrfs_root *root = BTRFS_I(inode)->root;
6316 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006317 struct extent_buffer *leaf;
6318 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006319 struct extent_map *em = NULL;
6320 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006321 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006322 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006323 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006324
Chris Masona52d9a82007-08-27 16:49:44 -04006325again:
Chris Mason890871b2009-09-02 16:24:52 -04006326 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006327 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006328 if (em)
6329 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006330 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006331
Chris Masona52d9a82007-08-27 16:49:44 -04006332 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006333 if (em->start > start || em->start + em->len <= start)
6334 free_extent_map(em);
6335 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006336 free_extent_map(em);
6337 else
6338 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006339 }
David Sterba172ddd62011-04-21 00:48:27 +02006340 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006341 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006342 err = -ENOMEM;
6343 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006344 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006345 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006346 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006347 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006348 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006349 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006350
6351 if (!path) {
6352 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006353 if (!path) {
6354 err = -ENOMEM;
6355 goto out;
6356 }
6357 /*
6358 * Chances are we'll be called again, so go ahead and do
6359 * readahead
6360 */
6361 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04006362 }
6363
Chris Mason179e29e2007-11-01 11:28:41 -04006364 ret = btrfs_lookup_file_extent(trans, root, path,
6365 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006366 if (ret < 0) {
6367 err = ret;
6368 goto out;
6369 }
6370
6371 if (ret != 0) {
6372 if (path->slots[0] == 0)
6373 goto not_found;
6374 path->slots[0]--;
6375 }
6376
Chris Mason5f39d392007-10-15 16:14:19 -04006377 leaf = path->nodes[0];
6378 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006379 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006380 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006381 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006382 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006383 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006384 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006385 /*
6386 * If we backup past the first extent we want to move forward
6387 * and see if there is an extent in front of us, otherwise we'll
6388 * say there is a hole for our whole search range which can
6389 * cause problems.
6390 */
6391 extent_end = start;
6392 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006393 }
6394
Chris Mason5f39d392007-10-15 16:14:19 -04006395 found_type = btrfs_file_extent_type(leaf, item);
6396 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006397 if (found_type == BTRFS_FILE_EXTENT_REG ||
6398 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006399 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006400 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006401 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6402 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006403 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006404 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006405 }
Josef Bacik25a50342013-10-14 12:08:38 -04006406next:
Yan Zheng9036c102008-10-30 14:19:41 -04006407 if (start >= extent_end) {
6408 path->slots[0]++;
6409 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6410 ret = btrfs_next_leaf(root, path);
6411 if (ret < 0) {
6412 err = ret;
6413 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006414 }
Yan Zheng9036c102008-10-30 14:19:41 -04006415 if (ret > 0)
6416 goto not_found;
6417 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006418 }
Yan Zheng9036c102008-10-30 14:19:41 -04006419 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6420 if (found_key.objectid != objectid ||
6421 found_key.type != BTRFS_EXTENT_DATA_KEY)
6422 goto not_found;
6423 if (start + len <= found_key.offset)
6424 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006425 if (start > found_key.offset)
6426 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006427 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006428 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006429 em->len = found_key.offset - start;
6430 goto not_found_em;
6431 }
6432
Filipe Manana7ffbb592014-06-09 03:48:05 +01006433 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6434
Yan Zhengd899e052008-10-30 14:25:28 -04006435 if (found_type == BTRFS_FILE_EXTENT_REG ||
6436 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006437 goto insert;
6438 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006439 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006440 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006441 size_t size;
6442 size_t extent_offset;
6443 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006444
Filipe Manana7ffbb592014-06-09 03:48:05 +01006445 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006446 goto out;
Yan689f9342007-10-29 11:41:07 -04006447
Chris Mason514ac8a2014-01-03 21:07:00 -08006448 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006449 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006450 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006451 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006452 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006453 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006454 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006455 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006456 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006457 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006458 if (btrfs_file_extent_compression(leaf, item) !=
6459 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006460 ret = uncompress_inline(path, inode, page,
6461 pg_offset,
6462 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006463 if (ret) {
6464 err = ret;
6465 goto out;
6466 }
Chris Masonc8b97812008-10-29 14:49:59 -04006467 } else {
6468 map = kmap(page);
6469 read_extent_buffer(leaf, map + pg_offset, ptr,
6470 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006471 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6472 memset(map + pg_offset + copy_size, 0,
6473 PAGE_CACHE_SIZE - pg_offset -
6474 copy_size);
6475 }
Chris Masonc8b97812008-10-29 14:49:59 -04006476 kunmap(page);
6477 }
Chris Mason179e29e2007-11-01 11:28:41 -04006478 flush_dcache_page(page);
6479 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006480 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006481 if (!trans) {
6482 kunmap(page);
6483 free_extent_map(em);
6484 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006485
David Sterbab3b4aa72011-04-21 01:20:15 +02006486 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006487 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006488
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006489 if (IS_ERR(trans))
6490 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006491 goto again;
6492 }
Chris Masonc8b97812008-10-29 14:49:59 -04006493 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006494 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006495 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006496 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006497 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006498 }
Chris Masond1310b22008-01-24 16:13:08 -05006499 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006500 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006501 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006502 }
6503not_found:
6504 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006505 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006506 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006507not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006508 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006509 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006510insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006511 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006512 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006513 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006514 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006515 err = -EIO;
6516 goto out;
6517 }
Chris Masond1310b22008-01-24 16:13:08 -05006518
6519 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006520 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006521 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006522 /* it is possible that someone inserted the extent into the tree
6523 * while we had the lock dropped. It is also possible that
6524 * an overlapping map exists in the tree
6525 */
Chris Masona52d9a82007-08-27 16:49:44 -04006526 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006527 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006528
6529 ret = 0;
6530
Chris Mason3b951512008-04-17 11:29:12 -04006531 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006532 if (existing && (existing->start > start ||
6533 existing->start + existing->len <= start)) {
6534 free_extent_map(existing);
6535 existing = NULL;
6536 }
Chris Mason3b951512008-04-17 11:29:12 -04006537 if (!existing) {
6538 existing = lookup_extent_mapping(em_tree, em->start,
6539 em->len);
6540 if (existing) {
6541 err = merge_extent_mapping(em_tree, existing,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006542 em, start);
Chris Mason3b951512008-04-17 11:29:12 -04006543 free_extent_map(existing);
6544 if (err) {
6545 free_extent_map(em);
6546 em = NULL;
6547 }
6548 } else {
6549 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006550 free_extent_map(em);
6551 em = NULL;
6552 }
6553 } else {
6554 free_extent_map(em);
6555 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006556 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006557 }
Chris Masona52d9a82007-08-27 16:49:44 -04006558 }
Chris Mason890871b2009-09-02 16:24:52 -04006559 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006560out:
liubo1abe9b82011-03-24 11:18:59 +00006561
Steven Rostedt4cd8587c2013-11-14 22:57:29 -05006562 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006563
Chris Masonf4219502008-07-22 11:18:09 -04006564 if (path)
6565 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006566 if (trans) {
6567 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006568 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006569 err = ret;
6570 }
Chris Masona52d9a82007-08-27 16:49:44 -04006571 if (err) {
6572 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006573 return ERR_PTR(err);
6574 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006575 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006576 return em;
6577}
6578
Chris Masonec29ed52011-02-23 16:23:20 -05006579struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6580 size_t pg_offset, u64 start, u64 len,
6581 int create)
6582{
6583 struct extent_map *em;
6584 struct extent_map *hole_em = NULL;
6585 u64 range_start = start;
6586 u64 end;
6587 u64 found;
6588 u64 found_end;
6589 int err = 0;
6590
6591 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6592 if (IS_ERR(em))
6593 return em;
6594 if (em) {
6595 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006596 * if our em maps to
6597 * - a hole or
6598 * - a pre-alloc extent,
6599 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006600 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006601 if (em->block_start != EXTENT_MAP_HOLE &&
6602 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006603 return em;
6604 else
6605 hole_em = em;
6606 }
6607
6608 /* check to see if we've wrapped (len == -1 or similar) */
6609 end = start + len;
6610 if (end < start)
6611 end = (u64)-1;
6612 else
6613 end -= 1;
6614
6615 em = NULL;
6616
6617 /* ok, we didn't find anything, lets look for delalloc */
6618 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6619 end, len, EXTENT_DELALLOC, 1);
6620 found_end = range_start + found;
6621 if (found_end < range_start)
6622 found_end = (u64)-1;
6623
6624 /*
6625 * we didn't find anything useful, return
6626 * the original results from get_extent()
6627 */
6628 if (range_start > end || found_end <= start) {
6629 em = hole_em;
6630 hole_em = NULL;
6631 goto out;
6632 }
6633
6634 /* adjust the range_start to make sure it doesn't
6635 * go backwards from the start they passed in
6636 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05306637 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006638 found = found_end - range_start;
6639
6640 if (found > 0) {
6641 u64 hole_start = start;
6642 u64 hole_len = len;
6643
David Sterba172ddd62011-04-21 00:48:27 +02006644 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006645 if (!em) {
6646 err = -ENOMEM;
6647 goto out;
6648 }
6649 /*
6650 * when btrfs_get_extent can't find anything it
6651 * returns one huge hole
6652 *
6653 * make sure what it found really fits our range, and
6654 * adjust to make sure it is based on the start from
6655 * the caller
6656 */
6657 if (hole_em) {
6658 u64 calc_end = extent_map_end(hole_em);
6659
6660 if (calc_end <= start || (hole_em->start > end)) {
6661 free_extent_map(hole_em);
6662 hole_em = NULL;
6663 } else {
6664 hole_start = max(hole_em->start, start);
6665 hole_len = calc_end - hole_start;
6666 }
6667 }
6668 em->bdev = NULL;
6669 if (hole_em && range_start > hole_start) {
6670 /* our hole starts before our delalloc, so we
6671 * have to return just the parts of the hole
6672 * that go until the delalloc starts
6673 */
6674 em->len = min(hole_len,
6675 range_start - hole_start);
6676 em->start = hole_start;
6677 em->orig_start = hole_start;
6678 /*
6679 * don't adjust block start at all,
6680 * it is fixed at EXTENT_MAP_HOLE
6681 */
6682 em->block_start = hole_em->block_start;
6683 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006684 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6685 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006686 } else {
6687 em->start = range_start;
6688 em->len = found;
6689 em->orig_start = range_start;
6690 em->block_start = EXTENT_MAP_DELALLOC;
6691 em->block_len = found;
6692 }
6693 } else if (hole_em) {
6694 return hole_em;
6695 }
6696out:
6697
6698 free_extent_map(hole_em);
6699 if (err) {
6700 free_extent_map(em);
6701 return ERR_PTR(err);
6702 }
6703 return em;
6704}
6705
Josef Bacik4b46fce2010-05-23 11:00:55 -04006706static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6707 u64 start, u64 len)
6708{
6709 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006710 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006711 struct btrfs_key ins;
6712 u64 alloc_hint;
6713 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006714
Josef Bacik4b46fce2010-05-23 11:00:55 -04006715 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006716 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08006717 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006718 if (ret)
6719 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006720
Josef Bacik70c8a912012-10-11 16:54:30 -04006721 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006722 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006723 if (IS_ERR(em)) {
Miao Xiee570fd22014-06-19 10:42:50 +08006724 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006725 return em;
6726 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006727
6728 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6729 ins.offset, ins.offset, 0);
6730 if (ret) {
Miao Xiee570fd22014-06-19 10:42:50 +08006731 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006732 free_extent_map(em);
6733 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006734 }
Josef Bacik00361582013-08-14 14:02:47 -04006735
Josef Bacik4b46fce2010-05-23 11:00:55 -04006736 return em;
6737}
6738
Chris Mason46bfbb52010-05-26 11:04:10 -04006739/*
6740 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6741 * block must be cow'd
6742 */
Josef Bacik00361582013-08-14 14:02:47 -04006743noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006744 u64 *orig_start, u64 *orig_block_len,
6745 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006746{
Josef Bacik00361582013-08-14 14:02:47 -04006747 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006748 struct btrfs_path *path;
6749 int ret;
6750 struct extent_buffer *leaf;
6751 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006752 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006753 struct btrfs_file_extent_item *fi;
6754 struct btrfs_key key;
6755 u64 disk_bytenr;
6756 u64 backref_offset;
6757 u64 extent_end;
6758 u64 num_bytes;
6759 int slot;
6760 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006761 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006762
Chris Mason46bfbb52010-05-26 11:04:10 -04006763 path = btrfs_alloc_path();
6764 if (!path)
6765 return -ENOMEM;
6766
Josef Bacik00361582013-08-14 14:02:47 -04006767 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006768 offset, 0);
6769 if (ret < 0)
6770 goto out;
6771
6772 slot = path->slots[0];
6773 if (ret == 1) {
6774 if (slot == 0) {
6775 /* can't find the item, must cow */
6776 ret = 0;
6777 goto out;
6778 }
6779 slot--;
6780 }
6781 ret = 0;
6782 leaf = path->nodes[0];
6783 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006784 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006785 key.type != BTRFS_EXTENT_DATA_KEY) {
6786 /* not our file or wrong item type, must cow */
6787 goto out;
6788 }
6789
6790 if (key.offset > offset) {
6791 /* Wrong offset, must cow */
6792 goto out;
6793 }
6794
6795 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6796 found_type = btrfs_file_extent_type(leaf, fi);
6797 if (found_type != BTRFS_FILE_EXTENT_REG &&
6798 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6799 /* not a regular extent, must cow */
6800 goto out;
6801 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006802
6803 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6804 goto out;
6805
Miao Xiee77751a2013-12-27 21:11:50 +08006806 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6807 if (extent_end <= offset)
6808 goto out;
6809
Chris Mason46bfbb52010-05-26 11:04:10 -04006810 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006811 if (disk_bytenr == 0)
6812 goto out;
6813
6814 if (btrfs_file_extent_compression(leaf, fi) ||
6815 btrfs_file_extent_encryption(leaf, fi) ||
6816 btrfs_file_extent_other_encoding(leaf, fi))
6817 goto out;
6818
Chris Mason46bfbb52010-05-26 11:04:10 -04006819 backref_offset = btrfs_file_extent_offset(leaf, fi);
6820
Josef Bacik7ee9e442013-06-21 16:37:03 -04006821 if (orig_start) {
6822 *orig_start = key.offset - backref_offset;
6823 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6824 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6825 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006826
Chris Mason46bfbb52010-05-26 11:04:10 -04006827 if (btrfs_extent_readonly(root, disk_bytenr))
6828 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006829
6830 num_bytes = min(offset + *len, extent_end) - offset;
6831 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6832 u64 range_end;
6833
6834 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
6835 ret = test_range_bit(io_tree, offset, range_end,
6836 EXTENT_DELALLOC, 0, NULL);
6837 if (ret) {
6838 ret = -EAGAIN;
6839 goto out;
6840 }
6841 }
6842
Josef Bacik1bda19e2013-10-18 12:10:36 -04006843 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006844
6845 /*
6846 * look for other files referencing this extent, if we
6847 * find any we must cow
6848 */
Josef Bacik00361582013-08-14 14:02:47 -04006849 trans = btrfs_join_transaction(root);
6850 if (IS_ERR(trans)) {
6851 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006852 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006853 }
6854
6855 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6856 key.offset - backref_offset, disk_bytenr);
6857 btrfs_end_transaction(trans, root);
6858 if (ret) {
6859 ret = 0;
6860 goto out;
6861 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006862
6863 /*
6864 * adjust disk_bytenr and num_bytes to cover just the bytes
6865 * in this extent we are about to write. If there
6866 * are any csums in that range we have to cow in order
6867 * to keep the csums correct
6868 */
6869 disk_bytenr += backref_offset;
6870 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006871 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6872 goto out;
6873 /*
6874 * all of the above have passed, it is safe to overwrite this extent
6875 * without cow
6876 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006877 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006878 ret = 1;
6879out:
6880 btrfs_free_path(path);
6881 return ret;
6882}
6883
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006884bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
6885{
6886 struct radix_tree_root *root = &inode->i_mapping->page_tree;
6887 int found = false;
6888 void **pagep = NULL;
6889 struct page *page = NULL;
6890 int start_idx;
6891 int end_idx;
6892
6893 start_idx = start >> PAGE_CACHE_SHIFT;
6894
6895 /*
6896 * end is the last byte in the last page. end == start is legal
6897 */
6898 end_idx = end >> PAGE_CACHE_SHIFT;
6899
6900 rcu_read_lock();
6901
6902 /* Most of the code in this while loop is lifted from
6903 * find_get_page. It's been modified to begin searching from a
6904 * page and return just the first page found in that range. If the
6905 * found idx is less than or equal to the end idx then we know that
6906 * a page exists. If no pages are found or if those pages are
6907 * outside of the range then we're fine (yay!) */
6908 while (page == NULL &&
6909 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
6910 page = radix_tree_deref_slot(pagep);
6911 if (unlikely(!page))
6912 break;
6913
6914 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01006915 if (radix_tree_deref_retry(page)) {
6916 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006917 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01006918 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006919 /*
6920 * Otherwise, shmem/tmpfs must be storing a swap entry
6921 * here as an exceptional entry: so return it without
6922 * attempting to raise page count.
6923 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01006924 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006925 break; /* TODO: Is this relevant for this use case? */
6926 }
6927
Filipe Manana91405152014-06-05 13:22:24 +01006928 if (!page_cache_get_speculative(page)) {
6929 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006930 continue;
Filipe Manana91405152014-06-05 13:22:24 +01006931 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006932
6933 /*
6934 * Has the page moved?
6935 * This is part of the lockless pagecache protocol. See
6936 * include/linux/pagemap.h for details.
6937 */
6938 if (unlikely(page != *pagep)) {
6939 page_cache_release(page);
6940 page = NULL;
6941 }
6942 }
6943
6944 if (page) {
6945 if (page->index <= end_idx)
6946 found = true;
6947 page_cache_release(page);
6948 }
6949
6950 rcu_read_unlock();
6951 return found;
6952}
6953
Josef Bacikeb838e72012-07-31 16:28:48 -04006954static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6955 struct extent_state **cached_state, int writing)
6956{
6957 struct btrfs_ordered_extent *ordered;
6958 int ret = 0;
6959
6960 while (1) {
6961 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6962 0, cached_state);
6963 /*
6964 * We're concerned with the entire range that we're going to be
6965 * doing DIO to, so we need to make sure theres no ordered
6966 * extents in this range.
6967 */
6968 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6969 lockend - lockstart + 1);
6970
6971 /*
6972 * We need to make sure there are no buffered pages in this
6973 * range either, we could have raced between the invalidate in
6974 * generic_file_direct_write and locking the extent. The
6975 * invalidate needs to happen so that reads after a write do not
6976 * get stale data.
6977 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07006978 if (!ordered &&
6979 (!writing ||
6980 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04006981 break;
6982
6983 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6984 cached_state, GFP_NOFS);
6985
6986 if (ordered) {
6987 btrfs_start_ordered_extent(inode, ordered, 1);
6988 btrfs_put_ordered_extent(ordered);
6989 } else {
6990 /* Screw you mmap */
6991 ret = filemap_write_and_wait_range(inode->i_mapping,
6992 lockstart,
6993 lockend);
6994 if (ret)
6995 break;
6996
6997 /*
6998 * If we found a page that couldn't be invalidated just
6999 * fall back to buffered.
7000 */
7001 ret = invalidate_inode_pages2_range(inode->i_mapping,
7002 lockstart >> PAGE_CACHE_SHIFT,
7003 lockend >> PAGE_CACHE_SHIFT);
7004 if (ret)
7005 break;
7006 }
7007
7008 cond_resched();
7009 }
7010
7011 return ret;
7012}
7013
Josef Bacik69ffb542012-09-11 15:40:07 -04007014static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7015 u64 len, u64 orig_start,
7016 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007017 u64 orig_block_len, u64 ram_bytes,
7018 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007019{
7020 struct extent_map_tree *em_tree;
7021 struct extent_map *em;
7022 struct btrfs_root *root = BTRFS_I(inode)->root;
7023 int ret;
7024
7025 em_tree = &BTRFS_I(inode)->extent_tree;
7026 em = alloc_extent_map();
7027 if (!em)
7028 return ERR_PTR(-ENOMEM);
7029
7030 em->start = start;
7031 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007032 em->mod_start = start;
7033 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007034 em->len = len;
7035 em->block_len = block_len;
7036 em->block_start = block_start;
7037 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007038 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007039 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007040 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007041 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7042 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007043 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007044
7045 do {
7046 btrfs_drop_extent_cache(inode, em->start,
7047 em->start + em->len - 1, 0);
7048 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007049 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007050 write_unlock(&em_tree->lock);
7051 } while (ret == -EEXIST);
7052
7053 if (ret) {
7054 free_extent_map(em);
7055 return ERR_PTR(ret);
7056 }
7057
7058 return em;
7059}
7060
7061
Josef Bacik4b46fce2010-05-23 11:00:55 -04007062static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7063 struct buffer_head *bh_result, int create)
7064{
7065 struct extent_map *em;
7066 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007067 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007068 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007069 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007070 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007071 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007072 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007073
Miao Xie172a5042013-02-21 02:48:22 -07007074 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04007075 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007076 else
Josef Bacikc3298612012-08-03 16:49:19 -04007077 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007078
Josef Bacikc3298612012-08-03 16:49:19 -04007079 lockstart = start;
7080 lockend = start + len - 1;
7081
Josef Bacikeb838e72012-07-31 16:28:48 -04007082 /*
7083 * If this errors out it's because we couldn't invalidate pagecache for
7084 * this range and we need to fallback to buffered.
7085 */
7086 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7087 return -ENOTBLK;
7088
Josef Bacik4b46fce2010-05-23 11:00:55 -04007089 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007090 if (IS_ERR(em)) {
7091 ret = PTR_ERR(em);
7092 goto unlock_err;
7093 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007094
7095 /*
7096 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7097 * io. INLINE is special, and we could probably kludge it in here, but
7098 * it's still buffered so for safety lets just fall back to the generic
7099 * buffered path.
7100 *
7101 * For COMPRESSED we _have_ to read the entire extent in so we can
7102 * decompress it, so there will be buffering required no matter what we
7103 * do, so go ahead and fallback to buffered.
7104 *
7105 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7106 * to buffered IO. Don't blame me, this is the price we pay for using
7107 * the generic code.
7108 */
7109 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7110 em->block_start == EXTENT_MAP_INLINE) {
7111 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007112 ret = -ENOTBLK;
7113 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007114 }
7115
7116 /* Just a good old fashioned hole, return */
7117 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7118 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7119 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007120 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007121 }
7122
7123 /*
7124 * We don't allocate a new extent in the following cases
7125 *
7126 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7127 * existing extent.
7128 * 2) The extent is marked as PREALLOC. We're good to go here and can
7129 * just use the extent.
7130 *
7131 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007132 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007133 len = min(len, em->len - (start - em->start));
7134 lockstart = start + len;
7135 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007136 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007137
7138 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7139 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7140 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007141 int type;
7142 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04007143 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007144
7145 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7146 type = BTRFS_ORDERED_PREALLOC;
7147 else
7148 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007149 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007150 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007151
Josef Bacik00361582013-08-14 14:02:47 -04007152 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007153 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04007154 if (type == BTRFS_ORDERED_PREALLOC) {
7155 free_extent_map(em);
7156 em = create_pinned_em(inode, start, len,
7157 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05007158 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007159 orig_block_len,
7160 ram_bytes, type);
Filipe Manana555e1282014-07-07 12:35:21 +01007161 if (IS_ERR(em)) {
7162 ret = PTR_ERR(em);
Josef Bacik69ffb542012-09-11 15:40:07 -04007163 goto unlock_err;
Filipe Manana555e1282014-07-07 12:35:21 +01007164 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007165 }
7166
Chris Mason46bfbb52010-05-26 11:04:10 -04007167 ret = btrfs_add_ordered_extent_dio(inode, start,
7168 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04007169 if (ret) {
7170 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007171 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007172 }
7173 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007174 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007175 }
Josef Bacik00361582013-08-14 14:02:47 -04007176
Chris Mason46bfbb52010-05-26 11:04:10 -04007177 /*
7178 * this will cow the extent, reset the len in case we changed
7179 * it above
7180 */
7181 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007182 free_extent_map(em);
7183 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007184 if (IS_ERR(em)) {
7185 ret = PTR_ERR(em);
7186 goto unlock_err;
7187 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007188 len = min(len, em->len - (start - em->start));
7189unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007190 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7191 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007192 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007193 bh_result->b_bdev = em->bdev;
7194 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007195 if (create) {
7196 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7197 set_buffer_new(bh_result);
7198
7199 /*
7200 * Need to update the i_size under the extent lock so buffered
7201 * readers will get the updated i_size when we unlock.
7202 */
7203 if (start + len > i_size_read(inode))
7204 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007205
Miao Xie172a5042013-02-21 02:48:22 -07007206 spin_lock(&BTRFS_I(inode)->lock);
7207 BTRFS_I(inode)->outstanding_extents++;
7208 spin_unlock(&BTRFS_I(inode)->lock);
7209
Miao Xie09348562013-02-07 10:12:07 +00007210 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7211 lockstart + len - 1, EXTENT_DELALLOC, NULL,
7212 &cached_state, GFP_NOFS);
7213 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04007214 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007215
Josef Bacikeb838e72012-07-31 16:28:48 -04007216 /*
7217 * In the case of write we need to clear and unlock the entire range,
7218 * in the case of read we need to unlock only the end area that we
7219 * aren't using if there is any left over space.
7220 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007221 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007222 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7223 lockend, unlock_bits, 1, 0,
7224 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007225 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007226 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007227 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007228
Josef Bacik4b46fce2010-05-23 11:00:55 -04007229 free_extent_map(em);
7230
7231 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007232
7233unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007234 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7235 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
7236 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007237}
7238
Josef Bacik4b46fce2010-05-23 11:00:55 -04007239static void btrfs_endio_direct_read(struct bio *bio, int err)
7240{
Miao Xiee65e1532010-11-22 03:04:43 +00007241 struct btrfs_dio_private *dip = bio->bi_private;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007242 struct bio_vec *bvec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007243 struct inode *inode = dip->inode;
7244 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04007245 struct bio *dio_bio;
Miao Xiefacc8a222013-07-25 19:22:34 +08007246 u32 *csums = (u32 *)dip->csum;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007247 u64 start;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007248 int i;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007249
7250 start = dip->logical_offset;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007251 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007252 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
7253 struct page *page = bvec->bv_page;
7254 char *kaddr;
7255 u32 csum = ~(u32)0;
7256 unsigned long flags;
7257
7258 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08007259 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00007260 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007261 csum, bvec->bv_len);
7262 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08007263 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007264 local_irq_restore(flags);
7265
7266 flush_dcache_page(bvec->bv_page);
Kent Overstreet2c30c712013-11-07 12:20:26 -08007267 if (csum != csums[i]) {
Miao Xiefacc8a222013-07-25 19:22:34 +08007268 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007269 btrfs_ino(inode), start, csum,
Kent Overstreet2c30c712013-11-07 12:20:26 -08007270 csums[i]);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007271 err = -EIO;
7272 }
7273 }
7274
7275 start += bvec->bv_len;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007276 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007277
7278 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007279 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007280 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007281
Josef Bacik4b46fce2010-05-23 11:00:55 -04007282 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007283
7284 /* If we had a csum failure make sure to clear the uptodate flag */
7285 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007286 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7287 dio_end_io(dio_bio, err);
7288 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007289}
7290
7291static void btrfs_endio_direct_write(struct bio *bio, int err)
7292{
7293 struct btrfs_dio_private *dip = bio->bi_private;
7294 struct inode *inode = dip->inode;
7295 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007296 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007297 u64 ordered_offset = dip->logical_offset;
7298 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04007299 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007300 int ret;
7301
7302 if (err)
7303 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05007304again:
7305 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7306 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04007307 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007308 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05007309 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007310
Liu Bo9e0af232014-08-15 23:36:53 +08007311 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7312 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08007313 btrfs_queue_work(root->fs_info->endio_write_workers,
7314 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05007315out_test:
7316 /*
7317 * our bio might span multiple ordered extents. If we haven't
7318 * completed the accounting for the whole dio, go back and try again
7319 */
7320 if (ordered_offset < dip->logical_offset + dip->bytes) {
7321 ordered_bytes = dip->logical_offset + dip->bytes -
7322 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04007323 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05007324 goto again;
7325 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007326out_done:
Chris Mason9be33952013-05-17 18:30:14 -04007327 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007328
Josef Bacik4b46fce2010-05-23 11:00:55 -04007329 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007330
7331 /* If we had an error make sure to clear the uptodate flag */
7332 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04007333 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7334 dio_end_io(dio_bio, err);
7335 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007336}
7337
Chris Masoneaf25d92010-05-25 09:48:28 -04007338static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7339 struct bio *bio, int mirror_num,
7340 unsigned long bio_flags, u64 offset)
7341{
7342 int ret;
7343 struct btrfs_root *root = BTRFS_I(inode)->root;
7344 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007345 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007346 return 0;
7347}
7348
Miao Xiee65e1532010-11-22 03:04:43 +00007349static void btrfs_end_dio_bio(struct bio *bio, int err)
7350{
7351 struct btrfs_dio_private *dip = bio->bi_private;
7352
7353 if (err) {
Frank Holtonefe120a2013-12-20 11:37:06 -05007354 btrfs_err(BTRFS_I(dip->inode)->root->fs_info,
7355 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007356 btrfs_ino(dip->inode), bio->bi_rw,
Kent Overstreet4f024f32013-10-11 15:44:27 -07007357 (unsigned long long)bio->bi_iter.bi_sector,
7358 bio->bi_iter.bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00007359 dip->errors = 1;
7360
7361 /*
7362 * before atomic variable goto zero, we must make sure
7363 * dip->errors is perceived to be set.
7364 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007365 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007366 }
7367
7368 /* if there are more bios still pending for this dio, just exit */
7369 if (!atomic_dec_and_test(&dip->pending_bios))
7370 goto out;
7371
Chris Mason9be33952013-05-17 18:30:14 -04007372 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007373 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007374 } else {
7375 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00007376 bio_endio(dip->orig_bio, 0);
7377 }
7378out:
7379 bio_put(bio);
7380}
7381
7382static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7383 u64 first_sector, gfp_t gfp_flags)
7384{
7385 int nr_vecs = bio_get_nr_vecs(bdev);
7386 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7387}
7388
7389static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7390 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007391 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007392{
Miao Xiefacc8a222013-07-25 19:22:34 +08007393 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00007394 int write = rw & REQ_WRITE;
7395 struct btrfs_root *root = BTRFS_I(inode)->root;
7396 int ret;
7397
Josef Bacikb812ce22012-11-16 13:56:32 -05007398 if (async_submit)
7399 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7400
Miao Xiee65e1532010-11-22 03:04:43 +00007401 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007402
7403 if (!write) {
7404 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7405 if (ret)
7406 goto err;
7407 }
Miao Xiee65e1532010-11-22 03:04:43 +00007408
Josef Bacik1ae39932011-04-06 14:41:34 -04007409 if (skip_sum)
7410 goto map;
7411
7412 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007413 ret = btrfs_wq_submit_bio(root->fs_info,
7414 inode, rw, bio, 0, 0,
7415 file_offset,
7416 __btrfs_submit_bio_start_direct_io,
7417 __btrfs_submit_bio_done);
7418 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007419 } else if (write) {
7420 /*
7421 * If we aren't doing async submit, calculate the csum of the
7422 * bio now.
7423 */
7424 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7425 if (ret)
7426 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007427 } else if (!skip_sum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08007428 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
7429 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007430 if (ret)
7431 goto err;
7432 }
Miao Xiee65e1532010-11-22 03:04:43 +00007433
Josef Bacik1ae39932011-04-06 14:41:34 -04007434map:
7435 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007436err:
7437 bio_put(bio);
7438 return ret;
7439}
7440
7441static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7442 int skip_sum)
7443{
7444 struct inode *inode = dip->inode;
7445 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007446 struct bio *bio;
7447 struct bio *orig_bio = dip->orig_bio;
7448 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007449 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00007450 u64 file_offset = dip->logical_offset;
7451 u64 submit_len = 0;
7452 u64 map_length;
7453 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007454 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007455 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007456
Kent Overstreet4f024f32013-10-11 15:44:27 -07007457 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007458 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007459 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08007460 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00007461 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08007462
Kent Overstreet4f024f32013-10-11 15:44:27 -07007463 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04007464 bio = orig_bio;
7465 goto submit;
7466 }
7467
David Woodhouse53b381b2013-01-29 18:40:14 -05007468 /* async crcs make it difficult to collect full stripe writes. */
7469 if (btrfs_get_alloc_profile(root, 1) &
7470 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7471 async_submit = 0;
7472 else
7473 async_submit = 1;
7474
Josef Bacik02f57c72011-04-06 14:25:44 -04007475 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7476 if (!bio)
7477 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08007478
Josef Bacik02f57c72011-04-06 14:25:44 -04007479 bio->bi_private = dip;
7480 bio->bi_end_io = btrfs_end_dio_bio;
7481 atomic_inc(&dip->pending_bios);
7482
Miao Xiee65e1532010-11-22 03:04:43 +00007483 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7484 if (unlikely(map_length < submit_len + bvec->bv_len ||
7485 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7486 bvec->bv_offset) < bvec->bv_len)) {
7487 /*
7488 * inc the count before we submit the bio so
7489 * we know the end IO handler won't happen before
7490 * we inc the count. Otherwise, the dip might get freed
7491 * before we're done setting it up
7492 */
7493 atomic_inc(&dip->pending_bios);
7494 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7495 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007496 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007497 if (ret) {
7498 bio_put(bio);
7499 atomic_dec(&dip->pending_bios);
7500 goto out_err;
7501 }
7502
Miao Xiee65e1532010-11-22 03:04:43 +00007503 start_sector += submit_len >> 9;
7504 file_offset += submit_len;
7505
7506 submit_len = 0;
7507 nr_pages = 0;
7508
7509 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7510 start_sector, GFP_NOFS);
7511 if (!bio)
7512 goto out_err;
7513 bio->bi_private = dip;
7514 bio->bi_end_io = btrfs_end_dio_bio;
7515
Kent Overstreet4f024f32013-10-11 15:44:27 -07007516 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007517 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007518 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007519 &map_length, NULL, 0);
7520 if (ret) {
7521 bio_put(bio);
7522 goto out_err;
7523 }
7524 } else {
7525 submit_len += bvec->bv_len;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307526 nr_pages++;
Miao Xiee65e1532010-11-22 03:04:43 +00007527 bvec++;
7528 }
7529 }
7530
Josef Bacik02f57c72011-04-06 14:25:44 -04007531submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007532 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007533 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007534 if (!ret)
7535 return 0;
7536
7537 bio_put(bio);
7538out_err:
7539 dip->errors = 1;
7540 /*
7541 * before atomic variable goto zero, we must
7542 * make sure dip->errors is perceived to be set.
7543 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007544 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00007545 if (atomic_dec_and_test(&dip->pending_bios))
7546 bio_io_error(dip->orig_bio);
7547
7548 /* bio_end_io() will handle error, so we needn't return it */
7549 return 0;
7550}
7551
Chris Mason9be33952013-05-17 18:30:14 -04007552static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7553 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007554{
7555 struct btrfs_root *root = BTRFS_I(inode)->root;
7556 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007557 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007558 int skip_sum;
Miao Xiefacc8a222013-07-25 19:22:34 +08007559 int sum_len;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007560 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007561 int ret = 0;
Miao Xiefacc8a222013-07-25 19:22:34 +08007562 u16 csum_size;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007563
7564 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7565
Chris Mason9be33952013-05-17 18:30:14 -04007566 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007567 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007568 ret = -ENOMEM;
7569 goto free_ordered;
7570 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007571
Miao Xiefacc8a222013-07-25 19:22:34 +08007572 if (!skip_sum && !write) {
7573 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
Kent Overstreet4f024f32013-10-11 15:44:27 -07007574 sum_len = dio_bio->bi_iter.bi_size >>
7575 inode->i_sb->s_blocksize_bits;
Miao Xiefacc8a222013-07-25 19:22:34 +08007576 sum_len *= csum_size;
7577 } else {
7578 sum_len = 0;
7579 }
7580
7581 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007582 if (!dip) {
7583 ret = -ENOMEM;
7584 goto free_io_bio;
7585 }
7586
7587 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007588 dip->inode = inode;
7589 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007590 dip->bytes = dio_bio->bi_iter.bi_size;
7591 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04007592 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007593 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007594 dip->orig_bio = io_bio;
7595 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007596 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007597
7598 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007599 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007600 else
Chris Mason9be33952013-05-17 18:30:14 -04007601 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007602
Miao Xiee65e1532010-11-22 03:04:43 +00007603 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7604 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007605 return;
Chris Mason9be33952013-05-17 18:30:14 -04007606
7607free_io_bio:
7608 bio_put(io_bio);
7609
Josef Bacik4b46fce2010-05-23 11:00:55 -04007610free_ordered:
7611 /*
7612 * If this is a write, we need to clean up the reserved space and kill
7613 * the ordered extent.
7614 */
7615 if (write) {
7616 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007617 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007618 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7619 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7620 btrfs_free_reserved_extent(root, ordered->start,
Miao Xiee570fd22014-06-19 10:42:50 +08007621 ordered->disk_len, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007622 btrfs_put_ordered_extent(ordered);
7623 btrfs_put_ordered_extent(ordered);
7624 }
Chris Mason9be33952013-05-17 18:30:14 -04007625 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007626}
7627
Chris Mason5a5f79b2010-05-26 21:33:37 -04007628static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04007629 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04007630{
7631 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007632 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007633 unsigned blocksize_mask = root->sectorsize - 1;
7634 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007635
7636 if (offset & blocksize_mask)
7637 goto out;
7638
Al Viro28060d52014-03-22 05:15:17 -04007639 if (iov_iter_alignment(iter) & blocksize_mask)
7640 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007641
Al Viro28060d52014-03-22 05:15:17 -04007642 /* If this is a write we don't need to check anymore */
7643 if (rw & WRITE)
7644 return 0;
7645 /*
7646 * Check to make sure we don't have duplicate iov_base's in this
7647 * iovec, if so return EINVAL, otherwise we'll get csum errors
7648 * when reading back.
7649 */
7650 for (seg = 0; seg < iter->nr_segs; seg++) {
7651 for (i = seg + 1; i < iter->nr_segs; i++) {
7652 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00007653 goto out;
7654 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007655 }
7656 retval = 0;
7657out:
7658 return retval;
7659}
Josef Bacikeb838e72012-07-31 16:28:48 -04007660
Chris Mason16432982008-04-10 10:23:21 -04007661static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05007662 struct iov_iter *iter, loff_t offset)
Chris Mason16432982008-04-10 10:23:21 -04007663{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007664 struct file *file = iocb->ki_filp;
7665 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007666 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007667 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007668 bool wakeup = true;
7669 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007670 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007671
Al Viro28060d52014-03-22 05:15:17 -04007672 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007673 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007674
Miao Xie38851cc2013-02-08 07:04:11 +00007675 atomic_inc(&inode->i_dio_count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01007676 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00007677
Josef Bacik0e267c42013-07-02 10:38:02 -04007678 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08007679 * The generic stuff only does filemap_write_and_wait_range, which
7680 * isn't enough if we've written compressed pages to this area, so
7681 * we need to flush the dirty pages again to make absolutely sure
7682 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04007683 */
Al Viroa6cbcd42014-03-04 22:38:00 -05007684 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08007685 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7686 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08007687 filemap_fdatawrite_range(inode->i_mapping, offset,
7688 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04007689
Miao Xie09348562013-02-07 10:12:07 +00007690 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007691 /*
7692 * If the write DIO is beyond the EOF, we need update
7693 * the isize, but it is protected by i_mutex. So we can
7694 * not unlock the i_mutex at this case.
7695 */
7696 if (offset + count <= inode->i_size) {
7697 mutex_unlock(&inode->i_mutex);
7698 relock = true;
7699 }
Miao Xie09348562013-02-07 10:12:07 +00007700 ret = btrfs_delalloc_reserve_space(inode, count);
7701 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007702 goto out;
7703 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7704 &BTRFS_I(inode)->runtime_flags))) {
7705 inode_dio_done(inode);
7706 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7707 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007708 }
7709
7710 ret = __blockdev_direct_IO(rw, iocb, inode,
7711 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Al Viro31b14032014-03-05 01:33:16 -05007712 iter, offset, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007713 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007714 if (rw & WRITE) {
7715 if (ret < 0 && ret != -EIOCBQUEUED)
7716 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007717 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007718 btrfs_delalloc_release_space(inode,
7719 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007720 else
7721 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007722 }
Miao Xie38851cc2013-02-08 07:04:11 +00007723out:
Miao Xie2e60a512013-02-08 07:01:08 +00007724 if (wakeup)
7725 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007726 if (relock)
7727 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007728
7729 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007730}
7731
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007732#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7733
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007734static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7735 __u64 start, __u64 len)
7736{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007737 int ret;
7738
7739 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7740 if (ret)
7741 return ret;
7742
Chris Masonec29ed52011-02-23 16:23:20 -05007743 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007744}
7745
Chris Mason9ebefb182007-06-15 13:50:00 -04007746int btrfs_readpage(struct file *file, struct page *page)
7747{
Chris Masond1310b22008-01-24 16:13:08 -05007748 struct extent_io_tree *tree;
7749 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007750 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007751}
Chris Mason1832a6d2007-12-21 16:27:21 -05007752
Chris Mason39279cc2007-06-12 06:35:45 -04007753static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7754{
Chris Masond1310b22008-01-24 16:13:08 -05007755 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007756
7757
7758 if (current->flags & PF_MEMALLOC) {
7759 redirty_page_for_writepage(wbc, page);
7760 unlock_page(page);
7761 return 0;
7762 }
Chris Masond1310b22008-01-24 16:13:08 -05007763 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007764 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7765}
Chris Mason39279cc2007-06-12 06:35:45 -04007766
Eric Sandeen48a3b632013-04-25 20:41:01 +00007767static int btrfs_writepages(struct address_space *mapping,
7768 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007769{
Chris Masond1310b22008-01-24 16:13:08 -05007770 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007771
Chris Masond1310b22008-01-24 16:13:08 -05007772 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007773 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7774}
7775
Chris Mason3ab2fb52007-11-08 10:59:22 -05007776static int
7777btrfs_readpages(struct file *file, struct address_space *mapping,
7778 struct list_head *pages, unsigned nr_pages)
7779{
Chris Masond1310b22008-01-24 16:13:08 -05007780 struct extent_io_tree *tree;
7781 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007782 return extent_readpages(tree, mapping, pages, nr_pages,
7783 btrfs_get_extent);
7784}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007785static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007786{
Chris Masond1310b22008-01-24 16:13:08 -05007787 struct extent_io_tree *tree;
7788 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007789 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007790
Chris Masond1310b22008-01-24 16:13:08 -05007791 tree = &BTRFS_I(page->mapping->host)->io_tree;
7792 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007793 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007794 if (ret == 1) {
7795 ClearPagePrivate(page);
7796 set_page_private(page, 0);
7797 page_cache_release(page);
7798 }
7799 return ret;
7800}
Chris Mason39279cc2007-06-12 06:35:45 -04007801
Chris Masone6dcd2d2008-07-17 12:53:50 -04007802static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7803{
Chris Mason98509cfc2008-09-11 15:51:43 -04007804 if (PageWriteback(page) || PageDirty(page))
7805 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007806 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007807}
7808
Lukas Czernerd47992f2013-05-21 23:17:23 -04007809static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7810 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007811{
Josef Bacik5fd02042012-05-02 14:00:54 -04007812 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007813 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007814 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007815 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007816 u64 page_start = page_offset(page);
7817 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007818 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04007819
Chris Mason8b62b722009-09-02 16:53:46 -04007820 /*
7821 * we have the page locked, so new writeback can't start,
7822 * and the dirty bit won't be cleared while we are here.
7823 *
7824 * Wait for IO on this page so that we can safely clear
7825 * the PagePrivate2 bit and do ordered accounting
7826 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007827 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007828
Josef Bacik5fd02042012-05-02 14:00:54 -04007829 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007830 if (offset) {
7831 btrfs_releasepage(page, GFP_NOFS);
7832 return;
7833 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007834
7835 if (!inode_evicting)
7836 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
7837 ordered = btrfs_lookup_ordered_extent(inode, page_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007838 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007839 /*
7840 * IO on this page will never be started, so we need
7841 * to account for any ordered extents now
7842 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007843 if (!inode_evicting)
7844 clear_extent_bit(tree, page_start, page_end,
7845 EXTENT_DIRTY | EXTENT_DELALLOC |
7846 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7847 EXTENT_DEFRAG, 1, 0, &cached_state,
7848 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007849 /*
7850 * whoever cleared the private bit is responsible
7851 * for the finish_ordered_io
7852 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04007853 if (TestClearPagePrivate2(page)) {
7854 struct btrfs_ordered_inode_tree *tree;
7855 u64 new_len;
7856
7857 tree = &BTRFS_I(inode)->ordered_tree;
7858
7859 spin_lock_irq(&tree->lock);
7860 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
7861 new_len = page_start - ordered->file_offset;
7862 if (new_len < ordered->truncated_len)
7863 ordered->truncated_len = new_len;
7864 spin_unlock_irq(&tree->lock);
7865
7866 if (btrfs_dec_test_ordered_pending(inode, &ordered,
7867 page_start,
7868 PAGE_CACHE_SIZE, 1))
7869 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007870 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007871 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007872 if (!inode_evicting) {
7873 cached_state = NULL;
7874 lock_extent_bits(tree, page_start, page_end, 0,
7875 &cached_state);
7876 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007877 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007878
7879 if (!inode_evicting) {
7880 clear_extent_bit(tree, page_start, page_end,
7881 EXTENT_LOCKED | EXTENT_DIRTY |
7882 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
7883 EXTENT_DEFRAG, 1, 1,
7884 &cached_state, GFP_NOFS);
7885
7886 __btrfs_releasepage(page, GFP_NOFS);
7887 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007888
Chris Mason4a096752008-07-21 10:29:44 -04007889 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007890 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007891 ClearPagePrivate(page);
7892 set_page_private(page, 0);
7893 page_cache_release(page);
7894 }
Chris Mason39279cc2007-06-12 06:35:45 -04007895}
7896
Chris Mason9ebefb182007-06-15 13:50:00 -04007897/*
7898 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7899 * called from a page fault handler when a page is first dirtied. Hence we must
7900 * be careful to check for EOF conditions here. We set the page up correctly
7901 * for a written page which means we get ENOSPC checking when writing into
7902 * holes and correct delalloc and unwritten extent mapping on filesystems that
7903 * support these features.
7904 *
7905 * We are not allowed to take the i_mutex here so we have to play games to
7906 * protect against truncate races as the page could now be beyond EOF. Because
7907 * vmtruncate() writes the inode size before removing pages, once we have the
7908 * page lock we can determine safely if the page is beyond EOF. If it is not
7909 * beyond EOF, then the page is guaranteed safe against truncation until we
7910 * unlock the page.
7911 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007912int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007913{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007914 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007915 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007916 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007917 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7918 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007919 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007920 char *kaddr;
7921 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007922 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007923 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007924 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007925 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007926 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007927
Jan Karab2b5ef52012-06-12 16:20:45 +02007928 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007929 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007930 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007931 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007932 reserved = 1;
7933 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007934 if (ret) {
7935 if (ret == -ENOMEM)
7936 ret = VM_FAULT_OOM;
7937 else /* -ENOSPC, -EIO, etc */
7938 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007939 if (reserved)
7940 goto out;
7941 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007942 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007943
Nick Piggin56a76f82009-03-31 15:23:23 -07007944 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007945again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007946 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007947 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007948 page_start = page_offset(page);
7949 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007950
Chris Mason9ebefb182007-06-15 13:50:00 -04007951 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007952 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007953 /* page got truncated out from underneath us */
7954 goto out_unlock;
7955 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007956 wait_on_page_writeback(page);
7957
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007958 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007959 set_page_extent_mapped(page);
7960
Chris Masoneb84ae02008-07-17 13:53:27 -04007961 /*
7962 * we can't set the delalloc bits if there are pending ordered
7963 * extents. Drop our locks and wait for them to finish
7964 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007965 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7966 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007967 unlock_extent_cached(io_tree, page_start, page_end,
7968 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007969 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007970 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007971 btrfs_put_ordered_extent(ordered);
7972 goto again;
7973 }
7974
Josef Bacikfbf19082009-10-01 17:10:23 -04007975 /*
7976 * XXX - page_mkwrite gets called every time the page is dirtied, even
7977 * if it was already dirty, so for space accounting reasons we need to
7978 * clear any delalloc bits for the range we are fixing to save. There
7979 * is probably a better way to do this, but for now keep consistent with
7980 * prepare_pages in the normal write path.
7981 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007982 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007983 EXTENT_DIRTY | EXTENT_DELALLOC |
7984 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007985 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007986
Josef Bacik2ac55d42010-02-03 19:33:23 +00007987 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7988 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007989 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007990 unlock_extent_cached(io_tree, page_start, page_end,
7991 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007992 ret = VM_FAULT_SIGBUS;
7993 goto out_unlock;
7994 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007995 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007996
7997 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007998 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007999 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04008000 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04008001 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008002
Chris Masone6dcd2d2008-07-17 12:53:50 -04008003 if (zero_start != PAGE_CACHE_SIZE) {
8004 kaddr = kmap(page);
8005 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8006 flush_dcache_page(page);
8007 kunmap(page);
8008 }
Chris Mason247e7432008-07-17 12:53:51 -04008009 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008010 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008011 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008012
Chris Mason257c62e2009-10-13 13:21:08 -04008013 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8014 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008015 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008016
Josef Bacik2ac55d42010-02-03 19:33:23 +00008017 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04008018
8019out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02008020 if (!ret) {
8021 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04008022 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02008023 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008024 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008025out:
Josef Bacikec39e182012-01-12 19:10:12 -05008026 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05008027out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008028 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04008029 return ret;
8030}
8031
Josef Bacika41ad392011-01-31 15:30:16 -05008032static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008033{
8034 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008035 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04008036 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05008037 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008038 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04008039 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04008040 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008041
Josef Bacik0ef8b722013-10-25 16:13:35 -04008042 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8043 (u64)-1);
8044 if (ret)
8045 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008046
Josef Bacikfcb80c22011-05-03 10:40:22 -04008047 /*
8048 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8049 * 3 things going on here
8050 *
8051 * 1) We need to reserve space for our orphan item and the space to
8052 * delete our orphan item. Lord knows we don't want to have a dangling
8053 * orphan item because we didn't reserve space to remove it.
8054 *
8055 * 2) We need to reserve space to update our inode.
8056 *
8057 * 3) We need to have something to cache all the space that is going to
8058 * be free'd up by the truncate operation, but also have some slack
8059 * space reserved in case it uses space during the truncate (thank you
8060 * very much snapshotting).
8061 *
8062 * And we need these to all be seperate. The fact is we can use alot of
8063 * space doing the truncate, and we have no earthly idea how much space
8064 * we will use, so we need the truncate reservation to be seperate so it
8065 * doesn't end up using space reserved for updating the inode or
8066 * removing the orphan item. We also need to be able to stop the
8067 * transaction and start a new one, which means we need to be able to
8068 * update the inode several times, and we have no idea of knowing how
8069 * many times that will be, so we can't just reserve 1 item for the
8070 * entirety of the opration, so that has to be done seperately as well.
8071 * Then there is the orphan item, which does indeed need to be held on
8072 * to for the whole operation, and we need nobody to touch this reserved
8073 * space except the orphan code.
8074 *
8075 * So that leaves us with
8076 *
8077 * 1) root->orphan_block_rsv - for the orphan deletion.
8078 * 2) rsv - for the truncate reservation, which we will steal from the
8079 * transaction reservation.
8080 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8081 * updating the inode.
8082 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06008083 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008084 if (!rsv)
8085 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008086 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008087 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008088
Josef Bacik907cbce2011-08-08 13:46:15 -04008089 /*
Josef Bacik07127182011-08-19 10:29:59 -04008090 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008091 * 1 for updating the inode.
8092 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008093 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008094 if (IS_ERR(trans)) {
8095 err = PTR_ERR(trans);
8096 goto out;
8097 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008098
Josef Bacik907cbce2011-08-08 13:46:15 -04008099 /* Migrate the slack space for the truncate to our reserve */
8100 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8101 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008102 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008103
Chris Mason5a3f23d2009-03-31 13:27:11 -04008104 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008105 * So if we truncate and then write and fsync we normally would just
8106 * write the extents that changed, which is a problem if we need to
8107 * first truncate that entire inode. So set this flag so we write out
8108 * all of the extents in the inode to the sync log so we're completely
8109 * safe.
8110 */
8111 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008112 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008113
Yan, Zheng80825102009-11-12 09:35:36 +00008114 while (1) {
8115 ret = btrfs_truncate_inode_items(trans, root, inode,
8116 inode->i_size,
8117 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008118 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05008119 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008120 break;
Josef Bacik3893e332011-01-31 16:03:11 -05008121 }
Chris Mason39279cc2007-06-12 06:35:45 -04008122
Josef Bacikfcb80c22011-05-03 10:40:22 -04008123 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008124 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008125 if (ret) {
8126 err = ret;
8127 break;
8128 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04008129
Yan, Zheng80825102009-11-12 09:35:36 +00008130 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008131 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008132
8133 trans = btrfs_start_transaction(root, 2);
8134 if (IS_ERR(trans)) {
8135 ret = err = PTR_ERR(trans);
8136 trans = NULL;
8137 break;
8138 }
8139
8140 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8141 rsv, min_size);
8142 BUG_ON(ret); /* shouldn't happen */
8143 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008144 }
8145
8146 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04008147 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008148 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05008149 if (ret)
8150 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00008151 }
8152
Chris Mason917c16b2011-11-08 14:49:59 -05008153 if (trans) {
8154 trans->block_rsv = &root->fs_info->trans_block_rsv;
8155 ret = btrfs_update_inode(trans, root, inode);
8156 if (ret && !err)
8157 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04008158
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008159 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00008160 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05008161 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008162
8163out:
8164 btrfs_free_block_rsv(root, rsv);
8165
Josef Bacik3893e332011-01-31 16:03:11 -05008166 if (ret && !err)
8167 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05008168
Josef Bacik3893e332011-01-31 16:03:11 -05008169 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008170}
8171
Sven Wegener3b963622008-06-09 21:57:42 -04008172/*
Chris Masond352ac62008-09-29 15:18:18 -04008173 * create a new subvolume directory/inode (helper for the ioctl).
8174 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008175int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008176 struct btrfs_root *new_root,
8177 struct btrfs_root *parent_root,
8178 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008179{
Chris Mason39279cc2007-06-12 06:35:45 -04008180 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008181 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008182 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008183
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008184 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8185 new_dirid, new_dirid,
8186 S_IFDIR | (~current_umask() & S_IRWXUGO),
8187 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008188 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008189 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008190 inode->i_op = &btrfs_dir_inode_operations;
8191 inode->i_fop = &btrfs_dir_file_operations;
8192
Miklos Szeredibfe86842011-10-28 14:13:29 +02008193 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04008194 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008195 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008196
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008197 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8198 if (err)
8199 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008200 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008201 new_root->root_key.objectid, err);
8202
Yan, Zheng76dda932009-09-21 16:00:26 -04008203 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008204
Yan, Zheng76dda932009-09-21 16:00:26 -04008205 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008206 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008207}
8208
Chris Mason39279cc2007-06-12 06:35:45 -04008209struct inode *btrfs_alloc_inode(struct super_block *sb)
8210{
8211 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008212 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008213
8214 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8215 if (!ei)
8216 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008217
8218 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008219 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008220 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008221 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008222 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008223 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008224 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008225 ei->disk_i_size = 0;
8226 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008227 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008228 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008229 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008230 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008231 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008232
Josef Bacik9e0baf62011-07-15 15:16:44 +00008233 spin_lock_init(&ei->lock);
8234 ei->outstanding_extents = 0;
8235 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008236
Josef Bacik72ac3c02012-05-23 14:13:11 -04008237 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08008238 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008239
Miao Xie16cdcec2011-04-22 18:12:22 +08008240 ei->delayed_node = NULL;
8241
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008242 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008243 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02008244 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8245 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04008246 ei->io_tree.track_uptodate = 1;
8247 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05008248 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008249 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05008250 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008251 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008252 INIT_LIST_HEAD(&ei->delalloc_inodes);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008253 RB_CLEAR_NODE(&ei->rb_node);
8254
8255 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008256}
8257
Josef Bacikaaedb552013-10-11 14:44:09 -04008258#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8259void btrfs_test_destroy_inode(struct inode *inode)
8260{
8261 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8262 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8263}
8264#endif
8265
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008266static void btrfs_i_callback(struct rcu_head *head)
8267{
8268 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008269 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8270}
8271
Chris Mason39279cc2007-06-12 06:35:45 -04008272void btrfs_destroy_inode(struct inode *inode)
8273{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008274 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008275 struct btrfs_root *root = BTRFS_I(inode)->root;
8276
Al Virob3d9b7a2012-06-09 13:51:19 -04008277 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008278 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008279 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8280 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008281 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8282 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008283 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008284
Chris Mason5a3f23d2009-03-31 13:27:11 -04008285 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008286 * This can happen where we create an inode, but somebody else also
8287 * created the same inode and we need to destroy the one we already
8288 * created.
8289 */
8290 if (!root)
8291 goto free;
8292
Josef Bacik8a35d952012-05-23 14:26:42 -04008293 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8294 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008295 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008296 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04008297 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04008298 }
Josef Bacik7b128762008-07-24 12:17:14 -04008299
Chris Masond3977122009-01-05 21:25:51 -05008300 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008301 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8302 if (!ordered)
8303 break;
8304 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008305 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008306 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008307 btrfs_remove_ordered_extent(inode, ordered);
8308 btrfs_put_ordered_extent(ordered);
8309 btrfs_put_ordered_extent(ordered);
8310 }
8311 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008312 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008313 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05008314free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008315 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04008316}
8317
Al Viro45321ac2010-06-07 13:43:19 -04008318int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008319{
8320 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008321
Naohiro Aota6379ef92013-06-06 09:56:34 +00008322 if (root == NULL)
8323 return 1;
8324
Liu Bofa6ac872013-02-20 14:10:23 +00008325 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008326 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008327 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008328 else
Al Viro45321ac2010-06-07 13:43:19 -04008329 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008330}
8331
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008332static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008333{
8334 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8335
8336 inode_init_once(&ei->vfs_inode);
8337}
8338
8339void btrfs_destroy_cachep(void)
8340{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008341 /*
8342 * Make sure all delayed rcu free inodes are flushed before we
8343 * destroy cache.
8344 */
8345 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04008346 if (btrfs_inode_cachep)
8347 kmem_cache_destroy(btrfs_inode_cachep);
8348 if (btrfs_trans_handle_cachep)
8349 kmem_cache_destroy(btrfs_trans_handle_cachep);
8350 if (btrfs_transaction_cachep)
8351 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008352 if (btrfs_path_cachep)
8353 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05008354 if (btrfs_free_space_cachep)
8355 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008356 if (btrfs_delalloc_work_cachep)
8357 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008358}
8359
8360int btrfs_init_cachep(void)
8361{
David Sterba837e1972012-09-07 03:00:48 -06008362 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008363 sizeof(struct btrfs_inode), 0,
8364 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008365 if (!btrfs_inode_cachep)
8366 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008367
David Sterba837e1972012-09-07 03:00:48 -06008368 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008369 sizeof(struct btrfs_trans_handle), 0,
8370 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008371 if (!btrfs_trans_handle_cachep)
8372 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008373
David Sterba837e1972012-09-07 03:00:48 -06008374 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008375 sizeof(struct btrfs_transaction), 0,
8376 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008377 if (!btrfs_transaction_cachep)
8378 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008379
David Sterba837e1972012-09-07 03:00:48 -06008380 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008381 sizeof(struct btrfs_path), 0,
8382 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008383 if (!btrfs_path_cachep)
8384 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008385
David Sterba837e1972012-09-07 03:00:48 -06008386 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008387 sizeof(struct btrfs_free_space), 0,
8388 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8389 if (!btrfs_free_space_cachep)
8390 goto fail;
8391
Miao Xie8ccf6f192012-10-25 09:28:04 +00008392 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8393 sizeof(struct btrfs_delalloc_work), 0,
8394 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8395 NULL);
8396 if (!btrfs_delalloc_work_cachep)
8397 goto fail;
8398
Chris Mason39279cc2007-06-12 06:35:45 -04008399 return 0;
8400fail:
8401 btrfs_destroy_cachep();
8402 return -ENOMEM;
8403}
8404
8405static int btrfs_getattr(struct vfsmount *mnt,
8406 struct dentry *dentry, struct kstat *stat)
8407{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008408 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04008409 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05008410 u32 blocksize = inode->i_sb->s_blocksize;
8411
Chris Mason39279cc2007-06-12 06:35:45 -04008412 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008413 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05008414 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008415
8416 spin_lock(&BTRFS_I(inode)->lock);
8417 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8418 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008419 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008420 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008421 return 0;
8422}
8423
Chris Masond3977122009-01-05 21:25:51 -05008424static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8425 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008426{
8427 struct btrfs_trans_handle *trans;
8428 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008429 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008430 struct inode *new_inode = new_dentry->d_inode;
8431 struct inode *old_inode = old_dentry->d_inode;
8432 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008433 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008434 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008435 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008436 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008437
Li Zefan33345d012011-04-20 10:31:50 +08008438 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008439 return -EPERM;
8440
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008441 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008442 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008443 return -EXDEV;
8444
Li Zefan33345d012011-04-20 10:31:50 +08008445 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8446 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008447 return -ENOTEMPTY;
8448
Chris Mason39279cc2007-06-12 06:35:45 -04008449 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008450 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008451 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008452
8453
8454 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04008455 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05008456 new_dentry->d_name.name,
8457 new_dentry->d_name.len);
8458
8459 if (ret) {
8460 if (ret == -EEXIST) {
8461 /* we shouldn't get
8462 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308463 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05008464 return ret;
8465 }
8466 } else {
8467 /* maybe -EOVERFLOW */
8468 return ret;
8469 }
8470 }
8471 ret = 0;
8472
Chris Mason5a3f23d2009-03-31 13:27:11 -04008473 /*
Chris Mason8d875f92014-08-12 10:47:42 -07008474 * we're using rename to replace one file with another. Start IO on it
8475 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04008476 */
Chris Mason8d875f92014-08-12 10:47:42 -07008477 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04008478 filemap_flush(old_inode->i_mapping);
8479
Yan, Zheng76dda932009-09-21 16:00:26 -04008480 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008481 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008482 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008483 /*
8484 * We want to reserve the absolute worst case amount of items. So if
8485 * both inodes are subvols and we need to unlink them then that would
8486 * require 4 item modifications, but if they are both normal inodes it
8487 * would require 5 item modifications, so we'll assume their normal
8488 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8489 * should cover the worst case number of items we'll modify.
8490 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008491 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008492 if (IS_ERR(trans)) {
8493 ret = PTR_ERR(trans);
8494 goto out_notrans;
8495 }
Chris Mason5f39d392007-10-15 16:14:19 -04008496
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008497 if (dest != root)
8498 btrfs_record_root_in_trans(trans, dest);
8499
Yan, Zhenga5719522009-09-24 09:17:31 -04008500 ret = btrfs_set_inode_index(new_dir, &index);
8501 if (ret)
8502 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008503
Miao Xie67de1172013-12-26 13:07:06 +08008504 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08008505 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008506 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08008507 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008508 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008509 ret = btrfs_insert_inode_ref(trans, dest,
8510 new_dentry->d_name.name,
8511 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008512 old_ino,
8513 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008514 if (ret)
8515 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008516 /*
8517 * this is an ugly little race, but the rename is required
8518 * to make sure that if we crash, the inode is either at the
8519 * old name or the new one. pinning the log transaction lets
8520 * us make sure we don't allow a log commit to come in after
8521 * we unlink the name but before we add the new name back in.
8522 */
8523 btrfs_pin_log_trans(root);
8524 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04008525
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008526 inode_inc_iversion(old_dir);
8527 inode_inc_iversion(new_dir);
8528 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008529 old_dir->i_ctime = old_dir->i_mtime = ctime;
8530 new_dir->i_ctime = new_dir->i_mtime = ctime;
8531 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008532
Chris Mason12fcfd22009-03-24 10:24:20 -04008533 if (old_dentry->d_parent != new_dentry->d_parent)
8534 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8535
Li Zefan33345d012011-04-20 10:31:50 +08008536 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008537 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8538 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8539 old_dentry->d_name.name,
8540 old_dentry->d_name.len);
8541 } else {
Al Viro92986792011-03-04 17:14:37 +00008542 ret = __btrfs_unlink_inode(trans, root, old_dir,
8543 old_dentry->d_inode,
8544 old_dentry->d_name.name,
8545 old_dentry->d_name.len);
8546 if (!ret)
8547 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008548 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008549 if (ret) {
8550 btrfs_abort_transaction(trans, root, ret);
8551 goto out_fail;
8552 }
Chris Mason39279cc2007-06-12 06:35:45 -04008553
8554 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008555 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008556 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008557 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008558 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8559 root_objectid = BTRFS_I(new_inode)->location.objectid;
8560 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8561 root_objectid,
8562 new_dentry->d_name.name,
8563 new_dentry->d_name.len);
8564 BUG_ON(new_inode->i_nlink == 0);
8565 } else {
8566 ret = btrfs_unlink_inode(trans, dest, new_dir,
8567 new_dentry->d_inode,
8568 new_dentry->d_name.name,
8569 new_dentry->d_name.len);
8570 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008571 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008572 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008573 if (ret) {
8574 btrfs_abort_transaction(trans, root, ret);
8575 goto out_fail;
8576 }
Chris Mason39279cc2007-06-12 06:35:45 -04008577 }
Josef Bacikaec74772008-07-24 12:12:38 -04008578
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008579 ret = btrfs_add_link(trans, new_dir, old_inode,
8580 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008581 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008582 if (ret) {
8583 btrfs_abort_transaction(trans, root, ret);
8584 goto out_fail;
8585 }
Chris Mason39279cc2007-06-12 06:35:45 -04008586
Miao Xie67de1172013-12-26 13:07:06 +08008587 if (old_inode->i_nlink == 1)
8588 BTRFS_I(old_inode)->dir_index = index;
8589
Li Zefan33345d012011-04-20 10:31:50 +08008590 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008591 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008592 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008593 btrfs_end_log_trans(root);
8594 }
Chris Mason39279cc2007-06-12 06:35:45 -04008595out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008596 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008597out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008598 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008599 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008600
Chris Mason39279cc2007-06-12 06:35:45 -04008601 return ret;
8602}
8603
Miklos Szeredi80ace852014-07-23 15:15:32 +02008604static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
8605 struct inode *new_dir, struct dentry *new_dentry,
8606 unsigned int flags)
8607{
8608 if (flags & ~RENAME_NOREPLACE)
8609 return -EINVAL;
8610
8611 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
8612}
8613
Miao Xie8ccf6f192012-10-25 09:28:04 +00008614static void btrfs_run_delalloc_work(struct btrfs_work *work)
8615{
8616 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04008617 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008618
8619 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8620 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04008621 inode = delalloc_work->inode;
8622 if (delalloc_work->wait) {
8623 btrfs_wait_ordered_range(inode, 0, (u64)-1);
8624 } else {
8625 filemap_flush(inode->i_mapping);
8626 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8627 &BTRFS_I(inode)->runtime_flags))
8628 filemap_flush(inode->i_mapping);
8629 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008630
8631 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04008632 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008633 else
Josef Bacik9f23e282013-10-28 15:03:41 -04008634 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008635 complete(&delalloc_work->completion);
8636}
8637
8638struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8639 int wait, int delay_iput)
8640{
8641 struct btrfs_delalloc_work *work;
8642
8643 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8644 if (!work)
8645 return NULL;
8646
8647 init_completion(&work->completion);
8648 INIT_LIST_HEAD(&work->list);
8649 work->inode = inode;
8650 work->wait = wait;
8651 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08008652 WARN_ON_ONCE(!inode);
8653 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
8654 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008655
8656 return work;
8657}
8658
8659void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8660{
8661 wait_for_completion(&work->completion);
8662 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8663}
8664
Chris Masond352ac62008-09-29 15:18:18 -04008665/*
8666 * some fairly slow code that needs optimization. This walks the list
8667 * of all the inodes with pending delalloc and forces them to disk.
8668 */
Miao Xie6c255e62014-03-06 13:55:01 +08008669static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
8670 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04008671{
Chris Masonea8c2812008-08-04 23:17:27 -04008672 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008673 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008674 struct btrfs_delalloc_work *work, *next;
8675 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008676 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008677 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008678
Miao Xie8ccf6f192012-10-25 09:28:04 +00008679 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008680 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008681
Miao Xie573bfb72014-03-06 13:55:03 +08008682 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008683 spin_lock(&root->delalloc_lock);
8684 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008685 while (!list_empty(&splice)) {
8686 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008687 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008688
Miao Xieeb73c1b2013-05-15 07:48:22 +00008689 list_move_tail(&binode->delalloc_inodes,
8690 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008691 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008692 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008693 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008694 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008695 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008696 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008697
8698 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8699 if (unlikely(!work)) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04008700 if (delay_iput)
8701 btrfs_add_delayed_iput(inode);
8702 else
8703 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008704 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008705 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008706 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008707 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08008708 btrfs_queue_work(root->fs_info->flush_workers,
8709 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08008710 ret++;
8711 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008712 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008713 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008714 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008715 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008716 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008717
Wang Shilonga1ecaab2014-04-02 19:53:32 +08008718out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008719 list_for_each_entry_safe(work, next, &works, list) {
8720 list_del_init(&work->list);
8721 btrfs_wait_and_free_delalloc_work(work);
8722 }
8723
Miao Xieeb73c1b2013-05-15 07:48:22 +00008724 if (!list_empty_careful(&splice)) {
8725 spin_lock(&root->delalloc_lock);
8726 list_splice_tail(&splice, &root->delalloc_inodes);
8727 spin_unlock(&root->delalloc_lock);
8728 }
Miao Xie573bfb72014-03-06 13:55:03 +08008729 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008730 return ret;
8731}
8732
8733int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8734{
8735 int ret;
8736
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008737 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008738 return -EROFS;
8739
Miao Xie6c255e62014-03-06 13:55:01 +08008740 ret = __start_delalloc_inodes(root, delay_iput, -1);
8741 if (ret > 0)
8742 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00008743 /*
8744 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008745 * we have to make sure the IO is actually started and that
8746 * ordered extents get created before we return
8747 */
8748 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008749 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008750 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008751 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008752 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8753 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008754 }
8755 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008756 return ret;
8757}
8758
Miao Xie6c255e62014-03-06 13:55:01 +08008759int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
8760 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008761{
8762 struct btrfs_root *root;
8763 struct list_head splice;
8764 int ret;
8765
Wang Shilong2c21b4d2014-01-14 19:42:20 +08008766 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00008767 return -EROFS;
8768
8769 INIT_LIST_HEAD(&splice);
8770
Miao Xie573bfb72014-03-06 13:55:03 +08008771 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008772 spin_lock(&fs_info->delalloc_root_lock);
8773 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08008774 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008775 root = list_first_entry(&splice, struct btrfs_root,
8776 delalloc_root);
8777 root = btrfs_grab_fs_root(root);
8778 BUG_ON(!root);
8779 list_move_tail(&root->delalloc_root,
8780 &fs_info->delalloc_roots);
8781 spin_unlock(&fs_info->delalloc_root_lock);
8782
Miao Xie6c255e62014-03-06 13:55:01 +08008783 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008784 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08008785 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00008786 goto out;
8787
Miao Xie6c255e62014-03-06 13:55:01 +08008788 if (nr != -1) {
8789 nr -= ret;
8790 WARN_ON(nr < 0);
8791 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008792 spin_lock(&fs_info->delalloc_root_lock);
8793 }
8794 spin_unlock(&fs_info->delalloc_root_lock);
8795
Miao Xie6c255e62014-03-06 13:55:01 +08008796 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00008797 atomic_inc(&fs_info->async_submit_draining);
8798 while (atomic_read(&fs_info->nr_async_submits) ||
8799 atomic_read(&fs_info->async_delalloc_pages)) {
8800 wait_event(fs_info->async_submit_wait,
8801 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8802 atomic_read(&fs_info->async_delalloc_pages) == 0));
8803 }
8804 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008805out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008806 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008807 spin_lock(&fs_info->delalloc_root_lock);
8808 list_splice_tail(&splice, &fs_info->delalloc_roots);
8809 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008810 }
Miao Xie573bfb72014-03-06 13:55:03 +08008811 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00008812 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008813}
8814
Chris Mason39279cc2007-06-12 06:35:45 -04008815static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8816 const char *symname)
8817{
8818 struct btrfs_trans_handle *trans;
8819 struct btrfs_root *root = BTRFS_I(dir)->root;
8820 struct btrfs_path *path;
8821 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008822 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008823 int err;
8824 int drop_inode = 0;
8825 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308826 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008827 int name_len;
8828 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008829 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008830 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008831 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008832
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008833 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04008834 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8835 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008836
Josef Bacik9ed74f22009-09-11 16:12:44 -04008837 /*
8838 * 2 items for inode item and ref
8839 * 2 items for dir items
8840 * 1 item for xattr if selinux is on
8841 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008842 trans = btrfs_start_transaction(root, 5);
8843 if (IS_ERR(trans))
8844 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008845
Li Zefan581bb052011-04-20 10:06:11 +08008846 err = btrfs_find_free_ino(root, &objectid);
8847 if (err)
8848 goto out_unlock;
8849
Josef Bacikaec74772008-07-24 12:12:38 -04008850 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008851 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008852 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008853 if (IS_ERR(inode)) {
8854 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008855 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008856 }
Chris Mason39279cc2007-06-12 06:35:45 -04008857
Casey Schauflerad19db72011-12-15 10:09:07 -05008858 /*
8859 * If the active LSM wants to access the inode during
8860 * d_instantiate it needs these. Smack checks to see
8861 * if the filesystem supports xattrs by looking at the
8862 * ops vector.
8863 */
8864 inode->i_fop = &btrfs_file_operations;
8865 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07008866 inode->i_mapping->a_ops = &btrfs_aops;
8867 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
8868 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
8869
8870 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
8871 if (err)
8872 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05008873
Josef Bacika1b075d2010-11-19 20:36:11 +00008874 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008875 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07008876 goto out_unlock_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008877
8878 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008879 if (!path) {
8880 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -07008881 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -07008882 }
Li Zefan33345d012011-04-20 10:31:50 +08008883 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008884 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02008885 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04008886 datasize = btrfs_file_extent_calc_inline_size(name_len);
8887 err = btrfs_insert_empty_item(trans, root, path, &key,
8888 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008889 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00008890 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -07008891 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -04008892 }
Chris Mason5f39d392007-10-15 16:14:19 -04008893 leaf = path->nodes[0];
8894 ei = btrfs_item_ptr(leaf, path->slots[0],
8895 struct btrfs_file_extent_item);
8896 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8897 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008898 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008899 btrfs_set_file_extent_encryption(leaf, ei, 0);
8900 btrfs_set_file_extent_compression(leaf, ei, 0);
8901 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8902 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8903
Chris Mason39279cc2007-06-12 06:35:45 -04008904 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008905 write_extent_buffer(leaf, symname, ptr, name_len);
8906 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008907 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008908
Chris Mason39279cc2007-06-12 06:35:45 -04008909 inode->i_op = &btrfs_symlink_inode_operations;
8910 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008911 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008912 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008913 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04008914 err = btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07008915 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -04008916 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07008917 goto out_unlock_inode;
8918 }
8919
8920 unlock_new_inode(inode);
8921 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008922
8923out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008924 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008925 if (drop_inode) {
8926 inode_dec_link_count(inode);
8927 iput(inode);
8928 }
Liu Bob53d3f52012-11-14 14:34:34 +00008929 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008930 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07008931
8932out_unlock_inode:
8933 drop_inode = 1;
8934 unlock_new_inode(inode);
8935 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -04008936}
Chris Mason16432982008-04-10 10:23:21 -04008937
Josef Bacik0af3d002010-06-21 14:48:16 -04008938static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8939 u64 start, u64 num_bytes, u64 min_size,
8940 loff_t actual_len, u64 *alloc_hint,
8941 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008942{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008943 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8944 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008945 struct btrfs_root *root = BTRFS_I(inode)->root;
8946 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008947 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008948 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008949 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008950 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008951 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008952
Josef Bacik0af3d002010-06-21 14:48:16 -04008953 if (trans)
8954 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008955 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008956 if (own_trans) {
8957 trans = btrfs_start_transaction(root, 3);
8958 if (IS_ERR(trans)) {
8959 ret = PTR_ERR(trans);
8960 break;
8961 }
Yan Zhengd899e052008-10-30 14:25:28 -04008962 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008963
Chris Mason154ea282013-03-05 11:11:26 -05008964 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8965 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04008966 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08008967 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008968 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008969 if (own_trans)
8970 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008971 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008972 }
8973
Yan Zhengd899e052008-10-30 14:25:28 -04008974 ret = insert_reserved_file_extent(trans, inode,
8975 cur_offset, ins.objectid,
8976 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008977 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008978 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008979 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04008980 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08008981 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008982 btrfs_abort_transaction(trans, root, ret);
8983 if (own_trans)
8984 btrfs_end_transaction(trans, root);
8985 break;
8986 }
Chris Masona1ed8352009-09-11 12:27:37 -04008987 btrfs_drop_extent_cache(inode, cur_offset,
8988 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008989
Josef Bacik5dc562c2012-08-17 13:14:17 -04008990 em = alloc_extent_map();
8991 if (!em) {
8992 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8993 &BTRFS_I(inode)->runtime_flags);
8994 goto next;
8995 }
8996
8997 em->start = cur_offset;
8998 em->orig_start = cur_offset;
8999 em->len = ins.offset;
9000 em->block_start = ins.objectid;
9001 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009002 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009003 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009004 em->bdev = root->fs_info->fs_devices->latest_bdev;
9005 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9006 em->generation = trans->transid;
9007
9008 while (1) {
9009 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009010 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009011 write_unlock(&em_tree->lock);
9012 if (ret != -EEXIST)
9013 break;
9014 btrfs_drop_extent_cache(inode, cur_offset,
9015 cur_offset + ins.offset - 1,
9016 0);
9017 }
9018 free_extent_map(em);
9019next:
Yan Zhengd899e052008-10-30 14:25:28 -04009020 num_bytes -= ins.offset;
9021 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009022 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009023
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009024 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009025 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009026 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009027 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009028 (actual_len > inode->i_size) &&
9029 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009030 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009031 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009032 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009033 i_size = cur_offset;
9034 i_size_write(inode, i_size);
9035 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009036 }
9037
Yan Zhengd899e052008-10-30 14:25:28 -04009038 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009039
9040 if (ret) {
9041 btrfs_abort_transaction(trans, root, ret);
9042 if (own_trans)
9043 btrfs_end_transaction(trans, root);
9044 break;
9045 }
Yan Zhengd899e052008-10-30 14:25:28 -04009046
Josef Bacik0af3d002010-06-21 14:48:16 -04009047 if (own_trans)
9048 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009049 }
Yan Zhengd899e052008-10-30 14:25:28 -04009050 return ret;
9051}
9052
Josef Bacik0af3d002010-06-21 14:48:16 -04009053int btrfs_prealloc_file_range(struct inode *inode, int mode,
9054 u64 start, u64 num_bytes, u64 min_size,
9055 loff_t actual_len, u64 *alloc_hint)
9056{
9057 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9058 min_size, actual_len, alloc_hint,
9059 NULL);
9060}
9061
9062int btrfs_prealloc_file_range_trans(struct inode *inode,
9063 struct btrfs_trans_handle *trans, int mode,
9064 u64 start, u64 num_bytes, u64 min_size,
9065 loff_t actual_len, u64 *alloc_hint)
9066{
9067 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9068 min_size, actual_len, alloc_hint, trans);
9069}
9070
Chris Masone6dcd2d2008-07-17 12:53:50 -04009071static int btrfs_set_page_dirty(struct page *page)
9072{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009073 return __set_page_dirty_nobuffers(page);
9074}
9075
Al Viro10556cb2011-06-20 19:28:19 -04009076static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009077{
Li Zefanb83cc962010-12-20 16:04:08 +08009078 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009079 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009080
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009081 if (mask & MAY_WRITE &&
9082 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9083 if (btrfs_root_readonly(root))
9084 return -EROFS;
9085 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9086 return -EACCES;
9087 }
Al Viro2830ba72011-06-20 19:16:29 -04009088 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009089}
Chris Mason39279cc2007-06-12 06:35:45 -04009090
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009091static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9092{
9093 struct btrfs_trans_handle *trans;
9094 struct btrfs_root *root = BTRFS_I(dir)->root;
9095 struct inode *inode = NULL;
9096 u64 objectid;
9097 u64 index;
9098 int ret = 0;
9099
9100 /*
9101 * 5 units required for adding orphan entry
9102 */
9103 trans = btrfs_start_transaction(root, 5);
9104 if (IS_ERR(trans))
9105 return PTR_ERR(trans);
9106
9107 ret = btrfs_find_free_ino(root, &objectid);
9108 if (ret)
9109 goto out;
9110
9111 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9112 btrfs_ino(dir), objectid, mode, &index);
9113 if (IS_ERR(inode)) {
9114 ret = PTR_ERR(inode);
9115 inode = NULL;
9116 goto out;
9117 }
9118
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009119 inode->i_fop = &btrfs_file_operations;
9120 inode->i_op = &btrfs_file_inode_operations;
9121
9122 inode->i_mapping->a_ops = &btrfs_aops;
9123 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9124 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9125
Chris Masonb0d5d102014-09-08 13:08:51 -07009126 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9127 if (ret)
9128 goto out_inode;
9129
9130 ret = btrfs_update_inode(trans, root, inode);
9131 if (ret)
9132 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009133 ret = btrfs_orphan_add(trans, inode);
9134 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -07009135 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009136
Filipe Manana5762b5c2014-08-01 00:10:32 +01009137 /*
9138 * We set number of links to 0 in btrfs_new_inode(), and here we set
9139 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9140 * through:
9141 *
9142 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9143 */
9144 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -07009145 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009146 d_tmpfile(dentry, inode);
9147 mark_inode_dirty(inode);
9148
9149out:
9150 btrfs_end_transaction(trans, root);
9151 if (ret)
9152 iput(inode);
9153 btrfs_balance_delayed_items(root);
9154 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009155 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -07009156
9157out_inode:
9158 unlock_new_inode(inode);
9159 goto out;
9160
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009161}
9162
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009163static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05009164 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04009165 .lookup = btrfs_lookup,
9166 .create = btrfs_create,
9167 .unlink = btrfs_unlink,
9168 .link = btrfs_link,
9169 .mkdir = btrfs_mkdir,
9170 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +02009171 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -04009172 .symlink = btrfs_symlink,
9173 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04009174 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009175 .setxattr = btrfs_setxattr,
9176 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009177 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009178 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009179 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009180 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009181 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009182 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009183 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -04009184};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009185static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009186 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05009187 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009188 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009189 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07009190 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009191};
Yan, Zheng76dda932009-09-21 16:00:26 -04009192
Alexey Dobriyan828c0952009-10-01 15:43:56 -07009193static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009194 .llseek = generic_file_llseek,
9195 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04009196 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009197 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009198#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04009199 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04009200#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04009201 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04009202 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04009203};
9204
Chris Masond1310b22008-01-24 16:13:08 -05009205static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04009206 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05009207 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04009208 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009209 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009210 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04009211 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05009212 .set_bit_hook = btrfs_set_bit_hook,
9213 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04009214 .merge_extent_hook = btrfs_merge_extent_hook,
9215 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04009216};
9217
Chris Mason35054392009-01-21 13:11:13 -05009218/*
9219 * btrfs doesn't support the bmap operation because swapfiles
9220 * use bmap to make a mapping of extents in the file. They assume
9221 * these extents won't change over the life of the file and they
9222 * use the bmap result to do IO directly to the drive.
9223 *
9224 * the btrfs bmap call would return logical addresses that aren't
9225 * suitable for IO and they also will change frequently as COW
9226 * operations happen. So, swapfile + btrfs == corruption.
9227 *
9228 * For now we're avoiding this by dropping bmap.
9229 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009230static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009231 .readpage = btrfs_readpage,
9232 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04009233 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05009234 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04009235 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04009236 .invalidatepage = btrfs_invalidatepage,
9237 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04009238 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02009239 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04009240};
9241
Alexey Dobriyan7f094102009-09-21 17:01:10 -07009242static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04009243 .readpage = btrfs_readpage,
9244 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04009245 .invalidatepage = btrfs_invalidatepage,
9246 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04009247};
9248
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009249static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009250 .getattr = btrfs_getattr,
9251 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009252 .setxattr = btrfs_setxattr,
9253 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05009254 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009255 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05009256 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05009257 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009258 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009259 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009260 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009261};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009262static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04009263 .getattr = btrfs_getattr,
9264 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009265 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009266 .setxattr = btrfs_setxattr,
9267 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04009268 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04009269 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02009270 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -08009271 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04009272 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04009273};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07009274static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04009275 .readlink = generic_readlink,
9276 .follow_link = page_follow_link_light,
9277 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00009278 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05009279 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05009280 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05009281 .setxattr = btrfs_setxattr,
9282 .getxattr = btrfs_getxattr,
9283 .listxattr = btrfs_listxattr,
9284 .removexattr = btrfs_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -04009285 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04009286};
Yan, Zheng76dda932009-09-21 16:00:26 -04009287
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04009288const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04009289 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04009290 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04009291};