blob: f0e41b8407397ac6d09edaafb69d18d8bf79c181 [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 Mason4b4e25f2008-11-20 10:22:27 -050046#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040047#include "ctree.h"
48#include "disk-io.h"
49#include "transaction.h"
50#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040051#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040052#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040053#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040054#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020055#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040056#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050057#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050058#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080059#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000060#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040061#include "hash.h"
Chris Mason39279cc2007-06-12 06:35:45 -040062
63struct btrfs_iget_args {
64 u64 ino;
65 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 Masond3977122009-01-05 21:25:51 -0500128static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400129 struct btrfs_root *root, struct inode *inode,
130 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000131 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400132 struct page **compressed_pages)
133{
134 struct btrfs_key key;
135 struct btrfs_path *path;
136 struct extent_buffer *leaf;
137 struct page *page = NULL;
138 char *kaddr;
139 unsigned long ptr;
140 struct btrfs_file_extent_item *ei;
141 int err = 0;
142 int ret;
143 size_t cur_size = size;
144 size_t datasize;
145 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400146
Li Zefanfe3f5662011-03-28 08:30:38 +0000147 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400148 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400149
Chris Masond3977122009-01-05 21:25:51 -0500150 path = btrfs_alloc_path();
151 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400152 return -ENOMEM;
153
Chris Masonb9473432009-03-13 11:00:37 -0400154 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400155
Li Zefan33345d012011-04-20 10:31:50 +0800156 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400157 key.offset = start;
158 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400159 datasize = btrfs_file_extent_calc_inline_size(cur_size);
160
161 inode_add_bytes(inode, size);
162 ret = btrfs_insert_empty_item(trans, root, path, &key,
163 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400164 if (ret) {
165 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400166 goto fail;
167 }
168 leaf = path->nodes[0];
169 ei = btrfs_item_ptr(leaf, path->slots[0],
170 struct btrfs_file_extent_item);
171 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
172 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
173 btrfs_set_file_extent_encryption(leaf, ei, 0);
174 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
175 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
176 ptr = btrfs_file_extent_inline_start(ei);
177
Li Zefan261507a02010-12-17 14:21:50 +0800178 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400179 struct page *cpage;
180 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500181 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400182 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500183 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400184 PAGE_CACHE_SIZE);
185
Cong Wang7ac687d2011-11-25 23:14:28 +0800186 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400187 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800188 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400189
190 i++;
191 ptr += cur_size;
192 compressed_size -= cur_size;
193 }
194 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800195 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400196 } else {
197 page = find_get_page(inode->i_mapping,
198 start >> PAGE_CACHE_SHIFT);
199 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800200 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400201 offset = start & (PAGE_CACHE_SIZE - 1);
202 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800203 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400204 page_cache_release(page);
205 }
206 btrfs_mark_buffer_dirty(leaf);
207 btrfs_free_path(path);
208
Yan, Zhengc21677542009-11-12 09:34:21 +0000209 /*
210 * we're an inline extent, so nobody can
211 * extend the file past i_size without locking
212 * a page we already have locked.
213 *
214 * We must do any isize and inode updates
215 * before we unlock the pages. Otherwise we
216 * could end up racing with unlink.
217 */
Chris Masonc8b97812008-10-29 14:49:59 -0400218 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100219 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc21677542009-11-12 09:34:21 +0000220
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100221 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400222fail:
223 btrfs_free_path(path);
224 return err;
225}
226
227
228/*
229 * conditionally insert an inline extent into the file. This
230 * does the checks required to make sure the data is small enough
231 * to fit as an inline extent.
232 */
Josef Bacik00361582013-08-14 14:02:47 -0400233static noinline int cow_file_range_inline(struct btrfs_root *root,
234 struct inode *inode, u64 start,
235 u64 end, size_t compressed_size,
236 int compress_type,
237 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400238{
Josef Bacik00361582013-08-14 14:02:47 -0400239 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400240 u64 isize = i_size_read(inode);
241 u64 actual_end = min(end + 1, isize);
242 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000243 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400244 u64 data_len = inline_len;
245 int ret;
246
247 if (compressed_size)
248 data_len = compressed_size;
249
250 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400251 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400252 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
253 (!compressed_size &&
254 (actual_end & (root->sectorsize - 1)) == 0) ||
255 end + 1 < isize ||
256 data_len > root->fs_info->max_inline) {
257 return 1;
258 }
259
Josef Bacik00361582013-08-14 14:02:47 -0400260 trans = btrfs_join_transaction(root);
261 if (IS_ERR(trans))
262 return PTR_ERR(trans);
263 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
264
Josef Bacik26714852012-08-29 12:24:27 -0400265 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400266 if (ret) {
267 btrfs_abort_transaction(trans, root, ret);
268 goto out;
269 }
Chris Masonc8b97812008-10-29 14:49:59 -0400270
271 if (isize > actual_end)
272 inline_len = min_t(u64, isize, actual_end);
273 ret = insert_inline_extent(trans, root, inode, start,
274 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000275 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400276 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100277 btrfs_abort_transaction(trans, root, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400278 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400279 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400280 ret = 1;
281 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100282 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400283
Josef Bacikbdc20e62013-02-28 13:23:38 -0500284 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400285 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400286 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400287out:
288 btrfs_end_transaction(trans, root);
289 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400290}
291
Chris Mason771ed682008-11-06 22:02:51 -0500292struct async_extent {
293 u64 start;
294 u64 ram_size;
295 u64 compressed_size;
296 struct page **pages;
297 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800298 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500299 struct list_head list;
300};
301
302struct async_cow {
303 struct inode *inode;
304 struct btrfs_root *root;
305 struct page *locked_page;
306 u64 start;
307 u64 end;
308 struct list_head extents;
309 struct btrfs_work work;
310};
311
312static noinline int add_async_extent(struct async_cow *cow,
313 u64 start, u64 ram_size,
314 u64 compressed_size,
315 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800316 unsigned long nr_pages,
317 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500318{
319 struct async_extent *async_extent;
320
321 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100322 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500323 async_extent->start = start;
324 async_extent->ram_size = ram_size;
325 async_extent->compressed_size = compressed_size;
326 async_extent->pages = pages;
327 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800328 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500329 list_add_tail(&async_extent->list, &cow->extents);
330 return 0;
331}
332
Chris Masonc8b97812008-10-29 14:49:59 -0400333/*
Chris Mason771ed682008-11-06 22:02:51 -0500334 * we create compressed extents in two phases. The first
335 * phase compresses a range of pages that have already been
336 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400337 *
Chris Mason771ed682008-11-06 22:02:51 -0500338 * This is done inside an ordered work queue, and the compression
339 * is spread across many cpus. The actual IO submission is step
340 * two, and the ordered work queue takes care of making sure that
341 * happens in the same order things were put onto the queue by
342 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400343 *
Chris Mason771ed682008-11-06 22:02:51 -0500344 * If this code finds it can't get good compression, it puts an
345 * entry onto the work queue to write the uncompressed bytes. This
346 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300347 * are written in the same order that the flusher thread sent them
348 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400349 */
Chris Mason771ed682008-11-06 22:02:51 -0500350static noinline int compress_file_range(struct inode *inode,
351 struct page *locked_page,
352 u64 start, u64 end,
353 struct async_cow *async_cow,
354 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400355{
356 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400357 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400358 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400359 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500360 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400361 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400362 struct page **pages = NULL;
363 unsigned long nr_pages;
364 unsigned long nr_pages_ret = 0;
365 unsigned long total_compressed = 0;
366 unsigned long total_in = 0;
367 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500368 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400369 int i;
370 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800371 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400372 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400373
Liu Bo4cb13e52012-03-29 09:57:45 -0400374 /* if this is a small write inside eof, kick off a defrag */
375 if ((end - start + 1) < 16 * 1024 &&
376 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400377 btrfs_add_inode_defrag(NULL, inode);
378
Chris Mason42dc7ba2008-12-15 11:44:56 -0500379 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400380again:
381 will_compress = 0;
382 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
383 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
384
Chris Masonf03d9302009-02-04 09:31:06 -0500385 /*
386 * we don't want to send crud past the end of i_size through
387 * compression, that's just a waste of CPU time. So, if the
388 * end of the file is before the start of our current
389 * requested range of bytes, we bail out to the uncompressed
390 * cleanup code that can deal with all of this.
391 *
392 * It isn't really the fastest way to fix things, but this is a
393 * very uncommon corner.
394 */
395 if (actual_end <= start)
396 goto cleanup_and_bail_uncompressed;
397
Chris Masonc8b97812008-10-29 14:49:59 -0400398 total_compressed = actual_end - start;
399
400 /* we want to make sure that amount of ram required to uncompress
401 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500402 * of a compressed extent to 128k. This is a crucial number
403 * because it also controls how easily we can spread reads across
404 * cpus for decompression.
405 *
406 * We also want to make sure the amount of IO required to do
407 * a random read is reasonably small, so we limit the size of
408 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400409 */
410 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000411 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500412 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400413 total_in = 0;
414 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400415
Chris Mason771ed682008-11-06 22:02:51 -0500416 /*
417 * we do compression for mount -o compress and when the
418 * inode has not been flagged as nocompress. This flag can
419 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400420 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200421 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500422 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000423 (BTRFS_I(inode)->force_compress) ||
424 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400425 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400426 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800427 if (!pages) {
428 /* just bail out to the uncompressed code */
429 goto cont;
430 }
Chris Mason179e29e2007-11-01 11:28:41 -0400431
Li Zefan261507a02010-12-17 14:21:50 +0800432 if (BTRFS_I(inode)->force_compress)
433 compress_type = BTRFS_I(inode)->force_compress;
434
Chris Mason4adaa612013-03-26 13:07:00 -0400435 /*
436 * we need to call clear_page_dirty_for_io on each
437 * page in the range. Otherwise applications with the file
438 * mmap'd can wander in and change the page contents while
439 * we are compressing them.
440 *
441 * If the compression fails for any reason, we set the pages
442 * dirty again later on.
443 */
444 extent_range_clear_dirty_for_io(inode, start, end);
445 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800446 ret = btrfs_compress_pages(compress_type,
447 inode->i_mapping, start,
448 total_compressed, pages,
449 nr_pages, &nr_pages_ret,
450 &total_in,
451 &total_compressed,
452 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400453
454 if (!ret) {
455 unsigned long offset = total_compressed &
456 (PAGE_CACHE_SIZE - 1);
457 struct page *page = pages[nr_pages_ret - 1];
458 char *kaddr;
459
460 /* zero the tail end of the last page, we might be
461 * sending it down to disk
462 */
463 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800464 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400465 memset(kaddr + offset, 0,
466 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800467 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400468 }
469 will_compress = 1;
470 }
471 }
Li Zefan560f7d72011-09-08 10:22:01 +0800472cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400473 if (start == 0) {
474 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500475 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400476 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500477 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400478 */
Josef Bacik00361582013-08-14 14:02:47 -0400479 ret = cow_file_range_inline(root, inode, start, end,
480 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400481 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500482 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400483 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000484 total_compressed,
485 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400486 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100487 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400488 unsigned long clear_flags = EXTENT_DELALLOC |
489 EXTENT_DEFRAG;
490 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
491
Chris Mason771ed682008-11-06 22:02:51 -0500492 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100493 * inline extent creation worked or returned error,
494 * we don't need to create any more async work items.
495 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500496 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400497 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400498 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400499 PAGE_CLEAR_DIRTY |
500 PAGE_SET_WRITEBACK |
501 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400502 goto free_pages_out;
503 }
504 }
505
506 if (will_compress) {
507 /*
508 * we aren't doing an inline extent round the compressed size
509 * up to a block size boundary so the allocator does sane
510 * things
511 */
Qu Wenruofda28322013-02-26 08:10:22 +0000512 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400513
514 /*
515 * one last check to make sure the compression is really a
516 * win, compare the page count read with the blocks on disk
517 */
Qu Wenruofda28322013-02-26 08:10:22 +0000518 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400519 if (total_compressed >= total_in) {
520 will_compress = 0;
521 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400522 num_bytes = total_in;
523 }
524 }
525 if (!will_compress && pages) {
526 /*
527 * the compression code ran but failed to make things smaller,
528 * free any pages it allocated and our page pointer array
529 */
530 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400531 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400532 page_cache_release(pages[i]);
533 }
534 kfree(pages);
535 pages = NULL;
536 total_compressed = 0;
537 nr_pages_ret = 0;
538
539 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500540 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
541 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500542 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500543 }
Chris Masonc8b97812008-10-29 14:49:59 -0400544 }
Chris Mason771ed682008-11-06 22:02:51 -0500545 if (will_compress) {
546 *num_added += 1;
547
548 /* the async work queues will take care of doing actual
549 * allocation on disk for these compressed pages,
550 * and will submit them to the elevator.
551 */
552 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800553 total_compressed, pages, nr_pages_ret,
554 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500555
Yan, Zheng24ae6362010-12-06 07:02:36 +0000556 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500557 start += num_bytes;
558 pages = NULL;
559 cond_resched();
560 goto again;
561 }
562 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500563cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500564 /*
565 * No compression, but we still need to write the pages in
566 * the file we've been given so far. redirty the locked
567 * page if it corresponds to our extent and set things up
568 * for the async work queue to run cow_file_range to do
569 * the normal delalloc dance
570 */
571 if (page_offset(locked_page) >= start &&
572 page_offset(locked_page) <= end) {
573 __set_page_dirty_nobuffers(locked_page);
574 /* unlocked later on in the async handlers */
575 }
Chris Mason4adaa612013-03-26 13:07:00 -0400576 if (redirty)
577 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800578 add_async_extent(async_cow, start, end - start + 1,
579 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500580 *num_added += 1;
581 }
582
583out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100584 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500585
586free_pages_out:
587 for (i = 0; i < nr_pages_ret; i++) {
588 WARN_ON(pages[i]->mapping);
589 page_cache_release(pages[i]);
590 }
Chris Masond3977122009-01-05 21:25:51 -0500591 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500592
593 goto out;
594}
595
596/*
597 * phase two of compressed writeback. This is the ordered portion
598 * of the code, which only gets called in the order the work was
599 * queued. We walk all the async extents created by compress_file_range
600 * and send them down to the disk.
601 */
602static noinline int submit_compressed_extents(struct inode *inode,
603 struct async_cow *async_cow)
604{
605 struct async_extent *async_extent;
606 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500607 struct btrfs_key ins;
608 struct extent_map *em;
609 struct btrfs_root *root = BTRFS_I(inode)->root;
610 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
611 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500612 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500613
614 if (list_empty(&async_cow->extents))
615 return 0;
616
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500617again:
Chris Masond3977122009-01-05 21:25:51 -0500618 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500619 async_extent = list_entry(async_cow->extents.next,
620 struct async_extent, list);
621 list_del(&async_extent->list);
622
623 io_tree = &BTRFS_I(inode)->io_tree;
624
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500625retry:
Chris Mason771ed682008-11-06 22:02:51 -0500626 /* did the compression code fall back to uncompressed IO? */
627 if (!async_extent->pages) {
628 int page_started = 0;
629 unsigned long nr_written = 0;
630
631 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000632 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100633 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500634
635 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500636 ret = cow_file_range(inode, async_cow->locked_page,
637 async_extent->start,
638 async_extent->start +
639 async_extent->ram_size - 1,
640 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500641
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100642 /* JDM XXX */
643
Chris Mason771ed682008-11-06 22:02:51 -0500644 /*
645 * if page_started, cow_file_range inserted an
646 * inline extent and took care of all the unlocking
647 * and IO for us. Otherwise, we need to submit
648 * all those pages down to the drive.
649 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500650 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500651 extent_write_locked_range(io_tree,
652 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500653 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500654 async_extent->ram_size - 1,
655 btrfs_get_extent,
656 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500657 else if (ret)
658 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500659 kfree(async_extent);
660 cond_resched();
661 continue;
662 }
663
664 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100665 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500666
Josef Bacik00361582013-08-14 14:02:47 -0400667 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500668 async_extent->compressed_size,
669 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500670 0, alloc_hint, &ins, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500671 if (ret) {
672 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500673
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500674 for (i = 0; i < async_extent->nr_pages; i++) {
675 WARN_ON(async_extent->pages[i]->mapping);
676 page_cache_release(async_extent->pages[i]);
677 }
678 kfree(async_extent->pages);
679 async_extent->nr_pages = 0;
680 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500681
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400682 if (ret == -ENOSPC) {
683 unlock_extent(io_tree, async_extent->start,
684 async_extent->start +
685 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100686 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400687 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500688 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500689 }
690
Yan, Zhengc21677542009-11-12 09:34:21 +0000691 /*
692 * here we're doing allocation and writeback of the
693 * compressed pages
694 */
695 btrfs_drop_extent_cache(inode, async_extent->start,
696 async_extent->start +
697 async_extent->ram_size - 1, 0);
698
David Sterba172ddd62011-04-21 00:48:27 +0200699 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000700 if (!em) {
701 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500702 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000703 }
Chris Mason771ed682008-11-06 22:02:51 -0500704 em->start = async_extent->start;
705 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500706 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400707 em->mod_start = em->start;
708 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500709
710 em->block_start = ins.objectid;
711 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500712 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400713 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500714 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800715 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500716 set_bit(EXTENT_FLAG_PINNED, &em->flags);
717 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400718 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500719
Chris Masond3977122009-01-05 21:25:51 -0500720 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400721 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400722 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400723 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500724 if (ret != -EEXIST) {
725 free_extent_map(em);
726 break;
727 }
728 btrfs_drop_extent_cache(inode, async_extent->start,
729 async_extent->start +
730 async_extent->ram_size - 1, 0);
731 }
732
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500733 if (ret)
734 goto out_free_reserve;
735
Li Zefan261507a02010-12-17 14:21:50 +0800736 ret = btrfs_add_ordered_extent_compress(inode,
737 async_extent->start,
738 ins.objectid,
739 async_extent->ram_size,
740 ins.offset,
741 BTRFS_ORDERED_COMPRESSED,
742 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500743 if (ret)
744 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500745
Chris Mason771ed682008-11-06 22:02:51 -0500746 /*
747 * clear dirty, set writeback and unlock the pages.
748 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400749 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400750 async_extent->start +
751 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400752 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
753 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400754 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500755 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500756 async_extent->start,
757 async_extent->ram_size,
758 ins.objectid,
759 ins.offset, async_extent->pages,
760 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500761 alloc_hint = ins.objectid + ins.offset;
762 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500763 if (ret)
764 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500765 cond_resched();
766 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100767 ret = 0;
768out:
769 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500770out_free_reserve:
771 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100772out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400773 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500774 async_extent->start +
775 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400776 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400777 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
778 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
779 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100780 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500781 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500782}
783
Josef Bacik4b46fce2010-05-23 11:00:55 -0400784static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
785 u64 num_bytes)
786{
787 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
788 struct extent_map *em;
789 u64 alloc_hint = 0;
790
791 read_lock(&em_tree->lock);
792 em = search_extent_mapping(em_tree, start, num_bytes);
793 if (em) {
794 /*
795 * if block start isn't an actual block number then find the
796 * first block in this inode and use that as a hint. If that
797 * block is also bogus then just don't worry about it.
798 */
799 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
800 free_extent_map(em);
801 em = search_extent_mapping(em_tree, 0, 0);
802 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
803 alloc_hint = em->block_start;
804 if (em)
805 free_extent_map(em);
806 } else {
807 alloc_hint = em->block_start;
808 free_extent_map(em);
809 }
810 }
811 read_unlock(&em_tree->lock);
812
813 return alloc_hint;
814}
815
Chris Mason771ed682008-11-06 22:02:51 -0500816/*
817 * when extent_io.c finds a delayed allocation range in the file,
818 * the call backs end up in this code. The basic idea is to
819 * allocate extents on disk for the range, and create ordered data structs
820 * in ram to track those extents.
821 *
822 * locked_page is the page that writepage had locked already. We use
823 * it to make sure we don't do extra locks or unlocks.
824 *
825 * *page_started is set to one if we unlock locked_page and do everything
826 * required to start IO on it. It may be clean and already done with
827 * IO when we return.
828 */
Josef Bacik00361582013-08-14 14:02:47 -0400829static noinline int cow_file_range(struct inode *inode,
830 struct page *locked_page,
831 u64 start, u64 end, int *page_started,
832 unsigned long *nr_written,
833 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500834{
Josef Bacik00361582013-08-14 14:02:47 -0400835 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500836 u64 alloc_hint = 0;
837 u64 num_bytes;
838 unsigned long ram_size;
839 u64 disk_num_bytes;
840 u64 cur_alloc_size;
841 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500842 struct btrfs_key ins;
843 struct extent_map *em;
844 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
845 int ret = 0;
846
Liu Bo83eea1f2012-07-10 05:28:39 -0600847 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500848
Qu Wenruofda28322013-02-26 08:10:22 +0000849 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500850 num_bytes = max(blocksize, num_bytes);
851 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500852
Chris Mason4cb53002011-05-24 15:35:30 -0400853 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400854 if (num_bytes < 64 * 1024 &&
855 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400856 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400857
Chris Mason771ed682008-11-06 22:02:51 -0500858 if (start == 0) {
859 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400860 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
861 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500862 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400863 extent_clear_unlock_delalloc(inode, start, end, NULL,
864 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400865 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400866 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
867 PAGE_END_WRITEBACK);
Yan, Zhengc21677542009-11-12 09:34:21 +0000868
Chris Mason771ed682008-11-06 22:02:51 -0500869 *nr_written = *nr_written +
870 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
871 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500872 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100873 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100874 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500875 }
876 }
Chris Masonc8b97812008-10-29 14:49:59 -0400877
878 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200879 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400880
Josef Bacik4b46fce2010-05-23 11:00:55 -0400881 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400882 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400883
Chris Masond3977122009-01-05 21:25:51 -0500884 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400885 unsigned long op;
886
Josef Bacik287a0ab2010-03-19 18:07:23 +0000887 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400888 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500889 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500890 &ins, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400891 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100892 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500893
David Sterba172ddd62011-04-21 00:48:27 +0200894 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000895 if (!em) {
896 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000897 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000898 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400899 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500900 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500901 ram_size = ins.offset;
902 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400903 em->mod_start = em->start;
904 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400905
Chris Masone6dcd2d2008-07-17 12:53:50 -0400906 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400907 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500908 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400909 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400910 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400911 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400912 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400913
Chris Masond3977122009-01-05 21:25:51 -0500914 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400915 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400916 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400917 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400918 if (ret != -EEXIST) {
919 free_extent_map(em);
920 break;
921 }
922 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400923 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400924 }
Liu Boace68ba2013-04-22 10:53:47 +0000925 if (ret)
926 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400927
Chris Mason98d20f62008-04-14 09:46:10 -0400928 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400929 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500930 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000931 if (ret)
932 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400933
Yan Zheng17d217f2008-12-12 10:03:38 -0500934 if (root->root_key.objectid ==
935 BTRFS_DATA_RELOC_TREE_OBJECTID) {
936 ret = btrfs_reloc_clone_csums(inode, start,
937 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -0400938 if (ret)
Liu Boace68ba2013-04-22 10:53:47 +0000939 goto out_reserve;
Yan Zheng17d217f2008-12-12 10:03:38 -0500940 }
941
Chris Masond3977122009-01-05 21:25:51 -0500942 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400943 break;
Chris Masond3977122009-01-05 21:25:51 -0500944
Chris Masonc8b97812008-10-29 14:49:59 -0400945 /* we're not doing compressed IO, don't unlock the first
946 * page (which the caller expects to stay locked), don't
947 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400948 *
949 * Do set the Private2 bit so we know this page was properly
950 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400951 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400952 op = unlock ? PAGE_UNLOCK : 0;
953 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -0400954
Josef Bacikc2790a22013-07-29 11:20:47 -0400955 extent_clear_unlock_delalloc(inode, start,
956 start + ram_size - 1, locked_page,
957 EXTENT_LOCKED | EXTENT_DELALLOC,
958 op);
Chris Masonc8b97812008-10-29 14:49:59 -0400959 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500960 num_bytes -= cur_alloc_size;
961 alloc_hint = ins.objectid + ins.offset;
962 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400963 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100964out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500965 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000966
Liu Boace68ba2013-04-22 10:53:47 +0000967out_reserve:
968 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100969out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -0400970 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -0400971 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
972 EXTENT_DELALLOC | EXTENT_DEFRAG,
973 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
974 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100975 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500976}
Chris Masonc8b97812008-10-29 14:49:59 -0400977
Chris Mason771ed682008-11-06 22:02:51 -0500978/*
979 * work queue call back to started compression on a file and pages
980 */
981static noinline void async_cow_start(struct btrfs_work *work)
982{
983 struct async_cow *async_cow;
984 int num_added = 0;
985 async_cow = container_of(work, struct async_cow, work);
986
987 compress_file_range(async_cow->inode, async_cow->locked_page,
988 async_cow->start, async_cow->end, async_cow,
989 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -0400990 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -0600991 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -0500992 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -0400993 }
Chris Mason771ed682008-11-06 22:02:51 -0500994}
995
996/*
997 * work queue call back to submit previously compressed pages
998 */
999static noinline void async_cow_submit(struct btrfs_work *work)
1000{
1001 struct async_cow *async_cow;
1002 struct btrfs_root *root;
1003 unsigned long nr_pages;
1004
1005 async_cow = container_of(work, struct async_cow, work);
1006
1007 root = async_cow->root;
1008 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1009 PAGE_CACHE_SHIFT;
1010
Josef Bacik66657b32012-08-01 15:36:24 -04001011 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001012 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001013 waitqueue_active(&root->fs_info->async_submit_wait))
1014 wake_up(&root->fs_info->async_submit_wait);
1015
Chris Masond3977122009-01-05 21:25:51 -05001016 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001017 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001018}
Chris Masonc8b97812008-10-29 14:49:59 -04001019
Chris Mason771ed682008-11-06 22:02:51 -05001020static noinline void async_cow_free(struct btrfs_work *work)
1021{
1022 struct async_cow *async_cow;
1023 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001024 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001025 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001026 kfree(async_cow);
1027}
1028
1029static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1030 u64 start, u64 end, int *page_started,
1031 unsigned long *nr_written)
1032{
1033 struct async_cow *async_cow;
1034 struct btrfs_root *root = BTRFS_I(inode)->root;
1035 unsigned long nr_pages;
1036 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001037 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001038
Chris Masona3429ab2009-10-08 12:30:20 -04001039 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1040 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001041 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001042 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001043 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001044 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001045 async_cow->root = root;
1046 async_cow->locked_page = locked_page;
1047 async_cow->start = start;
1048
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001049 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001050 cur_end = end;
1051 else
1052 cur_end = min(end, start + 512 * 1024 - 1);
1053
1054 async_cow->end = cur_end;
1055 INIT_LIST_HEAD(&async_cow->extents);
1056
1057 async_cow->work.func = async_cow_start;
1058 async_cow->work.ordered_func = async_cow_submit;
1059 async_cow->work.ordered_free = async_cow_free;
1060 async_cow->work.flags = 0;
1061
Chris Mason771ed682008-11-06 22:02:51 -05001062 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1063 PAGE_CACHE_SHIFT;
1064 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1065
1066 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1067 &async_cow->work);
1068
1069 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1070 wait_event(root->fs_info->async_submit_wait,
1071 (atomic_read(&root->fs_info->async_delalloc_pages) <
1072 limit));
1073 }
1074
Chris Masond3977122009-01-05 21:25:51 -05001075 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001076 atomic_read(&root->fs_info->async_delalloc_pages)) {
1077 wait_event(root->fs_info->async_submit_wait,
1078 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1079 0));
1080 }
1081
1082 *nr_written += nr_pages;
1083 start = cur_end + 1;
1084 }
1085 *page_started = 1;
1086 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001087}
1088
Chris Masond3977122009-01-05 21:25:51 -05001089static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001090 u64 bytenr, u64 num_bytes)
1091{
1092 int ret;
1093 struct btrfs_ordered_sum *sums;
1094 LIST_HEAD(list);
1095
Yan Zheng07d400a2009-01-06 11:42:00 -05001096 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001097 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001098 if (ret == 0 && list_empty(&list))
1099 return 0;
1100
1101 while (!list_empty(&list)) {
1102 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1103 list_del(&sums->list);
1104 kfree(sums);
1105 }
1106 return 1;
1107}
1108
Chris Masond352ac62008-09-29 15:18:18 -04001109/*
1110 * when nowcow writeback call back. This checks for snapshots or COW copies
1111 * of the extents that exist in the file, and COWs the file as required.
1112 *
1113 * If no cow copies or snapshots exist, we write directly to the existing
1114 * blocks on disk
1115 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001116static noinline int run_delalloc_nocow(struct inode *inode,
1117 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001118 u64 start, u64 end, int *page_started, int force,
1119 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001120{
Chris Masonbe20aa92007-12-17 20:14:01 -05001121 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001122 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001123 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001124 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001125 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001126 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001127 u64 cow_start;
1128 u64 cur_offset;
1129 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001130 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001131 u64 disk_bytenr;
1132 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001133 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001134 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001135 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001136 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001137 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001138 int nocow;
1139 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001140 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001141 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001142
1143 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001144 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001145 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1146 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001147 EXTENT_DO_ACCOUNTING |
1148 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001149 PAGE_CLEAR_DIRTY |
1150 PAGE_SET_WRITEBACK |
1151 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001152 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001153 }
Li Zefan82d59022011-04-20 10:33:24 +08001154
Liu Bo83eea1f2012-07-10 05:28:39 -06001155 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001156
1157 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001158 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001159 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001160 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001161
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001162 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001163 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1164 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001165 EXTENT_DO_ACCOUNTING |
1166 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001167 PAGE_CLEAR_DIRTY |
1168 PAGE_SET_WRITEBACK |
1169 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001170 btrfs_free_path(path);
1171 return PTR_ERR(trans);
1172 }
1173
Josef Bacik74b21072011-04-13 12:02:53 -04001174 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001175
Yan Zheng80ff3852008-10-30 14:20:02 -04001176 cow_start = (u64)-1;
1177 cur_offset = start;
1178 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001179 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001180 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001181 if (ret < 0) {
1182 btrfs_abort_transaction(trans, root, ret);
1183 goto error;
1184 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001185 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1186 leaf = path->nodes[0];
1187 btrfs_item_key_to_cpu(leaf, &found_key,
1188 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001189 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001190 found_key.type == BTRFS_EXTENT_DATA_KEY)
1191 path->slots[0]--;
1192 }
1193 check_prev = 0;
1194next_slot:
1195 leaf = path->nodes[0];
1196 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1197 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001198 if (ret < 0) {
1199 btrfs_abort_transaction(trans, root, ret);
1200 goto error;
1201 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001202 if (ret > 0)
1203 break;
1204 leaf = path->nodes[0];
1205 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001206
Yan Zheng80ff3852008-10-30 14:20:02 -04001207 nocow = 0;
1208 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001209 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001210 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001211
Li Zefan33345d012011-04-20 10:31:50 +08001212 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001213 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1214 found_key.offset > end)
1215 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001216
Yan Zheng80ff3852008-10-30 14:20:02 -04001217 if (found_key.offset > cur_offset) {
1218 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001219 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001220 goto out_check;
1221 }
Chris Masonc31f8832008-01-08 15:46:31 -05001222
Yan Zheng80ff3852008-10-30 14:20:02 -04001223 fi = btrfs_item_ptr(leaf, path->slots[0],
1224 struct btrfs_file_extent_item);
1225 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001226
Josef Bacikcc95bef2013-04-04 14:31:27 -04001227 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001228 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1229 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001230 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001231 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001232 extent_end = found_key.offset +
1233 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001234 disk_num_bytes =
1235 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001236 if (extent_end <= start) {
1237 path->slots[0]++;
1238 goto next_slot;
1239 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001240 if (disk_bytenr == 0)
1241 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001242 if (btrfs_file_extent_compression(leaf, fi) ||
1243 btrfs_file_extent_encryption(leaf, fi) ||
1244 btrfs_file_extent_other_encoding(leaf, fi))
1245 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001246 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1247 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001248 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001249 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001250 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001251 found_key.offset -
1252 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001253 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001254 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001255 disk_bytenr += cur_offset - found_key.offset;
1256 num_bytes = min(end + 1, extent_end) - cur_offset;
1257 /*
1258 * force cow if csum exists in the range.
1259 * this ensure that csum for a given extent are
1260 * either valid or do not exist.
1261 */
1262 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1263 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001264 nocow = 1;
1265 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1266 extent_end = found_key.offset +
1267 btrfs_file_extent_inline_len(leaf, fi);
1268 extent_end = ALIGN(extent_end, root->sectorsize);
1269 } else {
1270 BUG_ON(1);
1271 }
1272out_check:
1273 if (extent_end <= start) {
1274 path->slots[0]++;
1275 goto next_slot;
1276 }
1277 if (!nocow) {
1278 if (cow_start == (u64)-1)
1279 cow_start = cur_offset;
1280 cur_offset = extent_end;
1281 if (cur_offset > end)
1282 break;
1283 path->slots[0]++;
1284 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001285 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001286
David Sterbab3b4aa72011-04-21 01:20:15 +02001287 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001288 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001289 ret = cow_file_range(inode, locked_page,
1290 cow_start, found_key.offset - 1,
1291 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001292 if (ret) {
1293 btrfs_abort_transaction(trans, root, ret);
1294 goto error;
1295 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001296 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001297 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001298
Yan Zhengd899e052008-10-30 14:25:28 -04001299 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1300 struct extent_map *em;
1301 struct extent_map_tree *em_tree;
1302 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001303 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001304 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001305 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001306 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001307 em->len = num_bytes;
1308 em->block_len = num_bytes;
1309 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001310 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001311 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001312 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001313 em->mod_start = em->start;
1314 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001315 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001316 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001317 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001318 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001319 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001320 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001321 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001322 if (ret != -EEXIST) {
1323 free_extent_map(em);
1324 break;
1325 }
1326 btrfs_drop_extent_cache(inode, em->start,
1327 em->start + em->len - 1, 0);
1328 }
1329 type = BTRFS_ORDERED_PREALLOC;
1330 } else {
1331 type = BTRFS_ORDERED_NOCOW;
1332 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001333
1334 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001335 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001336 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001337
Yan, Zhengefa56462010-05-16 10:49:59 -04001338 if (root->root_key.objectid ==
1339 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1340 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1341 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001342 if (ret) {
1343 btrfs_abort_transaction(trans, root, ret);
1344 goto error;
1345 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001346 }
1347
Josef Bacikc2790a22013-07-29 11:20:47 -04001348 extent_clear_unlock_delalloc(inode, cur_offset,
1349 cur_offset + num_bytes - 1,
1350 locked_page, EXTENT_LOCKED |
1351 EXTENT_DELALLOC, PAGE_UNLOCK |
1352 PAGE_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001353 cur_offset = extent_end;
1354 if (cur_offset > end)
1355 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001356 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001357 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001358
Josef Bacik17ca04a2012-05-31 15:58:55 -04001359 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001360 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001361 cur_offset = end;
1362 }
1363
Yan Zheng80ff3852008-10-30 14:20:02 -04001364 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001365 ret = cow_file_range(inode, locked_page, cow_start, end,
1366 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001367 if (ret) {
1368 btrfs_abort_transaction(trans, root, ret);
1369 goto error;
1370 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001371 }
1372
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001373error:
Miao Xiea698d0752012-09-20 01:51:59 -06001374 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001375 if (!ret)
1376 ret = err;
1377
Josef Bacik17ca04a2012-05-31 15:58:55 -04001378 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001379 extent_clear_unlock_delalloc(inode, cur_offset, end,
1380 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001381 EXTENT_DELALLOC | EXTENT_DEFRAG |
1382 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1383 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001384 PAGE_SET_WRITEBACK |
1385 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001386 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001387 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001388}
1389
Chris Masond352ac62008-09-29 15:18:18 -04001390/*
1391 * extent_io.c call back to do delayed allocation processing
1392 */
Chris Masonc8b97812008-10-29 14:49:59 -04001393static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001394 u64 start, u64 end, int *page_started,
1395 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001396{
Chris Masonbe20aa92007-12-17 20:14:01 -05001397 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001398 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001399
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001400 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001401 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001402 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001403 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001404 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001405 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001406 } else if (!btrfs_test_opt(root, COMPRESS) &&
1407 !(BTRFS_I(inode)->force_compress) &&
1408 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001409 ret = cow_file_range(inode, locked_page, start, end,
1410 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001411 } else {
1412 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1413 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001414 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001415 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001416 }
Chris Masonb888db22007-08-27 16:49:44 -04001417 return ret;
1418}
1419
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001420static void btrfs_split_extent_hook(struct inode *inode,
1421 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001422{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001423 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001424 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001425 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001426
Josef Bacik9e0baf62011-07-15 15:16:44 +00001427 spin_lock(&BTRFS_I(inode)->lock);
1428 BTRFS_I(inode)->outstanding_extents++;
1429 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001430}
1431
1432/*
1433 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1434 * extents so we can keep track of new extents that are just merged onto old
1435 * extents, such as when we are doing sequential writes, so we can properly
1436 * account for the metadata space we'll need.
1437 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001438static void btrfs_merge_extent_hook(struct inode *inode,
1439 struct extent_state *new,
1440 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001441{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001442 /* not delalloc, ignore it */
1443 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001444 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001445
Josef Bacik9e0baf62011-07-15 15:16:44 +00001446 spin_lock(&BTRFS_I(inode)->lock);
1447 BTRFS_I(inode)->outstanding_extents--;
1448 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001449}
1450
Miao Xieeb73c1b2013-05-15 07:48:22 +00001451static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1452 struct inode *inode)
1453{
1454 spin_lock(&root->delalloc_lock);
1455 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1456 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1457 &root->delalloc_inodes);
1458 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1459 &BTRFS_I(inode)->runtime_flags);
1460 root->nr_delalloc_inodes++;
1461 if (root->nr_delalloc_inodes == 1) {
1462 spin_lock(&root->fs_info->delalloc_root_lock);
1463 BUG_ON(!list_empty(&root->delalloc_root));
1464 list_add_tail(&root->delalloc_root,
1465 &root->fs_info->delalloc_roots);
1466 spin_unlock(&root->fs_info->delalloc_root_lock);
1467 }
1468 }
1469 spin_unlock(&root->delalloc_lock);
1470}
1471
1472static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1473 struct inode *inode)
1474{
1475 spin_lock(&root->delalloc_lock);
1476 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1477 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1478 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1479 &BTRFS_I(inode)->runtime_flags);
1480 root->nr_delalloc_inodes--;
1481 if (!root->nr_delalloc_inodes) {
1482 spin_lock(&root->fs_info->delalloc_root_lock);
1483 BUG_ON(list_empty(&root->delalloc_root));
1484 list_del_init(&root->delalloc_root);
1485 spin_unlock(&root->fs_info->delalloc_root_lock);
1486 }
1487 }
1488 spin_unlock(&root->delalloc_lock);
1489}
1490
Chris Masond352ac62008-09-29 15:18:18 -04001491/*
1492 * extent_io.c set_bit_hook, used to track delayed allocation
1493 * bytes in this file, and to maintain the list of inodes that
1494 * have pending delalloc work to be done.
1495 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001496static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001497 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001498{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001499
Chris Mason75eff682008-12-15 15:54:40 -05001500 /*
1501 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001502 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001503 * bit, which is only set or cleared with irqs on
1504 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001505 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001506 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001507 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001508 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001509
Josef Bacik9e0baf62011-07-15 15:16:44 +00001510 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001511 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001512 } else {
1513 spin_lock(&BTRFS_I(inode)->lock);
1514 BTRFS_I(inode)->outstanding_extents++;
1515 spin_unlock(&BTRFS_I(inode)->lock);
1516 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001517
Miao Xie963d6782013-01-29 10:10:51 +00001518 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1519 root->fs_info->delalloc_batch);
Miao Xiedf0af1a52013-01-29 10:11:59 +00001520 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001521 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a52013-01-29 10:11:59 +00001522 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001523 &BTRFS_I(inode)->runtime_flags))
1524 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a52013-01-29 10:11:59 +00001525 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001526 }
Chris Mason291d6732008-01-29 15:55:23 -05001527}
1528
Chris Masond352ac62008-09-29 15:18:18 -04001529/*
1530 * extent_io.c clear_bit_hook, see set_bit_hook for why
1531 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001532static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001533 struct extent_state *state,
1534 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001535{
Chris Mason75eff682008-12-15 15:54:40 -05001536 /*
1537 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001538 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001539 * bit, which is only set or cleared with irqs on
1540 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001541 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001542 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001543 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001544 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001545
Josef Bacik9e0baf62011-07-15 15:16:44 +00001546 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001547 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001548 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1549 spin_lock(&BTRFS_I(inode)->lock);
1550 BTRFS_I(inode)->outstanding_extents--;
1551 spin_unlock(&BTRFS_I(inode)->lock);
1552 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001553
1554 if (*bits & EXTENT_DO_ACCOUNTING)
1555 btrfs_delalloc_release_metadata(inode, len);
1556
Josef Bacik0cb59c92010-07-02 12:14:14 -04001557 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001558 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001559 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001560
Miao Xie963d6782013-01-29 10:10:51 +00001561 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1562 root->fs_info->delalloc_batch);
Miao Xiedf0af1a52013-01-29 10:11:59 +00001563 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001564 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001565 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a52013-01-29 10:11:59 +00001566 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001567 &BTRFS_I(inode)->runtime_flags))
1568 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a52013-01-29 10:11:59 +00001569 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001570 }
Chris Mason291d6732008-01-29 15:55:23 -05001571}
1572
Chris Masond352ac62008-09-29 15:18:18 -04001573/*
1574 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1575 * we don't create bios that span stripes or chunks
1576 */
David Woodhouse64a16702009-07-15 23:29:37 +01001577int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001578 size_t size, struct bio *bio,
1579 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001580{
1581 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001582 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001583 u64 length = 0;
1584 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001585 int ret;
1586
Chris Mason771ed682008-11-06 22:02:51 -05001587 if (bio_flags & EXTENT_BIO_COMPRESSED)
1588 return 0;
1589
Chris Masonf2d8d742008-04-21 10:03:05 -04001590 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001591 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001592 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001593 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001594 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001595 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001596 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001597 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001598 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001599}
1600
Chris Masond352ac62008-09-29 15:18:18 -04001601/*
1602 * in order to insert checksums into the metadata in large chunks,
1603 * we wait until bio submission time. All the pages in the bio are
1604 * checksummed and sums are attached onto the ordered extent record.
1605 *
1606 * At IO completion time the cums attached on the ordered extent record
1607 * are inserted into the btree
1608 */
Chris Masond3977122009-01-05 21:25:51 -05001609static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1610 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001611 unsigned long bio_flags,
1612 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001613{
Chris Mason065631f2008-02-20 12:07:25 -05001614 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001615 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001616
Chris Masond20f7042008-12-08 16:58:54 -05001617 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001618 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001619 return 0;
1620}
Chris Masone0156402008-04-16 11:15:20 -04001621
Chris Mason4a69a412008-11-06 22:03:00 -05001622/*
1623 * in order to insert checksums into the metadata in large chunks,
1624 * we wait until bio submission time. All the pages in the bio are
1625 * checksummed and sums are attached onto the ordered extent record.
1626 *
1627 * At IO completion time the cums attached on the ordered extent record
1628 * are inserted into the btree
1629 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001630static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001631 int mirror_num, unsigned long bio_flags,
1632 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001633{
1634 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001635 int ret;
1636
1637 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1638 if (ret)
1639 bio_endio(bio, ret);
1640 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001641}
1642
Chris Masond352ac62008-09-29 15:18:18 -04001643/*
Chris Masoncad321a2008-12-17 14:51:42 -05001644 * extent_io.c submission hook. This does the right thing for csum calculation
1645 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001646 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001647static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001648 int mirror_num, unsigned long bio_flags,
1649 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001650{
1651 struct btrfs_root *root = BTRFS_I(inode)->root;
1652 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001653 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001654 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001655 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001656
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001657 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001658
Liu Bo83eea1f2012-07-10 05:28:39 -06001659 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001660 metadata = 2;
1661
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001662 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001663 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1664 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001665 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001666
Chris Masond20f7042008-12-08 16:58:54 -05001667 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001668 ret = btrfs_submit_compressed_read(inode, bio,
1669 mirror_num,
1670 bio_flags);
1671 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001672 } else if (!skip_sum) {
1673 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1674 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001675 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001676 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001677 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001678 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001679 /* csum items have already been cloned */
1680 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1681 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001682 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001683 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001684 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001685 bio_flags, bio_offset,
1686 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001687 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001688 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001689 } else if (!skip_sum) {
1690 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1691 if (ret)
1692 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001693 }
1694
Chris Mason0b86a832008-03-24 15:01:56 -04001695mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001696 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1697
1698out:
1699 if (ret < 0)
1700 bio_endio(bio, ret);
1701 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001702}
Chris Mason6885f302008-02-20 16:11:05 -05001703
Chris Masond352ac62008-09-29 15:18:18 -04001704/*
1705 * given a list of ordered sums record them in the inode. This happens
1706 * at IO completion time based on sums calculated at bio submission time.
1707 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001708static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001709 struct inode *inode, u64 file_offset,
1710 struct list_head *list)
1711{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001712 struct btrfs_ordered_sum *sum;
1713
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001714 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001715 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001716 btrfs_csum_file_blocks(trans,
1717 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001718 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001719 }
1720 return 0;
1721}
1722
Josef Bacik2ac55d42010-02-03 19:33:23 +00001723int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1724 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001725{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001726 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001727 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001728 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001729}
1730
Chris Masond352ac62008-09-29 15:18:18 -04001731/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001732struct btrfs_writepage_fixup {
1733 struct page *page;
1734 struct btrfs_work work;
1735};
1736
Christoph Hellwigb2950862008-12-02 09:54:17 -05001737static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001738{
1739 struct btrfs_writepage_fixup *fixup;
1740 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001741 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001742 struct page *page;
1743 struct inode *inode;
1744 u64 page_start;
1745 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001746 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001747
1748 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1749 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001750again:
Chris Mason247e7432008-07-17 12:53:51 -04001751 lock_page(page);
1752 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1753 ClearPageChecked(page);
1754 goto out_page;
1755 }
1756
1757 inode = page->mapping->host;
1758 page_start = page_offset(page);
1759 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1760
Josef Bacik2ac55d42010-02-03 19:33:23 +00001761 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001762 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001763
1764 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001765 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001766 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001767
1768 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1769 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001770 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1771 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001772 unlock_page(page);
1773 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001774 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001775 goto again;
1776 }
Chris Mason247e7432008-07-17 12:53:51 -04001777
Jeff Mahoney87826df2012-02-15 16:23:57 +01001778 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1779 if (ret) {
1780 mapping_set_error(page->mapping, ret);
1781 end_extent_writepage(page, ret, page_start, page_end);
1782 ClearPageChecked(page);
1783 goto out;
1784 }
1785
Josef Bacik2ac55d42010-02-03 19:33:23 +00001786 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001787 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001788 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001789out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001790 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1791 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001792out_page:
1793 unlock_page(page);
1794 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001795 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001796}
1797
1798/*
1799 * There are a few paths in the higher layers of the kernel that directly
1800 * set the page dirty bit without asking the filesystem if it is a
1801 * good idea. This causes problems because we want to make sure COW
1802 * properly happens and the data=ordered rules are followed.
1803 *
Chris Masonc8b97812008-10-29 14:49:59 -04001804 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001805 * hasn't been properly setup for IO. We kick off an async process
1806 * to fix it up. The async helper will wait for ordered extents, set
1807 * the delalloc bit and make it safe to write the page.
1808 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001809static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001810{
1811 struct inode *inode = page->mapping->host;
1812 struct btrfs_writepage_fixup *fixup;
1813 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001814
Chris Mason8b62b722009-09-02 16:53:46 -04001815 /* this page is properly in the ordered list */
1816 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001817 return 0;
1818
1819 if (PageChecked(page))
1820 return -EAGAIN;
1821
1822 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1823 if (!fixup)
1824 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001825
Chris Mason247e7432008-07-17 12:53:51 -04001826 SetPageChecked(page);
1827 page_cache_get(page);
1828 fixup->work.func = btrfs_writepage_fixup_worker;
1829 fixup->page = page;
1830 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001831 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001832}
1833
Yan Zhengd899e052008-10-30 14:25:28 -04001834static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1835 struct inode *inode, u64 file_pos,
1836 u64 disk_bytenr, u64 disk_num_bytes,
1837 u64 num_bytes, u64 ram_bytes,
1838 u8 compression, u8 encryption,
1839 u16 other_encoding, int extent_type)
1840{
1841 struct btrfs_root *root = BTRFS_I(inode)->root;
1842 struct btrfs_file_extent_item *fi;
1843 struct btrfs_path *path;
1844 struct extent_buffer *leaf;
1845 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001846 int ret;
1847
1848 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001849 if (!path)
1850 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001851
Chris Masonb9473432009-03-13 11:00:37 -04001852 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001853
1854 /*
1855 * we may be replacing one extent in the tree with another.
1856 * The new extent is pinned in the extent map, and we don't want
1857 * to drop it from the cache until it is completely in the btree.
1858 *
1859 * So, tell btrfs_drop_extents to leave this extent in the cache.
1860 * the caller is expected to unpin it and allow it to be merged
1861 * with the others.
1862 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001863 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001864 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001865 if (ret)
1866 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001867
Li Zefan33345d012011-04-20 10:31:50 +08001868 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001869 ins.offset = file_pos;
1870 ins.type = BTRFS_EXTENT_DATA_KEY;
1871 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001872 if (ret)
1873 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001874 leaf = path->nodes[0];
1875 fi = btrfs_item_ptr(leaf, path->slots[0],
1876 struct btrfs_file_extent_item);
1877 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1878 btrfs_set_file_extent_type(leaf, fi, extent_type);
1879 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1880 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1881 btrfs_set_file_extent_offset(leaf, fi, 0);
1882 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1883 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1884 btrfs_set_file_extent_compression(leaf, fi, compression);
1885 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1886 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001887
Yan Zhengd899e052008-10-30 14:25:28 -04001888 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001889 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001890
1891 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001892
1893 ins.objectid = disk_bytenr;
1894 ins.offset = disk_num_bytes;
1895 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001896 ret = btrfs_alloc_reserved_file_extent(trans, root,
1897 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001898 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001899out:
Yan Zhengd899e052008-10-30 14:25:28 -04001900 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001901
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001902 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001903}
1904
Liu Bo38c227d2013-01-29 03:18:40 +00001905/* snapshot-aware defrag */
1906struct sa_defrag_extent_backref {
1907 struct rb_node node;
1908 struct old_sa_defrag_extent *old;
1909 u64 root_id;
1910 u64 inum;
1911 u64 file_pos;
1912 u64 extent_offset;
1913 u64 num_bytes;
1914 u64 generation;
1915};
1916
1917struct old_sa_defrag_extent {
1918 struct list_head list;
1919 struct new_sa_defrag_extent *new;
1920
1921 u64 extent_offset;
1922 u64 bytenr;
1923 u64 offset;
1924 u64 len;
1925 int count;
1926};
1927
1928struct new_sa_defrag_extent {
1929 struct rb_root root;
1930 struct list_head head;
1931 struct btrfs_path *path;
1932 struct inode *inode;
1933 u64 file_pos;
1934 u64 len;
1935 u64 bytenr;
1936 u64 disk_len;
1937 u8 compress_type;
1938};
1939
1940static int backref_comp(struct sa_defrag_extent_backref *b1,
1941 struct sa_defrag_extent_backref *b2)
1942{
1943 if (b1->root_id < b2->root_id)
1944 return -1;
1945 else if (b1->root_id > b2->root_id)
1946 return 1;
1947
1948 if (b1->inum < b2->inum)
1949 return -1;
1950 else if (b1->inum > b2->inum)
1951 return 1;
1952
1953 if (b1->file_pos < b2->file_pos)
1954 return -1;
1955 else if (b1->file_pos > b2->file_pos)
1956 return 1;
1957
1958 /*
1959 * [------------------------------] ===> (a range of space)
1960 * |<--->| |<---->| =============> (fs/file tree A)
1961 * |<---------------------------->| ===> (fs/file tree B)
1962 *
1963 * A range of space can refer to two file extents in one tree while
1964 * refer to only one file extent in another tree.
1965 *
1966 * So we may process a disk offset more than one time(two extents in A)
1967 * and locate at the same extent(one extent in B), then insert two same
1968 * backrefs(both refer to the extent in B).
1969 */
1970 return 0;
1971}
1972
1973static void backref_insert(struct rb_root *root,
1974 struct sa_defrag_extent_backref *backref)
1975{
1976 struct rb_node **p = &root->rb_node;
1977 struct rb_node *parent = NULL;
1978 struct sa_defrag_extent_backref *entry;
1979 int ret;
1980
1981 while (*p) {
1982 parent = *p;
1983 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
1984
1985 ret = backref_comp(backref, entry);
1986 if (ret < 0)
1987 p = &(*p)->rb_left;
1988 else
1989 p = &(*p)->rb_right;
1990 }
1991
1992 rb_link_node(&backref->node, parent, p);
1993 rb_insert_color(&backref->node, root);
1994}
1995
1996/*
1997 * Note the backref might has changed, and in this case we just return 0.
1998 */
1999static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2000 void *ctx)
2001{
2002 struct btrfs_file_extent_item *extent;
2003 struct btrfs_fs_info *fs_info;
2004 struct old_sa_defrag_extent *old = ctx;
2005 struct new_sa_defrag_extent *new = old->new;
2006 struct btrfs_path *path = new->path;
2007 struct btrfs_key key;
2008 struct btrfs_root *root;
2009 struct sa_defrag_extent_backref *backref;
2010 struct extent_buffer *leaf;
2011 struct inode *inode = new->inode;
2012 int slot;
2013 int ret;
2014 u64 extent_offset;
2015 u64 num_bytes;
2016
2017 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2018 inum == btrfs_ino(inode))
2019 return 0;
2020
2021 key.objectid = root_id;
2022 key.type = BTRFS_ROOT_ITEM_KEY;
2023 key.offset = (u64)-1;
2024
2025 fs_info = BTRFS_I(inode)->root->fs_info;
2026 root = btrfs_read_fs_root_no_name(fs_info, &key);
2027 if (IS_ERR(root)) {
2028 if (PTR_ERR(root) == -ENOENT)
2029 return 0;
2030 WARN_ON(1);
2031 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2032 inum, offset, root_id);
2033 return PTR_ERR(root);
2034 }
2035
2036 key.objectid = inum;
2037 key.type = BTRFS_EXTENT_DATA_KEY;
2038 if (offset > (u64)-1 << 32)
2039 key.offset = 0;
2040 else
2041 key.offset = offset;
2042
2043 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2044 if (ret < 0) {
2045 WARN_ON(1);
2046 return ret;
2047 }
Josef Bacik50f13192013-07-22 12:50:37 -04002048 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002049
2050 while (1) {
2051 cond_resched();
2052
2053 leaf = path->nodes[0];
2054 slot = path->slots[0];
2055
2056 if (slot >= btrfs_header_nritems(leaf)) {
2057 ret = btrfs_next_leaf(root, path);
2058 if (ret < 0) {
2059 goto out;
2060 } else if (ret > 0) {
2061 ret = 0;
2062 goto out;
2063 }
2064 continue;
2065 }
2066
2067 path->slots[0]++;
2068
2069 btrfs_item_key_to_cpu(leaf, &key, slot);
2070
2071 if (key.objectid > inum)
2072 goto out;
2073
2074 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2075 continue;
2076
2077 extent = btrfs_item_ptr(leaf, slot,
2078 struct btrfs_file_extent_item);
2079
2080 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2081 continue;
2082
Liu Boe68afa42013-07-01 22:13:26 +08002083 /*
2084 * 'offset' refers to the exact key.offset,
2085 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2086 * (key.offset - extent_offset).
2087 */
2088 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002089 continue;
2090
Liu Boe68afa42013-07-01 22:13:26 +08002091 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002092 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002093
Liu Bo38c227d2013-01-29 03:18:40 +00002094 if (extent_offset >= old->extent_offset + old->offset +
2095 old->len || extent_offset + num_bytes <=
2096 old->extent_offset + old->offset)
2097 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002098 break;
2099 }
2100
2101 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2102 if (!backref) {
2103 ret = -ENOENT;
2104 goto out;
2105 }
2106
2107 backref->root_id = root_id;
2108 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002109 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002110 backref->num_bytes = num_bytes;
2111 backref->extent_offset = extent_offset;
2112 backref->generation = btrfs_file_extent_generation(leaf, extent);
2113 backref->old = old;
2114 backref_insert(&new->root, backref);
2115 old->count++;
2116out:
2117 btrfs_release_path(path);
2118 WARN_ON(ret);
2119 return ret;
2120}
2121
2122static noinline bool record_extent_backrefs(struct btrfs_path *path,
2123 struct new_sa_defrag_extent *new)
2124{
2125 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2126 struct old_sa_defrag_extent *old, *tmp;
2127 int ret;
2128
2129 new->path = path;
2130
2131 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002132 ret = iterate_inodes_from_logical(old->bytenr +
2133 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002134 path, record_one_backref,
2135 old);
2136 BUG_ON(ret < 0 && ret != -ENOENT);
2137
2138 /* no backref to be processed for this extent */
2139 if (!old->count) {
2140 list_del(&old->list);
2141 kfree(old);
2142 }
2143 }
2144
2145 if (list_empty(&new->head))
2146 return false;
2147
2148 return true;
2149}
2150
2151static int relink_is_mergable(struct extent_buffer *leaf,
2152 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002153 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002154{
Liu Bo116e0022013-08-02 16:30:40 +08002155 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002156 return 0;
2157
2158 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2159 return 0;
2160
Liu Bo116e0022013-08-02 16:30:40 +08002161 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2162 return 0;
2163
2164 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002165 btrfs_file_extent_other_encoding(leaf, fi))
2166 return 0;
2167
2168 return 1;
2169}
2170
2171/*
2172 * Note the backref might has changed, and in this case we just return 0.
2173 */
2174static noinline int relink_extent_backref(struct btrfs_path *path,
2175 struct sa_defrag_extent_backref *prev,
2176 struct sa_defrag_extent_backref *backref)
2177{
2178 struct btrfs_file_extent_item *extent;
2179 struct btrfs_file_extent_item *item;
2180 struct btrfs_ordered_extent *ordered;
2181 struct btrfs_trans_handle *trans;
2182 struct btrfs_fs_info *fs_info;
2183 struct btrfs_root *root;
2184 struct btrfs_key key;
2185 struct extent_buffer *leaf;
2186 struct old_sa_defrag_extent *old = backref->old;
2187 struct new_sa_defrag_extent *new = old->new;
2188 struct inode *src_inode = new->inode;
2189 struct inode *inode;
2190 struct extent_state *cached = NULL;
2191 int ret = 0;
2192 u64 start;
2193 u64 len;
2194 u64 lock_start;
2195 u64 lock_end;
2196 bool merge = false;
2197 int index;
2198
2199 if (prev && prev->root_id == backref->root_id &&
2200 prev->inum == backref->inum &&
2201 prev->file_pos + prev->num_bytes == backref->file_pos)
2202 merge = true;
2203
2204 /* step 1: get root */
2205 key.objectid = backref->root_id;
2206 key.type = BTRFS_ROOT_ITEM_KEY;
2207 key.offset = (u64)-1;
2208
2209 fs_info = BTRFS_I(src_inode)->root->fs_info;
2210 index = srcu_read_lock(&fs_info->subvol_srcu);
2211
2212 root = btrfs_read_fs_root_no_name(fs_info, &key);
2213 if (IS_ERR(root)) {
2214 srcu_read_unlock(&fs_info->subvol_srcu, index);
2215 if (PTR_ERR(root) == -ENOENT)
2216 return 0;
2217 return PTR_ERR(root);
2218 }
Liu Bo38c227d2013-01-29 03:18:40 +00002219
2220 /* step 2: get inode */
2221 key.objectid = backref->inum;
2222 key.type = BTRFS_INODE_ITEM_KEY;
2223 key.offset = 0;
2224
2225 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2226 if (IS_ERR(inode)) {
2227 srcu_read_unlock(&fs_info->subvol_srcu, index);
2228 return 0;
2229 }
2230
2231 srcu_read_unlock(&fs_info->subvol_srcu, index);
2232
2233 /* step 3: relink backref */
2234 lock_start = backref->file_pos;
2235 lock_end = backref->file_pos + backref->num_bytes - 1;
2236 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2237 0, &cached);
2238
2239 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2240 if (ordered) {
2241 btrfs_put_ordered_extent(ordered);
2242 goto out_unlock;
2243 }
2244
2245 trans = btrfs_join_transaction(root);
2246 if (IS_ERR(trans)) {
2247 ret = PTR_ERR(trans);
2248 goto out_unlock;
2249 }
2250
2251 key.objectid = backref->inum;
2252 key.type = BTRFS_EXTENT_DATA_KEY;
2253 key.offset = backref->file_pos;
2254
2255 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2256 if (ret < 0) {
2257 goto out_free_path;
2258 } else if (ret > 0) {
2259 ret = 0;
2260 goto out_free_path;
2261 }
2262
2263 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2264 struct btrfs_file_extent_item);
2265
2266 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2267 backref->generation)
2268 goto out_free_path;
2269
2270 btrfs_release_path(path);
2271
2272 start = backref->file_pos;
2273 if (backref->extent_offset < old->extent_offset + old->offset)
2274 start += old->extent_offset + old->offset -
2275 backref->extent_offset;
2276
2277 len = min(backref->extent_offset + backref->num_bytes,
2278 old->extent_offset + old->offset + old->len);
2279 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2280
2281 ret = btrfs_drop_extents(trans, root, inode, start,
2282 start + len, 1);
2283 if (ret)
2284 goto out_free_path;
2285again:
2286 key.objectid = btrfs_ino(inode);
2287 key.type = BTRFS_EXTENT_DATA_KEY;
2288 key.offset = start;
2289
Liu Boa09a0a72013-03-11 09:20:58 +00002290 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002291 if (merge) {
2292 struct btrfs_file_extent_item *fi;
2293 u64 extent_len;
2294 struct btrfs_key found_key;
2295
2296 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2297 if (ret < 0)
2298 goto out_free_path;
2299
2300 path->slots[0]--;
2301 leaf = path->nodes[0];
2302 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2303
2304 fi = btrfs_item_ptr(leaf, path->slots[0],
2305 struct btrfs_file_extent_item);
2306 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2307
Liu Bo116e0022013-08-02 16:30:40 +08002308 if (extent_len + found_key.offset == start &&
2309 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002310 btrfs_set_file_extent_num_bytes(leaf, fi,
2311 extent_len + len);
2312 btrfs_mark_buffer_dirty(leaf);
2313 inode_add_bytes(inode, len);
2314
2315 ret = 1;
2316 goto out_free_path;
2317 } else {
2318 merge = false;
2319 btrfs_release_path(path);
2320 goto again;
2321 }
2322 }
2323
2324 ret = btrfs_insert_empty_item(trans, root, path, &key,
2325 sizeof(*extent));
2326 if (ret) {
2327 btrfs_abort_transaction(trans, root, ret);
2328 goto out_free_path;
2329 }
2330
2331 leaf = path->nodes[0];
2332 item = btrfs_item_ptr(leaf, path->slots[0],
2333 struct btrfs_file_extent_item);
2334 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2335 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2336 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2337 btrfs_set_file_extent_num_bytes(leaf, item, len);
2338 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2339 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2340 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2341 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2342 btrfs_set_file_extent_encryption(leaf, item, 0);
2343 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2344
2345 btrfs_mark_buffer_dirty(leaf);
2346 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002347 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002348
2349 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2350 new->disk_len, 0,
2351 backref->root_id, backref->inum,
2352 new->file_pos, 0); /* start - extent_offset */
2353 if (ret) {
2354 btrfs_abort_transaction(trans, root, ret);
2355 goto out_free_path;
2356 }
2357
2358 ret = 1;
2359out_free_path:
2360 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002361 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002362 btrfs_end_transaction(trans, root);
2363out_unlock:
2364 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2365 &cached, GFP_NOFS);
2366 iput(inode);
2367 return ret;
2368}
2369
2370static void relink_file_extents(struct new_sa_defrag_extent *new)
2371{
2372 struct btrfs_path *path;
2373 struct old_sa_defrag_extent *old, *tmp;
2374 struct sa_defrag_extent_backref *backref;
2375 struct sa_defrag_extent_backref *prev = NULL;
2376 struct inode *inode;
2377 struct btrfs_root *root;
2378 struct rb_node *node;
2379 int ret;
2380
2381 inode = new->inode;
2382 root = BTRFS_I(inode)->root;
2383
2384 path = btrfs_alloc_path();
2385 if (!path)
2386 return;
2387
2388 if (!record_extent_backrefs(path, new)) {
2389 btrfs_free_path(path);
2390 goto out;
2391 }
2392 btrfs_release_path(path);
2393
2394 while (1) {
2395 node = rb_first(&new->root);
2396 if (!node)
2397 break;
2398 rb_erase(node, &new->root);
2399
2400 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2401
2402 ret = relink_extent_backref(path, prev, backref);
2403 WARN_ON(ret < 0);
2404
2405 kfree(prev);
2406
2407 if (ret == 1)
2408 prev = backref;
2409 else
2410 prev = NULL;
2411 cond_resched();
2412 }
2413 kfree(prev);
2414
2415 btrfs_free_path(path);
2416
2417 list_for_each_entry_safe(old, tmp, &new->head, list) {
2418 list_del(&old->list);
2419 kfree(old);
2420 }
2421out:
2422 atomic_dec(&root->fs_info->defrag_running);
2423 wake_up(&root->fs_info->transaction_wait);
2424
2425 kfree(new);
2426}
2427
2428static struct new_sa_defrag_extent *
2429record_old_file_extents(struct inode *inode,
2430 struct btrfs_ordered_extent *ordered)
2431{
2432 struct btrfs_root *root = BTRFS_I(inode)->root;
2433 struct btrfs_path *path;
2434 struct btrfs_key key;
2435 struct old_sa_defrag_extent *old, *tmp;
2436 struct new_sa_defrag_extent *new;
2437 int ret;
2438
2439 new = kmalloc(sizeof(*new), GFP_NOFS);
2440 if (!new)
2441 return NULL;
2442
2443 new->inode = inode;
2444 new->file_pos = ordered->file_offset;
2445 new->len = ordered->len;
2446 new->bytenr = ordered->start;
2447 new->disk_len = ordered->disk_len;
2448 new->compress_type = ordered->compress_type;
2449 new->root = RB_ROOT;
2450 INIT_LIST_HEAD(&new->head);
2451
2452 path = btrfs_alloc_path();
2453 if (!path)
2454 goto out_kfree;
2455
2456 key.objectid = btrfs_ino(inode);
2457 key.type = BTRFS_EXTENT_DATA_KEY;
2458 key.offset = new->file_pos;
2459
2460 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2461 if (ret < 0)
2462 goto out_free_path;
2463 if (ret > 0 && path->slots[0] > 0)
2464 path->slots[0]--;
2465
2466 /* find out all the old extents for the file range */
2467 while (1) {
2468 struct btrfs_file_extent_item *extent;
2469 struct extent_buffer *l;
2470 int slot;
2471 u64 num_bytes;
2472 u64 offset;
2473 u64 end;
2474 u64 disk_bytenr;
2475 u64 extent_offset;
2476
2477 l = path->nodes[0];
2478 slot = path->slots[0];
2479
2480 if (slot >= btrfs_header_nritems(l)) {
2481 ret = btrfs_next_leaf(root, path);
2482 if (ret < 0)
2483 goto out_free_list;
2484 else if (ret > 0)
2485 break;
2486 continue;
2487 }
2488
2489 btrfs_item_key_to_cpu(l, &key, slot);
2490
2491 if (key.objectid != btrfs_ino(inode))
2492 break;
2493 if (key.type != BTRFS_EXTENT_DATA_KEY)
2494 break;
2495 if (key.offset >= new->file_pos + new->len)
2496 break;
2497
2498 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2499
2500 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2501 if (key.offset + num_bytes < new->file_pos)
2502 goto next;
2503
2504 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2505 if (!disk_bytenr)
2506 goto next;
2507
2508 extent_offset = btrfs_file_extent_offset(l, extent);
2509
2510 old = kmalloc(sizeof(*old), GFP_NOFS);
2511 if (!old)
2512 goto out_free_list;
2513
2514 offset = max(new->file_pos, key.offset);
2515 end = min(new->file_pos + new->len, key.offset + num_bytes);
2516
2517 old->bytenr = disk_bytenr;
2518 old->extent_offset = extent_offset;
2519 old->offset = offset - key.offset;
2520 old->len = end - offset;
2521 old->new = new;
2522 old->count = 0;
2523 list_add_tail(&old->list, &new->head);
2524next:
2525 path->slots[0]++;
2526 cond_resched();
2527 }
2528
2529 btrfs_free_path(path);
2530 atomic_inc(&root->fs_info->defrag_running);
2531
2532 return new;
2533
2534out_free_list:
2535 list_for_each_entry_safe(old, tmp, &new->head, list) {
2536 list_del(&old->list);
2537 kfree(old);
2538 }
2539out_free_path:
2540 btrfs_free_path(path);
2541out_kfree:
2542 kfree(new);
2543 return NULL;
2544}
2545
Chris Mason5d13a982009-03-13 11:41:46 -04002546/*
2547 * helper function for btrfs_finish_ordered_io, this
2548 * just reads in some of the csum leaves to prime them into ram
2549 * before we start the transaction. It limits the amount of btree
2550 * reads required while inside the transaction.
2551 */
Chris Masond352ac62008-09-29 15:18:18 -04002552/* as ordered data IO finishes, this gets called so we can finish
2553 * an ordered extent if the range of bytes in the file it covers are
2554 * fully written.
2555 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002556static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002557{
Josef Bacik5fd02042012-05-02 14:00:54 -04002558 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002559 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002560 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002561 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002562 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002563 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002564 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002565 int ret = 0;
2566 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002567 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002568 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002569
Liu Bo83eea1f2012-07-10 05:28:39 -06002570 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002571
Josef Bacik5fd02042012-05-02 14:00:54 -04002572 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2573 ret = -EIO;
2574 goto out;
2575 }
2576
Josef Bacik77cef2e2013-08-29 13:57:21 -04002577 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2578 truncated = true;
2579 logical_len = ordered_extent->truncated_len;
2580 /* Truncated the entire extent, don't bother adding */
2581 if (!logical_len)
2582 goto out;
2583 }
2584
Yan, Zhengc21677542009-11-12 09:34:21 +00002585 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002586 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002587 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2588 if (nolock)
2589 trans = btrfs_join_transaction_nolock(root);
2590 else
2591 trans = btrfs_join_transaction(root);
2592 if (IS_ERR(trans)) {
2593 ret = PTR_ERR(trans);
2594 trans = NULL;
2595 goto out;
Yan, Zhengc21677542009-11-12 09:34:21 +00002596 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002597 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2598 ret = btrfs_update_inode_fallback(trans, root, inode);
2599 if (ret) /* -ENOMEM or corruption */
2600 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc21677542009-11-12 09:34:21 +00002601 goto out;
2602 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002603
Josef Bacik2ac55d42010-02-03 19:33:23 +00002604 lock_extent_bits(io_tree, ordered_extent->file_offset,
2605 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002606 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002607
Liu Bo38c227d2013-01-29 03:18:40 +00002608 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2609 ordered_extent->file_offset + ordered_extent->len - 1,
2610 EXTENT_DEFRAG, 1, cached_state);
2611 if (ret) {
2612 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2613 if (last_snapshot >= BTRFS_I(inode)->generation)
2614 /* the inode is shared */
2615 new = record_old_file_extents(inode, ordered_extent);
2616
2617 clear_extent_bit(io_tree, ordered_extent->file_offset,
2618 ordered_extent->file_offset + ordered_extent->len - 1,
2619 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2620 }
2621
Josef Bacik0cb59c92010-07-02 12:14:14 -04002622 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04002623 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002624 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04002625 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002626 if (IS_ERR(trans)) {
2627 ret = PTR_ERR(trans);
2628 trans = NULL;
2629 goto out_unlock;
2630 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002631 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc21677542009-11-12 09:34:21 +00002632
Chris Masonc8b97812008-10-29 14:49:59 -04002633 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002634 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002635 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002636 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002637 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002638 ordered_extent->file_offset,
2639 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002640 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002641 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002642 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002643 ret = insert_reserved_file_extent(trans, inode,
2644 ordered_extent->file_offset,
2645 ordered_extent->start,
2646 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002647 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002648 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002649 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002650 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002651 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2652 ordered_extent->file_offset, ordered_extent->len,
2653 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002654 if (ret < 0) {
2655 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002656 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002657 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002658
Chris Masone6dcd2d2008-07-17 12:53:50 -04002659 add_pending_csums(trans, inode, ordered_extent->file_offset,
2660 &ordered_extent->list);
2661
Josef Bacik6c760c02012-11-09 10:53:21 -05002662 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2663 ret = btrfs_update_inode_fallback(trans, root, inode);
2664 if (ret) { /* -ENOMEM or corruption */
2665 btrfs_abort_transaction(trans, root, ret);
2666 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002667 }
2668 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002669out_unlock:
2670 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2671 ordered_extent->file_offset +
2672 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc21677542009-11-12 09:34:21 +00002673out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002674 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002675 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002676 if (trans)
2677 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002678
Josef Bacik77cef2e2013-08-29 13:57:21 -04002679 if (ret || truncated) {
2680 u64 start, end;
2681
2682 if (truncated)
2683 start = ordered_extent->file_offset + logical_len;
2684 else
2685 start = ordered_extent->file_offset;
2686 end = ordered_extent->file_offset + ordered_extent->len - 1;
2687 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2688
2689 /* Drop the cache for the part of the extent we didn't write. */
2690 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002691
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002692 /*
2693 * If the ordered extent had an IOERR or something else went
2694 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002695 * back to the allocator. We only free the extent in the
2696 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002697 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002698 if ((ret || !logical_len) &&
2699 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002700 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2701 btrfs_free_reserved_extent(root, ordered_extent->start,
2702 ordered_extent->disk_len);
2703 }
2704
2705
Josef Bacik5fd02042012-05-02 14:00:54 -04002706 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002707 * This needs to be done to make sure anybody waiting knows we are done
2708 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002709 */
2710 btrfs_remove_ordered_extent(inode, ordered_extent);
2711
Liu Bo38c227d2013-01-29 03:18:40 +00002712 /* for snapshot-aware defrag */
2713 if (new)
2714 relink_file_extents(new);
2715
Chris Masone6dcd2d2008-07-17 12:53:50 -04002716 /* once for us */
2717 btrfs_put_ordered_extent(ordered_extent);
2718 /* once for the tree */
2719 btrfs_put_ordered_extent(ordered_extent);
2720
Josef Bacik5fd02042012-05-02 14:00:54 -04002721 return ret;
2722}
2723
2724static void finish_ordered_fn(struct btrfs_work *work)
2725{
2726 struct btrfs_ordered_extent *ordered_extent;
2727 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2728 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002729}
2730
Christoph Hellwigb2950862008-12-02 09:54:17 -05002731static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002732 struct extent_state *state, int uptodate)
2733{
Josef Bacik5fd02042012-05-02 14:00:54 -04002734 struct inode *inode = page->mapping->host;
2735 struct btrfs_root *root = BTRFS_I(inode)->root;
2736 struct btrfs_ordered_extent *ordered_extent = NULL;
2737 struct btrfs_workers *workers;
2738
liubo1abe9b82011-03-24 11:18:59 +00002739 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2740
Chris Mason8b62b722009-09-02 16:53:46 -04002741 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002742 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2743 end - start + 1, uptodate))
2744 return 0;
2745
2746 ordered_extent->work.func = finish_ordered_fn;
2747 ordered_extent->work.flags = 0;
2748
Liu Bo83eea1f2012-07-10 05:28:39 -06002749 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002750 workers = &root->fs_info->endio_freespace_worker;
2751 else
2752 workers = &root->fs_info->endio_write_workers;
2753 btrfs_queue_worker(workers, &ordered_extent->work);
2754
2755 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002756}
2757
Chris Masond352ac62008-09-29 15:18:18 -04002758/*
Chris Masond352ac62008-09-29 15:18:18 -04002759 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002760 * if there's a match, we allow the bio to finish. If not, the code in
2761 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002762 */
Miao Xiefacc8a22013-07-25 19:22:34 +08002763static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2764 u64 phy_offset, struct page *page,
2765 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002766{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002767 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002768 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002769 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002770 char *kaddr;
Chris Masonff79f812007-10-15 16:22:25 -04002771 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiefacc8a22013-07-25 19:22:34 +08002772 u32 csum_expected;
Chris Masonff79f812007-10-15 16:22:25 -04002773 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002774 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2775 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002776
Chris Masond20f7042008-12-08 16:58:54 -05002777 if (PageChecked(page)) {
2778 ClearPageChecked(page);
2779 goto good;
2780 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002781
2782 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002783 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002784
Yan Zheng17d217f2008-12-12 10:03:38 -05002785 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002786 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002787 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2788 GFP_NOFS);
2789 return 0;
2790 }
2791
Miao Xiefacc8a22013-07-25 19:22:34 +08002792 phy_offset >>= inode->i_sb->s_blocksize_bits;
2793 csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
Chris Masond3977122009-01-05 21:25:51 -05002794
Miao Xiefacc8a22013-07-25 19:22:34 +08002795 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00002796 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002797 btrfs_csum_final(csum, (char *)&csum);
Miao Xiefacc8a22013-07-25 19:22:34 +08002798 if (csum != csum_expected)
Chris Mason07157aa2007-08-30 08:50:51 -04002799 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002800
Cong Wang7ac687d2011-11-25 23:14:28 +08002801 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002802good:
Chris Mason07157aa2007-08-30 08:50:51 -04002803 return 0;
2804
2805zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002806 if (__ratelimit(&_rs))
Miao Xiefacc8a22013-07-25 19:22:34 +08002807 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002808 btrfs_ino(page->mapping->host), start, csum, csum_expected);
Chris Masondb945352007-10-15 16:15:53 -04002809 memset(kaddr + offset, 1, end - start + 1);
2810 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002811 kunmap_atomic(kaddr);
Miao Xiefacc8a22013-07-25 19:22:34 +08002812 if (csum_expected == 0)
Chris Mason3b951512008-04-17 11:29:12 -04002813 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002814 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002815}
Chris Masonb888db22007-08-27 16:49:44 -04002816
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002817struct delayed_iput {
2818 struct list_head list;
2819 struct inode *inode;
2820};
2821
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002822/* JDM: If this is fs-wide, why can't we add a pointer to
2823 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002824void btrfs_add_delayed_iput(struct inode *inode)
2825{
2826 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2827 struct delayed_iput *delayed;
2828
2829 if (atomic_add_unless(&inode->i_count, -1, 1))
2830 return;
2831
2832 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2833 delayed->inode = inode;
2834
2835 spin_lock(&fs_info->delayed_iput_lock);
2836 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2837 spin_unlock(&fs_info->delayed_iput_lock);
2838}
2839
2840void btrfs_run_delayed_iputs(struct btrfs_root *root)
2841{
2842 LIST_HEAD(list);
2843 struct btrfs_fs_info *fs_info = root->fs_info;
2844 struct delayed_iput *delayed;
2845 int empty;
2846
2847 spin_lock(&fs_info->delayed_iput_lock);
2848 empty = list_empty(&fs_info->delayed_iputs);
2849 spin_unlock(&fs_info->delayed_iput_lock);
2850 if (empty)
2851 return;
2852
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002853 spin_lock(&fs_info->delayed_iput_lock);
2854 list_splice_init(&fs_info->delayed_iputs, &list);
2855 spin_unlock(&fs_info->delayed_iput_lock);
2856
2857 while (!list_empty(&list)) {
2858 delayed = list_entry(list.next, struct delayed_iput, list);
2859 list_del(&delayed->list);
2860 iput(delayed->inode);
2861 kfree(delayed);
2862 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002863}
2864
Yan, Zhengd68fc572010-05-16 10:49:58 -04002865/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002866 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002867 * files in the subvolume, it removes orphan item and frees block_rsv
2868 * structure.
2869 */
2870void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2871 struct btrfs_root *root)
2872{
Josef Bacik90290e12011-12-02 15:44:12 -05002873 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002874 int ret;
2875
Josef Bacik8a35d952012-05-23 14:26:42 -04002876 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002877 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2878 return;
2879
Josef Bacik90290e12011-12-02 15:44:12 -05002880 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002881 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002882 spin_unlock(&root->orphan_lock);
2883 return;
2884 }
2885
2886 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2887 spin_unlock(&root->orphan_lock);
2888 return;
2889 }
2890
2891 block_rsv = root->orphan_block_rsv;
2892 root->orphan_block_rsv = NULL;
2893 spin_unlock(&root->orphan_lock);
2894
Yan, Zhengd68fc572010-05-16 10:49:58 -04002895 if (root->orphan_item_inserted &&
2896 btrfs_root_refs(&root->root_item) > 0) {
2897 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2898 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002899 if (ret)
2900 btrfs_abort_transaction(trans, root, ret);
2901 else
2902 root->orphan_item_inserted = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002903 }
2904
Josef Bacik90290e12011-12-02 15:44:12 -05002905 if (block_rsv) {
2906 WARN_ON(block_rsv->size > 0);
2907 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002908 }
2909}
2910
2911/*
Josef Bacik7b128762008-07-24 12:17:14 -04002912 * This creates an orphan entry for the given inode in case something goes
2913 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002914 *
2915 * NOTE: caller of this function should reserve 5 units of metadata for
2916 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002917 */
2918int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2919{
2920 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002921 struct btrfs_block_rsv *block_rsv = NULL;
2922 int reserve = 0;
2923 int insert = 0;
2924 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002925
Yan, Zhengd68fc572010-05-16 10:49:58 -04002926 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002927 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002928 if (!block_rsv)
2929 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002930 }
2931
Yan, Zhengd68fc572010-05-16 10:49:58 -04002932 spin_lock(&root->orphan_lock);
2933 if (!root->orphan_block_rsv) {
2934 root->orphan_block_rsv = block_rsv;
2935 } else if (block_rsv) {
2936 btrfs_free_block_rsv(root, block_rsv);
2937 block_rsv = NULL;
2938 }
Josef Bacik7b128762008-07-24 12:17:14 -04002939
Josef Bacik8a35d952012-05-23 14:26:42 -04002940 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2941 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002942#if 0
2943 /*
2944 * For proper ENOSPC handling, we should do orphan
2945 * cleanup when mounting. But this introduces backward
2946 * compatibility issue.
2947 */
2948 if (!xchg(&root->orphan_item_inserted, 1))
2949 insert = 2;
2950 else
2951 insert = 1;
2952#endif
2953 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002954 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002955 }
Josef Bacik7b128762008-07-24 12:17:14 -04002956
Josef Bacik72ac3c02012-05-23 14:13:11 -04002957 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2958 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002959 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002960 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002961
Yan, Zhengd68fc572010-05-16 10:49:58 -04002962 /* grab metadata reservation from transaction handle */
2963 if (reserve) {
2964 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002965 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002966 }
2967
2968 /* insert an orphan item to track this unlinked/truncated file */
2969 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002970 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04002971 if (ret) {
Josef Bacik4ef31a42013-08-13 14:10:08 -04002972 if (reserve) {
2973 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2974 &BTRFS_I(inode)->runtime_flags);
2975 btrfs_orphan_release_metadata(inode);
2976 }
2977 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04002978 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2979 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002980 btrfs_abort_transaction(trans, root, ret);
2981 return ret;
2982 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002983 }
2984 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002985 }
2986
2987 /* insert an orphan item to track subvolume contains orphan files */
2988 if (insert >= 2) {
2989 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2990 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002991 if (ret && ret != -EEXIST) {
2992 btrfs_abort_transaction(trans, root, ret);
2993 return ret;
2994 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002995 }
2996 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002997}
2998
2999/*
3000 * We have done the truncate/delete so we can go ahead and remove the orphan
3001 * item for this particular inode.
3002 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003003static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3004 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003005{
3006 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003007 int delete_item = 0;
3008 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003009 int ret = 0;
3010
Yan, Zhengd68fc572010-05-16 10:49:58 -04003011 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003012 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3013 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003014 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003015
Josef Bacik72ac3c02012-05-23 14:13:11 -04003016 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3017 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003018 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003019 spin_unlock(&root->orphan_lock);
3020
Josef Bacik4ef31a42013-08-13 14:10:08 -04003021 if (trans && delete_item)
Li Zefan33345d012011-04-20 10:31:50 +08003022 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003023
Josef Bacik8a35d952012-05-23 14:26:42 -04003024 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003025 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003026 atomic_dec(&root->orphan_inodes);
3027 }
Josef Bacik7b128762008-07-24 12:17:14 -04003028
Josef Bacik4ef31a42013-08-13 14:10:08 -04003029 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003030}
3031
3032/*
3033 * this cleans up any orphans that may be left on the list from the last use
3034 * of this root.
3035 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003036int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003037{
3038 struct btrfs_path *path;
3039 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003040 struct btrfs_key key, found_key;
3041 struct btrfs_trans_handle *trans;
3042 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003043 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003044 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3045
Yan, Zhengd68fc572010-05-16 10:49:58 -04003046 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003047 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003048
3049 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003050 if (!path) {
3051 ret = -ENOMEM;
3052 goto out;
3053 }
Josef Bacik7b128762008-07-24 12:17:14 -04003054 path->reada = -1;
3055
3056 key.objectid = BTRFS_ORPHAN_OBJECTID;
3057 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3058 key.offset = (u64)-1;
3059
Josef Bacik7b128762008-07-24 12:17:14 -04003060 while (1) {
3061 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003062 if (ret < 0)
3063 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003064
3065 /*
3066 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003067 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003068 * find the key and see if we have stuff that matches
3069 */
3070 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003071 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003072 if (path->slots[0] == 0)
3073 break;
3074 path->slots[0]--;
3075 }
3076
3077 /* pull out the item */
3078 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003079 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3080
3081 /* make sure the item matches what we want */
3082 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3083 break;
3084 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3085 break;
3086
3087 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003088 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003089
3090 /*
3091 * this is where we are basically btrfs_lookup, without the
3092 * crossing root thing. we store the inode number in the
3093 * offset of the orphan item.
3094 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003095
3096 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003097 btrfs_err(root->fs_info,
3098 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003099 ret = -EINVAL;
3100 goto out;
3101 }
3102
3103 last_objectid = found_key.offset;
3104
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003105 found_key.objectid = found_key.offset;
3106 found_key.type = BTRFS_INODE_ITEM_KEY;
3107 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003108 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003109 ret = PTR_RET(inode);
3110 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003111 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003112
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003113 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3114 struct btrfs_root *dead_root;
3115 struct btrfs_fs_info *fs_info = root->fs_info;
3116 int is_dead_root = 0;
3117
3118 /*
3119 * this is an orphan in the tree root. Currently these
3120 * could come from 2 sources:
3121 * a) a snapshot deletion in progress
3122 * b) a free space cache inode
3123 * We need to distinguish those two, as the snapshot
3124 * orphan must not get deleted.
3125 * find_dead_roots already ran before us, so if this
3126 * is a snapshot deletion, we should find the root
3127 * in the dead_roots list
3128 */
3129 spin_lock(&fs_info->trans_lock);
3130 list_for_each_entry(dead_root, &fs_info->dead_roots,
3131 root_list) {
3132 if (dead_root->root_key.objectid ==
3133 found_key.objectid) {
3134 is_dead_root = 1;
3135 break;
3136 }
3137 }
3138 spin_unlock(&fs_info->trans_lock);
3139 if (is_dead_root) {
3140 /* prevent this orphan from being found again */
3141 key.offset = found_key.objectid - 1;
3142 continue;
3143 }
3144 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003145 /*
3146 * Inode is already gone but the orphan item is still there,
3147 * kill the orphan item.
3148 */
3149 if (ret == -ESTALE) {
3150 trans = btrfs_start_transaction(root, 1);
3151 if (IS_ERR(trans)) {
3152 ret = PTR_ERR(trans);
3153 goto out;
3154 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003155 btrfs_debug(root->fs_info, "auto deleting %Lu",
3156 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003157 ret = btrfs_del_orphan_item(trans, root,
3158 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003159 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003160 if (ret)
3161 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003162 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003163 }
Josef Bacik7b128762008-07-24 12:17:14 -04003164
Josef Bacik7b128762008-07-24 12:17:14 -04003165 /*
3166 * add this inode to the orphan list so btrfs_orphan_del does
3167 * the proper thing when we hit it
3168 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003169 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3170 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003171 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003172
Josef Bacik7b128762008-07-24 12:17:14 -04003173 /* if we have links, this was a truncate, lets do that */
3174 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003175 if (!S_ISREG(inode->i_mode)) {
3176 WARN_ON(1);
3177 iput(inode);
3178 continue;
3179 }
Josef Bacik7b128762008-07-24 12:17:14 -04003180 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003181
3182 /* 1 for the orphan item deletion. */
3183 trans = btrfs_start_transaction(root, 1);
3184 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003185 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003186 ret = PTR_ERR(trans);
3187 goto out;
3188 }
3189 ret = btrfs_orphan_add(trans, inode);
3190 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003191 if (ret) {
3192 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003193 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003194 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003195
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003196 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003197 if (ret)
3198 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003199 } else {
3200 nr_unlink++;
3201 }
3202
3203 /* this will do delete_inode and everything for us */
3204 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003205 if (ret)
3206 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003207 }
Miao Xie3254c872011-11-10 20:45:05 -05003208 /* release the path since we're done with it */
3209 btrfs_release_path(path);
3210
Yan, Zhengd68fc572010-05-16 10:49:58 -04003211 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3212
3213 if (root->orphan_block_rsv)
3214 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3215 (u64)-1);
3216
3217 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003218 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003219 if (!IS_ERR(trans))
3220 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003221 }
Josef Bacik7b128762008-07-24 12:17:14 -04003222
3223 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003224 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003225 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003226 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003227
3228out:
3229 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003230 btrfs_crit(root->fs_info,
3231 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003232 btrfs_free_path(path);
3233 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003234}
3235
Chris Masond352ac62008-09-29 15:18:18 -04003236/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003237 * very simple check to peek ahead in the leaf looking for xattrs. If we
3238 * don't find any xattrs, we know there can't be any acls.
3239 *
3240 * slot is the slot the inode is in, objectid is the objectid of the inode
3241 */
3242static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3243 int slot, u64 objectid)
3244{
3245 u32 nritems = btrfs_header_nritems(leaf);
3246 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003247 static u64 xattr_access = 0;
3248 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003249 int scanned = 0;
3250
Josef Bacikf23b5a52013-06-19 10:16:26 -04003251 if (!xattr_access) {
3252 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3253 strlen(POSIX_ACL_XATTR_ACCESS));
3254 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3255 strlen(POSIX_ACL_XATTR_DEFAULT));
3256 }
3257
Chris Mason46a53cc2009-04-27 11:47:50 -04003258 slot++;
3259 while (slot < nritems) {
3260 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3261
3262 /* we found a different objectid, there must not be acls */
3263 if (found_key.objectid != objectid)
3264 return 0;
3265
3266 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003267 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3268 if (found_key.offset == xattr_access ||
3269 found_key.offset == xattr_default)
3270 return 1;
3271 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003272
3273 /*
3274 * we found a key greater than an xattr key, there can't
3275 * be any acls later on
3276 */
3277 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3278 return 0;
3279
3280 slot++;
3281 scanned++;
3282
3283 /*
3284 * it goes inode, inode backrefs, xattrs, extents,
3285 * so if there are a ton of hard links to an inode there can
3286 * be a lot of backrefs. Don't waste time searching too hard,
3287 * this is just an optimization
3288 */
3289 if (scanned >= 8)
3290 break;
3291 }
3292 /* we hit the end of the leaf before we found an xattr or
3293 * something larger than an xattr. We have to assume the inode
3294 * has acls
3295 */
3296 return 1;
3297}
3298
3299/*
Chris Masond352ac62008-09-29 15:18:18 -04003300 * read an inode from the btree into the in-memory inode
3301 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003302static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003303{
3304 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003305 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003306 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003307 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003308 struct btrfs_root *root = BTRFS_I(inode)->root;
3309 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003310 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003311 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003312 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003313 bool filled = false;
3314
3315 ret = btrfs_fill_inode(inode, &rdev);
3316 if (!ret)
3317 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003318
3319 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003320 if (!path)
3321 goto make_bad;
3322
Josef Bacikd90c7322011-05-17 09:50:54 -04003323 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003324 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003325
Chris Mason39279cc2007-06-12 06:35:45 -04003326 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003327 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003328 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003329
Chris Mason5f39d392007-10-15 16:14:19 -04003330 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003331
3332 if (filled)
3333 goto cache_acl;
3334
Chris Mason5f39d392007-10-15 16:14:19 -04003335 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3336 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003337 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003338 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003339 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3340 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003341 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003342
3343 tspec = btrfs_inode_atime(inode_item);
3344 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3345 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3346
3347 tspec = btrfs_inode_mtime(inode_item);
3348 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3349 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3350
3351 tspec = btrfs_inode_ctime(inode_item);
3352 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3353 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3354
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003355 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003356 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003357 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3358
3359 /*
3360 * If we were modified in the current generation and evicted from memory
3361 * and then re-read we need to do a full sync since we don't have any
3362 * idea about which extents were modified before we were evicted from
3363 * cache.
3364 */
3365 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3366 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3367 &BTRFS_I(inode)->runtime_flags);
3368
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003369 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003370 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003371 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003372 rdev = btrfs_inode_rdev(leaf, inode_item);
3373
Josef Bacikaec74772008-07-24 12:12:38 -04003374 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003375 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003376cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003377 /*
3378 * try to precache a NULL acl entry for files that don't have
3379 * any xattrs or acls
3380 */
Li Zefan33345d012011-04-20 10:31:50 +08003381 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3382 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003383 if (!maybe_acls)
3384 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003385
Chris Mason39279cc2007-06-12 06:35:45 -04003386 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003387
Chris Mason39279cc2007-06-12 06:35:45 -04003388 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003389 case S_IFREG:
3390 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003391 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003392 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003393 inode->i_fop = &btrfs_file_operations;
3394 inode->i_op = &btrfs_file_inode_operations;
3395 break;
3396 case S_IFDIR:
3397 inode->i_fop = &btrfs_dir_file_operations;
3398 if (root == root->fs_info->tree_root)
3399 inode->i_op = &btrfs_dir_ro_inode_operations;
3400 else
3401 inode->i_op = &btrfs_dir_inode_operations;
3402 break;
3403 case S_IFLNK:
3404 inode->i_op = &btrfs_symlink_inode_operations;
3405 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003406 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003407 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003408 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003409 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003410 init_special_inode(inode, inode->i_mode, rdev);
3411 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003412 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003413
3414 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003415 return;
3416
3417make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003418 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003419 make_bad_inode(inode);
3420}
3421
Chris Masond352ac62008-09-29 15:18:18 -04003422/*
3423 * given a leaf and an inode, copy the inode fields into the leaf
3424 */
Chris Masone02119d2008-09-05 16:13:11 -04003425static void fill_inode_item(struct btrfs_trans_handle *trans,
3426 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003427 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003428 struct inode *inode)
3429{
Liu Bo51fab692012-12-27 09:01:21 +00003430 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003431
Liu Bo51fab692012-12-27 09:01:21 +00003432 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003433
Liu Bo51fab692012-12-27 09:01:21 +00003434 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3435 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3436 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3437 &token);
3438 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3439 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003440
Liu Bo51fab692012-12-27 09:01:21 +00003441 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3442 inode->i_atime.tv_sec, &token);
3443 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3444 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003445
Liu Bo51fab692012-12-27 09:01:21 +00003446 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3447 inode->i_mtime.tv_sec, &token);
3448 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3449 inode->i_mtime.tv_nsec, &token);
3450
3451 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3452 inode->i_ctime.tv_sec, &token);
3453 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3454 inode->i_ctime.tv_nsec, &token);
3455
3456 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3457 &token);
3458 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3459 &token);
3460 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3461 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3462 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3463 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3464 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003465}
3466
Chris Masond352ac62008-09-29 15:18:18 -04003467/*
3468 * copy everything in the in-memory inode into the btree.
3469 */
Chris Mason21151332011-11-10 20:39:08 -05003470static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003471 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003472{
3473 struct btrfs_inode_item *inode_item;
3474 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003475 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003476 int ret;
3477
3478 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003479 if (!path)
3480 return -ENOMEM;
3481
Chris Masonb9473432009-03-13 11:00:37 -04003482 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003483 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3484 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003485 if (ret) {
3486 if (ret > 0)
3487 ret = -ENOENT;
3488 goto failed;
3489 }
3490
Chris Masonb4ce94d2009-02-04 09:25:08 -05003491 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003492 leaf = path->nodes[0];
3493 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003494 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003495
Chris Masone02119d2008-09-05 16:13:11 -04003496 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003497 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003498 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003499 ret = 0;
3500failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003501 btrfs_free_path(path);
3502 return ret;
3503}
3504
Chris Masond352ac62008-09-29 15:18:18 -04003505/*
Chris Mason21151332011-11-10 20:39:08 -05003506 * copy everything in the in-memory inode into the btree.
3507 */
3508noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3509 struct btrfs_root *root, struct inode *inode)
3510{
3511 int ret;
3512
3513 /*
3514 * If the inode is a free space inode, we can deadlock during commit
3515 * if we put it into the delayed code.
3516 *
3517 * The data relocation inode should also be directly updated
3518 * without delay
3519 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003520 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003521 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003522 btrfs_update_root_times(trans, root);
3523
Chris Mason21151332011-11-10 20:39:08 -05003524 ret = btrfs_delayed_update_inode(trans, root, inode);
3525 if (!ret)
3526 btrfs_set_inode_last_trans(trans, inode);
3527 return ret;
3528 }
3529
3530 return btrfs_update_inode_item(trans, root, inode);
3531}
3532
Josef Bacikbe6aef62012-10-22 15:43:12 -04003533noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3534 struct btrfs_root *root,
3535 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003536{
3537 int ret;
3538
3539 ret = btrfs_update_inode(trans, root, inode);
3540 if (ret == -ENOSPC)
3541 return btrfs_update_inode_item(trans, root, inode);
3542 return ret;
3543}
3544
3545/*
Chris Masond352ac62008-09-29 15:18:18 -04003546 * unlink helper that gets used here in inode.c and in the tree logging
3547 * recovery code. It remove a link in a directory with a given name, and
3548 * also drops the back refs in the inode to the directory
3549 */
Al Viro92986792011-03-04 17:14:37 +00003550static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3551 struct btrfs_root *root,
3552 struct inode *dir, struct inode *inode,
3553 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003554{
3555 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003556 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003557 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003558 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003559 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003560 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003561 u64 ino = btrfs_ino(inode);
3562 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003563
3564 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003565 if (!path) {
3566 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003567 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003568 }
3569
Chris Masonb9473432009-03-13 11:00:37 -04003570 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003571 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003572 name, name_len, -1);
3573 if (IS_ERR(di)) {
3574 ret = PTR_ERR(di);
3575 goto err;
3576 }
3577 if (!di) {
3578 ret = -ENOENT;
3579 goto err;
3580 }
Chris Mason5f39d392007-10-15 16:14:19 -04003581 leaf = path->nodes[0];
3582 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003583 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003584 if (ret)
3585 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003586 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003587
Li Zefan33345d012011-04-20 10:31:50 +08003588 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3589 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003590 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003591 btrfs_info(root->fs_info,
3592 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003593 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003594 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003595 goto err;
3596 }
3597
Miao Xie16cdcec2011-04-22 18:12:22 +08003598 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003599 if (ret) {
3600 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003601 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003602 }
Chris Mason39279cc2007-06-12 06:35:45 -04003603
Chris Masone02119d2008-09-05 16:13:11 -04003604 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003605 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003606 if (ret != 0 && ret != -ENOENT) {
3607 btrfs_abort_transaction(trans, root, ret);
3608 goto err;
3609 }
Chris Masone02119d2008-09-05 16:13:11 -04003610
3611 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3612 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003613 if (ret == -ENOENT)
3614 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003615 else if (ret)
3616 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003617err:
3618 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003619 if (ret)
3620 goto out;
3621
3622 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003623 inode_inc_iversion(inode);
3624 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003625 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003626 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003627out:
Chris Mason39279cc2007-06-12 06:35:45 -04003628 return ret;
3629}
3630
Al Viro92986792011-03-04 17:14:37 +00003631int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3632 struct btrfs_root *root,
3633 struct inode *dir, struct inode *inode,
3634 const char *name, int name_len)
3635{
3636 int ret;
3637 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3638 if (!ret) {
3639 btrfs_drop_nlink(inode);
3640 ret = btrfs_update_inode(trans, root, inode);
3641 }
3642 return ret;
3643}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003644
3645/*
3646 * helper to start transaction for unlink and rmdir.
3647 *
Josef Bacikd52be812013-05-29 14:54:47 -04003648 * unlink and rmdir are special in btrfs, they do not always free space, so
3649 * if we cannot make our reservations the normal way try and see if there is
3650 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3651 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003652 */
Josef Bacikd52be812013-05-29 14:54:47 -04003653static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003654{
3655 struct btrfs_trans_handle *trans;
3656 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003657 int ret;
3658
Josef Bacike70bea52011-10-11 14:18:24 -04003659 /*
3660 * 1 for the possible orphan item
3661 * 1 for the dir item
3662 * 1 for the dir index
3663 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003664 * 1 for the inode
3665 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003666 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003667 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3668 return trans;
3669
Josef Bacikd52be812013-05-29 14:54:47 -04003670 if (PTR_ERR(trans) == -ENOSPC) {
3671 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003672
Josef Bacikd52be812013-05-29 14:54:47 -04003673 trans = btrfs_start_transaction(root, 0);
3674 if (IS_ERR(trans))
3675 return trans;
3676 ret = btrfs_cond_migrate_bytes(root->fs_info,
3677 &root->fs_info->trans_block_rsv,
3678 num_bytes, 5);
3679 if (ret) {
3680 btrfs_end_transaction(trans, root);
3681 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003682 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003683 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003684 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003685 }
Josef Bacikd52be812013-05-29 14:54:47 -04003686 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003687}
3688
Chris Mason39279cc2007-06-12 06:35:45 -04003689static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3690{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003691 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003692 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003693 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003694 int ret;
3695
Josef Bacikd52be812013-05-29 14:54:47 -04003696 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003697 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003698 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003699
Chris Mason12fcfd22009-03-24 10:24:20 -04003700 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3701
Chris Masone02119d2008-09-05 16:13:11 -04003702 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3703 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003704 if (ret)
3705 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003706
Yan, Zhenga22285a2010-05-16 10:48:46 -04003707 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003708 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003709 if (ret)
3710 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003711 }
Josef Bacik7b128762008-07-24 12:17:14 -04003712
Tsutomu Itohb5324022011-07-19 07:27:20 +00003713out:
Josef Bacikd52be812013-05-29 14:54:47 -04003714 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003715 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003716 return ret;
3717}
3718
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003719int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3720 struct btrfs_root *root,
3721 struct inode *dir, u64 objectid,
3722 const char *name, int name_len)
3723{
3724 struct btrfs_path *path;
3725 struct extent_buffer *leaf;
3726 struct btrfs_dir_item *di;
3727 struct btrfs_key key;
3728 u64 index;
3729 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003730 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003731
3732 path = btrfs_alloc_path();
3733 if (!path)
3734 return -ENOMEM;
3735
Li Zefan33345d012011-04-20 10:31:50 +08003736 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003737 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003738 if (IS_ERR_OR_NULL(di)) {
3739 if (!di)
3740 ret = -ENOENT;
3741 else
3742 ret = PTR_ERR(di);
3743 goto out;
3744 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003745
3746 leaf = path->nodes[0];
3747 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3748 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3749 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003750 if (ret) {
3751 btrfs_abort_transaction(trans, root, ret);
3752 goto out;
3753 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003754 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003755
3756 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3757 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003758 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003759 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003760 if (ret != -ENOENT) {
3761 btrfs_abort_transaction(trans, root, ret);
3762 goto out;
3763 }
Li Zefan33345d012011-04-20 10:31:50 +08003764 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003765 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003766 if (IS_ERR_OR_NULL(di)) {
3767 if (!di)
3768 ret = -ENOENT;
3769 else
3770 ret = PTR_ERR(di);
3771 btrfs_abort_transaction(trans, root, ret);
3772 goto out;
3773 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003774
3775 leaf = path->nodes[0];
3776 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003777 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003778 index = key.offset;
3779 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003780 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003781
Miao Xie16cdcec2011-04-22 18:12:22 +08003782 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003783 if (ret) {
3784 btrfs_abort_transaction(trans, root, ret);
3785 goto out;
3786 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003787
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003788 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003789 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003790 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003791 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003792 if (ret)
3793 btrfs_abort_transaction(trans, root, ret);
3794out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003795 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003796 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003797}
3798
Chris Mason39279cc2007-06-12 06:35:45 -04003799static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3800{
3801 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003802 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003803 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003804 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003805
David Sterbab3ae2442012-09-13 16:04:34 -06003806 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003807 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003808 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3809 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003810
Josef Bacikd52be812013-05-29 14:54:47 -04003811 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003812 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003813 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003814
Li Zefan33345d012011-04-20 10:31:50 +08003815 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003816 err = btrfs_unlink_subvol(trans, root, dir,
3817 BTRFS_I(inode)->location.objectid,
3818 dentry->d_name.name,
3819 dentry->d_name.len);
3820 goto out;
3821 }
3822
Josef Bacik7b128762008-07-24 12:17:14 -04003823 err = btrfs_orphan_add(trans, inode);
3824 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003825 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003826
Chris Mason39279cc2007-06-12 06:35:45 -04003827 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003828 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3829 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003830 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003831 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003832out:
Josef Bacikd52be812013-05-29 14:54:47 -04003833 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003834 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003835
Chris Mason39279cc2007-06-12 06:35:45 -04003836 return err;
3837}
3838
Chris Mason323ac952008-10-01 19:05:46 -04003839/*
Chris Mason39279cc2007-06-12 06:35:45 -04003840 * this can truncate away extent items, csum items and directory items.
3841 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003842 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003843 *
3844 * csum items that cross the new i_size are truncated to the new size
3845 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003846 *
3847 * min_type is the minimum key type to truncate down to. If set to 0, this
3848 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003849 */
Yan, Zheng80825102009-11-12 09:35:36 +00003850int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3851 struct btrfs_root *root,
3852 struct inode *inode,
3853 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003854{
Chris Mason39279cc2007-06-12 06:35:45 -04003855 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003856 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003857 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003858 struct btrfs_key key;
3859 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003860 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003861 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003862 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003863 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003864 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003865 int found_extent;
3866 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003867 int pending_del_nr = 0;
3868 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003869 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003870 int ret;
3871 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003872 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003873
3874 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003875
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003876 path = btrfs_alloc_path();
3877 if (!path)
3878 return -ENOMEM;
3879 path->reada = -1;
3880
Josef Bacik5dc562c2012-08-17 13:14:17 -04003881 /*
3882 * We want to drop from the next block forward in case this new size is
3883 * not block aligned since we will be keeping the last block of the
3884 * extent just the way it is.
3885 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003886 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00003887 btrfs_drop_extent_cache(inode, ALIGN(new_size,
3888 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003889
Miao Xie16cdcec2011-04-22 18:12:22 +08003890 /*
3891 * This function is also used to drop the items in the log tree before
3892 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3893 * it is used to drop the loged items. So we shouldn't kill the delayed
3894 * items.
3895 */
3896 if (min_type == 0 && root == BTRFS_I(inode)->root)
3897 btrfs_kill_delayed_inode_items(inode);
3898
Li Zefan33345d012011-04-20 10:31:50 +08003899 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003900 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003901 key.type = (u8)-1;
3902
Chris Mason85e21ba2008-01-29 15:11:36 -05003903search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003904 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003905 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003906 if (ret < 0) {
3907 err = ret;
3908 goto out;
3909 }
Chris Masond3977122009-01-05 21:25:51 -05003910
Chris Mason85e21ba2008-01-29 15:11:36 -05003911 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003912 /* there are no items in the tree for us to truncate, we're
3913 * done
3914 */
Yan, Zheng80825102009-11-12 09:35:36 +00003915 if (path->slots[0] == 0)
3916 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003917 path->slots[0]--;
3918 }
3919
Chris Masond3977122009-01-05 21:25:51 -05003920 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003921 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003922 leaf = path->nodes[0];
3923 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3924 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003925
Li Zefan33345d012011-04-20 10:31:50 +08003926 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003927 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003928
Chris Mason85e21ba2008-01-29 15:11:36 -05003929 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003930 break;
3931
Chris Mason5f39d392007-10-15 16:14:19 -04003932 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003933 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003934 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003935 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003936 extent_type = btrfs_file_extent_type(leaf, fi);
3937 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003938 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003939 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003940 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003941 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003942 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003943 }
Yan008630c2007-11-07 13:31:09 -05003944 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003945 }
Yan, Zheng80825102009-11-12 09:35:36 +00003946 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003947 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003948 } else {
3949 if (item_end < new_size)
3950 break;
3951 if (found_key.offset >= new_size)
3952 del_item = 1;
3953 else
3954 del_item = 0;
3955 }
Chris Mason39279cc2007-06-12 06:35:45 -04003956 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003957 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003958 if (found_type != BTRFS_EXTENT_DATA_KEY)
3959 goto delete;
3960
3961 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003962 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003963 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003964 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003965 u64 orig_num_bytes =
3966 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00003967 extent_num_bytes = ALIGN(new_size -
3968 found_key.offset,
3969 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04003970 btrfs_set_file_extent_num_bytes(leaf, fi,
3971 extent_num_bytes);
3972 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003973 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003974 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003975 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003976 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003977 } else {
Chris Masondb945352007-10-15 16:15:53 -04003978 extent_num_bytes =
3979 btrfs_file_extent_disk_num_bytes(leaf,
3980 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003981 extent_offset = found_key.offset -
3982 btrfs_file_extent_offset(leaf, fi);
3983
Chris Mason39279cc2007-06-12 06:35:45 -04003984 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003985 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003986 if (extent_start != 0) {
3987 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003988 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003989 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003990 }
3991 }
Chris Mason90692182008-02-08 13:49:28 -05003992 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003993 /*
3994 * we can't truncate inline items that have had
3995 * special encodings
3996 */
3997 if (!del_item &&
3998 btrfs_file_extent_compression(leaf, fi) == 0 &&
3999 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4000 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004001 u32 size = new_size - found_key.offset;
4002
4003 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004004 inode_sub_bytes(inode, item_end + 1 -
4005 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004006 }
4007 size =
4008 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004009 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004010 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004011 inode_sub_bytes(inode, item_end + 1 -
4012 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004013 }
Chris Mason39279cc2007-06-12 06:35:45 -04004014 }
Chris Mason179e29e2007-11-01 11:28:41 -04004015delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004016 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004017 if (!pending_del_nr) {
4018 /* no pending yet, add ourselves */
4019 pending_del_slot = path->slots[0];
4020 pending_del_nr = 1;
4021 } else if (pending_del_nr &&
4022 path->slots[0] + 1 == pending_del_slot) {
4023 /* hop on the pending chunk */
4024 pending_del_nr++;
4025 pending_del_slot = path->slots[0];
4026 } else {
Chris Masond3977122009-01-05 21:25:51 -05004027 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004028 }
Chris Mason39279cc2007-06-12 06:35:45 -04004029 } else {
4030 break;
4031 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004032 if (found_extent && (root->ref_cows ||
4033 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004034 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004035 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004036 extent_num_bytes, 0,
4037 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004038 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004039 BUG_ON(ret);
4040 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004041
Yan, Zheng80825102009-11-12 09:35:36 +00004042 if (found_type == BTRFS_INODE_ITEM_KEY)
4043 break;
4044
4045 if (path->slots[0] == 0 ||
4046 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004047 if (pending_del_nr) {
4048 ret = btrfs_del_items(trans, root, path,
4049 pending_del_slot,
4050 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004051 if (ret) {
4052 btrfs_abort_transaction(trans,
4053 root, ret);
4054 goto error;
4055 }
Yan, Zheng80825102009-11-12 09:35:36 +00004056 pending_del_nr = 0;
4057 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004058 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004059 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004060 } else {
4061 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004062 }
Chris Mason39279cc2007-06-12 06:35:45 -04004063 }
Yan, Zheng80825102009-11-12 09:35:36 +00004064out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004065 if (pending_del_nr) {
4066 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4067 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004068 if (ret)
4069 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004070 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004071error:
Chris Mason39279cc2007-06-12 06:35:45 -04004072 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004073 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004074}
4075
Chris Masona52d9a82007-08-27 16:49:44 -04004076/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004077 * btrfs_truncate_page - read, zero a chunk and write a page
4078 * @inode - inode that we're zeroing
4079 * @from - the offset to start zeroing
4080 * @len - the length to zero, 0 to zero the entire range respective to the
4081 * offset
4082 * @front - zero up to the offset instead of from the offset on
4083 *
4084 * This will find the page for the "from" offset and cow the page and zero the
4085 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004086 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004087int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4088 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004089{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004090 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004091 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004092 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4093 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004094 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004095 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004096 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004097 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4098 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4099 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004100 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004101 int ret = 0;
4102 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004103 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004104
Josef Bacik2aaa6652012-08-29 14:27:18 -04004105 if ((offset & (blocksize - 1)) == 0 &&
4106 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004107 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004108 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004109 if (ret)
4110 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004111
Chris Mason211c17f2008-05-15 09:13:45 -04004112again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004113 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004114 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004115 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004116 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004117 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004118 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004119
4120 page_start = page_offset(page);
4121 page_end = page_start + PAGE_CACHE_SIZE - 1;
4122
Chris Masona52d9a82007-08-27 16:49:44 -04004123 if (!PageUptodate(page)) {
4124 ret = btrfs_readpage(NULL, page);
4125 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004126 if (page->mapping != mapping) {
4127 unlock_page(page);
4128 page_cache_release(page);
4129 goto again;
4130 }
Chris Masona52d9a82007-08-27 16:49:44 -04004131 if (!PageUptodate(page)) {
4132 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004133 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004134 }
4135 }
Chris Mason211c17f2008-05-15 09:13:45 -04004136 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004137
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004138 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004139 set_page_extent_mapped(page);
4140
4141 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4142 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004143 unlock_extent_cached(io_tree, page_start, page_end,
4144 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004145 unlock_page(page);
4146 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004147 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004148 btrfs_put_ordered_extent(ordered);
4149 goto again;
4150 }
4151
Josef Bacik2ac55d42010-02-03 19:33:23 +00004152 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004153 EXTENT_DIRTY | EXTENT_DELALLOC |
4154 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004155 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004156
Josef Bacik2ac55d42010-02-03 19:33:23 +00004157 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4158 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004159 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004160 unlock_extent_cached(io_tree, page_start, page_end,
4161 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004162 goto out_unlock;
4163 }
4164
Chris Masone6dcd2d2008-07-17 12:53:50 -04004165 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004166 if (!len)
4167 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004168 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004169 if (front)
4170 memset(kaddr, 0, offset);
4171 else
4172 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004173 flush_dcache_page(page);
4174 kunmap(page);
4175 }
Chris Mason247e7432008-07-17 12:53:51 -04004176 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004177 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004178 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4179 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004180
Chris Mason89642222008-07-24 09:41:53 -04004181out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004182 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004183 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004184 unlock_page(page);
4185 page_cache_release(page);
4186out:
4187 return ret;
4188}
4189
Josef Bacik695a0d02011-03-04 15:46:53 -05004190/*
4191 * This function puts in dummy file extents for the area we're creating a hole
4192 * for. So if we are truncating this file to a larger size we need to insert
4193 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4194 * the range between oldsize and size
4195 */
Josef Bacika41ad392011-01-31 15:30:16 -05004196int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004197{
4198 struct btrfs_trans_handle *trans;
4199 struct btrfs_root *root = BTRFS_I(inode)->root;
4200 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004201 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004202 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004203 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004204 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4205 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004206 u64 last_byte;
4207 u64 cur_offset;
4208 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004209 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004210
Josef Bacika71754f2013-06-17 17:14:39 -04004211 /*
4212 * If our size started in the middle of a page we need to zero out the
4213 * rest of the page before we expand the i_size, otherwise we could
4214 * expose stale data.
4215 */
4216 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4217 if (err)
4218 return err;
4219
Yan Zheng9036c102008-10-30 14:19:41 -04004220 if (size <= hole_start)
4221 return 0;
4222
Yan Zheng9036c102008-10-30 14:19:41 -04004223 while (1) {
4224 struct btrfs_ordered_extent *ordered;
4225 btrfs_wait_ordered_range(inode, hole_start,
4226 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004227 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004228 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004229 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4230 if (!ordered)
4231 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004232 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4233 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004234 btrfs_put_ordered_extent(ordered);
4235 }
4236
Yan Zheng9036c102008-10-30 14:19:41 -04004237 cur_offset = hole_start;
4238 while (1) {
4239 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4240 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004241 if (IS_ERR(em)) {
4242 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004243 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004244 break;
4245 }
Yan Zheng9036c102008-10-30 14:19:41 -04004246 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004247 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004248 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004249 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004250 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004251
Miao Xie36423202011-12-14 20:12:02 -05004252 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004253 if (IS_ERR(trans)) {
4254 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004255 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004256 }
Yan, Zheng80825102009-11-12 09:35:36 +00004257
Josef Bacik5dc562c2012-08-17 13:14:17 -04004258 err = btrfs_drop_extents(trans, root, inode,
4259 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004260 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004261 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004262 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004263 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004264 break;
Miao Xie5b397372011-09-11 10:52:24 -04004265 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004266
Yan Zheng9036c102008-10-30 14:19:41 -04004267 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004268 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004269 0, hole_size, 0, hole_size,
4270 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004271 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004272 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004273 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004274 break;
Miao Xie5b397372011-09-11 10:52:24 -04004275 }
Yan, Zheng80825102009-11-12 09:35:36 +00004276
Josef Bacik5dc562c2012-08-17 13:14:17 -04004277 btrfs_drop_extent_cache(inode, cur_offset,
4278 cur_offset + hole_size - 1, 0);
4279 hole_em = alloc_extent_map();
4280 if (!hole_em) {
4281 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4282 &BTRFS_I(inode)->runtime_flags);
4283 goto next;
4284 }
4285 hole_em->start = cur_offset;
4286 hole_em->len = hole_size;
4287 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004288
Josef Bacik5dc562c2012-08-17 13:14:17 -04004289 hole_em->block_start = EXTENT_MAP_HOLE;
4290 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004291 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004292 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004293 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4294 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4295 hole_em->generation = trans->transid;
4296
4297 while (1) {
4298 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004299 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004300 write_unlock(&em_tree->lock);
4301 if (err != -EEXIST)
4302 break;
4303 btrfs_drop_extent_cache(inode, cur_offset,
4304 cur_offset +
4305 hole_size - 1, 0);
4306 }
4307 free_extent_map(hole_em);
4308next:
Miao Xie36423202011-12-14 20:12:02 -05004309 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004310 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004311 }
4312 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004313 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004314 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004315 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004316 break;
4317 }
4318
Yan, Zhenga22285a2010-05-16 10:48:46 -04004319 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004320 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4321 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004322 return err;
4323}
4324
Eric Sandeen3972f262013-01-12 02:57:22 +00004325static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004326{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004327 struct btrfs_root *root = BTRFS_I(inode)->root;
4328 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004329 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004330 loff_t newsize = attr->ia_size;
4331 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004332 int ret;
4333
Eric Sandeen3972f262013-01-12 02:57:22 +00004334 /*
4335 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4336 * special case where we need to update the times despite not having
4337 * these flags set. For all other operations the VFS set these flags
4338 * explicitly if it wants a timestamp update.
4339 */
4340 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4341 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4342
Josef Bacika41ad392011-01-31 15:30:16 -05004343 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004344 truncate_pagecache(inode, oldsize, newsize);
4345 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004346 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004347 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004348
Miao Xief4a2f4c2011-12-14 20:12:01 -05004349 trans = btrfs_start_transaction(root, 1);
4350 if (IS_ERR(trans))
4351 return PTR_ERR(trans);
4352
4353 i_size_write(inode, newsize);
4354 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4355 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004356 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004357 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004358
Josef Bacika41ad392011-01-31 15:30:16 -05004359 /*
4360 * We're truncating a file that used to have good data down to
4361 * zero. Make sure it gets into the ordered flush list so that
4362 * any new writes get down to disk quickly.
4363 */
4364 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004365 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4366 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004367
Josef Bacikf3fe8202013-01-07 17:03:21 -05004368 /*
4369 * 1 for the orphan item we're going to add
4370 * 1 for the orphan item deletion.
4371 */
4372 trans = btrfs_start_transaction(root, 2);
4373 if (IS_ERR(trans))
4374 return PTR_ERR(trans);
4375
4376 /*
4377 * We need to do this in case we fail at _any_ point during the
4378 * actual truncate. Once we do the truncate_setsize we could
4379 * invalidate pages which forces any outstanding ordered io to
4380 * be instantly completed which will give us extents that need
4381 * to be truncated. If we fail to get an orphan inode down we
4382 * could have left over extents that were never meant to live,
4383 * so we need to garuntee from this point on that everything
4384 * will be consistent.
4385 */
4386 ret = btrfs_orphan_add(trans, inode);
4387 btrfs_end_transaction(trans, root);
4388 if (ret)
4389 return ret;
4390
Josef Bacika41ad392011-01-31 15:30:16 -05004391 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4392 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004393
4394 /* Disable nonlocked read DIO to avoid the end less truncate */
4395 btrfs_inode_block_unlocked_dio(inode);
4396 inode_dio_wait(inode);
4397 btrfs_inode_resume_unlocked_dio(inode);
4398
Josef Bacika41ad392011-01-31 15:30:16 -05004399 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004400 if (ret && inode->i_nlink)
4401 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004402 }
4403
Josef Bacika41ad392011-01-31 15:30:16 -05004404 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004405}
4406
Chris Mason39279cc2007-06-12 06:35:45 -04004407static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4408{
4409 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004410 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004411 int err;
4412
Li Zefanb83cc962010-12-20 16:04:08 +08004413 if (btrfs_root_readonly(root))
4414 return -EROFS;
4415
Chris Mason39279cc2007-06-12 06:35:45 -04004416 err = inode_change_ok(inode, attr);
4417 if (err)
4418 return err;
4419
Chris Mason5a3f23d2009-03-31 13:27:11 -04004420 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004421 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004422 if (err)
4423 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004424 }
Yan Zheng9036c102008-10-30 14:19:41 -04004425
Christoph Hellwig10257742010-06-04 11:30:02 +02004426 if (attr->ia_valid) {
4427 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004428 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004429 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004430
Josef Bacik22c44fe2011-11-30 10:45:38 -05004431 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004432 err = btrfs_acl_chmod(inode);
4433 }
4434
Chris Mason39279cc2007-06-12 06:35:45 -04004435 return err;
4436}
Chris Mason61295eb2008-01-14 16:24:38 -05004437
Al Virobd555972010-06-07 11:35:40 -04004438void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004439{
4440 struct btrfs_trans_handle *trans;
4441 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004442 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004443 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004444 int ret;
4445
liubo1abe9b82011-03-24 11:18:59 +00004446 trace_btrfs_inode_evict(inode);
4447
Chris Mason39279cc2007-06-12 06:35:45 -04004448 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004449 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004450 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004451 goto no_delete;
4452
Chris Mason39279cc2007-06-12 06:35:45 -04004453 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004454 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004455 goto no_delete;
4456 }
Al Virobd555972010-06-07 11:35:40 -04004457 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004458 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004459
Yan, Zhengc71bf092009-11-12 09:34:40 +00004460 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004461 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004462 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004463 goto no_delete;
4464 }
4465
Yan, Zheng76dda932009-09-21 16:00:26 -04004466 if (inode->i_nlink > 0) {
4467 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4468 goto no_delete;
4469 }
4470
Miao Xie0e8c36a2012-12-19 06:59:51 +00004471 ret = btrfs_commit_inode_delayed_inode(inode);
4472 if (ret) {
4473 btrfs_orphan_del(NULL, inode);
4474 goto no_delete;
4475 }
4476
Miao Xie66d8f3d2012-09-06 04:02:28 -06004477 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004478 if (!rsv) {
4479 btrfs_orphan_del(NULL, inode);
4480 goto no_delete;
4481 }
Josef Bacik4a338542011-08-29 11:01:31 -04004482 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004483 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004484 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004485
Chris Masondbe674a2008-07-17 12:54:05 -04004486 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004487
Josef Bacik4289a662011-08-05 13:22:24 -04004488 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004489 * This is a bit simpler than btrfs_truncate since we've already
4490 * reserved our space for our orphan item in the unlink, so we just
4491 * need to reserve some slack space in case we add bytes and update
4492 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004493 */
Yan, Zheng80825102009-11-12 09:35:36 +00004494 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004495 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4496 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004497
Josef Bacik726c35f2011-09-26 15:46:06 -04004498 /*
4499 * Try and steal from the global reserve since we will
4500 * likely not use this space anyway, we want to try as
4501 * hard as possible to get this to work.
4502 */
4503 if (ret)
4504 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4505
Yan, Zhengd68fc572010-05-16 10:49:58 -04004506 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004507 btrfs_warn(root->fs_info,
4508 "Could not get space for a delete, will truncate on mount %d",
4509 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004510 btrfs_orphan_del(NULL, inode);
4511 btrfs_free_block_rsv(root, rsv);
4512 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004513 }
4514
Miao Xie0e8c36a2012-12-19 06:59:51 +00004515 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004516 if (IS_ERR(trans)) {
4517 btrfs_orphan_del(NULL, inode);
4518 btrfs_free_block_rsv(root, rsv);
4519 goto no_delete;
4520 }
4521
4522 trans->block_rsv = rsv;
4523
Yan, Zhengd68fc572010-05-16 10:49:58 -04004524 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004525 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004526 break;
4527
Miao Xie8407aa42012-09-07 01:43:32 -06004528 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004529 btrfs_end_transaction(trans, root);
4530 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004531 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004532 }
4533
Josef Bacik4289a662011-08-05 13:22:24 -04004534 btrfs_free_block_rsv(root, rsv);
4535
Josef Bacik4ef31a42013-08-13 14:10:08 -04004536 /*
4537 * Errors here aren't a big deal, it just means we leave orphan items
4538 * in the tree. They will be cleaned up on the next mount.
4539 */
Yan, Zheng80825102009-11-12 09:35:36 +00004540 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004541 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004542 btrfs_orphan_del(trans, inode);
4543 } else {
4544 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004545 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004546
Josef Bacik4289a662011-08-05 13:22:24 -04004547 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004548 if (!(root == root->fs_info->tree_root ||
4549 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004550 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004551
Chris Mason54aa1f42007-06-22 14:16:25 -04004552 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004553 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004554no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004555 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004556 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004557 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004558}
4559
4560/*
4561 * this returns the key found in the dir entry in the location pointer.
4562 * If no dir entries were found, location->objectid is 0.
4563 */
4564static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4565 struct btrfs_key *location)
4566{
4567 const char *name = dentry->d_name.name;
4568 int namelen = dentry->d_name.len;
4569 struct btrfs_dir_item *di;
4570 struct btrfs_path *path;
4571 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004572 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004573
4574 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004575 if (!path)
4576 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004577
Li Zefan33345d012011-04-20 10:31:50 +08004578 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004579 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004580 if (IS_ERR(di))
4581 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004582
David Sterbac7040052011-04-19 18:00:01 +02004583 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004584 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004585
Chris Mason5f39d392007-10-15 16:14:19 -04004586 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004587out:
Chris Mason39279cc2007-06-12 06:35:45 -04004588 btrfs_free_path(path);
4589 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004590out_err:
4591 location->objectid = 0;
4592 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004593}
4594
4595/*
4596 * when we hit a tree root in a directory, the btrfs part of the inode
4597 * needs to be changed to reflect the root directory of the tree root. This
4598 * is kind of like crossing a mount point.
4599 */
4600static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004601 struct inode *dir,
4602 struct dentry *dentry,
4603 struct btrfs_key *location,
4604 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004605{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004606 struct btrfs_path *path;
4607 struct btrfs_root *new_root;
4608 struct btrfs_root_ref *ref;
4609 struct extent_buffer *leaf;
4610 int ret;
4611 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004612
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004613 path = btrfs_alloc_path();
4614 if (!path) {
4615 err = -ENOMEM;
4616 goto out;
4617 }
Chris Mason39279cc2007-06-12 06:35:45 -04004618
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004619 err = -ENOENT;
4620 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4621 BTRFS_I(dir)->root->root_key.objectid,
4622 location->objectid);
4623 if (ret) {
4624 if (ret < 0)
4625 err = ret;
4626 goto out;
4627 }
Chris Mason39279cc2007-06-12 06:35:45 -04004628
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004629 leaf = path->nodes[0];
4630 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004631 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004632 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4633 goto out;
4634
4635 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4636 (unsigned long)(ref + 1),
4637 dentry->d_name.len);
4638 if (ret)
4639 goto out;
4640
David Sterbab3b4aa72011-04-21 01:20:15 +02004641 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004642
4643 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4644 if (IS_ERR(new_root)) {
4645 err = PTR_ERR(new_root);
4646 goto out;
4647 }
4648
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004649 *sub_root = new_root;
4650 location->objectid = btrfs_root_dirid(&new_root->root_item);
4651 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004652 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004653 err = 0;
4654out:
4655 btrfs_free_path(path);
4656 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004657}
4658
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004659static void inode_tree_add(struct inode *inode)
4660{
4661 struct btrfs_root *root = BTRFS_I(inode)->root;
4662 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004663 struct rb_node **p;
4664 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004665 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004666
Al Viro1d3382c2010-10-23 15:19:20 -04004667 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004668 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004669again:
4670 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004671 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004672 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004673 while (*p) {
4674 parent = *p;
4675 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4676
Li Zefan33345d012011-04-20 10:31:50 +08004677 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004678 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004679 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004680 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004681 else {
4682 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004683 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004684 rb_erase(parent, &root->inode_tree);
4685 RB_CLEAR_NODE(parent);
4686 spin_unlock(&root->inode_lock);
4687 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004688 }
4689 }
4690 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4691 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4692 spin_unlock(&root->inode_lock);
4693}
4694
4695static void inode_tree_del(struct inode *inode)
4696{
4697 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004698 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004699
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004700 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004701 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004702 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004703 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004704 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004705 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004706 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004707
Josef Bacik0af3d002010-06-21 14:48:16 -04004708 /*
4709 * Free space cache has inodes in the tree root, but the tree root has a
4710 * root_refs of 0, so this could end up dropping the tree root as a
4711 * snapshot, so we need the extra !root->fs_info->tree_root check to
4712 * make sure we don't drop it.
4713 */
4714 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4715 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004716 synchronize_srcu(&root->fs_info->subvol_srcu);
4717 spin_lock(&root->inode_lock);
4718 empty = RB_EMPTY_ROOT(&root->inode_tree);
4719 spin_unlock(&root->inode_lock);
4720 if (empty)
4721 btrfs_add_dead_root(root);
4722 }
4723}
4724
Jeff Mahoney143bede2012-03-01 14:56:26 +01004725void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004726{
4727 struct rb_node *node;
4728 struct rb_node *prev;
4729 struct btrfs_inode *entry;
4730 struct inode *inode;
4731 u64 objectid = 0;
4732
4733 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4734
4735 spin_lock(&root->inode_lock);
4736again:
4737 node = root->inode_tree.rb_node;
4738 prev = NULL;
4739 while (node) {
4740 prev = node;
4741 entry = rb_entry(node, struct btrfs_inode, rb_node);
4742
Li Zefan33345d012011-04-20 10:31:50 +08004743 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004744 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004745 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004746 node = node->rb_right;
4747 else
4748 break;
4749 }
4750 if (!node) {
4751 while (prev) {
4752 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004753 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004754 node = prev;
4755 break;
4756 }
4757 prev = rb_next(prev);
4758 }
4759 }
4760 while (node) {
4761 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004762 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004763 inode = igrab(&entry->vfs_inode);
4764 if (inode) {
4765 spin_unlock(&root->inode_lock);
4766 if (atomic_read(&inode->i_count) > 1)
4767 d_prune_aliases(inode);
4768 /*
Al Viro45321ac2010-06-07 13:43:19 -04004769 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004770 * the inode cache when its usage count
4771 * hits zero.
4772 */
4773 iput(inode);
4774 cond_resched();
4775 spin_lock(&root->inode_lock);
4776 goto again;
4777 }
4778
4779 if (cond_resched_lock(&root->inode_lock))
4780 goto again;
4781
4782 node = rb_next(node);
4783 }
4784 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004785}
4786
Chris Masone02119d2008-09-05 16:13:11 -04004787static int btrfs_init_locked_inode(struct inode *inode, void *p)
4788{
4789 struct btrfs_iget_args *args = p;
4790 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004791 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004792 return 0;
4793}
4794
4795static int btrfs_find_actor(struct inode *inode, void *opaque)
4796{
4797 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004798 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004799 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004800}
4801
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004802static struct inode *btrfs_iget_locked(struct super_block *s,
4803 u64 objectid,
4804 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004805{
4806 struct inode *inode;
4807 struct btrfs_iget_args args;
4808 args.ino = objectid;
4809 args.root = root;
4810
4811 inode = iget5_locked(s, objectid, btrfs_find_actor,
4812 btrfs_init_locked_inode,
4813 (void *)&args);
4814 return inode;
4815}
4816
Balaji Rao1a54ef82008-07-21 02:01:04 +05304817/* Get an inode object given its location and corresponding root.
4818 * Returns in *is_new if the inode was read from disk
4819 */
4820struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004821 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304822{
4823 struct inode *inode;
4824
4825 inode = btrfs_iget_locked(s, location->objectid, root);
4826 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004827 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304828
4829 if (inode->i_state & I_NEW) {
4830 BTRFS_I(inode)->root = root;
4831 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4832 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004833 if (!is_bad_inode(inode)) {
4834 inode_tree_add(inode);
4835 unlock_new_inode(inode);
4836 if (new)
4837 *new = 1;
4838 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004839 unlock_new_inode(inode);
4840 iput(inode);
4841 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004842 }
4843 }
4844
Balaji Rao1a54ef82008-07-21 02:01:04 +05304845 return inode;
4846}
4847
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004848static struct inode *new_simple_dir(struct super_block *s,
4849 struct btrfs_key *key,
4850 struct btrfs_root *root)
4851{
4852 struct inode *inode = new_inode(s);
4853
4854 if (!inode)
4855 return ERR_PTR(-ENOMEM);
4856
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004857 BTRFS_I(inode)->root = root;
4858 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004859 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004860
4861 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004862 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004863 inode->i_fop = &simple_dir_operations;
4864 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4865 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4866
4867 return inode;
4868}
4869
Chris Mason3de45862008-11-17 21:02:50 -05004870struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004871{
Chris Masond3977122009-01-05 21:25:51 -05004872 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004873 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004874 struct btrfs_root *sub_root = root;
4875 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004876 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004877 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004878
4879 if (dentry->d_name.len > BTRFS_NAME_LEN)
4880 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004881
Jeff Layton39e3c952012-11-28 11:30:53 -05004882 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04004883 if (ret < 0)
4884 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004885
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004886 if (location.objectid == 0)
4887 return NULL;
4888
4889 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004890 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004891 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004892 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004893
4894 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4895
Yan, Zheng76dda932009-09-21 16:00:26 -04004896 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004897 ret = fixup_tree_root_location(root, dir, dentry,
4898 &location, &sub_root);
4899 if (ret < 0) {
4900 if (ret != -ENOENT)
4901 inode = ERR_PTR(ret);
4902 else
4903 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4904 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004905 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004906 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004907 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4908
Julia Lawall34d19ba2011-01-24 19:55:19 +00004909 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004910 down_read(&root->fs_info->cleanup_work_sem);
4911 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004912 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004913 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04004914 if (ret) {
4915 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004916 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04004917 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00004918 }
4919
Chris Mason3de45862008-11-17 21:02:50 -05004920 return inode;
4921}
4922
Nick Pigginfe15ce42011-01-07 17:49:23 +11004923static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004924{
4925 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004926 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004927
Li Zefan848cce02012-02-21 17:04:28 +08004928 if (!inode && !IS_ROOT(dentry))
4929 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004930
Li Zefan848cce02012-02-21 17:04:28 +08004931 if (inode) {
4932 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004933 if (btrfs_root_refs(&root->root_item) == 0)
4934 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004935
4936 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4937 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004938 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004939 return 0;
4940}
4941
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004942static void btrfs_dentry_release(struct dentry *dentry)
4943{
4944 if (dentry->d_fsdata)
4945 kfree(dentry->d_fsdata);
4946}
4947
Chris Mason3de45862008-11-17 21:02:50 -05004948static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004949 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004950{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004951 struct dentry *ret;
4952
4953 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04004954 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004955}
4956
Miao Xie16cdcec2011-04-22 18:12:22 +08004957unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004958 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4959};
4960
Al Viro9cdda8d2013-05-22 16:48:09 -04004961static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04004962{
Al Viro9cdda8d2013-05-22 16:48:09 -04004963 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04004964 struct btrfs_root *root = BTRFS_I(inode)->root;
4965 struct btrfs_item *item;
4966 struct btrfs_dir_item *di;
4967 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004968 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004969 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004970 struct list_head ins_list;
4971 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004972 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004973 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004974 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004975 unsigned char d_type;
4976 int over = 0;
4977 u32 di_cur;
4978 u32 di_total;
4979 u32 di_len;
4980 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004981 char tmp_name[32];
4982 char *name_ptr;
4983 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04004984 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004985
4986 /* FIXME, use a real flag for deciding about the key type */
4987 if (root->fs_info->tree_root == root)
4988 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004989
Al Viro9cdda8d2013-05-22 16:48:09 -04004990 if (!dir_emit_dots(file, ctx))
4991 return 0;
4992
David Woodhouse49593bf2008-08-17 17:08:36 +01004993 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004994 if (!path)
4995 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004996
Josef Bacik026fd312011-05-13 10:32:11 -04004997 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004998
Miao Xie16cdcec2011-04-22 18:12:22 +08004999 if (key_type == BTRFS_DIR_INDEX_KEY) {
5000 INIT_LIST_HEAD(&ins_list);
5001 INIT_LIST_HEAD(&del_list);
5002 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5003 }
5004
Chris Mason39279cc2007-06-12 06:35:45 -04005005 btrfs_set_key_type(&key, key_type);
Al Viro9cdda8d2013-05-22 16:48:09 -04005006 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005007 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005008
Chris Mason39279cc2007-06-12 06:35:45 -04005009 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5010 if (ret < 0)
5011 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005012
5013 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005014 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005015 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005016 if (slot >= btrfs_header_nritems(leaf)) {
5017 ret = btrfs_next_leaf(root, path);
5018 if (ret < 0)
5019 goto err;
5020 else if (ret > 0)
5021 break;
5022 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005023 }
Chris Mason3de45862008-11-17 21:02:50 -05005024
Chris Mason5f39d392007-10-15 16:14:19 -04005025 item = btrfs_item_nr(leaf, slot);
5026 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5027
5028 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005029 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005030 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005031 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005032 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005033 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005034 if (key_type == BTRFS_DIR_INDEX_KEY &&
5035 btrfs_should_delete_dir_index(&del_list,
5036 found_key.offset))
5037 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005038
Al Viro9cdda8d2013-05-22 16:48:09 -04005039 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005040 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005041
Chris Mason39279cc2007-06-12 06:35:45 -04005042 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5043 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005044 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005045
5046 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005047 struct btrfs_key location;
5048
Josef Bacik22a94d42011-03-16 16:47:17 -04005049 if (verify_dir_item(root, leaf, di))
5050 break;
5051
Chris Mason5f39d392007-10-15 16:14:19 -04005052 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005053 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005054 name_ptr = tmp_name;
5055 } else {
5056 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005057 if (!name_ptr) {
5058 ret = -ENOMEM;
5059 goto err;
5060 }
Chris Mason5f39d392007-10-15 16:14:19 -04005061 }
5062 read_extent_buffer(leaf, name_ptr,
5063 (unsigned long)(di + 1), name_len);
5064
5065 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5066 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005067
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005068
Chris Mason3de45862008-11-17 21:02:50 -05005069 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005070 * skip it.
5071 *
5072 * In contrast to old kernels, we insert the snapshot's
5073 * dir item and dir index after it has been created, so
5074 * we won't find a reference to our own snapshot. We
5075 * still keep the following code for backward
5076 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005077 */
5078 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5079 location.objectid == root->root_key.objectid) {
5080 over = 0;
5081 goto skip;
5082 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005083 over = !dir_emit(ctx, name_ptr, name_len,
5084 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005085
Chris Mason3de45862008-11-17 21:02:50 -05005086skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005087 if (name_ptr != tmp_name)
5088 kfree(name_ptr);
5089
Chris Mason39279cc2007-06-12 06:35:45 -04005090 if (over)
5091 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005092 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005093 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005094 di_cur += di_len;
5095 di = (struct btrfs_dir_item *)((char *)di + di_len);
5096 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005097next:
5098 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005099 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005100
Miao Xie16cdcec2011-04-22 18:12:22 +08005101 if (key_type == BTRFS_DIR_INDEX_KEY) {
5102 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005103 ctx->pos++;
5104 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005105 if (ret)
5106 goto nopos;
5107 }
5108
David Woodhouse49593bf2008-08-17 17:08:36 +01005109 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005110 ctx->pos++;
5111
5112 /*
5113 * Stop new entries from being returned after we return the last
5114 * entry.
5115 *
5116 * New directory entries are assigned a strictly increasing
5117 * offset. This means that new entries created during readdir
5118 * are *guaranteed* to be seen in the future by that readdir.
5119 * This has broken buggy programs which operate on names as
5120 * they're returned by readdir. Until we re-use freed offsets
5121 * we have this hack to stop new entries from being returned
5122 * under the assumption that they'll never reach this huge
5123 * offset.
5124 *
5125 * This is being careful not to overflow 32bit loff_t unless the
5126 * last entry requires it because doing so has broken 32bit apps
5127 * in the past.
5128 */
5129 if (key_type == BTRFS_DIR_INDEX_KEY) {
5130 if (ctx->pos >= INT_MAX)
5131 ctx->pos = LLONG_MAX;
5132 else
5133 ctx->pos = INT_MAX;
5134 }
Chris Mason39279cc2007-06-12 06:35:45 -04005135nopos:
5136 ret = 0;
5137err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005138 if (key_type == BTRFS_DIR_INDEX_KEY)
5139 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005140 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005141 return ret;
5142}
5143
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005144int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005145{
5146 struct btrfs_root *root = BTRFS_I(inode)->root;
5147 struct btrfs_trans_handle *trans;
5148 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005149 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005150
Josef Bacik72ac3c02012-05-23 14:13:11 -04005151 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005152 return 0;
5153
Liu Bo83eea1f2012-07-10 05:28:39 -06005154 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005155 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005156
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005157 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005158 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005159 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005160 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005161 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005162 if (IS_ERR(trans))
5163 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005164 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005165 }
5166 return ret;
5167}
5168
5169/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005170 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005171 * inode changes. But, it is most likely to find the inode in cache.
5172 * FIXME, needs more benchmarking...there are no reasons other than performance
5173 * to keep or drop this code.
5174 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005175static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005176{
5177 struct btrfs_root *root = BTRFS_I(inode)->root;
5178 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005179 int ret;
5180
Josef Bacik72ac3c02012-05-23 14:13:11 -04005181 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005182 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005183
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005184 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005185 if (IS_ERR(trans))
5186 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005187
5188 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005189 if (ret && ret == -ENOSPC) {
5190 /* whoops, lets try again with the full transaction */
5191 btrfs_end_transaction(trans, root);
5192 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005193 if (IS_ERR(trans))
5194 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005195
Chris Mason94b60442010-05-26 11:02:00 -04005196 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005197 }
Chris Mason39279cc2007-06-12 06:35:45 -04005198 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005199 if (BTRFS_I(inode)->delayed_node)
5200 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005201
5202 return ret;
5203}
5204
5205/*
5206 * This is a copy of file_update_time. We need this so we can return error on
5207 * ENOSPC for updating the inode in the case of file write and mmap writes.
5208 */
Josef Bacike41f9412012-03-26 09:46:47 -04005209static int btrfs_update_time(struct inode *inode, struct timespec *now,
5210 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005211{
Alexander Block2bc5565282012-06-15 09:49:33 +02005212 struct btrfs_root *root = BTRFS_I(inode)->root;
5213
5214 if (btrfs_root_readonly(root))
5215 return -EROFS;
5216
Josef Bacike41f9412012-03-26 09:46:47 -04005217 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005218 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005219 if (flags & S_CTIME)
5220 inode->i_ctime = *now;
5221 if (flags & S_MTIME)
5222 inode->i_mtime = *now;
5223 if (flags & S_ATIME)
5224 inode->i_atime = *now;
5225 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005226}
5227
Chris Masond352ac62008-09-29 15:18:18 -04005228/*
5229 * find the highest existing sequence number in a directory
5230 * and then set the in-memory index_cnt variable to reflect
5231 * free sequence numbers
5232 */
Josef Bacikaec74772008-07-24 12:12:38 -04005233static int btrfs_set_inode_index_count(struct inode *inode)
5234{
5235 struct btrfs_root *root = BTRFS_I(inode)->root;
5236 struct btrfs_key key, found_key;
5237 struct btrfs_path *path;
5238 struct extent_buffer *leaf;
5239 int ret;
5240
Li Zefan33345d012011-04-20 10:31:50 +08005241 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005242 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5243 key.offset = (u64)-1;
5244
5245 path = btrfs_alloc_path();
5246 if (!path)
5247 return -ENOMEM;
5248
5249 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5250 if (ret < 0)
5251 goto out;
5252 /* FIXME: we should be able to handle this */
5253 if (ret == 0)
5254 goto out;
5255 ret = 0;
5256
5257 /*
5258 * MAGIC NUMBER EXPLANATION:
5259 * since we search a directory based on f_pos we have to start at 2
5260 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5261 * else has to start at 2
5262 */
5263 if (path->slots[0] == 0) {
5264 BTRFS_I(inode)->index_cnt = 2;
5265 goto out;
5266 }
5267
5268 path->slots[0]--;
5269
5270 leaf = path->nodes[0];
5271 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5272
Li Zefan33345d012011-04-20 10:31:50 +08005273 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005274 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5275 BTRFS_I(inode)->index_cnt = 2;
5276 goto out;
5277 }
5278
5279 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5280out:
5281 btrfs_free_path(path);
5282 return ret;
5283}
5284
Chris Masond352ac62008-09-29 15:18:18 -04005285/*
5286 * helper to find a free sequence number in a given directory. This current
5287 * code is very simple, later versions will do smarter things in the btree
5288 */
Chris Mason3de45862008-11-17 21:02:50 -05005289int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005290{
5291 int ret = 0;
5292
5293 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005294 ret = btrfs_inode_delayed_dir_index_count(dir);
5295 if (ret) {
5296 ret = btrfs_set_inode_index_count(dir);
5297 if (ret)
5298 return ret;
5299 }
Josef Bacikaec74772008-07-24 12:12:38 -04005300 }
5301
Chris Mason00e4e6b2008-08-05 11:18:09 -04005302 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005303 BTRFS_I(dir)->index_cnt++;
5304
5305 return ret;
5306}
5307
Chris Mason39279cc2007-06-12 06:35:45 -04005308static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5309 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005310 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005311 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005312 u64 ref_objectid, u64 objectid,
5313 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005314{
5315 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005316 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005317 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005318 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005319 struct btrfs_inode_ref *ref;
5320 struct btrfs_key key[2];
5321 u32 sizes[2];
5322 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005323 int ret;
5324 int owner;
5325
Chris Mason5f39d392007-10-15 16:14:19 -04005326 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005327 if (!path)
5328 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005329
Chris Mason39279cc2007-06-12 06:35:45 -04005330 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005331 if (!inode) {
5332 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005333 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005334 }
Chris Mason39279cc2007-06-12 06:35:45 -04005335
Li Zefan581bb052011-04-20 10:06:11 +08005336 /*
5337 * we have to initialize this early, so we can reclaim the inode
5338 * number if we fail afterwards in this function.
5339 */
5340 inode->i_ino = objectid;
5341
Josef Bacikaec74772008-07-24 12:12:38 -04005342 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005343 trace_btrfs_inode_request(dir);
5344
Chris Mason3de45862008-11-17 21:02:50 -05005345 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005346 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005347 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005348 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005349 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005350 }
Josef Bacikaec74772008-07-24 12:12:38 -04005351 }
5352 /*
5353 * index_cnt is ignored for everything but a dir,
5354 * btrfs_get_inode_index_count has an explanation for the magic
5355 * number
5356 */
5357 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005358 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005359 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005360 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005361
Josef Bacik5dc562c2012-08-17 13:14:17 -04005362 /*
5363 * We could have gotten an inode number from somebody who was fsynced
5364 * and then removed in this same transaction, so let's just set full
5365 * sync since it will be a full sync anyway and this will blow away the
5366 * old info in the log.
5367 */
5368 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5369
Al Viro569254b2011-07-24 17:08:40 -04005370 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005371 owner = 0;
5372 else
5373 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005374
5375 key[0].objectid = objectid;
5376 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5377 key[0].offset = 0;
5378
Mark Fashehf1863732012-08-08 11:32:27 -07005379 /*
5380 * Start new inodes with an inode_ref. This is slightly more
5381 * efficient for small numbers of hard links since they will
5382 * be packed into one item. Extended refs will kick in if we
5383 * add more hard links than can fit in the ref item.
5384 */
Chris Mason9c583092008-01-29 15:15:18 -05005385 key[1].objectid = objectid;
5386 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5387 key[1].offset = ref_objectid;
5388
5389 sizes[0] = sizeof(struct btrfs_inode_item);
5390 sizes[1] = name_len + sizeof(*ref);
5391
Chris Masonb9473432009-03-13 11:00:37 -04005392 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005393 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5394 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005395 goto fail;
5396
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005397 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005398 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005399 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005400 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5401 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005402 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5403 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005404 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005405
5406 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5407 struct btrfs_inode_ref);
5408 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005409 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005410 ptr = (unsigned long)(ref + 1);
5411 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5412
Chris Mason5f39d392007-10-15 16:14:19 -04005413 btrfs_mark_buffer_dirty(path->nodes[0]);
5414 btrfs_free_path(path);
5415
Chris Mason39279cc2007-06-12 06:35:45 -04005416 location = &BTRFS_I(inode)->location;
5417 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005418 location->offset = 0;
5419 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5420
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005421 btrfs_inherit_iflags(inode, dir);
5422
Al Viro569254b2011-07-24 17:08:40 -04005423 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005424 if (btrfs_test_opt(root, NODATASUM))
5425 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005426 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005427 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5428 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005429 }
5430
Chris Mason39279cc2007-06-12 06:35:45 -04005431 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005432 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005433
5434 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005435 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005436
Alexander Block8ea05e32012-07-25 17:35:53 +02005437 btrfs_update_root_times(trans, root);
5438
Chris Mason39279cc2007-06-12 06:35:45 -04005439 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005440fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005441 if (dir)
5442 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005443 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005444 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005445 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005446}
5447
5448static inline u8 btrfs_inode_type(struct inode *inode)
5449{
5450 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5451}
5452
Chris Masond352ac62008-09-29 15:18:18 -04005453/*
5454 * utility function to add 'inode' into 'parent_inode' with
5455 * a give name and a given sequence number.
5456 * if 'add_backref' is true, also insert a backref from the
5457 * inode to the parent directory.
5458 */
Chris Masone02119d2008-09-05 16:13:11 -04005459int btrfs_add_link(struct btrfs_trans_handle *trans,
5460 struct inode *parent_inode, struct inode *inode,
5461 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005462{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005463 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005464 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005465 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005466 u64 ino = btrfs_ino(inode);
5467 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005468
Li Zefan33345d012011-04-20 10:31:50 +08005469 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005470 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5471 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005472 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005473 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5474 key.offset = 0;
5475 }
Chris Mason39279cc2007-06-12 06:35:45 -04005476
Li Zefan33345d012011-04-20 10:31:50 +08005477 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005478 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5479 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005480 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005481 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005482 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5483 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005484 }
5485
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005486 /* Nothing to clean up yet */
5487 if (ret)
5488 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005489
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005490 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5491 parent_inode, &key,
5492 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005493 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005494 goto fail_dir_item;
5495 else if (ret) {
5496 btrfs_abort_transaction(trans, root, ret);
5497 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005498 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005499
5500 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5501 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005502 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005503 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5504 ret = btrfs_update_inode(trans, root, parent_inode);
5505 if (ret)
5506 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005507 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005508
5509fail_dir_item:
5510 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5511 u64 local_index;
5512 int err;
5513 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5514 key.objectid, root->root_key.objectid,
5515 parent_ino, &local_index, name, name_len);
5516
5517 } else if (add_backref) {
5518 u64 local_index;
5519 int err;
5520
5521 err = btrfs_del_inode_ref(trans, root, name, name_len,
5522 ino, parent_ino, &local_index);
5523 }
5524 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005525}
5526
5527static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005528 struct inode *dir, struct dentry *dentry,
5529 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005530{
Josef Bacika1b075d2010-11-19 20:36:11 +00005531 int err = btrfs_add_link(trans, dir, inode,
5532 dentry->d_name.name, dentry->d_name.len,
5533 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005534 if (err > 0)
5535 err = -EEXIST;
5536 return err;
5537}
5538
Josef Bacik618e21d2007-07-11 10:18:17 -04005539static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005540 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005541{
5542 struct btrfs_trans_handle *trans;
5543 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005544 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005545 int err;
5546 int drop_inode = 0;
5547 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005548 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005549
5550 if (!new_valid_dev(rdev))
5551 return -EINVAL;
5552
Josef Bacik9ed74f22009-09-11 16:12:44 -04005553 /*
5554 * 2 for inode item and ref
5555 * 2 for dir items
5556 * 1 for xattr if selinux is on
5557 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005558 trans = btrfs_start_transaction(root, 5);
5559 if (IS_ERR(trans))
5560 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005561
Li Zefan581bb052011-04-20 10:06:11 +08005562 err = btrfs_find_free_ino(root, &objectid);
5563 if (err)
5564 goto out_unlock;
5565
Josef Bacikaec74772008-07-24 12:12:38 -04005566 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005567 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005568 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005569 if (IS_ERR(inode)) {
5570 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005571 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005572 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005573
Eric Paris2a7dba32011-02-01 11:05:39 -05005574 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005575 if (err) {
5576 drop_inode = 1;
5577 goto out_unlock;
5578 }
5579
Casey Schauflerad19db72011-12-15 10:09:07 -05005580 /*
5581 * If the active LSM wants to access the inode during
5582 * d_instantiate it needs these. Smack checks to see
5583 * if the filesystem supports xattrs by looking at the
5584 * ops vector.
5585 */
5586
5587 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005588 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005589 if (err)
5590 drop_inode = 1;
5591 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005592 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005593 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005594 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005595 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005596out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005597 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005598 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005599 if (drop_inode) {
5600 inode_dec_link_count(inode);
5601 iput(inode);
5602 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005603 return err;
5604}
5605
Chris Mason39279cc2007-06-12 06:35:45 -04005606static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005607 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005608{
5609 struct btrfs_trans_handle *trans;
5610 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005611 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005612 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005613 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005614 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005615 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005616
Josef Bacik9ed74f22009-09-11 16:12:44 -04005617 /*
5618 * 2 for inode item and ref
5619 * 2 for dir items
5620 * 1 for xattr if selinux is on
5621 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005622 trans = btrfs_start_transaction(root, 5);
5623 if (IS_ERR(trans))
5624 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005625
Li Zefan581bb052011-04-20 10:06:11 +08005626 err = btrfs_find_free_ino(root, &objectid);
5627 if (err)
5628 goto out_unlock;
5629
Josef Bacikaec74772008-07-24 12:12:38 -04005630 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005631 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005632 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005633 if (IS_ERR(inode)) {
5634 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005635 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005636 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005637 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005638
Eric Paris2a7dba32011-02-01 11:05:39 -05005639 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005640 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005641 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005642
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005643 err = btrfs_update_inode(trans, root, inode);
5644 if (err)
5645 goto out_unlock;
5646
Casey Schauflerad19db72011-12-15 10:09:07 -05005647 /*
5648 * If the active LSM wants to access the inode during
5649 * d_instantiate it needs these. Smack checks to see
5650 * if the filesystem supports xattrs by looking at the
5651 * ops vector.
5652 */
5653 inode->i_fop = &btrfs_file_operations;
5654 inode->i_op = &btrfs_file_inode_operations;
5655
Josef Bacika1b075d2010-11-19 20:36:11 +00005656 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005657 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005658 goto out_unlock;
5659
5660 inode->i_mapping->a_ops = &btrfs_aops;
5661 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5662 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5663 d_instantiate(dentry, inode);
5664
Chris Mason39279cc2007-06-12 06:35:45 -04005665out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005666 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005667 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005668 inode_dec_link_count(inode);
5669 iput(inode);
5670 }
Liu Bob53d3f52012-11-14 14:34:34 +00005671 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005672 return err;
5673}
5674
5675static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5676 struct dentry *dentry)
5677{
5678 struct btrfs_trans_handle *trans;
5679 struct btrfs_root *root = BTRFS_I(dir)->root;
5680 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005681 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005682 int err;
5683 int drop_inode = 0;
5684
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005685 /* do not allow sys_link's with other subvols of the same device */
5686 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005687 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005688
Mark Fashehf1863732012-08-08 11:32:27 -07005689 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005690 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005691
Chris Mason3de45862008-11-17 21:02:50 -05005692 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005693 if (err)
5694 goto fail;
5695
Yan, Zhenga22285a2010-05-16 10:48:46 -04005696 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005697 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005698 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005699 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005700 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005701 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005702 if (IS_ERR(trans)) {
5703 err = PTR_ERR(trans);
5704 goto fail;
5705 }
Chris Mason5f39d392007-10-15 16:14:19 -04005706
Miao Xie31534952011-04-13 13:19:21 +08005707 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005708 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005709 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6e2010-10-23 11:11:40 -04005710 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005711 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005712
Josef Bacika1b075d2010-11-19 20:36:11 +00005713 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005714
Yan, Zhenga5719522009-09-24 09:17:31 -04005715 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005716 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005717 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005718 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005719 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005720 if (err)
5721 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005722 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005723 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005724 }
Chris Mason39279cc2007-06-12 06:35:45 -04005725
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005726 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005727fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005728 if (drop_inode) {
5729 inode_dec_link_count(inode);
5730 iput(inode);
5731 }
Liu Bob53d3f52012-11-14 14:34:34 +00005732 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005733 return err;
5734}
5735
Al Viro18bb1db2011-07-26 01:41:39 -04005736static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005737{
Chris Masonb9d86662008-05-02 16:13:49 -04005738 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005739 struct btrfs_trans_handle *trans;
5740 struct btrfs_root *root = BTRFS_I(dir)->root;
5741 int err = 0;
5742 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005743 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005744 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005745
Josef Bacik9ed74f22009-09-11 16:12:44 -04005746 /*
5747 * 2 items for inode and ref
5748 * 2 items for dir items
5749 * 1 for xattr if selinux is on
5750 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005751 trans = btrfs_start_transaction(root, 5);
5752 if (IS_ERR(trans))
5753 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005754
Li Zefan581bb052011-04-20 10:06:11 +08005755 err = btrfs_find_free_ino(root, &objectid);
5756 if (err)
5757 goto out_fail;
5758
Josef Bacikaec74772008-07-24 12:12:38 -04005759 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005760 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005761 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005762 if (IS_ERR(inode)) {
5763 err = PTR_ERR(inode);
5764 goto out_fail;
5765 }
Chris Mason5f39d392007-10-15 16:14:19 -04005766
Chris Mason39279cc2007-06-12 06:35:45 -04005767 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005768
Eric Paris2a7dba32011-02-01 11:05:39 -05005769 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005770 if (err)
5771 goto out_fail;
5772
Chris Mason39279cc2007-06-12 06:35:45 -04005773 inode->i_op = &btrfs_dir_inode_operations;
5774 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005775
Chris Masondbe674a2008-07-17 12:54:05 -04005776 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005777 err = btrfs_update_inode(trans, root, inode);
5778 if (err)
5779 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005780
Josef Bacika1b075d2010-11-19 20:36:11 +00005781 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5782 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005783 if (err)
5784 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005785
Chris Mason39279cc2007-06-12 06:35:45 -04005786 d_instantiate(dentry, inode);
5787 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005788
5789out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005790 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005791 if (drop_on_err)
5792 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005793 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005794 return err;
5795}
5796
Chris Masond352ac62008-09-29 15:18:18 -04005797/* helper for btfs_get_extent. Given an existing extent in the tree,
5798 * and an extent that you want to insert, deal with overlap and insert
5799 * the new extent into the tree.
5800 */
Chris Mason3b951512008-04-17 11:29:12 -04005801static int merge_extent_mapping(struct extent_map_tree *em_tree,
5802 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005803 struct extent_map *em,
5804 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005805{
5806 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005807
Chris Masone6dcd2d2008-07-17 12:53:50 -04005808 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5809 start_diff = map_start - em->start;
5810 em->start = map_start;
5811 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005812 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5813 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005814 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005815 em->block_len -= start_diff;
5816 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005817 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005818}
5819
Chris Masonc8b97812008-10-29 14:49:59 -04005820static noinline int uncompress_inline(struct btrfs_path *path,
5821 struct inode *inode, struct page *page,
5822 size_t pg_offset, u64 extent_offset,
5823 struct btrfs_file_extent_item *item)
5824{
5825 int ret;
5826 struct extent_buffer *leaf = path->nodes[0];
5827 char *tmp;
5828 size_t max_size;
5829 unsigned long inline_size;
5830 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005831 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005832
5833 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005834 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005835 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5836 inline_size = btrfs_file_extent_inline_item_len(leaf,
5837 btrfs_item_nr(leaf, path->slots[0]));
5838 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005839 if (!tmp)
5840 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005841 ptr = btrfs_file_extent_inline_start(item);
5842
5843 read_extent_buffer(leaf, tmp, ptr, inline_size);
5844
Chris Mason5b050f02008-11-11 09:34:41 -05005845 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005846 ret = btrfs_decompress(compress_type, tmp, page,
5847 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005848 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005849 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005850 unsigned long copy_size = min_t(u64,
5851 PAGE_CACHE_SIZE - pg_offset,
5852 max_size - extent_offset);
5853 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005854 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005855 }
5856 kfree(tmp);
5857 return 0;
5858}
5859
Chris Masond352ac62008-09-29 15:18:18 -04005860/*
5861 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005862 * the ugly parts come from merging extents from the disk with the in-ram
5863 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005864 * where the in-ram extents might be locked pending data=ordered completion.
5865 *
5866 * This also copies inline extents directly into the page.
5867 */
Chris Masond3977122009-01-05 21:25:51 -05005868
Chris Masona52d9a82007-08-27 16:49:44 -04005869struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005870 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005871 int create)
5872{
5873 int ret;
5874 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005875 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005876 u64 extent_start = 0;
5877 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005878 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005879 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005880 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005881 struct btrfs_root *root = BTRFS_I(inode)->root;
5882 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005883 struct extent_buffer *leaf;
5884 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005885 struct extent_map *em = NULL;
5886 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005887 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005888 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005889 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005890
Chris Masona52d9a82007-08-27 16:49:44 -04005891again:
Chris Mason890871b2009-09-02 16:24:52 -04005892 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005893 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005894 if (em)
5895 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005896 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005897
Chris Masona52d9a82007-08-27 16:49:44 -04005898 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005899 if (em->start > start || em->start + em->len <= start)
5900 free_extent_map(em);
5901 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005902 free_extent_map(em);
5903 else
5904 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005905 }
David Sterba172ddd62011-04-21 00:48:27 +02005906 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005907 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005908 err = -ENOMEM;
5909 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005910 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005911 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005912 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005913 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005914 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005915 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005916
5917 if (!path) {
5918 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005919 if (!path) {
5920 err = -ENOMEM;
5921 goto out;
5922 }
5923 /*
5924 * Chances are we'll be called again, so go ahead and do
5925 * readahead
5926 */
5927 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005928 }
5929
Chris Mason179e29e2007-11-01 11:28:41 -04005930 ret = btrfs_lookup_file_extent(trans, root, path,
5931 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005932 if (ret < 0) {
5933 err = ret;
5934 goto out;
5935 }
5936
5937 if (ret != 0) {
5938 if (path->slots[0] == 0)
5939 goto not_found;
5940 path->slots[0]--;
5941 }
5942
Chris Mason5f39d392007-10-15 16:14:19 -04005943 leaf = path->nodes[0];
5944 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005945 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005946 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005947 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5948 found_type = btrfs_key_type(&found_key);
5949 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005950 found_type != BTRFS_EXTENT_DATA_KEY) {
5951 goto not_found;
5952 }
5953
Chris Mason5f39d392007-10-15 16:14:19 -04005954 found_type = btrfs_file_extent_type(leaf, item);
5955 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005956 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005957 if (found_type == BTRFS_FILE_EXTENT_REG ||
5958 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005959 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005960 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005961 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5962 size_t size;
5963 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00005964 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005965 }
5966
5967 if (start >= extent_end) {
5968 path->slots[0]++;
5969 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5970 ret = btrfs_next_leaf(root, path);
5971 if (ret < 0) {
5972 err = ret;
5973 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005974 }
Yan Zheng9036c102008-10-30 14:19:41 -04005975 if (ret > 0)
5976 goto not_found;
5977 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005978 }
Yan Zheng9036c102008-10-30 14:19:41 -04005979 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5980 if (found_key.objectid != objectid ||
5981 found_key.type != BTRFS_EXTENT_DATA_KEY)
5982 goto not_found;
5983 if (start + len <= found_key.offset)
5984 goto not_found;
5985 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04005986 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04005987 em->len = found_key.offset - start;
5988 goto not_found_em;
5989 }
5990
Josef Bacikcc95bef2013-04-04 14:31:27 -04005991 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005992 if (found_type == BTRFS_FILE_EXTENT_REG ||
5993 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005994 em->start = extent_start;
5995 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005996 em->orig_start = extent_start -
5997 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05005998 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
5999 item);
Chris Masondb945352007-10-15 16:15:53 -04006000 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6001 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006002 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006003 goto insert;
6004 }
Li Zefan261507a02010-12-17 14:21:50 +08006005 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006006 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006007 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006008 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006009 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006010 } else {
6011 bytenr += btrfs_file_extent_offset(leaf, item);
6012 em->block_start = bytenr;
6013 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006014 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6015 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006016 }
Chris Masona52d9a82007-08-27 16:49:44 -04006017 goto insert;
6018 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006019 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006020 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006021 size_t size;
6022 size_t extent_offset;
6023 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006024
Chris Masona52d9a82007-08-27 16:49:44 -04006025 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006026 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006027 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006028 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006029 goto out;
6030 }
6031
Yan Zheng9036c102008-10-30 14:19:41 -04006032 size = btrfs_file_extent_inline_len(leaf, item);
6033 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006034 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006035 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006036 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006037 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006038 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006039 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006040 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006041 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006042 em->compress_type = compress_type;
6043 }
Yan689f9342007-10-29 11:41:07 -04006044 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006045 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006046 if (btrfs_file_extent_compression(leaf, item) !=
6047 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006048 ret = uncompress_inline(path, inode, page,
6049 pg_offset,
6050 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006051 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006052 } else {
6053 map = kmap(page);
6054 read_extent_buffer(leaf, map + pg_offset, ptr,
6055 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006056 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6057 memset(map + pg_offset + copy_size, 0,
6058 PAGE_CACHE_SIZE - pg_offset -
6059 copy_size);
6060 }
Chris Masonc8b97812008-10-29 14:49:59 -04006061 kunmap(page);
6062 }
Chris Mason179e29e2007-11-01 11:28:41 -04006063 flush_dcache_page(page);
6064 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006065 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006066 if (!trans) {
6067 kunmap(page);
6068 free_extent_map(em);
6069 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006070
David Sterbab3b4aa72011-04-21 01:20:15 +02006071 btrfs_release_path(path);
Josef Bacik7a7eaa402011-04-13 12:54:33 -04006072 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006073
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006074 if (IS_ERR(trans))
6075 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006076 goto again;
6077 }
Chris Masonc8b97812008-10-29 14:49:59 -04006078 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006079 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006080 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006081 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006082 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006083 }
Chris Masond1310b22008-01-24 16:13:08 -05006084 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006085 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006086 goto insert;
6087 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006088 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006089 }
6090not_found:
6091 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006092 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006093 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006094not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006095 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006096 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006097insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006098 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006099 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006100 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006101 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006102 err = -EIO;
6103 goto out;
6104 }
Chris Masond1310b22008-01-24 16:13:08 -05006105
6106 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006107 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006108 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006109 /* it is possible that someone inserted the extent into the tree
6110 * while we had the lock dropped. It is also possible that
6111 * an overlapping map exists in the tree
6112 */
Chris Masona52d9a82007-08-27 16:49:44 -04006113 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006114 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006115
6116 ret = 0;
6117
Chris Mason3b951512008-04-17 11:29:12 -04006118 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006119 if (existing && (existing->start > start ||
6120 existing->start + existing->len <= start)) {
6121 free_extent_map(existing);
6122 existing = NULL;
6123 }
Chris Mason3b951512008-04-17 11:29:12 -04006124 if (!existing) {
6125 existing = lookup_extent_mapping(em_tree, em->start,
6126 em->len);
6127 if (existing) {
6128 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006129 em, start,
6130 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006131 free_extent_map(existing);
6132 if (err) {
6133 free_extent_map(em);
6134 em = NULL;
6135 }
6136 } else {
6137 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006138 free_extent_map(em);
6139 em = NULL;
6140 }
6141 } else {
6142 free_extent_map(em);
6143 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006144 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006145 }
Chris Masona52d9a82007-08-27 16:49:44 -04006146 }
Chris Mason890871b2009-09-02 16:24:52 -04006147 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006148out:
liubo1abe9b82011-03-24 11:18:59 +00006149
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006150 if (em)
6151 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006152
Chris Masonf4219502008-07-22 11:18:09 -04006153 if (path)
6154 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006155 if (trans) {
6156 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006157 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006158 err = ret;
6159 }
Chris Masona52d9a82007-08-27 16:49:44 -04006160 if (err) {
6161 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006162 return ERR_PTR(err);
6163 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006164 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006165 return em;
6166}
6167
Chris Masonec29ed52011-02-23 16:23:20 -05006168struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6169 size_t pg_offset, u64 start, u64 len,
6170 int create)
6171{
6172 struct extent_map *em;
6173 struct extent_map *hole_em = NULL;
6174 u64 range_start = start;
6175 u64 end;
6176 u64 found;
6177 u64 found_end;
6178 int err = 0;
6179
6180 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6181 if (IS_ERR(em))
6182 return em;
6183 if (em) {
6184 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006185 * if our em maps to
6186 * - a hole or
6187 * - a pre-alloc extent,
6188 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006189 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006190 if (em->block_start != EXTENT_MAP_HOLE &&
6191 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006192 return em;
6193 else
6194 hole_em = em;
6195 }
6196
6197 /* check to see if we've wrapped (len == -1 or similar) */
6198 end = start + len;
6199 if (end < start)
6200 end = (u64)-1;
6201 else
6202 end -= 1;
6203
6204 em = NULL;
6205
6206 /* ok, we didn't find anything, lets look for delalloc */
6207 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6208 end, len, EXTENT_DELALLOC, 1);
6209 found_end = range_start + found;
6210 if (found_end < range_start)
6211 found_end = (u64)-1;
6212
6213 /*
6214 * we didn't find anything useful, return
6215 * the original results from get_extent()
6216 */
6217 if (range_start > end || found_end <= start) {
6218 em = hole_em;
6219 hole_em = NULL;
6220 goto out;
6221 }
6222
6223 /* adjust the range_start to make sure it doesn't
6224 * go backwards from the start they passed in
6225 */
6226 range_start = max(start,range_start);
6227 found = found_end - range_start;
6228
6229 if (found > 0) {
6230 u64 hole_start = start;
6231 u64 hole_len = len;
6232
David Sterba172ddd62011-04-21 00:48:27 +02006233 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006234 if (!em) {
6235 err = -ENOMEM;
6236 goto out;
6237 }
6238 /*
6239 * when btrfs_get_extent can't find anything it
6240 * returns one huge hole
6241 *
6242 * make sure what it found really fits our range, and
6243 * adjust to make sure it is based on the start from
6244 * the caller
6245 */
6246 if (hole_em) {
6247 u64 calc_end = extent_map_end(hole_em);
6248
6249 if (calc_end <= start || (hole_em->start > end)) {
6250 free_extent_map(hole_em);
6251 hole_em = NULL;
6252 } else {
6253 hole_start = max(hole_em->start, start);
6254 hole_len = calc_end - hole_start;
6255 }
6256 }
6257 em->bdev = NULL;
6258 if (hole_em && range_start > hole_start) {
6259 /* our hole starts before our delalloc, so we
6260 * have to return just the parts of the hole
6261 * that go until the delalloc starts
6262 */
6263 em->len = min(hole_len,
6264 range_start - hole_start);
6265 em->start = hole_start;
6266 em->orig_start = hole_start;
6267 /*
6268 * don't adjust block start at all,
6269 * it is fixed at EXTENT_MAP_HOLE
6270 */
6271 em->block_start = hole_em->block_start;
6272 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006273 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6274 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006275 } else {
6276 em->start = range_start;
6277 em->len = found;
6278 em->orig_start = range_start;
6279 em->block_start = EXTENT_MAP_DELALLOC;
6280 em->block_len = found;
6281 }
6282 } else if (hole_em) {
6283 return hole_em;
6284 }
6285out:
6286
6287 free_extent_map(hole_em);
6288 if (err) {
6289 free_extent_map(em);
6290 return ERR_PTR(err);
6291 }
6292 return em;
6293}
6294
Josef Bacik4b46fce2010-05-23 11:00:55 -04006295static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6296 u64 start, u64 len)
6297{
6298 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006299 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006300 struct btrfs_key ins;
6301 u64 alloc_hint;
6302 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006303
Josef Bacik4b46fce2010-05-23 11:00:55 -04006304 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006305 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006306 alloc_hint, &ins, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006307 if (ret)
6308 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006309
Josef Bacik70c8a912012-10-11 16:54:30 -04006310 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006311 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006312 if (IS_ERR(em)) {
6313 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6314 return em;
6315 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006316
6317 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6318 ins.offset, ins.offset, 0);
6319 if (ret) {
6320 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Josef Bacik00361582013-08-14 14:02:47 -04006321 free_extent_map(em);
6322 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006323 }
Josef Bacik00361582013-08-14 14:02:47 -04006324
Josef Bacik4b46fce2010-05-23 11:00:55 -04006325 return em;
6326}
6327
Chris Mason46bfbb52010-05-26 11:04:10 -04006328/*
6329 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6330 * block must be cow'd
6331 */
Josef Bacik00361582013-08-14 14:02:47 -04006332noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006333 u64 *orig_start, u64 *orig_block_len,
6334 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006335{
Josef Bacik00361582013-08-14 14:02:47 -04006336 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006337 struct btrfs_path *path;
6338 int ret;
6339 struct extent_buffer *leaf;
6340 struct btrfs_root *root = BTRFS_I(inode)->root;
6341 struct btrfs_file_extent_item *fi;
6342 struct btrfs_key key;
6343 u64 disk_bytenr;
6344 u64 backref_offset;
6345 u64 extent_end;
6346 u64 num_bytes;
6347 int slot;
6348 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006349 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Chris Mason46bfbb52010-05-26 11:04:10 -04006350 path = btrfs_alloc_path();
6351 if (!path)
6352 return -ENOMEM;
6353
Josef Bacik00361582013-08-14 14:02:47 -04006354 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006355 offset, 0);
6356 if (ret < 0)
6357 goto out;
6358
6359 slot = path->slots[0];
6360 if (ret == 1) {
6361 if (slot == 0) {
6362 /* can't find the item, must cow */
6363 ret = 0;
6364 goto out;
6365 }
6366 slot--;
6367 }
6368 ret = 0;
6369 leaf = path->nodes[0];
6370 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006371 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006372 key.type != BTRFS_EXTENT_DATA_KEY) {
6373 /* not our file or wrong item type, must cow */
6374 goto out;
6375 }
6376
6377 if (key.offset > offset) {
6378 /* Wrong offset, must cow */
6379 goto out;
6380 }
6381
6382 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6383 found_type = btrfs_file_extent_type(leaf, fi);
6384 if (found_type != BTRFS_FILE_EXTENT_REG &&
6385 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6386 /* not a regular extent, must cow */
6387 goto out;
6388 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006389
6390 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6391 goto out;
6392
Chris Mason46bfbb52010-05-26 11:04:10 -04006393 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006394 if (disk_bytenr == 0)
6395 goto out;
6396
6397 if (btrfs_file_extent_compression(leaf, fi) ||
6398 btrfs_file_extent_encryption(leaf, fi) ||
6399 btrfs_file_extent_other_encoding(leaf, fi))
6400 goto out;
6401
Chris Mason46bfbb52010-05-26 11:04:10 -04006402 backref_offset = btrfs_file_extent_offset(leaf, fi);
6403
Josef Bacik7ee9e442013-06-21 16:37:03 -04006404 if (orig_start) {
6405 *orig_start = key.offset - backref_offset;
6406 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6407 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6408 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006409
Chris Mason46bfbb52010-05-26 11:04:10 -04006410 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason46bfbb52010-05-26 11:04:10 -04006411
6412 if (btrfs_extent_readonly(root, disk_bytenr))
6413 goto out;
6414
6415 /*
6416 * look for other files referencing this extent, if we
6417 * find any we must cow
6418 */
Josef Bacik00361582013-08-14 14:02:47 -04006419 trans = btrfs_join_transaction(root);
6420 if (IS_ERR(trans)) {
6421 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006422 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006423 }
6424
6425 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6426 key.offset - backref_offset, disk_bytenr);
6427 btrfs_end_transaction(trans, root);
6428 if (ret) {
6429 ret = 0;
6430 goto out;
6431 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006432
6433 /*
6434 * adjust disk_bytenr and num_bytes to cover just the bytes
6435 * in this extent we are about to write. If there
6436 * are any csums in that range we have to cow in order
6437 * to keep the csums correct
6438 */
6439 disk_bytenr += backref_offset;
6440 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006441 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006442 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6443 goto out;
6444 /*
6445 * all of the above have passed, it is safe to overwrite this extent
6446 * without cow
6447 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006448 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006449 ret = 1;
6450out:
6451 btrfs_free_path(path);
6452 return ret;
6453}
6454
Josef Bacikeb838e72012-07-31 16:28:48 -04006455static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6456 struct extent_state **cached_state, int writing)
6457{
6458 struct btrfs_ordered_extent *ordered;
6459 int ret = 0;
6460
6461 while (1) {
6462 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6463 0, cached_state);
6464 /*
6465 * We're concerned with the entire range that we're going to be
6466 * doing DIO to, so we need to make sure theres no ordered
6467 * extents in this range.
6468 */
6469 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6470 lockend - lockstart + 1);
6471
6472 /*
6473 * We need to make sure there are no buffered pages in this
6474 * range either, we could have raced between the invalidate in
6475 * generic_file_direct_write and locking the extent. The
6476 * invalidate needs to happen so that reads after a write do not
6477 * get stale data.
6478 */
6479 if (!ordered && (!writing ||
6480 !test_range_bit(&BTRFS_I(inode)->io_tree,
6481 lockstart, lockend, EXTENT_UPTODATE, 0,
6482 *cached_state)))
6483 break;
6484
6485 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6486 cached_state, GFP_NOFS);
6487
6488 if (ordered) {
6489 btrfs_start_ordered_extent(inode, ordered, 1);
6490 btrfs_put_ordered_extent(ordered);
6491 } else {
6492 /* Screw you mmap */
6493 ret = filemap_write_and_wait_range(inode->i_mapping,
6494 lockstart,
6495 lockend);
6496 if (ret)
6497 break;
6498
6499 /*
6500 * If we found a page that couldn't be invalidated just
6501 * fall back to buffered.
6502 */
6503 ret = invalidate_inode_pages2_range(inode->i_mapping,
6504 lockstart >> PAGE_CACHE_SHIFT,
6505 lockend >> PAGE_CACHE_SHIFT);
6506 if (ret)
6507 break;
6508 }
6509
6510 cond_resched();
6511 }
6512
6513 return ret;
6514}
6515
Josef Bacik69ffb542012-09-11 15:40:07 -04006516static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6517 u64 len, u64 orig_start,
6518 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006519 u64 orig_block_len, u64 ram_bytes,
6520 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006521{
6522 struct extent_map_tree *em_tree;
6523 struct extent_map *em;
6524 struct btrfs_root *root = BTRFS_I(inode)->root;
6525 int ret;
6526
6527 em_tree = &BTRFS_I(inode)->extent_tree;
6528 em = alloc_extent_map();
6529 if (!em)
6530 return ERR_PTR(-ENOMEM);
6531
6532 em->start = start;
6533 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006534 em->mod_start = start;
6535 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006536 em->len = len;
6537 em->block_len = block_len;
6538 em->block_start = block_start;
6539 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006540 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006541 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006542 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006543 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6544 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006545 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006546
6547 do {
6548 btrfs_drop_extent_cache(inode, em->start,
6549 em->start + em->len - 1, 0);
6550 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006551 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006552 write_unlock(&em_tree->lock);
6553 } while (ret == -EEXIST);
6554
6555 if (ret) {
6556 free_extent_map(em);
6557 return ERR_PTR(ret);
6558 }
6559
6560 return em;
6561}
6562
6563
Josef Bacik4b46fce2010-05-23 11:00:55 -04006564static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6565 struct buffer_head *bh_result, int create)
6566{
6567 struct extent_map *em;
6568 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006569 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006570 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006571 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006572 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04006573 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006574 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006575
Miao Xie172a5042013-02-21 02:48:22 -07006576 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006577 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006578 else
Josef Bacikc3298612012-08-03 16:49:19 -04006579 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006580
Josef Bacikc3298612012-08-03 16:49:19 -04006581 lockstart = start;
6582 lockend = start + len - 1;
6583
Josef Bacikeb838e72012-07-31 16:28:48 -04006584 /*
6585 * If this errors out it's because we couldn't invalidate pagecache for
6586 * this range and we need to fallback to buffered.
6587 */
6588 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6589 return -ENOTBLK;
6590
Josef Bacik4b46fce2010-05-23 11:00:55 -04006591 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006592 if (IS_ERR(em)) {
6593 ret = PTR_ERR(em);
6594 goto unlock_err;
6595 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006596
6597 /*
6598 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6599 * io. INLINE is special, and we could probably kludge it in here, but
6600 * it's still buffered so for safety lets just fall back to the generic
6601 * buffered path.
6602 *
6603 * For COMPRESSED we _have_ to read the entire extent in so we can
6604 * decompress it, so there will be buffering required no matter what we
6605 * do, so go ahead and fallback to buffered.
6606 *
6607 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6608 * to buffered IO. Don't blame me, this is the price we pay for using
6609 * the generic code.
6610 */
6611 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6612 em->block_start == EXTENT_MAP_INLINE) {
6613 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006614 ret = -ENOTBLK;
6615 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006616 }
6617
6618 /* Just a good old fashioned hole, return */
6619 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6620 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6621 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006622 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006623 }
6624
6625 /*
6626 * We don't allocate a new extent in the following cases
6627 *
6628 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6629 * existing extent.
6630 * 2) The extent is marked as PREALLOC. We're good to go here and can
6631 * just use the extent.
6632 *
6633 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006634 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006635 len = min(len, em->len - (start - em->start));
6636 lockstart = start + len;
6637 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006638 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006639
6640 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6641 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6642 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006643 int type;
6644 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006645 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006646
6647 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6648 type = BTRFS_ORDERED_PREALLOC;
6649 else
6650 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006651 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006652 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006653
Josef Bacik00361582013-08-14 14:02:47 -04006654 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006655 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006656 if (type == BTRFS_ORDERED_PREALLOC) {
6657 free_extent_map(em);
6658 em = create_pinned_em(inode, start, len,
6659 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006660 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006661 orig_block_len,
6662 ram_bytes, type);
Josef Bacik00361582013-08-14 14:02:47 -04006663 if (IS_ERR(em))
Josef Bacik69ffb542012-09-11 15:40:07 -04006664 goto unlock_err;
Josef Bacik69ffb542012-09-11 15:40:07 -04006665 }
6666
Chris Mason46bfbb52010-05-26 11:04:10 -04006667 ret = btrfs_add_ordered_extent_dio(inode, start,
6668 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04006669 if (ret) {
6670 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006671 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006672 }
6673 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006674 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006675 }
Josef Bacik00361582013-08-14 14:02:47 -04006676
Chris Mason46bfbb52010-05-26 11:04:10 -04006677 /*
6678 * this will cow the extent, reset the len in case we changed
6679 * it above
6680 */
6681 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006682 free_extent_map(em);
6683 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006684 if (IS_ERR(em)) {
6685 ret = PTR_ERR(em);
6686 goto unlock_err;
6687 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006688 len = min(len, em->len - (start - em->start));
6689unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006690 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6691 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006692 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006693 bh_result->b_bdev = em->bdev;
6694 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006695 if (create) {
6696 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6697 set_buffer_new(bh_result);
6698
6699 /*
6700 * Need to update the i_size under the extent lock so buffered
6701 * readers will get the updated i_size when we unlock.
6702 */
6703 if (start + len > i_size_read(inode))
6704 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006705
Miao Xie172a5042013-02-21 02:48:22 -07006706 spin_lock(&BTRFS_I(inode)->lock);
6707 BTRFS_I(inode)->outstanding_extents++;
6708 spin_unlock(&BTRFS_I(inode)->lock);
6709
Miao Xie09348562013-02-07 10:12:07 +00006710 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6711 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6712 &cached_state, GFP_NOFS);
6713 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006714 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006715
Josef Bacikeb838e72012-07-31 16:28:48 -04006716 /*
6717 * In the case of write we need to clear and unlock the entire range,
6718 * in the case of read we need to unlock only the end area that we
6719 * aren't using if there is any left over space.
6720 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006721 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006722 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6723 lockend, unlock_bits, 1, 0,
6724 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006725 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006726 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006727 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006728
Josef Bacik4b46fce2010-05-23 11:00:55 -04006729 free_extent_map(em);
6730
6731 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006732
6733unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006734 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6735 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6736 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006737}
6738
Josef Bacik4b46fce2010-05-23 11:00:55 -04006739static void btrfs_endio_direct_read(struct bio *bio, int err)
6740{
Miao Xiee65e1532010-11-22 03:04:43 +00006741 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006742 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6743 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006744 struct inode *inode = dip->inode;
6745 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006746 struct bio *dio_bio;
Miao Xiefacc8a22013-07-25 19:22:34 +08006747 u32 *csums = (u32 *)dip->csum;
6748 int index = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006749 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006750
6751 start = dip->logical_offset;
6752 do {
6753 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6754 struct page *page = bvec->bv_page;
6755 char *kaddr;
6756 u32 csum = ~(u32)0;
6757 unsigned long flags;
6758
6759 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006760 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006761 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006762 csum, bvec->bv_len);
6763 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006764 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006765 local_irq_restore(flags);
6766
6767 flush_dcache_page(bvec->bv_page);
Miao Xiefacc8a22013-07-25 19:22:34 +08006768 if (csum != csums[index]) {
6769 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006770 btrfs_ino(inode), start, csum,
6771 csums[index]);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006772 err = -EIO;
6773 }
6774 }
6775
6776 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006777 bvec++;
Miao Xiefacc8a22013-07-25 19:22:34 +08006778 index++;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006779 } while (bvec <= bvec_end);
6780
6781 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006782 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04006783 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006784
Josef Bacik4b46fce2010-05-23 11:00:55 -04006785 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006786
6787 /* If we had a csum failure make sure to clear the uptodate flag */
6788 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006789 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6790 dio_end_io(dio_bio, err);
6791 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006792}
6793
6794static void btrfs_endio_direct_write(struct bio *bio, int err)
6795{
6796 struct btrfs_dio_private *dip = bio->bi_private;
6797 struct inode *inode = dip->inode;
6798 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006799 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006800 u64 ordered_offset = dip->logical_offset;
6801 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04006802 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006803 int ret;
6804
6805 if (err)
6806 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006807again:
6808 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6809 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006810 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006811 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006812 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006813
Josef Bacik5fd02042012-05-02 14:00:54 -04006814 ordered->work.func = finish_ordered_fn;
6815 ordered->work.flags = 0;
6816 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6817 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006818out_test:
6819 /*
6820 * our bio might span multiple ordered extents. If we haven't
6821 * completed the accounting for the whole dio, go back and try again
6822 */
6823 if (ordered_offset < dip->logical_offset + dip->bytes) {
6824 ordered_bytes = dip->logical_offset + dip->bytes -
6825 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006826 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006827 goto again;
6828 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006829out_done:
Chris Mason9be33952013-05-17 18:30:14 -04006830 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006831
Josef Bacik4b46fce2010-05-23 11:00:55 -04006832 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006833
6834 /* If we had an error make sure to clear the uptodate flag */
6835 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006836 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6837 dio_end_io(dio_bio, err);
6838 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006839}
6840
Chris Masoneaf25d92010-05-25 09:48:28 -04006841static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6842 struct bio *bio, int mirror_num,
6843 unsigned long bio_flags, u64 offset)
6844{
6845 int ret;
6846 struct btrfs_root *root = BTRFS_I(inode)->root;
6847 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006848 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006849 return 0;
6850}
6851
Miao Xiee65e1532010-11-22 03:04:43 +00006852static void btrfs_end_dio_bio(struct bio *bio, int err)
6853{
6854 struct btrfs_dio_private *dip = bio->bi_private;
6855
6856 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006857 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006858 "sector %#Lx len %u err no %d\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006859 btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006860 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006861 dip->errors = 1;
6862
6863 /*
6864 * before atomic variable goto zero, we must make sure
6865 * dip->errors is perceived to be set.
6866 */
6867 smp_mb__before_atomic_dec();
6868 }
6869
6870 /* if there are more bios still pending for this dio, just exit */
6871 if (!atomic_dec_and_test(&dip->pending_bios))
6872 goto out;
6873
Chris Mason9be33952013-05-17 18:30:14 -04006874 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00006875 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04006876 } else {
6877 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00006878 bio_endio(dip->orig_bio, 0);
6879 }
6880out:
6881 bio_put(bio);
6882}
6883
6884static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6885 u64 first_sector, gfp_t gfp_flags)
6886{
6887 int nr_vecs = bio_get_nr_vecs(bdev);
6888 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6889}
6890
6891static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6892 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006893 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006894{
Miao Xiefacc8a22013-07-25 19:22:34 +08006895 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00006896 int write = rw & REQ_WRITE;
6897 struct btrfs_root *root = BTRFS_I(inode)->root;
6898 int ret;
6899
Josef Bacikb812ce22012-11-16 13:56:32 -05006900 if (async_submit)
6901 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6902
Miao Xiee65e1532010-11-22 03:04:43 +00006903 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006904
6905 if (!write) {
6906 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6907 if (ret)
6908 goto err;
6909 }
Miao Xiee65e1532010-11-22 03:04:43 +00006910
Josef Bacik1ae39932011-04-06 14:41:34 -04006911 if (skip_sum)
6912 goto map;
6913
6914 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006915 ret = btrfs_wq_submit_bio(root->fs_info,
6916 inode, rw, bio, 0, 0,
6917 file_offset,
6918 __btrfs_submit_bio_start_direct_io,
6919 __btrfs_submit_bio_done);
6920 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006921 } else if (write) {
6922 /*
6923 * If we aren't doing async submit, calculate the csum of the
6924 * bio now.
6925 */
6926 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6927 if (ret)
6928 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006929 } else if (!skip_sum) {
Miao Xiefacc8a22013-07-25 19:22:34 +08006930 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
6931 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006932 if (ret)
6933 goto err;
6934 }
Miao Xiee65e1532010-11-22 03:04:43 +00006935
Josef Bacik1ae39932011-04-06 14:41:34 -04006936map:
6937 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006938err:
6939 bio_put(bio);
6940 return ret;
6941}
6942
6943static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6944 int skip_sum)
6945{
6946 struct inode *inode = dip->inode;
6947 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00006948 struct bio *bio;
6949 struct bio *orig_bio = dip->orig_bio;
6950 struct bio_vec *bvec = orig_bio->bi_io_vec;
6951 u64 start_sector = orig_bio->bi_sector;
6952 u64 file_offset = dip->logical_offset;
6953 u64 submit_len = 0;
6954 u64 map_length;
6955 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006956 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006957 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006958
Miao Xiee65e1532010-11-22 03:04:43 +00006959 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05006960 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006961 &map_length, NULL, 0);
6962 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006963 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006964 return -EIO;
6965 }
Miao Xiefacc8a22013-07-25 19:22:34 +08006966
Josef Bacik02f57c72011-04-06 14:25:44 -04006967 if (map_length >= orig_bio->bi_size) {
6968 bio = orig_bio;
6969 goto submit;
6970 }
6971
David Woodhouse53b381b2013-01-29 18:40:14 -05006972 /* async crcs make it difficult to collect full stripe writes. */
6973 if (btrfs_get_alloc_profile(root, 1) &
6974 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
6975 async_submit = 0;
6976 else
6977 async_submit = 1;
6978
Josef Bacik02f57c72011-04-06 14:25:44 -04006979 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6980 if (!bio)
6981 return -ENOMEM;
6982 bio->bi_private = dip;
6983 bio->bi_end_io = btrfs_end_dio_bio;
6984 atomic_inc(&dip->pending_bios);
6985
Miao Xiee65e1532010-11-22 03:04:43 +00006986 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6987 if (unlikely(map_length < submit_len + bvec->bv_len ||
6988 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6989 bvec->bv_offset) < bvec->bv_len)) {
6990 /*
6991 * inc the count before we submit the bio so
6992 * we know the end IO handler won't happen before
6993 * we inc the count. Otherwise, the dip might get freed
6994 * before we're done setting it up
6995 */
6996 atomic_inc(&dip->pending_bios);
6997 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6998 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006999 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007000 if (ret) {
7001 bio_put(bio);
7002 atomic_dec(&dip->pending_bios);
7003 goto out_err;
7004 }
7005
Miao Xiee65e1532010-11-22 03:04:43 +00007006 start_sector += submit_len >> 9;
7007 file_offset += submit_len;
7008
7009 submit_len = 0;
7010 nr_pages = 0;
7011
7012 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7013 start_sector, GFP_NOFS);
7014 if (!bio)
7015 goto out_err;
7016 bio->bi_private = dip;
7017 bio->bi_end_io = btrfs_end_dio_bio;
7018
7019 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007020 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007021 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007022 &map_length, NULL, 0);
7023 if (ret) {
7024 bio_put(bio);
7025 goto out_err;
7026 }
7027 } else {
7028 submit_len += bvec->bv_len;
7029 nr_pages ++;
7030 bvec++;
7031 }
7032 }
7033
Josef Bacik02f57c72011-04-06 14:25:44 -04007034submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007035 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007036 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007037 if (!ret)
7038 return 0;
7039
7040 bio_put(bio);
7041out_err:
7042 dip->errors = 1;
7043 /*
7044 * before atomic variable goto zero, we must
7045 * make sure dip->errors is perceived to be set.
7046 */
7047 smp_mb__before_atomic_dec();
7048 if (atomic_dec_and_test(&dip->pending_bios))
7049 bio_io_error(dip->orig_bio);
7050
7051 /* bio_end_io() will handle error, so we needn't return it */
7052 return 0;
7053}
7054
Chris Mason9be33952013-05-17 18:30:14 -04007055static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7056 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007057{
7058 struct btrfs_root *root = BTRFS_I(inode)->root;
7059 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007060 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007061 int skip_sum;
Miao Xiefacc8a22013-07-25 19:22:34 +08007062 int sum_len;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007063 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007064 int ret = 0;
Miao Xiefacc8a22013-07-25 19:22:34 +08007065 u16 csum_size;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007066
7067 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7068
Chris Mason9be33952013-05-17 18:30:14 -04007069 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007070 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007071 ret = -ENOMEM;
7072 goto free_ordered;
7073 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007074
Miao Xiefacc8a22013-07-25 19:22:34 +08007075 if (!skip_sum && !write) {
7076 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7077 sum_len = dio_bio->bi_size >> inode->i_sb->s_blocksize_bits;
7078 sum_len *= csum_size;
7079 } else {
7080 sum_len = 0;
7081 }
7082
7083 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007084 if (!dip) {
7085 ret = -ENOMEM;
7086 goto free_io_bio;
7087 }
7088
7089 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007090 dip->inode = inode;
7091 dip->logical_offset = file_offset;
Miao Xiee6da5d22013-06-19 18:19:17 +08007092 dip->bytes = dio_bio->bi_size;
Chris Mason9be33952013-05-17 18:30:14 -04007093 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7094 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007095 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007096 dip->orig_bio = io_bio;
7097 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007098 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007099
7100 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007101 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007102 else
Chris Mason9be33952013-05-17 18:30:14 -04007103 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007104
Miao Xiee65e1532010-11-22 03:04:43 +00007105 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7106 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007107 return;
Chris Mason9be33952013-05-17 18:30:14 -04007108
7109free_io_bio:
7110 bio_put(io_bio);
7111
Josef Bacik4b46fce2010-05-23 11:00:55 -04007112free_ordered:
7113 /*
7114 * If this is a write, we need to clean up the reserved space and kill
7115 * the ordered extent.
7116 */
7117 if (write) {
7118 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007119 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007120 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7121 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7122 btrfs_free_reserved_extent(root, ordered->start,
7123 ordered->disk_len);
7124 btrfs_put_ordered_extent(ordered);
7125 btrfs_put_ordered_extent(ordered);
7126 }
Chris Mason9be33952013-05-17 18:30:14 -04007127 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007128}
7129
Chris Mason5a5f79b2010-05-26 21:33:37 -04007130static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7131 const struct iovec *iov, loff_t offset,
7132 unsigned long nr_segs)
7133{
7134 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007135 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007136 size_t size;
7137 unsigned long addr;
7138 unsigned blocksize_mask = root->sectorsize - 1;
7139 ssize_t retval = -EINVAL;
7140 loff_t end = offset;
7141
7142 if (offset & blocksize_mask)
7143 goto out;
7144
7145 /* Check the memory alignment. Blocks cannot straddle pages */
7146 for (seg = 0; seg < nr_segs; seg++) {
7147 addr = (unsigned long)iov[seg].iov_base;
7148 size = iov[seg].iov_len;
7149 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007150 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007151 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007152
7153 /* If this is a write we don't need to check anymore */
7154 if (rw & WRITE)
7155 continue;
7156
7157 /*
7158 * Check to make sure we don't have duplicate iov_base's in this
7159 * iovec, if so return EINVAL, otherwise we'll get csum errors
7160 * when reading back.
7161 */
7162 for (i = seg + 1; i < nr_segs; i++) {
7163 if (iov[seg].iov_base == iov[i].iov_base)
7164 goto out;
7165 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007166 }
7167 retval = 0;
7168out:
7169 return retval;
7170}
Josef Bacikeb838e72012-07-31 16:28:48 -04007171
Chris Mason16432982008-04-10 10:23:21 -04007172static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7173 const struct iovec *iov, loff_t offset,
7174 unsigned long nr_segs)
7175{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007176 struct file *file = iocb->ki_filp;
7177 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007178 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007179 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007180 bool wakeup = true;
7181 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007182 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007183
Chris Mason5a5f79b2010-05-26 21:33:37 -04007184 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007185 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007186 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007187
Miao Xie38851cc2013-02-08 07:04:11 +00007188 atomic_inc(&inode->i_dio_count);
7189 smp_mb__after_atomic_inc();
7190
Josef Bacik0e267c42013-07-02 10:38:02 -04007191 /*
7192 * The generic stuff only does filemap_write_and_wait_range, which isn't
7193 * enough if we've written compressed pages to this area, so we need to
7194 * call btrfs_wait_ordered_range to make absolutely sure that any
7195 * outstanding dirty pages are on disk.
7196 */
7197 count = iov_length(iov, nr_segs);
7198 btrfs_wait_ordered_range(inode, offset, count);
7199
Miao Xie09348562013-02-07 10:12:07 +00007200 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007201 /*
7202 * If the write DIO is beyond the EOF, we need update
7203 * the isize, but it is protected by i_mutex. So we can
7204 * not unlock the i_mutex at this case.
7205 */
7206 if (offset + count <= inode->i_size) {
7207 mutex_unlock(&inode->i_mutex);
7208 relock = true;
7209 }
Miao Xie09348562013-02-07 10:12:07 +00007210 ret = btrfs_delalloc_reserve_space(inode, count);
7211 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007212 goto out;
7213 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7214 &BTRFS_I(inode)->runtime_flags))) {
7215 inode_dio_done(inode);
7216 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7217 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007218 }
7219
7220 ret = __blockdev_direct_IO(rw, iocb, inode,
7221 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7222 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007223 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007224 if (rw & WRITE) {
7225 if (ret < 0 && ret != -EIOCBQUEUED)
7226 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007227 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007228 btrfs_delalloc_release_space(inode,
7229 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007230 else
7231 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007232 }
Miao Xie38851cc2013-02-08 07:04:11 +00007233out:
Miao Xie2e60a512013-02-08 07:01:08 +00007234 if (wakeup)
7235 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007236 if (relock)
7237 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007238
7239 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007240}
7241
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007242#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7243
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007244static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7245 __u64 start, __u64 len)
7246{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007247 int ret;
7248
7249 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7250 if (ret)
7251 return ret;
7252
Chris Masonec29ed52011-02-23 16:23:20 -05007253 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007254}
7255
Chris Mason9ebefb182007-06-15 13:50:00 -04007256int btrfs_readpage(struct file *file, struct page *page)
7257{
Chris Masond1310b22008-01-24 16:13:08 -05007258 struct extent_io_tree *tree;
7259 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007260 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007261}
Chris Mason1832a6d2007-12-21 16:27:21 -05007262
Chris Mason39279cc2007-06-12 06:35:45 -04007263static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7264{
Chris Masond1310b22008-01-24 16:13:08 -05007265 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007266
7267
7268 if (current->flags & PF_MEMALLOC) {
7269 redirty_page_for_writepage(wbc, page);
7270 unlock_page(page);
7271 return 0;
7272 }
Chris Masond1310b22008-01-24 16:13:08 -05007273 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007274 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7275}
Chris Mason39279cc2007-06-12 06:35:45 -04007276
Eric Sandeen48a3b632013-04-25 20:41:01 +00007277static int btrfs_writepages(struct address_space *mapping,
7278 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007279{
Chris Masond1310b22008-01-24 16:13:08 -05007280 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007281
Chris Masond1310b22008-01-24 16:13:08 -05007282 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007283 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7284}
7285
Chris Mason3ab2fb52007-11-08 10:59:22 -05007286static int
7287btrfs_readpages(struct file *file, struct address_space *mapping,
7288 struct list_head *pages, unsigned nr_pages)
7289{
Chris Masond1310b22008-01-24 16:13:08 -05007290 struct extent_io_tree *tree;
7291 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007292 return extent_readpages(tree, mapping, pages, nr_pages,
7293 btrfs_get_extent);
7294}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007295static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007296{
Chris Masond1310b22008-01-24 16:13:08 -05007297 struct extent_io_tree *tree;
7298 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007299 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007300
Chris Masond1310b22008-01-24 16:13:08 -05007301 tree = &BTRFS_I(page->mapping->host)->io_tree;
7302 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007303 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007304 if (ret == 1) {
7305 ClearPagePrivate(page);
7306 set_page_private(page, 0);
7307 page_cache_release(page);
7308 }
7309 return ret;
7310}
Chris Mason39279cc2007-06-12 06:35:45 -04007311
Chris Masone6dcd2d2008-07-17 12:53:50 -04007312static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7313{
Chris Mason98509cf2008-09-11 15:51:43 -04007314 if (PageWriteback(page) || PageDirty(page))
7315 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007316 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007317}
7318
Lukas Czernerd47992f2013-05-21 23:17:23 -04007319static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7320 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007321{
Josef Bacik5fd02042012-05-02 14:00:54 -04007322 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007323 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007324 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007325 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007326 u64 page_start = page_offset(page);
7327 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007328
Chris Mason8b62b722009-09-02 16:53:46 -04007329 /*
7330 * we have the page locked, so new writeback can't start,
7331 * and the dirty bit won't be cleared while we are here.
7332 *
7333 * Wait for IO on this page so that we can safely clear
7334 * the PagePrivate2 bit and do ordered accounting
7335 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007336 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007337
Josef Bacik5fd02042012-05-02 14:00:54 -04007338 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007339 if (offset) {
7340 btrfs_releasepage(page, GFP_NOFS);
7341 return;
7342 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007343 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007344 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007345 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007346 /*
7347 * IO on this page will never be started, so we need
7348 * to account for any ordered extents now
7349 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007350 clear_extent_bit(tree, page_start, page_end,
7351 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007352 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7353 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007354 /*
7355 * whoever cleared the private bit is responsible
7356 * for the finish_ordered_io
7357 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04007358 if (TestClearPagePrivate2(page)) {
7359 struct btrfs_ordered_inode_tree *tree;
7360 u64 new_len;
7361
7362 tree = &BTRFS_I(inode)->ordered_tree;
7363
7364 spin_lock_irq(&tree->lock);
7365 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
7366 new_len = page_start - ordered->file_offset;
7367 if (new_len < ordered->truncated_len)
7368 ordered->truncated_len = new_len;
7369 spin_unlock_irq(&tree->lock);
7370
7371 if (btrfs_dec_test_ordered_pending(inode, &ordered,
7372 page_start,
7373 PAGE_CACHE_SIZE, 1))
7374 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007375 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007376 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007377 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007378 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007379 }
7380 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007381 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007382 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7383 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007384 __btrfs_releasepage(page, GFP_NOFS);
7385
Chris Mason4a096752008-07-21 10:29:44 -04007386 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007387 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007388 ClearPagePrivate(page);
7389 set_page_private(page, 0);
7390 page_cache_release(page);
7391 }
Chris Mason39279cc2007-06-12 06:35:45 -04007392}
7393
Chris Mason9ebefb182007-06-15 13:50:00 -04007394/*
7395 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7396 * called from a page fault handler when a page is first dirtied. Hence we must
7397 * be careful to check for EOF conditions here. We set the page up correctly
7398 * for a written page which means we get ENOSPC checking when writing into
7399 * holes and correct delalloc and unwritten extent mapping on filesystems that
7400 * support these features.
7401 *
7402 * We are not allowed to take the i_mutex here so we have to play games to
7403 * protect against truncate races as the page could now be beyond EOF. Because
7404 * vmtruncate() writes the inode size before removing pages, once we have the
7405 * page lock we can determine safely if the page is beyond EOF. If it is not
7406 * beyond EOF, then the page is guaranteed safe against truncation until we
7407 * unlock the page.
7408 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007409int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007410{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007411 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007412 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007413 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007414 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7415 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007416 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007417 char *kaddr;
7418 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007419 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007420 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007421 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007422 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007423 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007424
Jan Karab2b5ef52012-06-12 16:20:45 +02007425 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007426 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007427 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007428 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007429 reserved = 1;
7430 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007431 if (ret) {
7432 if (ret == -ENOMEM)
7433 ret = VM_FAULT_OOM;
7434 else /* -ENOSPC, -EIO, etc */
7435 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007436 if (reserved)
7437 goto out;
7438 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007439 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007440
Nick Piggin56a76f82009-03-31 15:23:23 -07007441 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007442again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007443 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007444 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007445 page_start = page_offset(page);
7446 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007447
Chris Mason9ebefb182007-06-15 13:50:00 -04007448 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007449 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007450 /* page got truncated out from underneath us */
7451 goto out_unlock;
7452 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007453 wait_on_page_writeback(page);
7454
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007455 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007456 set_page_extent_mapped(page);
7457
Chris Masoneb84ae02008-07-17 13:53:27 -04007458 /*
7459 * we can't set the delalloc bits if there are pending ordered
7460 * extents. Drop our locks and wait for them to finish
7461 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007462 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7463 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007464 unlock_extent_cached(io_tree, page_start, page_end,
7465 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007466 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007467 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007468 btrfs_put_ordered_extent(ordered);
7469 goto again;
7470 }
7471
Josef Bacikfbf19082009-10-01 17:10:23 -04007472 /*
7473 * XXX - page_mkwrite gets called every time the page is dirtied, even
7474 * if it was already dirty, so for space accounting reasons we need to
7475 * clear any delalloc bits for the range we are fixing to save. There
7476 * is probably a better way to do this, but for now keep consistent with
7477 * prepare_pages in the normal write path.
7478 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007479 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007480 EXTENT_DIRTY | EXTENT_DELALLOC |
7481 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007482 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007483
Josef Bacik2ac55d42010-02-03 19:33:23 +00007484 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7485 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007486 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007487 unlock_extent_cached(io_tree, page_start, page_end,
7488 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007489 ret = VM_FAULT_SIGBUS;
7490 goto out_unlock;
7491 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007492 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007493
7494 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007495 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007496 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007497 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007498 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007499
Chris Masone6dcd2d2008-07-17 12:53:50 -04007500 if (zero_start != PAGE_CACHE_SIZE) {
7501 kaddr = kmap(page);
7502 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7503 flush_dcache_page(page);
7504 kunmap(page);
7505 }
Chris Mason247e7432008-07-17 12:53:51 -04007506 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007507 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007508 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007509
Chris Mason257c62e2009-10-13 13:21:08 -04007510 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7511 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007512 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007513
Josef Bacik2ac55d42010-02-03 19:33:23 +00007514 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007515
7516out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007517 if (!ret) {
7518 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007519 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007520 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007521 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007522out:
Josef Bacikec39e182012-01-12 19:10:12 -05007523 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007524out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007525 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007526 return ret;
7527}
7528
Josef Bacika41ad392011-01-31 15:30:16 -05007529static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007530{
7531 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007532 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007533 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007534 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007535 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007536 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007537 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007538
Chris Mason4a096752008-07-21 10:29:44 -04007539 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007540 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007541
Josef Bacikfcb80c22011-05-03 10:40:22 -04007542 /*
7543 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7544 * 3 things going on here
7545 *
7546 * 1) We need to reserve space for our orphan item and the space to
7547 * delete our orphan item. Lord knows we don't want to have a dangling
7548 * orphan item because we didn't reserve space to remove it.
7549 *
7550 * 2) We need to reserve space to update our inode.
7551 *
7552 * 3) We need to have something to cache all the space that is going to
7553 * be free'd up by the truncate operation, but also have some slack
7554 * space reserved in case it uses space during the truncate (thank you
7555 * very much snapshotting).
7556 *
7557 * And we need these to all be seperate. The fact is we can use alot of
7558 * space doing the truncate, and we have no earthly idea how much space
7559 * we will use, so we need the truncate reservation to be seperate so it
7560 * doesn't end up using space reserved for updating the inode or
7561 * removing the orphan item. We also need to be able to stop the
7562 * transaction and start a new one, which means we need to be able to
7563 * update the inode several times, and we have no idea of knowing how
7564 * many times that will be, so we can't just reserve 1 item for the
7565 * entirety of the opration, so that has to be done seperately as well.
7566 * Then there is the orphan item, which does indeed need to be held on
7567 * to for the whole operation, and we need nobody to touch this reserved
7568 * space except the orphan code.
7569 *
7570 * So that leaves us with
7571 *
7572 * 1) root->orphan_block_rsv - for the orphan deletion.
7573 * 2) rsv - for the truncate reservation, which we will steal from the
7574 * transaction reservation.
7575 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7576 * updating the inode.
7577 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007578 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007579 if (!rsv)
7580 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007581 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007582 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007583
Josef Bacik907cbce2011-08-08 13:46:15 -04007584 /*
Josef Bacik07127182011-08-19 10:29:59 -04007585 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007586 * 1 for updating the inode.
7587 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007588 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007589 if (IS_ERR(trans)) {
7590 err = PTR_ERR(trans);
7591 goto out;
7592 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007593
Josef Bacik907cbce2011-08-08 13:46:15 -04007594 /* Migrate the slack space for the truncate to our reserve */
7595 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7596 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007597 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007598
Chris Mason5a3f23d2009-03-31 13:27:11 -04007599 /*
7600 * setattr is responsible for setting the ordered_data_close flag,
7601 * but that is only tested during the last file release. That
7602 * could happen well after the next commit, leaving a great big
7603 * window where new writes may get lost if someone chooses to write
7604 * to this file after truncating to zero
7605 *
7606 * The inode doesn't have any dirty data here, and so if we commit
7607 * this is a noop. If someone immediately starts writing to the inode
7608 * it is very likely we'll catch some of their writes in this
7609 * transaction, and the commit will find this file on the ordered
7610 * data list with good things to send down.
7611 *
7612 * This is a best effort solution, there is still a window where
7613 * using truncate to replace the contents of the file will
7614 * end up with a zero length file after a crash.
7615 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007616 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7617 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007618 btrfs_add_ordered_operation(trans, root, inode);
7619
Josef Bacik5dc562c2012-08-17 13:14:17 -04007620 /*
7621 * So if we truncate and then write and fsync we normally would just
7622 * write the extents that changed, which is a problem if we need to
7623 * first truncate that entire inode. So set this flag so we write out
7624 * all of the extents in the inode to the sync log so we're completely
7625 * safe.
7626 */
7627 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007628 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007629
Yan, Zheng80825102009-11-12 09:35:36 +00007630 while (1) {
7631 ret = btrfs_truncate_inode_items(trans, root, inode,
7632 inode->i_size,
7633 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007634 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007635 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007636 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007637 }
Chris Mason39279cc2007-06-12 06:35:45 -04007638
Josef Bacikfcb80c22011-05-03 10:40:22 -04007639 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007640 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007641 if (ret) {
7642 err = ret;
7643 break;
7644 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007645
Yan, Zheng80825102009-11-12 09:35:36 +00007646 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007647 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007648
7649 trans = btrfs_start_transaction(root, 2);
7650 if (IS_ERR(trans)) {
7651 ret = err = PTR_ERR(trans);
7652 trans = NULL;
7653 break;
7654 }
7655
7656 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7657 rsv, min_size);
7658 BUG_ON(ret); /* shouldn't happen */
7659 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007660 }
7661
7662 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007663 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007664 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007665 if (ret)
7666 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007667 }
7668
Chris Mason917c16b2011-11-08 14:49:59 -05007669 if (trans) {
7670 trans->block_rsv = &root->fs_info->trans_block_rsv;
7671 ret = btrfs_update_inode(trans, root, inode);
7672 if (ret && !err)
7673 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007674
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007675 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007676 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007677 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007678
7679out:
7680 btrfs_free_block_rsv(root, rsv);
7681
Josef Bacik3893e332011-01-31 16:03:11 -05007682 if (ret && !err)
7683 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007684
Josef Bacik3893e332011-01-31 16:03:11 -05007685 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007686}
7687
Sven Wegener3b963622008-06-09 21:57:42 -04007688/*
Chris Masond352ac62008-09-29 15:18:18 -04007689 * create a new subvolume directory/inode (helper for the ioctl).
7690 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007691int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007692 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007693{
Chris Mason39279cc2007-06-12 06:35:45 -04007694 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007695 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007696 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007697
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007698 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7699 new_dirid, new_dirid,
7700 S_IFDIR | (~current_umask() & S_IRWXUGO),
7701 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007702 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007703 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007704 inode->i_op = &btrfs_dir_inode_operations;
7705 inode->i_fop = &btrfs_dir_file_operations;
7706
Miklos Szeredibfe86842011-10-28 14:13:29 +02007707 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007708 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007709
Yan, Zheng76dda932009-09-21 16:00:26 -04007710 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007711
Yan, Zheng76dda932009-09-21 16:00:26 -04007712 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007713 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007714}
7715
Chris Mason39279cc2007-06-12 06:35:45 -04007716struct inode *btrfs_alloc_inode(struct super_block *sb)
7717{
7718 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007719 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007720
7721 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7722 if (!ei)
7723 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007724
7725 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007726 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007727 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007728 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007729 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007730 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007731 ei->disk_i_size = 0;
7732 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007733 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007734 ei->index_cnt = (u64)-1;
7735 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007736 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007737
Josef Bacik9e0baf62011-07-15 15:16:44 +00007738 spin_lock_init(&ei->lock);
7739 ei->outstanding_extents = 0;
7740 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007741
Josef Bacik72ac3c02012-05-23 14:13:11 -04007742 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007743 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007744
Miao Xie16cdcec2011-04-22 18:12:22 +08007745 ei->delayed_node = NULL;
7746
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007747 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007748 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007749 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7750 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007751 ei->io_tree.track_uptodate = 1;
7752 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007753 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007754 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007755 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007756 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007757 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007758 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007759 RB_CLEAR_NODE(&ei->rb_node);
7760
7761 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007762}
7763
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007764static void btrfs_i_callback(struct rcu_head *head)
7765{
7766 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007767 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7768}
7769
Chris Mason39279cc2007-06-12 06:35:45 -04007770void btrfs_destroy_inode(struct inode *inode)
7771{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007772 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007773 struct btrfs_root *root = BTRFS_I(inode)->root;
7774
Al Virob3d9b7a2012-06-09 13:51:19 -04007775 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007776 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007777 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7778 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007779 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7780 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007781
Chris Mason5a3f23d2009-03-31 13:27:11 -04007782 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007783 * This can happen where we create an inode, but somebody else also
7784 * created the same inode and we need to destroy the one we already
7785 * created.
7786 */
7787 if (!root)
7788 goto free;
7789
7790 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007791 * Make sure we're properly removed from the ordered operation
7792 * lists.
7793 */
7794 smp_mb();
7795 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
Miao Xie199c2a92013-05-15 07:48:23 +00007796 spin_lock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007797 list_del_init(&BTRFS_I(inode)->ordered_operations);
Miao Xie199c2a92013-05-15 07:48:23 +00007798 spin_unlock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007799 }
7800
Josef Bacik8a35d952012-05-23 14:26:42 -04007801 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7802 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007803 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007804 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007805 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007806 }
Josef Bacik7b128762008-07-24 12:17:14 -04007807
Chris Masond3977122009-01-05 21:25:51 -05007808 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007809 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7810 if (!ordered)
7811 break;
7812 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007813 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007814 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007815 btrfs_remove_ordered_extent(inode, ordered);
7816 btrfs_put_ordered_extent(ordered);
7817 btrfs_put_ordered_extent(ordered);
7818 }
7819 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007820 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007821 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007822free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007823 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007824}
7825
Al Viro45321ac2010-06-07 13:43:19 -04007826int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007827{
7828 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007829
Naohiro Aota6379ef92013-06-06 09:56:34 +00007830 if (root == NULL)
7831 return 1;
7832
Liu Bofa6ac872013-02-20 14:10:23 +00007833 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007834 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007835 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007836 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007837 else
Al Viro45321ac2010-06-07 13:43:19 -04007838 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007839}
7840
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007841static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007842{
7843 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7844
7845 inode_init_once(&ei->vfs_inode);
7846}
7847
7848void btrfs_destroy_cachep(void)
7849{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007850 /*
7851 * Make sure all delayed rcu free inodes are flushed before we
7852 * destroy cache.
7853 */
7854 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007855 if (btrfs_inode_cachep)
7856 kmem_cache_destroy(btrfs_inode_cachep);
7857 if (btrfs_trans_handle_cachep)
7858 kmem_cache_destroy(btrfs_trans_handle_cachep);
7859 if (btrfs_transaction_cachep)
7860 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007861 if (btrfs_path_cachep)
7862 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007863 if (btrfs_free_space_cachep)
7864 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007865 if (btrfs_delalloc_work_cachep)
7866 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007867}
7868
7869int btrfs_init_cachep(void)
7870{
David Sterba837e1972012-09-07 03:00:48 -06007871 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007872 sizeof(struct btrfs_inode), 0,
7873 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007874 if (!btrfs_inode_cachep)
7875 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007876
David Sterba837e1972012-09-07 03:00:48 -06007877 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007878 sizeof(struct btrfs_trans_handle), 0,
7879 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007880 if (!btrfs_trans_handle_cachep)
7881 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007882
David Sterba837e1972012-09-07 03:00:48 -06007883 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007884 sizeof(struct btrfs_transaction), 0,
7885 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007886 if (!btrfs_transaction_cachep)
7887 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007888
David Sterba837e1972012-09-07 03:00:48 -06007889 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007890 sizeof(struct btrfs_path), 0,
7891 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007892 if (!btrfs_path_cachep)
7893 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007894
David Sterba837e1972012-09-07 03:00:48 -06007895 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007896 sizeof(struct btrfs_free_space), 0,
7897 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7898 if (!btrfs_free_space_cachep)
7899 goto fail;
7900
Miao Xie8ccf6f192012-10-25 09:28:04 +00007901 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7902 sizeof(struct btrfs_delalloc_work), 0,
7903 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7904 NULL);
7905 if (!btrfs_delalloc_work_cachep)
7906 goto fail;
7907
Chris Mason39279cc2007-06-12 06:35:45 -04007908 return 0;
7909fail:
7910 btrfs_destroy_cachep();
7911 return -ENOMEM;
7912}
7913
7914static int btrfs_getattr(struct vfsmount *mnt,
7915 struct dentry *dentry, struct kstat *stat)
7916{
Miao Xiedf0af1a52013-01-29 10:11:59 +00007917 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04007918 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007919 u32 blocksize = inode->i_sb->s_blocksize;
7920
Chris Mason39279cc2007-06-12 06:35:45 -04007921 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007922 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007923 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a52013-01-29 10:11:59 +00007924
7925 spin_lock(&BTRFS_I(inode)->lock);
7926 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
7927 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05007928 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a52013-01-29 10:11:59 +00007929 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007930 return 0;
7931}
7932
Chris Masond3977122009-01-05 21:25:51 -05007933static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7934 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007935{
7936 struct btrfs_trans_handle *trans;
7937 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007938 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007939 struct inode *new_inode = new_dentry->d_inode;
7940 struct inode *old_inode = old_dentry->d_inode;
7941 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007942 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007943 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007944 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007945 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007946
Li Zefan33345d012011-04-20 10:31:50 +08007947 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007948 return -EPERM;
7949
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007950 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007951 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007952 return -EXDEV;
7953
Li Zefan33345d012011-04-20 10:31:50 +08007954 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7955 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007956 return -ENOTEMPTY;
7957
Chris Mason39279cc2007-06-12 06:35:45 -04007958 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007959 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007960 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05007961
7962
7963 /* check for collisions, even if the name isn't there */
7964 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
7965 new_dentry->d_name.name,
7966 new_dentry->d_name.len);
7967
7968 if (ret) {
7969 if (ret == -EEXIST) {
7970 /* we shouldn't get
7971 * eexist without a new_inode */
7972 if (!new_inode) {
7973 WARN_ON(1);
7974 return ret;
7975 }
7976 } else {
7977 /* maybe -EOVERFLOW */
7978 return ret;
7979 }
7980 }
7981 ret = 0;
7982
Chris Mason5a3f23d2009-03-31 13:27:11 -04007983 /*
7984 * we're using rename to replace one file with another.
7985 * and the replacement file is large. Start IO on it now so
7986 * we don't add too much work to the end of the transaction
7987 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007988 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007989 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7990 filemap_flush(old_inode->i_mapping);
7991
Yan, Zheng76dda932009-09-21 16:00:26 -04007992 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007993 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007994 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007995 /*
7996 * We want to reserve the absolute worst case amount of items. So if
7997 * both inodes are subvols and we need to unlink them then that would
7998 * require 4 item modifications, but if they are both normal inodes it
7999 * would require 5 item modifications, so we'll assume their normal
8000 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8001 * should cover the worst case number of items we'll modify.
8002 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008003 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008004 if (IS_ERR(trans)) {
8005 ret = PTR_ERR(trans);
8006 goto out_notrans;
8007 }
Chris Mason5f39d392007-10-15 16:14:19 -04008008
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008009 if (dest != root)
8010 btrfs_record_root_in_trans(trans, dest);
8011
Yan, Zhenga5719522009-09-24 09:17:31 -04008012 ret = btrfs_set_inode_index(new_dir, &index);
8013 if (ret)
8014 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008015
Li Zefan33345d012011-04-20 10:31:50 +08008016 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008017 /* force full log commit if subvolume involved. */
8018 root->fs_info->last_trans_log_full_commit = trans->transid;
8019 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008020 ret = btrfs_insert_inode_ref(trans, dest,
8021 new_dentry->d_name.name,
8022 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008023 old_ino,
8024 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008025 if (ret)
8026 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008027 /*
8028 * this is an ugly little race, but the rename is required
8029 * to make sure that if we crash, the inode is either at the
8030 * old name or the new one. pinning the log transaction lets
8031 * us make sure we don't allow a log commit to come in after
8032 * we unlink the name but before we add the new name back in.
8033 */
8034 btrfs_pin_log_trans(root);
8035 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008036 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008037 * make sure the inode gets flushed if it is replacing
8038 * something.
8039 */
Li Zefan33345d012011-04-20 10:31:50 +08008040 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008041 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008042
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008043 inode_inc_iversion(old_dir);
8044 inode_inc_iversion(new_dir);
8045 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008046 old_dir->i_ctime = old_dir->i_mtime = ctime;
8047 new_dir->i_ctime = new_dir->i_mtime = ctime;
8048 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008049
Chris Mason12fcfd22009-03-24 10:24:20 -04008050 if (old_dentry->d_parent != new_dentry->d_parent)
8051 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8052
Li Zefan33345d012011-04-20 10:31:50 +08008053 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008054 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8055 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8056 old_dentry->d_name.name,
8057 old_dentry->d_name.len);
8058 } else {
Al Viro92986792011-03-04 17:14:37 +00008059 ret = __btrfs_unlink_inode(trans, root, old_dir,
8060 old_dentry->d_inode,
8061 old_dentry->d_name.name,
8062 old_dentry->d_name.len);
8063 if (!ret)
8064 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008065 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008066 if (ret) {
8067 btrfs_abort_transaction(trans, root, ret);
8068 goto out_fail;
8069 }
Chris Mason39279cc2007-06-12 06:35:45 -04008070
8071 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008072 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008073 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008074 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008075 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8076 root_objectid = BTRFS_I(new_inode)->location.objectid;
8077 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8078 root_objectid,
8079 new_dentry->d_name.name,
8080 new_dentry->d_name.len);
8081 BUG_ON(new_inode->i_nlink == 0);
8082 } else {
8083 ret = btrfs_unlink_inode(trans, dest, new_dir,
8084 new_dentry->d_inode,
8085 new_dentry->d_name.name,
8086 new_dentry->d_name.len);
8087 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008088 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008089 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008090 if (ret) {
8091 btrfs_abort_transaction(trans, root, ret);
8092 goto out_fail;
8093 }
Chris Mason39279cc2007-06-12 06:35:45 -04008094 }
Josef Bacikaec74772008-07-24 12:12:38 -04008095
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008096 ret = btrfs_add_link(trans, new_dir, old_inode,
8097 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008098 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008099 if (ret) {
8100 btrfs_abort_transaction(trans, root, ret);
8101 goto out_fail;
8102 }
Chris Mason39279cc2007-06-12 06:35:45 -04008103
Li Zefan33345d012011-04-20 10:31:50 +08008104 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008105 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008106 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008107 btrfs_end_log_trans(root);
8108 }
Chris Mason39279cc2007-06-12 06:35:45 -04008109out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008110 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008111out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008112 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008113 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008114
Chris Mason39279cc2007-06-12 06:35:45 -04008115 return ret;
8116}
8117
Miao Xie8ccf6f192012-10-25 09:28:04 +00008118static void btrfs_run_delalloc_work(struct btrfs_work *work)
8119{
8120 struct btrfs_delalloc_work *delalloc_work;
8121
8122 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8123 work);
8124 if (delalloc_work->wait)
8125 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8126 else
8127 filemap_flush(delalloc_work->inode->i_mapping);
8128
8129 if (delalloc_work->delay_iput)
8130 btrfs_add_delayed_iput(delalloc_work->inode);
8131 else
8132 iput(delalloc_work->inode);
8133 complete(&delalloc_work->completion);
8134}
8135
8136struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8137 int wait, int delay_iput)
8138{
8139 struct btrfs_delalloc_work *work;
8140
8141 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8142 if (!work)
8143 return NULL;
8144
8145 init_completion(&work->completion);
8146 INIT_LIST_HEAD(&work->list);
8147 work->inode = inode;
8148 work->wait = wait;
8149 work->delay_iput = delay_iput;
8150 work->work.func = btrfs_run_delalloc_work;
8151
8152 return work;
8153}
8154
8155void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8156{
8157 wait_for_completion(&work->completion);
8158 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8159}
8160
Chris Masond352ac62008-09-29 15:18:18 -04008161/*
8162 * some fairly slow code that needs optimization. This walks the list
8163 * of all the inodes with pending delalloc and forces them to disk.
8164 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00008165static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008166{
Chris Masonea8c2812008-08-04 23:17:27 -04008167 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008168 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008169 struct btrfs_delalloc_work *work, *next;
8170 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008171 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008172 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008173
Miao Xie8ccf6f192012-10-25 09:28:04 +00008174 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008175 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008176
Miao Xieeb73c1b2013-05-15 07:48:22 +00008177 spin_lock(&root->delalloc_lock);
8178 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008179 while (!list_empty(&splice)) {
8180 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008181 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008182
Miao Xieeb73c1b2013-05-15 07:48:22 +00008183 list_move_tail(&binode->delalloc_inodes,
8184 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008185 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a52013-01-29 10:11:59 +00008186 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008187 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008188 continue;
Miao Xiedf0af1a52013-01-29 10:11:59 +00008189 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008190 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008191
8192 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8193 if (unlikely(!work)) {
8194 ret = -ENOMEM;
8195 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008196 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008197 list_add_tail(&work->list, &works);
8198 btrfs_queue_worker(&root->fs_info->flush_workers,
8199 &work->work);
8200
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008201 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008202 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008203 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008204 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008205
Miao Xie1eafa6c2013-01-22 10:49:00 +00008206 list_for_each_entry_safe(work, next, &works, list) {
8207 list_del_init(&work->list);
8208 btrfs_wait_and_free_delalloc_work(work);
8209 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008210 return 0;
8211out:
8212 list_for_each_entry_safe(work, next, &works, list) {
8213 list_del_init(&work->list);
8214 btrfs_wait_and_free_delalloc_work(work);
8215 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008216
Miao Xieeb73c1b2013-05-15 07:48:22 +00008217 if (!list_empty_careful(&splice)) {
8218 spin_lock(&root->delalloc_lock);
8219 list_splice_tail(&splice, &root->delalloc_inodes);
8220 spin_unlock(&root->delalloc_lock);
8221 }
8222 return ret;
8223}
8224
8225int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8226{
8227 int ret;
8228
8229 if (root->fs_info->sb->s_flags & MS_RDONLY)
8230 return -EROFS;
8231
8232 ret = __start_delalloc_inodes(root, delay_iput);
8233 /*
8234 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008235 * we have to make sure the IO is actually started and that
8236 * ordered extents get created before we return
8237 */
8238 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008239 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008240 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008241 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008242 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8243 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008244 }
8245 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008246 return ret;
8247}
8248
8249int btrfs_start_all_delalloc_inodes(struct btrfs_fs_info *fs_info,
8250 int delay_iput)
8251{
8252 struct btrfs_root *root;
8253 struct list_head splice;
8254 int ret;
8255
8256 if (fs_info->sb->s_flags & MS_RDONLY)
8257 return -EROFS;
8258
8259 INIT_LIST_HEAD(&splice);
8260
8261 spin_lock(&fs_info->delalloc_root_lock);
8262 list_splice_init(&fs_info->delalloc_roots, &splice);
8263 while (!list_empty(&splice)) {
8264 root = list_first_entry(&splice, struct btrfs_root,
8265 delalloc_root);
8266 root = btrfs_grab_fs_root(root);
8267 BUG_ON(!root);
8268 list_move_tail(&root->delalloc_root,
8269 &fs_info->delalloc_roots);
8270 spin_unlock(&fs_info->delalloc_root_lock);
8271
8272 ret = __start_delalloc_inodes(root, delay_iput);
8273 btrfs_put_fs_root(root);
8274 if (ret)
8275 goto out;
8276
8277 spin_lock(&fs_info->delalloc_root_lock);
8278 }
8279 spin_unlock(&fs_info->delalloc_root_lock);
8280
8281 atomic_inc(&fs_info->async_submit_draining);
8282 while (atomic_read(&fs_info->nr_async_submits) ||
8283 atomic_read(&fs_info->async_delalloc_pages)) {
8284 wait_event(fs_info->async_submit_wait,
8285 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8286 atomic_read(&fs_info->async_delalloc_pages) == 0));
8287 }
8288 atomic_dec(&fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008289 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008290out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008291 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008292 spin_lock(&fs_info->delalloc_root_lock);
8293 list_splice_tail(&splice, &fs_info->delalloc_roots);
8294 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008295 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008296 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008297}
8298
Chris Mason39279cc2007-06-12 06:35:45 -04008299static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8300 const char *symname)
8301{
8302 struct btrfs_trans_handle *trans;
8303 struct btrfs_root *root = BTRFS_I(dir)->root;
8304 struct btrfs_path *path;
8305 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008306 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008307 int err;
8308 int drop_inode = 0;
8309 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008310 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008311 int name_len;
8312 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008313 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008314 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008315 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008316
8317 name_len = strlen(symname) + 1;
8318 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8319 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008320
Josef Bacik9ed74f22009-09-11 16:12:44 -04008321 /*
8322 * 2 items for inode item and ref
8323 * 2 items for dir items
8324 * 1 item for xattr if selinux is on
8325 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008326 trans = btrfs_start_transaction(root, 5);
8327 if (IS_ERR(trans))
8328 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008329
Li Zefan581bb052011-04-20 10:06:11 +08008330 err = btrfs_find_free_ino(root, &objectid);
8331 if (err)
8332 goto out_unlock;
8333
Josef Bacikaec74772008-07-24 12:12:38 -04008334 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008335 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008336 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008337 if (IS_ERR(inode)) {
8338 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008339 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008340 }
Chris Mason39279cc2007-06-12 06:35:45 -04008341
Eric Paris2a7dba32011-02-01 11:05:39 -05008342 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008343 if (err) {
8344 drop_inode = 1;
8345 goto out_unlock;
8346 }
8347
Casey Schauflerad19db72011-12-15 10:09:07 -05008348 /*
8349 * If the active LSM wants to access the inode during
8350 * d_instantiate it needs these. Smack checks to see
8351 * if the filesystem supports xattrs by looking at the
8352 * ops vector.
8353 */
8354 inode->i_fop = &btrfs_file_operations;
8355 inode->i_op = &btrfs_file_inode_operations;
8356
Josef Bacika1b075d2010-11-19 20:36:11 +00008357 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008358 if (err)
8359 drop_inode = 1;
8360 else {
8361 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008362 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008363 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008364 }
Chris Mason39279cc2007-06-12 06:35:45 -04008365 if (drop_inode)
8366 goto out_unlock;
8367
8368 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008369 if (!path) {
8370 err = -ENOMEM;
8371 drop_inode = 1;
8372 goto out_unlock;
8373 }
Li Zefan33345d012011-04-20 10:31:50 +08008374 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008375 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008376 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8377 datasize = btrfs_file_extent_calc_inline_size(name_len);
8378 err = btrfs_insert_empty_item(trans, root, path, &key,
8379 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008380 if (err) {
8381 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008382 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008383 goto out_unlock;
8384 }
Chris Mason5f39d392007-10-15 16:14:19 -04008385 leaf = path->nodes[0];
8386 ei = btrfs_item_ptr(leaf, path->slots[0],
8387 struct btrfs_file_extent_item);
8388 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8389 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008390 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008391 btrfs_set_file_extent_encryption(leaf, ei, 0);
8392 btrfs_set_file_extent_compression(leaf, ei, 0);
8393 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8394 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8395
Chris Mason39279cc2007-06-12 06:35:45 -04008396 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008397 write_extent_buffer(leaf, symname, ptr, name_len);
8398 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008399 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008400
Chris Mason39279cc2007-06-12 06:35:45 -04008401 inode->i_op = &btrfs_symlink_inode_operations;
8402 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008403 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008404 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008405 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008406 err = btrfs_update_inode(trans, root, inode);
8407 if (err)
8408 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008409
8410out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008411 if (!err)
8412 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008413 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008414 if (drop_inode) {
8415 inode_dec_link_count(inode);
8416 iput(inode);
8417 }
Liu Bob53d3f52012-11-14 14:34:34 +00008418 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008419 return err;
8420}
Chris Mason16432982008-04-10 10:23:21 -04008421
Josef Bacik0af3d002010-06-21 14:48:16 -04008422static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8423 u64 start, u64 num_bytes, u64 min_size,
8424 loff_t actual_len, u64 *alloc_hint,
8425 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008426{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008427 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8428 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008429 struct btrfs_root *root = BTRFS_I(inode)->root;
8430 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008431 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008432 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008433 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008434 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008435 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008436
Josef Bacik0af3d002010-06-21 14:48:16 -04008437 if (trans)
8438 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008439 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008440 if (own_trans) {
8441 trans = btrfs_start_transaction(root, 3);
8442 if (IS_ERR(trans)) {
8443 ret = PTR_ERR(trans);
8444 break;
8445 }
Yan Zhengd899e052008-10-30 14:25:28 -04008446 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008447
Chris Mason154ea282013-03-05 11:11:26 -05008448 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8449 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04008450 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
8451 *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008452 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008453 if (own_trans)
8454 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008455 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008456 }
8457
Yan Zhengd899e052008-10-30 14:25:28 -04008458 ret = insert_reserved_file_extent(trans, inode,
8459 cur_offset, ins.objectid,
8460 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008461 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008462 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008463 if (ret) {
8464 btrfs_abort_transaction(trans, root, ret);
8465 if (own_trans)
8466 btrfs_end_transaction(trans, root);
8467 break;
8468 }
Chris Masona1ed8352009-09-11 12:27:37 -04008469 btrfs_drop_extent_cache(inode, cur_offset,
8470 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008471
Josef Bacik5dc562c2012-08-17 13:14:17 -04008472 em = alloc_extent_map();
8473 if (!em) {
8474 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8475 &BTRFS_I(inode)->runtime_flags);
8476 goto next;
8477 }
8478
8479 em->start = cur_offset;
8480 em->orig_start = cur_offset;
8481 em->len = ins.offset;
8482 em->block_start = ins.objectid;
8483 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008484 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008485 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008486 em->bdev = root->fs_info->fs_devices->latest_bdev;
8487 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8488 em->generation = trans->transid;
8489
8490 while (1) {
8491 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008492 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008493 write_unlock(&em_tree->lock);
8494 if (ret != -EEXIST)
8495 break;
8496 btrfs_drop_extent_cache(inode, cur_offset,
8497 cur_offset + ins.offset - 1,
8498 0);
8499 }
8500 free_extent_map(em);
8501next:
Yan Zhengd899e052008-10-30 14:25:28 -04008502 num_bytes -= ins.offset;
8503 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008504 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008505
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008506 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008507 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008508 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008509 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008510 (actual_len > inode->i_size) &&
8511 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008512 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008513 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008514 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008515 i_size = cur_offset;
8516 i_size_write(inode, i_size);
8517 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008518 }
8519
Yan Zhengd899e052008-10-30 14:25:28 -04008520 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008521
8522 if (ret) {
8523 btrfs_abort_transaction(trans, root, ret);
8524 if (own_trans)
8525 btrfs_end_transaction(trans, root);
8526 break;
8527 }
Yan Zhengd899e052008-10-30 14:25:28 -04008528
Josef Bacik0af3d002010-06-21 14:48:16 -04008529 if (own_trans)
8530 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008531 }
Yan Zhengd899e052008-10-30 14:25:28 -04008532 return ret;
8533}
8534
Josef Bacik0af3d002010-06-21 14:48:16 -04008535int btrfs_prealloc_file_range(struct inode *inode, int mode,
8536 u64 start, u64 num_bytes, u64 min_size,
8537 loff_t actual_len, u64 *alloc_hint)
8538{
8539 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8540 min_size, actual_len, alloc_hint,
8541 NULL);
8542}
8543
8544int btrfs_prealloc_file_range_trans(struct inode *inode,
8545 struct btrfs_trans_handle *trans, int mode,
8546 u64 start, u64 num_bytes, u64 min_size,
8547 loff_t actual_len, u64 *alloc_hint)
8548{
8549 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8550 min_size, actual_len, alloc_hint, trans);
8551}
8552
Chris Masone6dcd2d2008-07-17 12:53:50 -04008553static int btrfs_set_page_dirty(struct page *page)
8554{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008555 return __set_page_dirty_nobuffers(page);
8556}
8557
Al Viro10556cb2011-06-20 19:28:19 -04008558static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008559{
Li Zefanb83cc962010-12-20 16:04:08 +08008560 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008561 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008562
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008563 if (mask & MAY_WRITE &&
8564 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8565 if (btrfs_root_readonly(root))
8566 return -EROFS;
8567 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8568 return -EACCES;
8569 }
Al Viro2830ba72011-06-20 19:16:29 -04008570 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008571}
Chris Mason39279cc2007-06-12 06:35:45 -04008572
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008573static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008574 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008575 .lookup = btrfs_lookup,
8576 .create = btrfs_create,
8577 .unlink = btrfs_unlink,
8578 .link = btrfs_link,
8579 .mkdir = btrfs_mkdir,
8580 .rmdir = btrfs_rmdir,
8581 .rename = btrfs_rename,
8582 .symlink = btrfs_symlink,
8583 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008584 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008585 .setxattr = btrfs_setxattr,
8586 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008587 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008588 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008589 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008590 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008591};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008592static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008593 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008594 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008595 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008596};
Yan, Zheng76dda932009-09-21 16:00:26 -04008597
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008598static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008599 .llseek = generic_file_llseek,
8600 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04008601 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008602 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008603#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008604 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008605#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008606 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008607 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008608};
8609
Chris Masond1310b22008-01-24 16:13:08 -05008610static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008611 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008612 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008613 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008614 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008615 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008616 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008617 .set_bit_hook = btrfs_set_bit_hook,
8618 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008619 .merge_extent_hook = btrfs_merge_extent_hook,
8620 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008621};
8622
Chris Mason35054392009-01-21 13:11:13 -05008623/*
8624 * btrfs doesn't support the bmap operation because swapfiles
8625 * use bmap to make a mapping of extents in the file. They assume
8626 * these extents won't change over the life of the file and they
8627 * use the bmap result to do IO directly to the drive.
8628 *
8629 * the btrfs bmap call would return logical addresses that aren't
8630 * suitable for IO and they also will change frequently as COW
8631 * operations happen. So, swapfile + btrfs == corruption.
8632 *
8633 * For now we're avoiding this by dropping bmap.
8634 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008635static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008636 .readpage = btrfs_readpage,
8637 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008638 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008639 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008640 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008641 .invalidatepage = btrfs_invalidatepage,
8642 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008643 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008644 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008645};
8646
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008647static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008648 .readpage = btrfs_readpage,
8649 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008650 .invalidatepage = btrfs_invalidatepage,
8651 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008652};
8653
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008654static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008655 .getattr = btrfs_getattr,
8656 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008657 .setxattr = btrfs_setxattr,
8658 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008659 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008660 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008661 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008662 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008663 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008664 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008665};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008666static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008667 .getattr = btrfs_getattr,
8668 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008669 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008670 .setxattr = btrfs_setxattr,
8671 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008672 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008673 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008674 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008675 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008676};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008677static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008678 .readlink = generic_readlink,
8679 .follow_link = page_follow_link_light,
8680 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008681 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008682 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008683 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008684 .setxattr = btrfs_setxattr,
8685 .getxattr = btrfs_getxattr,
8686 .listxattr = btrfs_listxattr,
8687 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008688 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008689 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008690};
Yan, Zheng76dda932009-09-21 16:00:26 -04008691
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008692const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008693 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008694 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008695};