blob: 939dd1ff34a7b93dbc08bf00f951786f6bd61a2a [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) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01002972 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002973 if (reserve) {
2974 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2975 &BTRFS_I(inode)->runtime_flags);
2976 btrfs_orphan_release_metadata(inode);
2977 }
2978 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04002979 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2980 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002981 btrfs_abort_transaction(trans, root, ret);
2982 return ret;
2983 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002984 }
2985 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002986 }
2987
2988 /* insert an orphan item to track subvolume contains orphan files */
2989 if (insert >= 2) {
2990 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2991 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002992 if (ret && ret != -EEXIST) {
2993 btrfs_abort_transaction(trans, root, ret);
2994 return ret;
2995 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002996 }
2997 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002998}
2999
3000/*
3001 * We have done the truncate/delete so we can go ahead and remove the orphan
3002 * item for this particular inode.
3003 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003004static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3005 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003006{
3007 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003008 int delete_item = 0;
3009 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003010 int ret = 0;
3011
Yan, Zhengd68fc572010-05-16 10:49:58 -04003012 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003013 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3014 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003015 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003016
Josef Bacik72ac3c02012-05-23 14:13:11 -04003017 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3018 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003019 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003020 spin_unlock(&root->orphan_lock);
3021
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003022 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003023 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003024 if (trans)
3025 ret = btrfs_del_orphan_item(trans, root,
3026 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003027 }
Josef Bacik7b128762008-07-24 12:17:14 -04003028
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003029 if (release_rsv)
3030 btrfs_orphan_release_metadata(inode);
3031
Josef Bacik4ef31a42013-08-13 14:10:08 -04003032 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003033}
3034
3035/*
3036 * this cleans up any orphans that may be left on the list from the last use
3037 * of this root.
3038 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003039int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003040{
3041 struct btrfs_path *path;
3042 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003043 struct btrfs_key key, found_key;
3044 struct btrfs_trans_handle *trans;
3045 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003046 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003047 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3048
Yan, Zhengd68fc572010-05-16 10:49:58 -04003049 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003050 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003051
3052 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003053 if (!path) {
3054 ret = -ENOMEM;
3055 goto out;
3056 }
Josef Bacik7b128762008-07-24 12:17:14 -04003057 path->reada = -1;
3058
3059 key.objectid = BTRFS_ORPHAN_OBJECTID;
3060 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3061 key.offset = (u64)-1;
3062
Josef Bacik7b128762008-07-24 12:17:14 -04003063 while (1) {
3064 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003065 if (ret < 0)
3066 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003067
3068 /*
3069 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003070 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003071 * find the key and see if we have stuff that matches
3072 */
3073 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003074 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003075 if (path->slots[0] == 0)
3076 break;
3077 path->slots[0]--;
3078 }
3079
3080 /* pull out the item */
3081 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003082 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3083
3084 /* make sure the item matches what we want */
3085 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3086 break;
3087 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3088 break;
3089
3090 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003091 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003092
3093 /*
3094 * this is where we are basically btrfs_lookup, without the
3095 * crossing root thing. we store the inode number in the
3096 * offset of the orphan item.
3097 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003098
3099 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003100 btrfs_err(root->fs_info,
3101 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003102 ret = -EINVAL;
3103 goto out;
3104 }
3105
3106 last_objectid = found_key.offset;
3107
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003108 found_key.objectid = found_key.offset;
3109 found_key.type = BTRFS_INODE_ITEM_KEY;
3110 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003111 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303112 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003113 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003114 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003115
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003116 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3117 struct btrfs_root *dead_root;
3118 struct btrfs_fs_info *fs_info = root->fs_info;
3119 int is_dead_root = 0;
3120
3121 /*
3122 * this is an orphan in the tree root. Currently these
3123 * could come from 2 sources:
3124 * a) a snapshot deletion in progress
3125 * b) a free space cache inode
3126 * We need to distinguish those two, as the snapshot
3127 * orphan must not get deleted.
3128 * find_dead_roots already ran before us, so if this
3129 * is a snapshot deletion, we should find the root
3130 * in the dead_roots list
3131 */
3132 spin_lock(&fs_info->trans_lock);
3133 list_for_each_entry(dead_root, &fs_info->dead_roots,
3134 root_list) {
3135 if (dead_root->root_key.objectid ==
3136 found_key.objectid) {
3137 is_dead_root = 1;
3138 break;
3139 }
3140 }
3141 spin_unlock(&fs_info->trans_lock);
3142 if (is_dead_root) {
3143 /* prevent this orphan from being found again */
3144 key.offset = found_key.objectid - 1;
3145 continue;
3146 }
3147 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003148 /*
3149 * Inode is already gone but the orphan item is still there,
3150 * kill the orphan item.
3151 */
3152 if (ret == -ESTALE) {
3153 trans = btrfs_start_transaction(root, 1);
3154 if (IS_ERR(trans)) {
3155 ret = PTR_ERR(trans);
3156 goto out;
3157 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003158 btrfs_debug(root->fs_info, "auto deleting %Lu",
3159 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003160 ret = btrfs_del_orphan_item(trans, root,
3161 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003162 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003163 if (ret)
3164 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003165 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003166 }
Josef Bacik7b128762008-07-24 12:17:14 -04003167
Josef Bacik7b128762008-07-24 12:17:14 -04003168 /*
3169 * add this inode to the orphan list so btrfs_orphan_del does
3170 * the proper thing when we hit it
3171 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003172 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3173 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003174 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003175
Josef Bacik7b128762008-07-24 12:17:14 -04003176 /* if we have links, this was a truncate, lets do that */
3177 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003178 if (!S_ISREG(inode->i_mode)) {
3179 WARN_ON(1);
3180 iput(inode);
3181 continue;
3182 }
Josef Bacik7b128762008-07-24 12:17:14 -04003183 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003184
3185 /* 1 for the orphan item deletion. */
3186 trans = btrfs_start_transaction(root, 1);
3187 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003188 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003189 ret = PTR_ERR(trans);
3190 goto out;
3191 }
3192 ret = btrfs_orphan_add(trans, inode);
3193 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003194 if (ret) {
3195 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003196 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003197 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003198
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003199 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003200 if (ret)
3201 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003202 } else {
3203 nr_unlink++;
3204 }
3205
3206 /* this will do delete_inode and everything for us */
3207 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003208 if (ret)
3209 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003210 }
Miao Xie3254c872011-11-10 20:45:05 -05003211 /* release the path since we're done with it */
3212 btrfs_release_path(path);
3213
Yan, Zhengd68fc572010-05-16 10:49:58 -04003214 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3215
3216 if (root->orphan_block_rsv)
3217 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3218 (u64)-1);
3219
3220 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003221 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003222 if (!IS_ERR(trans))
3223 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003224 }
Josef Bacik7b128762008-07-24 12:17:14 -04003225
3226 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003227 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003228 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003229 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003230
3231out:
3232 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003233 btrfs_crit(root->fs_info,
3234 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003235 btrfs_free_path(path);
3236 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003237}
3238
Chris Masond352ac62008-09-29 15:18:18 -04003239/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003240 * very simple check to peek ahead in the leaf looking for xattrs. If we
3241 * don't find any xattrs, we know there can't be any acls.
3242 *
3243 * slot is the slot the inode is in, objectid is the objectid of the inode
3244 */
3245static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3246 int slot, u64 objectid)
3247{
3248 u32 nritems = btrfs_header_nritems(leaf);
3249 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003250 static u64 xattr_access = 0;
3251 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003252 int scanned = 0;
3253
Josef Bacikf23b5a52013-06-19 10:16:26 -04003254 if (!xattr_access) {
3255 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3256 strlen(POSIX_ACL_XATTR_ACCESS));
3257 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3258 strlen(POSIX_ACL_XATTR_DEFAULT));
3259 }
3260
Chris Mason46a53cc2009-04-27 11:47:50 -04003261 slot++;
3262 while (slot < nritems) {
3263 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3264
3265 /* we found a different objectid, there must not be acls */
3266 if (found_key.objectid != objectid)
3267 return 0;
3268
3269 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003270 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3271 if (found_key.offset == xattr_access ||
3272 found_key.offset == xattr_default)
3273 return 1;
3274 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003275
3276 /*
3277 * we found a key greater than an xattr key, there can't
3278 * be any acls later on
3279 */
3280 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3281 return 0;
3282
3283 slot++;
3284 scanned++;
3285
3286 /*
3287 * it goes inode, inode backrefs, xattrs, extents,
3288 * so if there are a ton of hard links to an inode there can
3289 * be a lot of backrefs. Don't waste time searching too hard,
3290 * this is just an optimization
3291 */
3292 if (scanned >= 8)
3293 break;
3294 }
3295 /* we hit the end of the leaf before we found an xattr or
3296 * something larger than an xattr. We have to assume the inode
3297 * has acls
3298 */
3299 return 1;
3300}
3301
3302/*
Chris Masond352ac62008-09-29 15:18:18 -04003303 * read an inode from the btree into the in-memory inode
3304 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003305static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003306{
3307 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003308 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003309 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003310 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003311 struct btrfs_root *root = BTRFS_I(inode)->root;
3312 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003313 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003314 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003315 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003316 bool filled = false;
3317
3318 ret = btrfs_fill_inode(inode, &rdev);
3319 if (!ret)
3320 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003321
3322 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003323 if (!path)
3324 goto make_bad;
3325
Josef Bacikd90c7322011-05-17 09:50:54 -04003326 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003327 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003328
Chris Mason39279cc2007-06-12 06:35:45 -04003329 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003330 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003331 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003332
Chris Mason5f39d392007-10-15 16:14:19 -04003333 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003334
3335 if (filled)
3336 goto cache_acl;
3337
Chris Mason5f39d392007-10-15 16:14:19 -04003338 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3339 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003340 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003341 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003342 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3343 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003344 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003345
3346 tspec = btrfs_inode_atime(inode_item);
3347 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3348 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3349
3350 tspec = btrfs_inode_mtime(inode_item);
3351 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3352 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3353
3354 tspec = btrfs_inode_ctime(inode_item);
3355 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3356 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3357
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003358 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003359 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003360 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3361
3362 /*
3363 * If we were modified in the current generation and evicted from memory
3364 * and then re-read we need to do a full sync since we don't have any
3365 * idea about which extents were modified before we were evicted from
3366 * cache.
3367 */
3368 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3369 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3370 &BTRFS_I(inode)->runtime_flags);
3371
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003372 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003373 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003374 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003375 rdev = btrfs_inode_rdev(leaf, inode_item);
3376
Josef Bacikaec74772008-07-24 12:12:38 -04003377 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003378 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003379cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003380 /*
3381 * try to precache a NULL acl entry for files that don't have
3382 * any xattrs or acls
3383 */
Li Zefan33345d012011-04-20 10:31:50 +08003384 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3385 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003386 if (!maybe_acls)
3387 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003388
Chris Mason39279cc2007-06-12 06:35:45 -04003389 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003390
Chris Mason39279cc2007-06-12 06:35:45 -04003391 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003392 case S_IFREG:
3393 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003394 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003395 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003396 inode->i_fop = &btrfs_file_operations;
3397 inode->i_op = &btrfs_file_inode_operations;
3398 break;
3399 case S_IFDIR:
3400 inode->i_fop = &btrfs_dir_file_operations;
3401 if (root == root->fs_info->tree_root)
3402 inode->i_op = &btrfs_dir_ro_inode_operations;
3403 else
3404 inode->i_op = &btrfs_dir_inode_operations;
3405 break;
3406 case S_IFLNK:
3407 inode->i_op = &btrfs_symlink_inode_operations;
3408 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003409 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003410 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003411 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003412 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003413 init_special_inode(inode, inode->i_mode, rdev);
3414 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003415 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003416
3417 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003418 return;
3419
3420make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003421 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003422 make_bad_inode(inode);
3423}
3424
Chris Masond352ac62008-09-29 15:18:18 -04003425/*
3426 * given a leaf and an inode, copy the inode fields into the leaf
3427 */
Chris Masone02119d2008-09-05 16:13:11 -04003428static void fill_inode_item(struct btrfs_trans_handle *trans,
3429 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003430 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003431 struct inode *inode)
3432{
Liu Bo51fab692012-12-27 09:01:21 +00003433 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003434
Liu Bo51fab692012-12-27 09:01:21 +00003435 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003436
Liu Bo51fab692012-12-27 09:01:21 +00003437 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3438 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3439 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3440 &token);
3441 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3442 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003443
Liu Bo51fab692012-12-27 09:01:21 +00003444 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3445 inode->i_atime.tv_sec, &token);
3446 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3447 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003448
Liu Bo51fab692012-12-27 09:01:21 +00003449 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3450 inode->i_mtime.tv_sec, &token);
3451 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3452 inode->i_mtime.tv_nsec, &token);
3453
3454 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3455 inode->i_ctime.tv_sec, &token);
3456 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3457 inode->i_ctime.tv_nsec, &token);
3458
3459 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3460 &token);
3461 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3462 &token);
3463 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3464 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3465 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3466 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3467 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003468}
3469
Chris Masond352ac62008-09-29 15:18:18 -04003470/*
3471 * copy everything in the in-memory inode into the btree.
3472 */
Chris Mason21151332011-11-10 20:39:08 -05003473static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003474 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003475{
3476 struct btrfs_inode_item *inode_item;
3477 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003478 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003479 int ret;
3480
3481 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003482 if (!path)
3483 return -ENOMEM;
3484
Chris Masonb9473432009-03-13 11:00:37 -04003485 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003486 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3487 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003488 if (ret) {
3489 if (ret > 0)
3490 ret = -ENOENT;
3491 goto failed;
3492 }
3493
Chris Masonb4ce94d2009-02-04 09:25:08 -05003494 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003495 leaf = path->nodes[0];
3496 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003497 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003498
Chris Masone02119d2008-09-05 16:13:11 -04003499 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003500 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003501 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003502 ret = 0;
3503failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003504 btrfs_free_path(path);
3505 return ret;
3506}
3507
Chris Masond352ac62008-09-29 15:18:18 -04003508/*
Chris Mason21151332011-11-10 20:39:08 -05003509 * copy everything in the in-memory inode into the btree.
3510 */
3511noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3512 struct btrfs_root *root, struct inode *inode)
3513{
3514 int ret;
3515
3516 /*
3517 * If the inode is a free space inode, we can deadlock during commit
3518 * if we put it into the delayed code.
3519 *
3520 * The data relocation inode should also be directly updated
3521 * without delay
3522 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003523 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003524 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003525 btrfs_update_root_times(trans, root);
3526
Chris Mason21151332011-11-10 20:39:08 -05003527 ret = btrfs_delayed_update_inode(trans, root, inode);
3528 if (!ret)
3529 btrfs_set_inode_last_trans(trans, inode);
3530 return ret;
3531 }
3532
3533 return btrfs_update_inode_item(trans, root, inode);
3534}
3535
Josef Bacikbe6aef62012-10-22 15:43:12 -04003536noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3537 struct btrfs_root *root,
3538 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003539{
3540 int ret;
3541
3542 ret = btrfs_update_inode(trans, root, inode);
3543 if (ret == -ENOSPC)
3544 return btrfs_update_inode_item(trans, root, inode);
3545 return ret;
3546}
3547
3548/*
Chris Masond352ac62008-09-29 15:18:18 -04003549 * unlink helper that gets used here in inode.c and in the tree logging
3550 * recovery code. It remove a link in a directory with a given name, and
3551 * also drops the back refs in the inode to the directory
3552 */
Al Viro92986792011-03-04 17:14:37 +00003553static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3554 struct btrfs_root *root,
3555 struct inode *dir, struct inode *inode,
3556 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003557{
3558 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003559 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003560 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003561 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003562 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003563 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003564 u64 ino = btrfs_ino(inode);
3565 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003566
3567 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003568 if (!path) {
3569 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003570 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003571 }
3572
Chris Masonb9473432009-03-13 11:00:37 -04003573 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003574 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003575 name, name_len, -1);
3576 if (IS_ERR(di)) {
3577 ret = PTR_ERR(di);
3578 goto err;
3579 }
3580 if (!di) {
3581 ret = -ENOENT;
3582 goto err;
3583 }
Chris Mason5f39d392007-10-15 16:14:19 -04003584 leaf = path->nodes[0];
3585 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003586 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003587 if (ret)
3588 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003589 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003590
Li Zefan33345d012011-04-20 10:31:50 +08003591 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3592 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003593 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003594 btrfs_info(root->fs_info,
3595 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003596 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003597 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003598 goto err;
3599 }
3600
Miao Xie16cdcec2011-04-22 18:12:22 +08003601 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003602 if (ret) {
3603 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003604 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003605 }
Chris Mason39279cc2007-06-12 06:35:45 -04003606
Chris Masone02119d2008-09-05 16:13:11 -04003607 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003608 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003609 if (ret != 0 && ret != -ENOENT) {
3610 btrfs_abort_transaction(trans, root, ret);
3611 goto err;
3612 }
Chris Masone02119d2008-09-05 16:13:11 -04003613
3614 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3615 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003616 if (ret == -ENOENT)
3617 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003618 else if (ret)
3619 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003620err:
3621 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003622 if (ret)
3623 goto out;
3624
3625 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003626 inode_inc_iversion(inode);
3627 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003628 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003629 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003630out:
Chris Mason39279cc2007-06-12 06:35:45 -04003631 return ret;
3632}
3633
Al Viro92986792011-03-04 17:14:37 +00003634int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3635 struct btrfs_root *root,
3636 struct inode *dir, struct inode *inode,
3637 const char *name, int name_len)
3638{
3639 int ret;
3640 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3641 if (!ret) {
3642 btrfs_drop_nlink(inode);
3643 ret = btrfs_update_inode(trans, root, inode);
3644 }
3645 return ret;
3646}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003647
3648/*
3649 * helper to start transaction for unlink and rmdir.
3650 *
Josef Bacikd52be812013-05-29 14:54:47 -04003651 * unlink and rmdir are special in btrfs, they do not always free space, so
3652 * if we cannot make our reservations the normal way try and see if there is
3653 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3654 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003655 */
Josef Bacikd52be812013-05-29 14:54:47 -04003656static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003657{
3658 struct btrfs_trans_handle *trans;
3659 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003660 int ret;
3661
Josef Bacike70bea52011-10-11 14:18:24 -04003662 /*
3663 * 1 for the possible orphan item
3664 * 1 for the dir item
3665 * 1 for the dir index
3666 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003667 * 1 for the inode
3668 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003669 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003670 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3671 return trans;
3672
Josef Bacikd52be812013-05-29 14:54:47 -04003673 if (PTR_ERR(trans) == -ENOSPC) {
3674 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003675
Josef Bacikd52be812013-05-29 14:54:47 -04003676 trans = btrfs_start_transaction(root, 0);
3677 if (IS_ERR(trans))
3678 return trans;
3679 ret = btrfs_cond_migrate_bytes(root->fs_info,
3680 &root->fs_info->trans_block_rsv,
3681 num_bytes, 5);
3682 if (ret) {
3683 btrfs_end_transaction(trans, root);
3684 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003685 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003686 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003687 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003688 }
Josef Bacikd52be812013-05-29 14:54:47 -04003689 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003690}
3691
Chris Mason39279cc2007-06-12 06:35:45 -04003692static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3693{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003694 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003695 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003696 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003697 int ret;
3698
Josef Bacikd52be812013-05-29 14:54:47 -04003699 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003700 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003701 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003702
Chris Mason12fcfd22009-03-24 10:24:20 -04003703 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3704
Chris Masone02119d2008-09-05 16:13:11 -04003705 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3706 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003707 if (ret)
3708 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003709
Yan, Zhenga22285a2010-05-16 10:48:46 -04003710 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003711 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003712 if (ret)
3713 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003714 }
Josef Bacik7b128762008-07-24 12:17:14 -04003715
Tsutomu Itohb5324022011-07-19 07:27:20 +00003716out:
Josef Bacikd52be812013-05-29 14:54:47 -04003717 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003718 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003719 return ret;
3720}
3721
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003722int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3723 struct btrfs_root *root,
3724 struct inode *dir, u64 objectid,
3725 const char *name, int name_len)
3726{
3727 struct btrfs_path *path;
3728 struct extent_buffer *leaf;
3729 struct btrfs_dir_item *di;
3730 struct btrfs_key key;
3731 u64 index;
3732 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003733 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003734
3735 path = btrfs_alloc_path();
3736 if (!path)
3737 return -ENOMEM;
3738
Li Zefan33345d012011-04-20 10:31:50 +08003739 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003740 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003741 if (IS_ERR_OR_NULL(di)) {
3742 if (!di)
3743 ret = -ENOENT;
3744 else
3745 ret = PTR_ERR(di);
3746 goto out;
3747 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003748
3749 leaf = path->nodes[0];
3750 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3751 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3752 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003753 if (ret) {
3754 btrfs_abort_transaction(trans, root, ret);
3755 goto out;
3756 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003757 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003758
3759 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3760 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003761 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003762 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003763 if (ret != -ENOENT) {
3764 btrfs_abort_transaction(trans, root, ret);
3765 goto out;
3766 }
Li Zefan33345d012011-04-20 10:31:50 +08003767 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003768 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003769 if (IS_ERR_OR_NULL(di)) {
3770 if (!di)
3771 ret = -ENOENT;
3772 else
3773 ret = PTR_ERR(di);
3774 btrfs_abort_transaction(trans, root, ret);
3775 goto out;
3776 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003777
3778 leaf = path->nodes[0];
3779 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003780 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003781 index = key.offset;
3782 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003783 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003784
Miao Xie16cdcec2011-04-22 18:12:22 +08003785 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003786 if (ret) {
3787 btrfs_abort_transaction(trans, root, ret);
3788 goto out;
3789 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003790
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003791 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003792 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003793 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003794 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003795 if (ret)
3796 btrfs_abort_transaction(trans, root, ret);
3797out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003798 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003799 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003800}
3801
Chris Mason39279cc2007-06-12 06:35:45 -04003802static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3803{
3804 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003805 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003806 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003807 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003808
David Sterbab3ae2442012-09-13 16:04:34 -06003809 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003810 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003811 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3812 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003813
Josef Bacikd52be812013-05-29 14:54:47 -04003814 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003815 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003816 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003817
Li Zefan33345d012011-04-20 10:31:50 +08003818 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003819 err = btrfs_unlink_subvol(trans, root, dir,
3820 BTRFS_I(inode)->location.objectid,
3821 dentry->d_name.name,
3822 dentry->d_name.len);
3823 goto out;
3824 }
3825
Josef Bacik7b128762008-07-24 12:17:14 -04003826 err = btrfs_orphan_add(trans, inode);
3827 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003828 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003829
Chris Mason39279cc2007-06-12 06:35:45 -04003830 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003831 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3832 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003833 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003834 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003835out:
Josef Bacikd52be812013-05-29 14:54:47 -04003836 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003837 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003838
Chris Mason39279cc2007-06-12 06:35:45 -04003839 return err;
3840}
3841
Chris Mason323ac952008-10-01 19:05:46 -04003842/*
Chris Mason39279cc2007-06-12 06:35:45 -04003843 * this can truncate away extent items, csum items and directory items.
3844 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003845 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003846 *
3847 * csum items that cross the new i_size are truncated to the new size
3848 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003849 *
3850 * min_type is the minimum key type to truncate down to. If set to 0, this
3851 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003852 */
Yan, Zheng80825102009-11-12 09:35:36 +00003853int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3854 struct btrfs_root *root,
3855 struct inode *inode,
3856 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003857{
Chris Mason39279cc2007-06-12 06:35:45 -04003858 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003859 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003860 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003861 struct btrfs_key key;
3862 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003863 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003864 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003865 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003866 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04003867 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00003868 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003869 int found_extent;
3870 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003871 int pending_del_nr = 0;
3872 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003873 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003874 int ret;
3875 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003876 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003877
3878 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003879
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003880 path = btrfs_alloc_path();
3881 if (!path)
3882 return -ENOMEM;
3883 path->reada = -1;
3884
Josef Bacik5dc562c2012-08-17 13:14:17 -04003885 /*
3886 * We want to drop from the next block forward in case this new size is
3887 * not block aligned since we will be keeping the last block of the
3888 * extent just the way it is.
3889 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003890 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00003891 btrfs_drop_extent_cache(inode, ALIGN(new_size,
3892 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003893
Miao Xie16cdcec2011-04-22 18:12:22 +08003894 /*
3895 * This function is also used to drop the items in the log tree before
3896 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3897 * it is used to drop the loged items. So we shouldn't kill the delayed
3898 * items.
3899 */
3900 if (min_type == 0 && root == BTRFS_I(inode)->root)
3901 btrfs_kill_delayed_inode_items(inode);
3902
Li Zefan33345d012011-04-20 10:31:50 +08003903 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003904 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003905 key.type = (u8)-1;
3906
Chris Mason85e21ba2008-01-29 15:11:36 -05003907search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003908 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003909 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003910 if (ret < 0) {
3911 err = ret;
3912 goto out;
3913 }
Chris Masond3977122009-01-05 21:25:51 -05003914
Chris Mason85e21ba2008-01-29 15:11:36 -05003915 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003916 /* there are no items in the tree for us to truncate, we're
3917 * done
3918 */
Yan, Zheng80825102009-11-12 09:35:36 +00003919 if (path->slots[0] == 0)
3920 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003921 path->slots[0]--;
3922 }
3923
Chris Masond3977122009-01-05 21:25:51 -05003924 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003925 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003926 leaf = path->nodes[0];
3927 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3928 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003929
Li Zefan33345d012011-04-20 10:31:50 +08003930 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003931 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003932
Chris Mason85e21ba2008-01-29 15:11:36 -05003933 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003934 break;
3935
Chris Mason5f39d392007-10-15 16:14:19 -04003936 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003937 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003938 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003939 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003940 extent_type = btrfs_file_extent_type(leaf, fi);
3941 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003942 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003943 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003944 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003945 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003946 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003947 }
Yan008630c2007-11-07 13:31:09 -05003948 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003949 }
Yan, Zheng80825102009-11-12 09:35:36 +00003950 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003951 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003952 } else {
3953 if (item_end < new_size)
3954 break;
3955 if (found_key.offset >= new_size)
3956 del_item = 1;
3957 else
3958 del_item = 0;
3959 }
Chris Mason39279cc2007-06-12 06:35:45 -04003960 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003961 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003962 if (found_type != BTRFS_EXTENT_DATA_KEY)
3963 goto delete;
3964
Josef Bacik7f4f6e02013-08-29 16:43:28 -04003965 if (del_item)
3966 last_size = found_key.offset;
3967 else
3968 last_size = new_size;
3969
Chris Mason179e29e2007-11-01 11:28:41 -04003970 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003971 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003972 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003973 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003974 u64 orig_num_bytes =
3975 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00003976 extent_num_bytes = ALIGN(new_size -
3977 found_key.offset,
3978 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04003979 btrfs_set_file_extent_num_bytes(leaf, fi,
3980 extent_num_bytes);
3981 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003982 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003983 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003984 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003985 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003986 } else {
Chris Masondb945352007-10-15 16:15:53 -04003987 extent_num_bytes =
3988 btrfs_file_extent_disk_num_bytes(leaf,
3989 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003990 extent_offset = found_key.offset -
3991 btrfs_file_extent_offset(leaf, fi);
3992
Chris Mason39279cc2007-06-12 06:35:45 -04003993 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003994 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003995 if (extent_start != 0) {
3996 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003997 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003998 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003999 }
4000 }
Chris Mason90692182008-02-08 13:49:28 -05004001 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004002 /*
4003 * we can't truncate inline items that have had
4004 * special encodings
4005 */
4006 if (!del_item &&
4007 btrfs_file_extent_compression(leaf, fi) == 0 &&
4008 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4009 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004010 u32 size = new_size - found_key.offset;
4011
4012 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004013 inode_sub_bytes(inode, item_end + 1 -
4014 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004015 }
4016 size =
4017 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004018 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004019 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004020 inode_sub_bytes(inode, item_end + 1 -
4021 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004022 }
Chris Mason39279cc2007-06-12 06:35:45 -04004023 }
Chris Mason179e29e2007-11-01 11:28:41 -04004024delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004025 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004026 if (!pending_del_nr) {
4027 /* no pending yet, add ourselves */
4028 pending_del_slot = path->slots[0];
4029 pending_del_nr = 1;
4030 } else if (pending_del_nr &&
4031 path->slots[0] + 1 == pending_del_slot) {
4032 /* hop on the pending chunk */
4033 pending_del_nr++;
4034 pending_del_slot = path->slots[0];
4035 } else {
Chris Masond3977122009-01-05 21:25:51 -05004036 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004037 }
Chris Mason39279cc2007-06-12 06:35:45 -04004038 } else {
4039 break;
4040 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004041 if (found_extent && (root->ref_cows ||
4042 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004043 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004044 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004045 extent_num_bytes, 0,
4046 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004047 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004048 BUG_ON(ret);
4049 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004050
Yan, Zheng80825102009-11-12 09:35:36 +00004051 if (found_type == BTRFS_INODE_ITEM_KEY)
4052 break;
4053
4054 if (path->slots[0] == 0 ||
4055 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004056 if (pending_del_nr) {
4057 ret = btrfs_del_items(trans, root, path,
4058 pending_del_slot,
4059 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004060 if (ret) {
4061 btrfs_abort_transaction(trans,
4062 root, ret);
4063 goto error;
4064 }
Yan, Zheng80825102009-11-12 09:35:36 +00004065 pending_del_nr = 0;
4066 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004067 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004068 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004069 } else {
4070 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004071 }
Chris Mason39279cc2007-06-12 06:35:45 -04004072 }
Yan, Zheng80825102009-11-12 09:35:36 +00004073out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004074 if (pending_del_nr) {
4075 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4076 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004077 if (ret)
4078 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004079 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004080error:
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004081 if (last_size != (u64)-1)
4082 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004083 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004084 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004085}
4086
Chris Masona52d9a82007-08-27 16:49:44 -04004087/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004088 * btrfs_truncate_page - read, zero a chunk and write a page
4089 * @inode - inode that we're zeroing
4090 * @from - the offset to start zeroing
4091 * @len - the length to zero, 0 to zero the entire range respective to the
4092 * offset
4093 * @front - zero up to the offset instead of from the offset on
4094 *
4095 * This will find the page for the "from" offset and cow the page and zero the
4096 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004097 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004098int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4099 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004100{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004101 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004102 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004103 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4104 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004105 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004106 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004107 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004108 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4109 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4110 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004111 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004112 int ret = 0;
4113 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004114 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004115
Josef Bacik2aaa6652012-08-29 14:27:18 -04004116 if ((offset & (blocksize - 1)) == 0 &&
4117 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004118 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004119 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004120 if (ret)
4121 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004122
Chris Mason211c17f2008-05-15 09:13:45 -04004123again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004124 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004125 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004126 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004127 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004128 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004129 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004130
4131 page_start = page_offset(page);
4132 page_end = page_start + PAGE_CACHE_SIZE - 1;
4133
Chris Masona52d9a82007-08-27 16:49:44 -04004134 if (!PageUptodate(page)) {
4135 ret = btrfs_readpage(NULL, page);
4136 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004137 if (page->mapping != mapping) {
4138 unlock_page(page);
4139 page_cache_release(page);
4140 goto again;
4141 }
Chris Masona52d9a82007-08-27 16:49:44 -04004142 if (!PageUptodate(page)) {
4143 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004144 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004145 }
4146 }
Chris Mason211c17f2008-05-15 09:13:45 -04004147 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004148
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004149 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004150 set_page_extent_mapped(page);
4151
4152 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4153 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004154 unlock_extent_cached(io_tree, page_start, page_end,
4155 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004156 unlock_page(page);
4157 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004158 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004159 btrfs_put_ordered_extent(ordered);
4160 goto again;
4161 }
4162
Josef Bacik2ac55d42010-02-03 19:33:23 +00004163 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004164 EXTENT_DIRTY | EXTENT_DELALLOC |
4165 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004166 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004167
Josef Bacik2ac55d42010-02-03 19:33:23 +00004168 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4169 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004170 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004171 unlock_extent_cached(io_tree, page_start, page_end,
4172 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004173 goto out_unlock;
4174 }
4175
Chris Masone6dcd2d2008-07-17 12:53:50 -04004176 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004177 if (!len)
4178 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004179 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004180 if (front)
4181 memset(kaddr, 0, offset);
4182 else
4183 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004184 flush_dcache_page(page);
4185 kunmap(page);
4186 }
Chris Mason247e7432008-07-17 12:53:51 -04004187 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004188 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004189 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4190 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004191
Chris Mason89642222008-07-24 09:41:53 -04004192out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004193 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004194 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004195 unlock_page(page);
4196 page_cache_release(page);
4197out:
4198 return ret;
4199}
4200
Josef Bacik695a0d02011-03-04 15:46:53 -05004201/*
4202 * This function puts in dummy file extents for the area we're creating a hole
4203 * for. So if we are truncating this file to a larger size we need to insert
4204 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4205 * the range between oldsize and size
4206 */
Josef Bacika41ad392011-01-31 15:30:16 -05004207int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004208{
4209 struct btrfs_trans_handle *trans;
4210 struct btrfs_root *root = BTRFS_I(inode)->root;
4211 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004212 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004213 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004214 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004215 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4216 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004217 u64 last_byte;
4218 u64 cur_offset;
4219 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004220 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004221
Josef Bacika71754f2013-06-17 17:14:39 -04004222 /*
4223 * If our size started in the middle of a page we need to zero out the
4224 * rest of the page before we expand the i_size, otherwise we could
4225 * expose stale data.
4226 */
4227 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4228 if (err)
4229 return err;
4230
Yan Zheng9036c102008-10-30 14:19:41 -04004231 if (size <= hole_start)
4232 return 0;
4233
Yan Zheng9036c102008-10-30 14:19:41 -04004234 while (1) {
4235 struct btrfs_ordered_extent *ordered;
4236 btrfs_wait_ordered_range(inode, hole_start,
4237 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004238 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004239 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004240 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4241 if (!ordered)
4242 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004243 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4244 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004245 btrfs_put_ordered_extent(ordered);
4246 }
4247
Yan Zheng9036c102008-10-30 14:19:41 -04004248 cur_offset = hole_start;
4249 while (1) {
4250 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4251 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004252 if (IS_ERR(em)) {
4253 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004254 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004255 break;
4256 }
Yan Zheng9036c102008-10-30 14:19:41 -04004257 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004258 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004259 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004260 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004261 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004262
Miao Xie36423202011-12-14 20:12:02 -05004263 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004264 if (IS_ERR(trans)) {
4265 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004266 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004267 }
Yan, Zheng80825102009-11-12 09:35:36 +00004268
Josef Bacik5dc562c2012-08-17 13:14:17 -04004269 err = btrfs_drop_extents(trans, root, inode,
4270 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004271 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004272 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004273 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004274 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004275 break;
Miao Xie5b397372011-09-11 10:52:24 -04004276 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004277
Yan Zheng9036c102008-10-30 14:19:41 -04004278 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004279 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004280 0, hole_size, 0, hole_size,
4281 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004282 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004283 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004284 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004285 break;
Miao Xie5b397372011-09-11 10:52:24 -04004286 }
Yan, Zheng80825102009-11-12 09:35:36 +00004287
Josef Bacik5dc562c2012-08-17 13:14:17 -04004288 btrfs_drop_extent_cache(inode, cur_offset,
4289 cur_offset + hole_size - 1, 0);
4290 hole_em = alloc_extent_map();
4291 if (!hole_em) {
4292 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4293 &BTRFS_I(inode)->runtime_flags);
4294 goto next;
4295 }
4296 hole_em->start = cur_offset;
4297 hole_em->len = hole_size;
4298 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004299
Josef Bacik5dc562c2012-08-17 13:14:17 -04004300 hole_em->block_start = EXTENT_MAP_HOLE;
4301 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004302 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004303 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004304 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4305 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4306 hole_em->generation = trans->transid;
4307
4308 while (1) {
4309 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004310 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004311 write_unlock(&em_tree->lock);
4312 if (err != -EEXIST)
4313 break;
4314 btrfs_drop_extent_cache(inode, cur_offset,
4315 cur_offset +
4316 hole_size - 1, 0);
4317 }
4318 free_extent_map(hole_em);
4319next:
Miao Xie36423202011-12-14 20:12:02 -05004320 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004321 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004322 }
4323 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004324 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004325 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004326 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004327 break;
4328 }
4329
Yan, Zhenga22285a2010-05-16 10:48:46 -04004330 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004331 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4332 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004333 return err;
4334}
4335
Eric Sandeen3972f262013-01-12 02:57:22 +00004336static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004337{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004338 struct btrfs_root *root = BTRFS_I(inode)->root;
4339 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004340 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004341 loff_t newsize = attr->ia_size;
4342 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004343 int ret;
4344
Eric Sandeen3972f262013-01-12 02:57:22 +00004345 /*
4346 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4347 * special case where we need to update the times despite not having
4348 * these flags set. For all other operations the VFS set these flags
4349 * explicitly if it wants a timestamp update.
4350 */
4351 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4352 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4353
Josef Bacika41ad392011-01-31 15:30:16 -05004354 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004355 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004356 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004357 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004358 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004359
Miao Xief4a2f4c2011-12-14 20:12:01 -05004360 trans = btrfs_start_transaction(root, 1);
4361 if (IS_ERR(trans))
4362 return PTR_ERR(trans);
4363
4364 i_size_write(inode, newsize);
4365 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4366 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004367 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004368 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004369
Josef Bacika41ad392011-01-31 15:30:16 -05004370 /*
4371 * We're truncating a file that used to have good data down to
4372 * zero. Make sure it gets into the ordered flush list so that
4373 * any new writes get down to disk quickly.
4374 */
4375 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004376 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4377 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004378
Josef Bacikf3fe8202013-01-07 17:03:21 -05004379 /*
4380 * 1 for the orphan item we're going to add
4381 * 1 for the orphan item deletion.
4382 */
4383 trans = btrfs_start_transaction(root, 2);
4384 if (IS_ERR(trans))
4385 return PTR_ERR(trans);
4386
4387 /*
4388 * We need to do this in case we fail at _any_ point during the
4389 * actual truncate. Once we do the truncate_setsize we could
4390 * invalidate pages which forces any outstanding ordered io to
4391 * be instantly completed which will give us extents that need
4392 * to be truncated. If we fail to get an orphan inode down we
4393 * could have left over extents that were never meant to live,
4394 * so we need to garuntee from this point on that everything
4395 * will be consistent.
4396 */
4397 ret = btrfs_orphan_add(trans, inode);
4398 btrfs_end_transaction(trans, root);
4399 if (ret)
4400 return ret;
4401
Josef Bacika41ad392011-01-31 15:30:16 -05004402 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4403 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004404
4405 /* Disable nonlocked read DIO to avoid the end less truncate */
4406 btrfs_inode_block_unlocked_dio(inode);
4407 inode_dio_wait(inode);
4408 btrfs_inode_resume_unlocked_dio(inode);
4409
Josef Bacika41ad392011-01-31 15:30:16 -05004410 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004411 if (ret && inode->i_nlink) {
4412 int err;
4413
4414 /*
4415 * failed to truncate, disk_i_size is only adjusted down
4416 * as we remove extents, so it should represent the true
4417 * size of the inode, so reset the in memory size and
4418 * delete our orphan entry.
4419 */
4420 trans = btrfs_join_transaction(root);
4421 if (IS_ERR(trans)) {
4422 btrfs_orphan_del(NULL, inode);
4423 return ret;
4424 }
4425 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4426 err = btrfs_orphan_del(trans, inode);
4427 if (err)
4428 btrfs_abort_transaction(trans, root, err);
4429 btrfs_end_transaction(trans, root);
4430 }
Yan, Zheng80825102009-11-12 09:35:36 +00004431 }
4432
Josef Bacika41ad392011-01-31 15:30:16 -05004433 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004434}
4435
Chris Mason39279cc2007-06-12 06:35:45 -04004436static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4437{
4438 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004439 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004440 int err;
4441
Li Zefanb83cc962010-12-20 16:04:08 +08004442 if (btrfs_root_readonly(root))
4443 return -EROFS;
4444
Chris Mason39279cc2007-06-12 06:35:45 -04004445 err = inode_change_ok(inode, attr);
4446 if (err)
4447 return err;
4448
Chris Mason5a3f23d2009-03-31 13:27:11 -04004449 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004450 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004451 if (err)
4452 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004453 }
Yan Zheng9036c102008-10-30 14:19:41 -04004454
Christoph Hellwig10257742010-06-04 11:30:02 +02004455 if (attr->ia_valid) {
4456 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004457 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004458 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004459
Josef Bacik22c44fe2011-11-30 10:45:38 -05004460 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004461 err = btrfs_acl_chmod(inode);
4462 }
4463
Chris Mason39279cc2007-06-12 06:35:45 -04004464 return err;
4465}
Chris Mason61295eb2008-01-14 16:24:38 -05004466
Al Virobd555972010-06-07 11:35:40 -04004467void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004468{
4469 struct btrfs_trans_handle *trans;
4470 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004471 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004472 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004473 int ret;
4474
liubo1abe9b82011-03-24 11:18:59 +00004475 trace_btrfs_inode_evict(inode);
4476
Chris Mason39279cc2007-06-12 06:35:45 -04004477 truncate_inode_pages(&inode->i_data, 0);
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004478 if (inode->i_nlink &&
4479 ((btrfs_root_refs(&root->root_item) != 0 &&
4480 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4481 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004482 goto no_delete;
4483
Chris Mason39279cc2007-06-12 06:35:45 -04004484 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004485 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004486 goto no_delete;
4487 }
Al Virobd555972010-06-07 11:35:40 -04004488 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004489 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004490
Yan, Zhengc71bf092009-11-12 09:34:40 +00004491 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004492 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004493 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004494 goto no_delete;
4495 }
4496
Yan, Zheng76dda932009-09-21 16:00:26 -04004497 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004498 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4499 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004500 goto no_delete;
4501 }
4502
Miao Xie0e8c36a2012-12-19 06:59:51 +00004503 ret = btrfs_commit_inode_delayed_inode(inode);
4504 if (ret) {
4505 btrfs_orphan_del(NULL, inode);
4506 goto no_delete;
4507 }
4508
Miao Xie66d8f3d2012-09-06 04:02:28 -06004509 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004510 if (!rsv) {
4511 btrfs_orphan_del(NULL, inode);
4512 goto no_delete;
4513 }
Josef Bacik4a338542011-08-29 11:01:31 -04004514 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004515 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004516 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004517
Chris Masondbe674a2008-07-17 12:54:05 -04004518 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004519
Josef Bacik4289a662011-08-05 13:22:24 -04004520 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004521 * This is a bit simpler than btrfs_truncate since we've already
4522 * reserved our space for our orphan item in the unlink, so we just
4523 * need to reserve some slack space in case we add bytes and update
4524 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004525 */
Yan, Zheng80825102009-11-12 09:35:36 +00004526 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004527 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4528 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004529
Josef Bacik726c35f2011-09-26 15:46:06 -04004530 /*
4531 * Try and steal from the global reserve since we will
4532 * likely not use this space anyway, we want to try as
4533 * hard as possible to get this to work.
4534 */
4535 if (ret)
4536 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4537
Yan, Zhengd68fc572010-05-16 10:49:58 -04004538 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004539 btrfs_warn(root->fs_info,
4540 "Could not get space for a delete, will truncate on mount %d",
4541 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004542 btrfs_orphan_del(NULL, inode);
4543 btrfs_free_block_rsv(root, rsv);
4544 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004545 }
4546
Miao Xie0e8c36a2012-12-19 06:59:51 +00004547 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004548 if (IS_ERR(trans)) {
4549 btrfs_orphan_del(NULL, inode);
4550 btrfs_free_block_rsv(root, rsv);
4551 goto no_delete;
4552 }
4553
4554 trans->block_rsv = rsv;
4555
Yan, Zhengd68fc572010-05-16 10:49:58 -04004556 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004557 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004558 break;
4559
Miao Xie8407aa42012-09-07 01:43:32 -06004560 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004561 btrfs_end_transaction(trans, root);
4562 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004563 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004564 }
4565
Josef Bacik4289a662011-08-05 13:22:24 -04004566 btrfs_free_block_rsv(root, rsv);
4567
Josef Bacik4ef31a42013-08-13 14:10:08 -04004568 /*
4569 * Errors here aren't a big deal, it just means we leave orphan items
4570 * in the tree. They will be cleaned up on the next mount.
4571 */
Yan, Zheng80825102009-11-12 09:35:36 +00004572 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004573 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004574 btrfs_orphan_del(trans, inode);
4575 } else {
4576 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004577 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004578
Josef Bacik4289a662011-08-05 13:22:24 -04004579 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004580 if (!(root == root->fs_info->tree_root ||
4581 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004582 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004583
Chris Mason54aa1f42007-06-22 14:16:25 -04004584 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004585 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004586no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004587 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004588 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004589 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004590}
4591
4592/*
4593 * this returns the key found in the dir entry in the location pointer.
4594 * If no dir entries were found, location->objectid is 0.
4595 */
4596static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4597 struct btrfs_key *location)
4598{
4599 const char *name = dentry->d_name.name;
4600 int namelen = dentry->d_name.len;
4601 struct btrfs_dir_item *di;
4602 struct btrfs_path *path;
4603 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004604 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004605
4606 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004607 if (!path)
4608 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004609
Li Zefan33345d012011-04-20 10:31:50 +08004610 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004611 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004612 if (IS_ERR(di))
4613 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004614
David Sterbac7040052011-04-19 18:00:01 +02004615 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004616 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004617
Chris Mason5f39d392007-10-15 16:14:19 -04004618 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004619out:
Chris Mason39279cc2007-06-12 06:35:45 -04004620 btrfs_free_path(path);
4621 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004622out_err:
4623 location->objectid = 0;
4624 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004625}
4626
4627/*
4628 * when we hit a tree root in a directory, the btrfs part of the inode
4629 * needs to be changed to reflect the root directory of the tree root. This
4630 * is kind of like crossing a mount point.
4631 */
4632static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004633 struct inode *dir,
4634 struct dentry *dentry,
4635 struct btrfs_key *location,
4636 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004637{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004638 struct btrfs_path *path;
4639 struct btrfs_root *new_root;
4640 struct btrfs_root_ref *ref;
4641 struct extent_buffer *leaf;
4642 int ret;
4643 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004644
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004645 path = btrfs_alloc_path();
4646 if (!path) {
4647 err = -ENOMEM;
4648 goto out;
4649 }
Chris Mason39279cc2007-06-12 06:35:45 -04004650
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004651 err = -ENOENT;
4652 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4653 BTRFS_I(dir)->root->root_key.objectid,
4654 location->objectid);
4655 if (ret) {
4656 if (ret < 0)
4657 err = ret;
4658 goto out;
4659 }
Chris Mason39279cc2007-06-12 06:35:45 -04004660
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004661 leaf = path->nodes[0];
4662 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004663 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004664 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4665 goto out;
4666
4667 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4668 (unsigned long)(ref + 1),
4669 dentry->d_name.len);
4670 if (ret)
4671 goto out;
4672
David Sterbab3b4aa72011-04-21 01:20:15 +02004673 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004674
4675 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4676 if (IS_ERR(new_root)) {
4677 err = PTR_ERR(new_root);
4678 goto out;
4679 }
4680
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004681 *sub_root = new_root;
4682 location->objectid = btrfs_root_dirid(&new_root->root_item);
4683 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004684 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004685 err = 0;
4686out:
4687 btrfs_free_path(path);
4688 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004689}
4690
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004691static void inode_tree_add(struct inode *inode)
4692{
4693 struct btrfs_root *root = BTRFS_I(inode)->root;
4694 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004695 struct rb_node **p;
4696 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004697 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08004698 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004699
Al Viro1d3382c2010-10-23 15:19:20 -04004700 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004701 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004702 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004703 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004704 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004705 while (*p) {
4706 parent = *p;
4707 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4708
Li Zefan33345d012011-04-20 10:31:50 +08004709 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004710 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004711 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004712 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004713 else {
4714 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004715 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004716 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004717 RB_CLEAR_NODE(parent);
4718 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004719 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004720 }
4721 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004722 rb_link_node(new, parent, p);
4723 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004724 spin_unlock(&root->inode_lock);
4725}
4726
4727static void inode_tree_del(struct inode *inode)
4728{
4729 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004730 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004731
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004732 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004733 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004734 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004735 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004736 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004737 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004738 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004739
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004740 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004741 synchronize_srcu(&root->fs_info->subvol_srcu);
4742 spin_lock(&root->inode_lock);
4743 empty = RB_EMPTY_ROOT(&root->inode_tree);
4744 spin_unlock(&root->inode_lock);
4745 if (empty)
4746 btrfs_add_dead_root(root);
4747 }
4748}
4749
Jeff Mahoney143bede2012-03-01 14:56:26 +01004750void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004751{
4752 struct rb_node *node;
4753 struct rb_node *prev;
4754 struct btrfs_inode *entry;
4755 struct inode *inode;
4756 u64 objectid = 0;
4757
4758 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4759
4760 spin_lock(&root->inode_lock);
4761again:
4762 node = root->inode_tree.rb_node;
4763 prev = NULL;
4764 while (node) {
4765 prev = node;
4766 entry = rb_entry(node, struct btrfs_inode, rb_node);
4767
Li Zefan33345d012011-04-20 10:31:50 +08004768 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004769 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004770 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004771 node = node->rb_right;
4772 else
4773 break;
4774 }
4775 if (!node) {
4776 while (prev) {
4777 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004778 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004779 node = prev;
4780 break;
4781 }
4782 prev = rb_next(prev);
4783 }
4784 }
4785 while (node) {
4786 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004787 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004788 inode = igrab(&entry->vfs_inode);
4789 if (inode) {
4790 spin_unlock(&root->inode_lock);
4791 if (atomic_read(&inode->i_count) > 1)
4792 d_prune_aliases(inode);
4793 /*
Al Viro45321ac2010-06-07 13:43:19 -04004794 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004795 * the inode cache when its usage count
4796 * hits zero.
4797 */
4798 iput(inode);
4799 cond_resched();
4800 spin_lock(&root->inode_lock);
4801 goto again;
4802 }
4803
4804 if (cond_resched_lock(&root->inode_lock))
4805 goto again;
4806
4807 node = rb_next(node);
4808 }
4809 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004810}
4811
Chris Masone02119d2008-09-05 16:13:11 -04004812static int btrfs_init_locked_inode(struct inode *inode, void *p)
4813{
4814 struct btrfs_iget_args *args = p;
4815 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004816 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004817 return 0;
4818}
4819
4820static int btrfs_find_actor(struct inode *inode, void *opaque)
4821{
4822 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004823 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004824 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004825}
4826
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004827static struct inode *btrfs_iget_locked(struct super_block *s,
4828 u64 objectid,
4829 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004830{
4831 struct inode *inode;
4832 struct btrfs_iget_args args;
4833 args.ino = objectid;
4834 args.root = root;
4835
4836 inode = iget5_locked(s, objectid, btrfs_find_actor,
4837 btrfs_init_locked_inode,
4838 (void *)&args);
4839 return inode;
4840}
4841
Balaji Rao1a54ef82008-07-21 02:01:04 +05304842/* Get an inode object given its location and corresponding root.
4843 * Returns in *is_new if the inode was read from disk
4844 */
4845struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004846 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304847{
4848 struct inode *inode;
4849
4850 inode = btrfs_iget_locked(s, location->objectid, root);
4851 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004852 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304853
4854 if (inode->i_state & I_NEW) {
4855 BTRFS_I(inode)->root = root;
4856 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4857 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004858 if (!is_bad_inode(inode)) {
4859 inode_tree_add(inode);
4860 unlock_new_inode(inode);
4861 if (new)
4862 *new = 1;
4863 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004864 unlock_new_inode(inode);
4865 iput(inode);
4866 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004867 }
4868 }
4869
Balaji Rao1a54ef82008-07-21 02:01:04 +05304870 return inode;
4871}
4872
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004873static struct inode *new_simple_dir(struct super_block *s,
4874 struct btrfs_key *key,
4875 struct btrfs_root *root)
4876{
4877 struct inode *inode = new_inode(s);
4878
4879 if (!inode)
4880 return ERR_PTR(-ENOMEM);
4881
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004882 BTRFS_I(inode)->root = root;
4883 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004884 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004885
4886 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004887 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004888 inode->i_fop = &simple_dir_operations;
4889 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4890 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4891
4892 return inode;
4893}
4894
Chris Mason3de45862008-11-17 21:02:50 -05004895struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004896{
Chris Masond3977122009-01-05 21:25:51 -05004897 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004898 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004899 struct btrfs_root *sub_root = root;
4900 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004901 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004902 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004903
4904 if (dentry->d_name.len > BTRFS_NAME_LEN)
4905 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004906
Jeff Layton39e3c952012-11-28 11:30:53 -05004907 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04004908 if (ret < 0)
4909 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004910
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004911 if (location.objectid == 0)
4912 return NULL;
4913
4914 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004915 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004916 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004917 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004918
4919 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4920
Yan, Zheng76dda932009-09-21 16:00:26 -04004921 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004922 ret = fixup_tree_root_location(root, dir, dentry,
4923 &location, &sub_root);
4924 if (ret < 0) {
4925 if (ret != -ENOENT)
4926 inode = ERR_PTR(ret);
4927 else
4928 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4929 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004930 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004931 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004932 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4933
Julia Lawall34d19ba2011-01-24 19:55:19 +00004934 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004935 down_read(&root->fs_info->cleanup_work_sem);
4936 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004937 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004938 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04004939 if (ret) {
4940 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004941 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04004942 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00004943 }
4944
Chris Mason3de45862008-11-17 21:02:50 -05004945 return inode;
4946}
4947
Nick Pigginfe15ce42011-01-07 17:49:23 +11004948static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004949{
4950 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004951 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004952
Li Zefan848cce02012-02-21 17:04:28 +08004953 if (!inode && !IS_ROOT(dentry))
4954 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004955
Li Zefan848cce02012-02-21 17:04:28 +08004956 if (inode) {
4957 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004958 if (btrfs_root_refs(&root->root_item) == 0)
4959 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004960
4961 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4962 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004963 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004964 return 0;
4965}
4966
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004967static void btrfs_dentry_release(struct dentry *dentry)
4968{
4969 if (dentry->d_fsdata)
4970 kfree(dentry->d_fsdata);
4971}
4972
Chris Mason3de45862008-11-17 21:02:50 -05004973static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004974 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004975{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004976 struct dentry *ret;
4977
4978 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04004979 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004980}
4981
Miao Xie16cdcec2011-04-22 18:12:22 +08004982unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004983 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4984};
4985
Al Viro9cdda8d2013-05-22 16:48:09 -04004986static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04004987{
Al Viro9cdda8d2013-05-22 16:48:09 -04004988 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04004989 struct btrfs_root *root = BTRFS_I(inode)->root;
4990 struct btrfs_item *item;
4991 struct btrfs_dir_item *di;
4992 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004993 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004994 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004995 struct list_head ins_list;
4996 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004997 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004998 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004999 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005000 unsigned char d_type;
5001 int over = 0;
5002 u32 di_cur;
5003 u32 di_total;
5004 u32 di_len;
5005 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005006 char tmp_name[32];
5007 char *name_ptr;
5008 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005009 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005010
5011 /* FIXME, use a real flag for deciding about the key type */
5012 if (root->fs_info->tree_root == root)
5013 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005014
Al Viro9cdda8d2013-05-22 16:48:09 -04005015 if (!dir_emit_dots(file, ctx))
5016 return 0;
5017
David Woodhouse49593bf2008-08-17 17:08:36 +01005018 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005019 if (!path)
5020 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005021
Josef Bacik026fd312011-05-13 10:32:11 -04005022 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005023
Miao Xie16cdcec2011-04-22 18:12:22 +08005024 if (key_type == BTRFS_DIR_INDEX_KEY) {
5025 INIT_LIST_HEAD(&ins_list);
5026 INIT_LIST_HEAD(&del_list);
5027 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5028 }
5029
Chris Mason39279cc2007-06-12 06:35:45 -04005030 btrfs_set_key_type(&key, key_type);
Al Viro9cdda8d2013-05-22 16:48:09 -04005031 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005032 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005033
Chris Mason39279cc2007-06-12 06:35:45 -04005034 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5035 if (ret < 0)
5036 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005037
5038 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005039 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005040 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005041 if (slot >= btrfs_header_nritems(leaf)) {
5042 ret = btrfs_next_leaf(root, path);
5043 if (ret < 0)
5044 goto err;
5045 else if (ret > 0)
5046 break;
5047 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005048 }
Chris Mason3de45862008-11-17 21:02:50 -05005049
Ross Kirkdd3cc162013-09-16 15:58:09 +01005050 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005051 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5052
5053 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005054 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005055 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005056 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005057 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005058 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005059 if (key_type == BTRFS_DIR_INDEX_KEY &&
5060 btrfs_should_delete_dir_index(&del_list,
5061 found_key.offset))
5062 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005063
Al Viro9cdda8d2013-05-22 16:48:09 -04005064 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005065 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005066
Chris Mason39279cc2007-06-12 06:35:45 -04005067 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5068 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005069 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005070
5071 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005072 struct btrfs_key location;
5073
Josef Bacik22a94d42011-03-16 16:47:17 -04005074 if (verify_dir_item(root, leaf, di))
5075 break;
5076
Chris Mason5f39d392007-10-15 16:14:19 -04005077 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005078 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005079 name_ptr = tmp_name;
5080 } else {
5081 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005082 if (!name_ptr) {
5083 ret = -ENOMEM;
5084 goto err;
5085 }
Chris Mason5f39d392007-10-15 16:14:19 -04005086 }
5087 read_extent_buffer(leaf, name_ptr,
5088 (unsigned long)(di + 1), name_len);
5089
5090 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5091 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005092
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005093
Chris Mason3de45862008-11-17 21:02:50 -05005094 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005095 * skip it.
5096 *
5097 * In contrast to old kernels, we insert the snapshot's
5098 * dir item and dir index after it has been created, so
5099 * we won't find a reference to our own snapshot. We
5100 * still keep the following code for backward
5101 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005102 */
5103 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5104 location.objectid == root->root_key.objectid) {
5105 over = 0;
5106 goto skip;
5107 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005108 over = !dir_emit(ctx, name_ptr, name_len,
5109 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005110
Chris Mason3de45862008-11-17 21:02:50 -05005111skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005112 if (name_ptr != tmp_name)
5113 kfree(name_ptr);
5114
Chris Mason39279cc2007-06-12 06:35:45 -04005115 if (over)
5116 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005117 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005118 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005119 di_cur += di_len;
5120 di = (struct btrfs_dir_item *)((char *)di + di_len);
5121 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005122next:
5123 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005124 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005125
Miao Xie16cdcec2011-04-22 18:12:22 +08005126 if (key_type == BTRFS_DIR_INDEX_KEY) {
5127 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005128 ctx->pos++;
5129 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005130 if (ret)
5131 goto nopos;
5132 }
5133
David Woodhouse49593bf2008-08-17 17:08:36 +01005134 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005135 ctx->pos++;
5136
5137 /*
5138 * Stop new entries from being returned after we return the last
5139 * entry.
5140 *
5141 * New directory entries are assigned a strictly increasing
5142 * offset. This means that new entries created during readdir
5143 * are *guaranteed* to be seen in the future by that readdir.
5144 * This has broken buggy programs which operate on names as
5145 * they're returned by readdir. Until we re-use freed offsets
5146 * we have this hack to stop new entries from being returned
5147 * under the assumption that they'll never reach this huge
5148 * offset.
5149 *
5150 * This is being careful not to overflow 32bit loff_t unless the
5151 * last entry requires it because doing so has broken 32bit apps
5152 * in the past.
5153 */
5154 if (key_type == BTRFS_DIR_INDEX_KEY) {
5155 if (ctx->pos >= INT_MAX)
5156 ctx->pos = LLONG_MAX;
5157 else
5158 ctx->pos = INT_MAX;
5159 }
Chris Mason39279cc2007-06-12 06:35:45 -04005160nopos:
5161 ret = 0;
5162err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005163 if (key_type == BTRFS_DIR_INDEX_KEY)
5164 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005165 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005166 return ret;
5167}
5168
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005169int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005170{
5171 struct btrfs_root *root = BTRFS_I(inode)->root;
5172 struct btrfs_trans_handle *trans;
5173 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005174 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005175
Josef Bacik72ac3c02012-05-23 14:13:11 -04005176 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005177 return 0;
5178
Liu Bo83eea1f2012-07-10 05:28:39 -06005179 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005180 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005181
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005182 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005183 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005184 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005185 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005186 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005187 if (IS_ERR(trans))
5188 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005189 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005190 }
5191 return ret;
5192}
5193
5194/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005195 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005196 * inode changes. But, it is most likely to find the inode in cache.
5197 * FIXME, needs more benchmarking...there are no reasons other than performance
5198 * to keep or drop this code.
5199 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005200static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005201{
5202 struct btrfs_root *root = BTRFS_I(inode)->root;
5203 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005204 int ret;
5205
Josef Bacik72ac3c02012-05-23 14:13:11 -04005206 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005207 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005208
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005209 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005210 if (IS_ERR(trans))
5211 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005212
5213 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005214 if (ret && ret == -ENOSPC) {
5215 /* whoops, lets try again with the full transaction */
5216 btrfs_end_transaction(trans, root);
5217 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005218 if (IS_ERR(trans))
5219 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005220
Chris Mason94b60442010-05-26 11:02:00 -04005221 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005222 }
Chris Mason39279cc2007-06-12 06:35:45 -04005223 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005224 if (BTRFS_I(inode)->delayed_node)
5225 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005226
5227 return ret;
5228}
5229
5230/*
5231 * This is a copy of file_update_time. We need this so we can return error on
5232 * ENOSPC for updating the inode in the case of file write and mmap writes.
5233 */
Josef Bacike41f9412012-03-26 09:46:47 -04005234static int btrfs_update_time(struct inode *inode, struct timespec *now,
5235 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005236{
Alexander Block2bc5565282012-06-15 09:49:33 +02005237 struct btrfs_root *root = BTRFS_I(inode)->root;
5238
5239 if (btrfs_root_readonly(root))
5240 return -EROFS;
5241
Josef Bacike41f9412012-03-26 09:46:47 -04005242 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005243 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005244 if (flags & S_CTIME)
5245 inode->i_ctime = *now;
5246 if (flags & S_MTIME)
5247 inode->i_mtime = *now;
5248 if (flags & S_ATIME)
5249 inode->i_atime = *now;
5250 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005251}
5252
Chris Masond352ac62008-09-29 15:18:18 -04005253/*
5254 * find the highest existing sequence number in a directory
5255 * and then set the in-memory index_cnt variable to reflect
5256 * free sequence numbers
5257 */
Josef Bacikaec74772008-07-24 12:12:38 -04005258static int btrfs_set_inode_index_count(struct inode *inode)
5259{
5260 struct btrfs_root *root = BTRFS_I(inode)->root;
5261 struct btrfs_key key, found_key;
5262 struct btrfs_path *path;
5263 struct extent_buffer *leaf;
5264 int ret;
5265
Li Zefan33345d012011-04-20 10:31:50 +08005266 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005267 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5268 key.offset = (u64)-1;
5269
5270 path = btrfs_alloc_path();
5271 if (!path)
5272 return -ENOMEM;
5273
5274 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5275 if (ret < 0)
5276 goto out;
5277 /* FIXME: we should be able to handle this */
5278 if (ret == 0)
5279 goto out;
5280 ret = 0;
5281
5282 /*
5283 * MAGIC NUMBER EXPLANATION:
5284 * since we search a directory based on f_pos we have to start at 2
5285 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5286 * else has to start at 2
5287 */
5288 if (path->slots[0] == 0) {
5289 BTRFS_I(inode)->index_cnt = 2;
5290 goto out;
5291 }
5292
5293 path->slots[0]--;
5294
5295 leaf = path->nodes[0];
5296 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5297
Li Zefan33345d012011-04-20 10:31:50 +08005298 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005299 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5300 BTRFS_I(inode)->index_cnt = 2;
5301 goto out;
5302 }
5303
5304 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5305out:
5306 btrfs_free_path(path);
5307 return ret;
5308}
5309
Chris Masond352ac62008-09-29 15:18:18 -04005310/*
5311 * helper to find a free sequence number in a given directory. This current
5312 * code is very simple, later versions will do smarter things in the btree
5313 */
Chris Mason3de45862008-11-17 21:02:50 -05005314int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005315{
5316 int ret = 0;
5317
5318 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005319 ret = btrfs_inode_delayed_dir_index_count(dir);
5320 if (ret) {
5321 ret = btrfs_set_inode_index_count(dir);
5322 if (ret)
5323 return ret;
5324 }
Josef Bacikaec74772008-07-24 12:12:38 -04005325 }
5326
Chris Mason00e4e6b2008-08-05 11:18:09 -04005327 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005328 BTRFS_I(dir)->index_cnt++;
5329
5330 return ret;
5331}
5332
Chris Mason39279cc2007-06-12 06:35:45 -04005333static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5334 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005335 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005336 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005337 u64 ref_objectid, u64 objectid,
5338 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005339{
5340 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005341 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005342 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005343 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005344 struct btrfs_inode_ref *ref;
5345 struct btrfs_key key[2];
5346 u32 sizes[2];
5347 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005348 int ret;
5349 int owner;
5350
Chris Mason5f39d392007-10-15 16:14:19 -04005351 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005352 if (!path)
5353 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005354
Chris Mason39279cc2007-06-12 06:35:45 -04005355 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005356 if (!inode) {
5357 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005358 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005359 }
Chris Mason39279cc2007-06-12 06:35:45 -04005360
Li Zefan581bb052011-04-20 10:06:11 +08005361 /*
5362 * we have to initialize this early, so we can reclaim the inode
5363 * number if we fail afterwards in this function.
5364 */
5365 inode->i_ino = objectid;
5366
Josef Bacikaec74772008-07-24 12:12:38 -04005367 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005368 trace_btrfs_inode_request(dir);
5369
Chris Mason3de45862008-11-17 21:02:50 -05005370 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005371 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005372 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005373 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005374 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005375 }
Josef Bacikaec74772008-07-24 12:12:38 -04005376 }
5377 /*
5378 * index_cnt is ignored for everything but a dir,
5379 * btrfs_get_inode_index_count has an explanation for the magic
5380 * number
5381 */
5382 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005383 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005384 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005385 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005386
Josef Bacik5dc562c2012-08-17 13:14:17 -04005387 /*
5388 * We could have gotten an inode number from somebody who was fsynced
5389 * and then removed in this same transaction, so let's just set full
5390 * sync since it will be a full sync anyway and this will blow away the
5391 * old info in the log.
5392 */
5393 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5394
Al Viro569254b2011-07-24 17:08:40 -04005395 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005396 owner = 0;
5397 else
5398 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005399
5400 key[0].objectid = objectid;
5401 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5402 key[0].offset = 0;
5403
Mark Fashehf1863732012-08-08 11:32:27 -07005404 /*
5405 * Start new inodes with an inode_ref. This is slightly more
5406 * efficient for small numbers of hard links since they will
5407 * be packed into one item. Extended refs will kick in if we
5408 * add more hard links than can fit in the ref item.
5409 */
Chris Mason9c583092008-01-29 15:15:18 -05005410 key[1].objectid = objectid;
5411 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5412 key[1].offset = ref_objectid;
5413
5414 sizes[0] = sizeof(struct btrfs_inode_item);
5415 sizes[1] = name_len + sizeof(*ref);
5416
Chris Masonb9473432009-03-13 11:00:37 -04005417 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005418 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5419 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005420 goto fail;
5421
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005422 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005423 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005424 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005425 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5426 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005427 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5428 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005429 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005430
5431 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5432 struct btrfs_inode_ref);
5433 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005434 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005435 ptr = (unsigned long)(ref + 1);
5436 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5437
Chris Mason5f39d392007-10-15 16:14:19 -04005438 btrfs_mark_buffer_dirty(path->nodes[0]);
5439 btrfs_free_path(path);
5440
Chris Mason39279cc2007-06-12 06:35:45 -04005441 location = &BTRFS_I(inode)->location;
5442 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005443 location->offset = 0;
5444 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5445
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005446 btrfs_inherit_iflags(inode, dir);
5447
Al Viro569254b2011-07-24 17:08:40 -04005448 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005449 if (btrfs_test_opt(root, NODATASUM))
5450 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005451 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005452 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5453 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005454 }
5455
Chris Mason39279cc2007-06-12 06:35:45 -04005456 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005457 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005458
5459 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005460 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005461
Alexander Block8ea05e32012-07-25 17:35:53 +02005462 btrfs_update_root_times(trans, root);
5463
Chris Mason39279cc2007-06-12 06:35:45 -04005464 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005465fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005466 if (dir)
5467 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005468 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005469 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005470 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005471}
5472
5473static inline u8 btrfs_inode_type(struct inode *inode)
5474{
5475 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5476}
5477
Chris Masond352ac62008-09-29 15:18:18 -04005478/*
5479 * utility function to add 'inode' into 'parent_inode' with
5480 * a give name and a given sequence number.
5481 * if 'add_backref' is true, also insert a backref from the
5482 * inode to the parent directory.
5483 */
Chris Masone02119d2008-09-05 16:13:11 -04005484int btrfs_add_link(struct btrfs_trans_handle *trans,
5485 struct inode *parent_inode, struct inode *inode,
5486 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005487{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005488 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005489 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005490 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005491 u64 ino = btrfs_ino(inode);
5492 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005493
Li Zefan33345d012011-04-20 10:31:50 +08005494 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005495 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5496 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005497 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005498 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5499 key.offset = 0;
5500 }
Chris Mason39279cc2007-06-12 06:35:45 -04005501
Li Zefan33345d012011-04-20 10:31:50 +08005502 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005503 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5504 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005505 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005506 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005507 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5508 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005509 }
5510
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005511 /* Nothing to clean up yet */
5512 if (ret)
5513 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005514
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005515 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5516 parent_inode, &key,
5517 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005518 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005519 goto fail_dir_item;
5520 else if (ret) {
5521 btrfs_abort_transaction(trans, root, ret);
5522 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005523 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005524
5525 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5526 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005527 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005528 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5529 ret = btrfs_update_inode(trans, root, parent_inode);
5530 if (ret)
5531 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005532 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005533
5534fail_dir_item:
5535 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5536 u64 local_index;
5537 int err;
5538 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5539 key.objectid, root->root_key.objectid,
5540 parent_ino, &local_index, name, name_len);
5541
5542 } else if (add_backref) {
5543 u64 local_index;
5544 int err;
5545
5546 err = btrfs_del_inode_ref(trans, root, name, name_len,
5547 ino, parent_ino, &local_index);
5548 }
5549 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005550}
5551
5552static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005553 struct inode *dir, struct dentry *dentry,
5554 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005555{
Josef Bacika1b075d2010-11-19 20:36:11 +00005556 int err = btrfs_add_link(trans, dir, inode,
5557 dentry->d_name.name, dentry->d_name.len,
5558 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005559 if (err > 0)
5560 err = -EEXIST;
5561 return err;
5562}
5563
Josef Bacik618e21d2007-07-11 10:18:17 -04005564static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005565 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005566{
5567 struct btrfs_trans_handle *trans;
5568 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005569 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005570 int err;
5571 int drop_inode = 0;
5572 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005573 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005574
5575 if (!new_valid_dev(rdev))
5576 return -EINVAL;
5577
Josef Bacik9ed74f22009-09-11 16:12:44 -04005578 /*
5579 * 2 for inode item and ref
5580 * 2 for dir items
5581 * 1 for xattr if selinux is on
5582 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005583 trans = btrfs_start_transaction(root, 5);
5584 if (IS_ERR(trans))
5585 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005586
Li Zefan581bb052011-04-20 10:06:11 +08005587 err = btrfs_find_free_ino(root, &objectid);
5588 if (err)
5589 goto out_unlock;
5590
Josef Bacikaec74772008-07-24 12:12:38 -04005591 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005592 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005593 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005594 if (IS_ERR(inode)) {
5595 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005596 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005597 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005598
Eric Paris2a7dba32011-02-01 11:05:39 -05005599 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005600 if (err) {
5601 drop_inode = 1;
5602 goto out_unlock;
5603 }
5604
Casey Schauflerad19db72011-12-15 10:09:07 -05005605 /*
5606 * If the active LSM wants to access the inode during
5607 * d_instantiate it needs these. Smack checks to see
5608 * if the filesystem supports xattrs by looking at the
5609 * ops vector.
5610 */
5611
5612 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005613 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005614 if (err)
5615 drop_inode = 1;
5616 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005617 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005618 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005619 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005620 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005621out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005622 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005623 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005624 if (drop_inode) {
5625 inode_dec_link_count(inode);
5626 iput(inode);
5627 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005628 return err;
5629}
5630
Chris Mason39279cc2007-06-12 06:35:45 -04005631static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005632 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005633{
5634 struct btrfs_trans_handle *trans;
5635 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005636 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005637 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005638 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005639 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005640 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005641
Josef Bacik9ed74f22009-09-11 16:12:44 -04005642 /*
5643 * 2 for inode item and ref
5644 * 2 for dir items
5645 * 1 for xattr if selinux is on
5646 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005647 trans = btrfs_start_transaction(root, 5);
5648 if (IS_ERR(trans))
5649 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005650
Li Zefan581bb052011-04-20 10:06:11 +08005651 err = btrfs_find_free_ino(root, &objectid);
5652 if (err)
5653 goto out_unlock;
5654
Josef Bacikaec74772008-07-24 12:12:38 -04005655 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005656 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005657 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005658 if (IS_ERR(inode)) {
5659 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005660 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005661 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005662 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005663
Eric Paris2a7dba32011-02-01 11:05:39 -05005664 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005665 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005666 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005667
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005668 err = btrfs_update_inode(trans, root, inode);
5669 if (err)
5670 goto out_unlock;
5671
Casey Schauflerad19db72011-12-15 10:09:07 -05005672 /*
5673 * If the active LSM wants to access the inode during
5674 * d_instantiate it needs these. Smack checks to see
5675 * if the filesystem supports xattrs by looking at the
5676 * ops vector.
5677 */
5678 inode->i_fop = &btrfs_file_operations;
5679 inode->i_op = &btrfs_file_inode_operations;
5680
Josef Bacika1b075d2010-11-19 20:36:11 +00005681 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005682 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005683 goto out_unlock;
5684
5685 inode->i_mapping->a_ops = &btrfs_aops;
5686 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5687 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5688 d_instantiate(dentry, inode);
5689
Chris Mason39279cc2007-06-12 06:35:45 -04005690out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005691 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005692 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005693 inode_dec_link_count(inode);
5694 iput(inode);
5695 }
Liu Bob53d3f52012-11-14 14:34:34 +00005696 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005697 return err;
5698}
5699
5700static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5701 struct dentry *dentry)
5702{
5703 struct btrfs_trans_handle *trans;
5704 struct btrfs_root *root = BTRFS_I(dir)->root;
5705 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005706 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005707 int err;
5708 int drop_inode = 0;
5709
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005710 /* do not allow sys_link's with other subvols of the same device */
5711 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005712 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005713
Mark Fashehf1863732012-08-08 11:32:27 -07005714 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005715 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005716
Chris Mason3de45862008-11-17 21:02:50 -05005717 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005718 if (err)
5719 goto fail;
5720
Yan, Zhenga22285a2010-05-16 10:48:46 -04005721 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005722 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005723 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005724 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005725 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005726 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005727 if (IS_ERR(trans)) {
5728 err = PTR_ERR(trans);
5729 goto fail;
5730 }
Chris Mason5f39d392007-10-15 16:14:19 -04005731
Miao Xie31534952011-04-13 13:19:21 +08005732 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005733 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005734 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6e2010-10-23 11:11:40 -04005735 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005736 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005737
Josef Bacika1b075d2010-11-19 20:36:11 +00005738 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005739
Yan, Zhenga5719522009-09-24 09:17:31 -04005740 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005741 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005742 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005743 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005744 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005745 if (err)
5746 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005747 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005748 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005749 }
Chris Mason39279cc2007-06-12 06:35:45 -04005750
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005751 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005752fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005753 if (drop_inode) {
5754 inode_dec_link_count(inode);
5755 iput(inode);
5756 }
Liu Bob53d3f52012-11-14 14:34:34 +00005757 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005758 return err;
5759}
5760
Al Viro18bb1db2011-07-26 01:41:39 -04005761static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005762{
Chris Masonb9d86662008-05-02 16:13:49 -04005763 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005764 struct btrfs_trans_handle *trans;
5765 struct btrfs_root *root = BTRFS_I(dir)->root;
5766 int err = 0;
5767 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005768 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005769 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005770
Josef Bacik9ed74f22009-09-11 16:12:44 -04005771 /*
5772 * 2 items for inode and ref
5773 * 2 items for dir items
5774 * 1 for xattr if selinux is on
5775 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005776 trans = btrfs_start_transaction(root, 5);
5777 if (IS_ERR(trans))
5778 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005779
Li Zefan581bb052011-04-20 10:06:11 +08005780 err = btrfs_find_free_ino(root, &objectid);
5781 if (err)
5782 goto out_fail;
5783
Josef Bacikaec74772008-07-24 12:12:38 -04005784 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005785 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005786 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005787 if (IS_ERR(inode)) {
5788 err = PTR_ERR(inode);
5789 goto out_fail;
5790 }
Chris Mason5f39d392007-10-15 16:14:19 -04005791
Chris Mason39279cc2007-06-12 06:35:45 -04005792 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005793
Eric Paris2a7dba32011-02-01 11:05:39 -05005794 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005795 if (err)
5796 goto out_fail;
5797
Chris Mason39279cc2007-06-12 06:35:45 -04005798 inode->i_op = &btrfs_dir_inode_operations;
5799 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005800
Chris Masondbe674a2008-07-17 12:54:05 -04005801 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005802 err = btrfs_update_inode(trans, root, inode);
5803 if (err)
5804 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005805
Josef Bacika1b075d2010-11-19 20:36:11 +00005806 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5807 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005808 if (err)
5809 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005810
Chris Mason39279cc2007-06-12 06:35:45 -04005811 d_instantiate(dentry, inode);
5812 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005813
5814out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005815 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005816 if (drop_on_err)
5817 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005818 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005819 return err;
5820}
5821
Chris Masond352ac62008-09-29 15:18:18 -04005822/* helper for btfs_get_extent. Given an existing extent in the tree,
5823 * and an extent that you want to insert, deal with overlap and insert
5824 * the new extent into the tree.
5825 */
Chris Mason3b951512008-04-17 11:29:12 -04005826static int merge_extent_mapping(struct extent_map_tree *em_tree,
5827 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005828 struct extent_map *em,
5829 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005830{
5831 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005832
Chris Masone6dcd2d2008-07-17 12:53:50 -04005833 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5834 start_diff = map_start - em->start;
5835 em->start = map_start;
5836 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005837 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5838 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005839 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005840 em->block_len -= start_diff;
5841 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005842 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005843}
5844
Chris Masonc8b97812008-10-29 14:49:59 -04005845static noinline int uncompress_inline(struct btrfs_path *path,
5846 struct inode *inode, struct page *page,
5847 size_t pg_offset, u64 extent_offset,
5848 struct btrfs_file_extent_item *item)
5849{
5850 int ret;
5851 struct extent_buffer *leaf = path->nodes[0];
5852 char *tmp;
5853 size_t max_size;
5854 unsigned long inline_size;
5855 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005856 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005857
5858 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005859 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005860 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5861 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01005862 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04005863 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005864 if (!tmp)
5865 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005866 ptr = btrfs_file_extent_inline_start(item);
5867
5868 read_extent_buffer(leaf, tmp, ptr, inline_size);
5869
Chris Mason5b050f02008-11-11 09:34:41 -05005870 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005871 ret = btrfs_decompress(compress_type, tmp, page,
5872 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005873 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005874 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005875 unsigned long copy_size = min_t(u64,
5876 PAGE_CACHE_SIZE - pg_offset,
5877 max_size - extent_offset);
5878 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005879 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005880 }
5881 kfree(tmp);
5882 return 0;
5883}
5884
Chris Masond352ac62008-09-29 15:18:18 -04005885/*
5886 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005887 * the ugly parts come from merging extents from the disk with the in-ram
5888 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005889 * where the in-ram extents might be locked pending data=ordered completion.
5890 *
5891 * This also copies inline extents directly into the page.
5892 */
Chris Masond3977122009-01-05 21:25:51 -05005893
Chris Masona52d9a82007-08-27 16:49:44 -04005894struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005895 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005896 int create)
5897{
5898 int ret;
5899 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005900 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005901 u64 extent_start = 0;
5902 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005903 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005904 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005905 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005906 struct btrfs_root *root = BTRFS_I(inode)->root;
5907 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005908 struct extent_buffer *leaf;
5909 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005910 struct extent_map *em = NULL;
5911 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005912 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005913 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005914 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005915
Chris Masona52d9a82007-08-27 16:49:44 -04005916again:
Chris Mason890871b2009-09-02 16:24:52 -04005917 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005918 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005919 if (em)
5920 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005921 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005922
Chris Masona52d9a82007-08-27 16:49:44 -04005923 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005924 if (em->start > start || em->start + em->len <= start)
5925 free_extent_map(em);
5926 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005927 free_extent_map(em);
5928 else
5929 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005930 }
David Sterba172ddd62011-04-21 00:48:27 +02005931 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005932 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005933 err = -ENOMEM;
5934 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005935 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005936 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005937 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005938 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005939 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005940 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005941
5942 if (!path) {
5943 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005944 if (!path) {
5945 err = -ENOMEM;
5946 goto out;
5947 }
5948 /*
5949 * Chances are we'll be called again, so go ahead and do
5950 * readahead
5951 */
5952 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005953 }
5954
Chris Mason179e29e2007-11-01 11:28:41 -04005955 ret = btrfs_lookup_file_extent(trans, root, path,
5956 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005957 if (ret < 0) {
5958 err = ret;
5959 goto out;
5960 }
5961
5962 if (ret != 0) {
5963 if (path->slots[0] == 0)
5964 goto not_found;
5965 path->slots[0]--;
5966 }
5967
Chris Mason5f39d392007-10-15 16:14:19 -04005968 leaf = path->nodes[0];
5969 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005970 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005971 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005972 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5973 found_type = btrfs_key_type(&found_key);
5974 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005975 found_type != BTRFS_EXTENT_DATA_KEY) {
5976 goto not_found;
5977 }
5978
Chris Mason5f39d392007-10-15 16:14:19 -04005979 found_type = btrfs_file_extent_type(leaf, item);
5980 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005981 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005982 if (found_type == BTRFS_FILE_EXTENT_REG ||
5983 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005984 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005985 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005986 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5987 size_t size;
5988 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00005989 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005990 }
5991
5992 if (start >= extent_end) {
5993 path->slots[0]++;
5994 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5995 ret = btrfs_next_leaf(root, path);
5996 if (ret < 0) {
5997 err = ret;
5998 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005999 }
Yan Zheng9036c102008-10-30 14:19:41 -04006000 if (ret > 0)
6001 goto not_found;
6002 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006003 }
Yan Zheng9036c102008-10-30 14:19:41 -04006004 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6005 if (found_key.objectid != objectid ||
6006 found_key.type != BTRFS_EXTENT_DATA_KEY)
6007 goto not_found;
6008 if (start + len <= found_key.offset)
6009 goto not_found;
6010 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006011 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006012 em->len = found_key.offset - start;
6013 goto not_found_em;
6014 }
6015
Josef Bacikcc95bef2013-04-04 14:31:27 -04006016 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006017 if (found_type == BTRFS_FILE_EXTENT_REG ||
6018 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006019 em->start = extent_start;
6020 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006021 em->orig_start = extent_start -
6022 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006023 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6024 item);
Chris Masondb945352007-10-15 16:15:53 -04006025 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6026 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006027 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006028 goto insert;
6029 }
Li Zefan261507a02010-12-17 14:21:50 +08006030 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006031 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006032 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006033 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006034 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006035 } else {
6036 bytenr += btrfs_file_extent_offset(leaf, item);
6037 em->block_start = bytenr;
6038 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006039 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6040 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006041 }
Chris Masona52d9a82007-08-27 16:49:44 -04006042 goto insert;
6043 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006044 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006045 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006046 size_t size;
6047 size_t extent_offset;
6048 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006049
Chris Masona52d9a82007-08-27 16:49:44 -04006050 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006051 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006052 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006053 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006054 goto out;
6055 }
6056
Yan Zheng9036c102008-10-30 14:19:41 -04006057 size = btrfs_file_extent_inline_len(leaf, item);
6058 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006059 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006060 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006061 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006062 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006063 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006064 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006065 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006066 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006067 em->compress_type = compress_type;
6068 }
Yan689f9342007-10-29 11:41:07 -04006069 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006070 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006071 if (btrfs_file_extent_compression(leaf, item) !=
6072 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006073 ret = uncompress_inline(path, inode, page,
6074 pg_offset,
6075 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006076 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006077 } else {
6078 map = kmap(page);
6079 read_extent_buffer(leaf, map + pg_offset, ptr,
6080 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006081 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6082 memset(map + pg_offset + copy_size, 0,
6083 PAGE_CACHE_SIZE - pg_offset -
6084 copy_size);
6085 }
Chris Masonc8b97812008-10-29 14:49:59 -04006086 kunmap(page);
6087 }
Chris Mason179e29e2007-11-01 11:28:41 -04006088 flush_dcache_page(page);
6089 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006090 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006091 if (!trans) {
6092 kunmap(page);
6093 free_extent_map(em);
6094 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006095
David Sterbab3b4aa72011-04-21 01:20:15 +02006096 btrfs_release_path(path);
Josef Bacik7a7eaa402011-04-13 12:54:33 -04006097 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006098
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006099 if (IS_ERR(trans))
6100 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006101 goto again;
6102 }
Chris Masonc8b97812008-10-29 14:49:59 -04006103 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006104 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006105 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006106 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006107 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006108 }
Chris Masond1310b22008-01-24 16:13:08 -05006109 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006110 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006111 goto insert;
6112 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006113 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006114 }
6115not_found:
6116 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006117 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006118 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006119not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006120 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006121 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006122insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006123 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006124 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006125 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006126 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006127 err = -EIO;
6128 goto out;
6129 }
Chris Masond1310b22008-01-24 16:13:08 -05006130
6131 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006132 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006133 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006134 /* it is possible that someone inserted the extent into the tree
6135 * while we had the lock dropped. It is also possible that
6136 * an overlapping map exists in the tree
6137 */
Chris Masona52d9a82007-08-27 16:49:44 -04006138 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006139 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006140
6141 ret = 0;
6142
Chris Mason3b951512008-04-17 11:29:12 -04006143 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006144 if (existing && (existing->start > start ||
6145 existing->start + existing->len <= start)) {
6146 free_extent_map(existing);
6147 existing = NULL;
6148 }
Chris Mason3b951512008-04-17 11:29:12 -04006149 if (!existing) {
6150 existing = lookup_extent_mapping(em_tree, em->start,
6151 em->len);
6152 if (existing) {
6153 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006154 em, start,
6155 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006156 free_extent_map(existing);
6157 if (err) {
6158 free_extent_map(em);
6159 em = NULL;
6160 }
6161 } else {
6162 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006163 free_extent_map(em);
6164 em = NULL;
6165 }
6166 } else {
6167 free_extent_map(em);
6168 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006169 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006170 }
Chris Masona52d9a82007-08-27 16:49:44 -04006171 }
Chris Mason890871b2009-09-02 16:24:52 -04006172 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006173out:
liubo1abe9b82011-03-24 11:18:59 +00006174
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006175 if (em)
6176 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006177
Chris Masonf4219502008-07-22 11:18:09 -04006178 if (path)
6179 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006180 if (trans) {
6181 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006182 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006183 err = ret;
6184 }
Chris Masona52d9a82007-08-27 16:49:44 -04006185 if (err) {
6186 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006187 return ERR_PTR(err);
6188 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006189 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006190 return em;
6191}
6192
Chris Masonec29ed52011-02-23 16:23:20 -05006193struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6194 size_t pg_offset, u64 start, u64 len,
6195 int create)
6196{
6197 struct extent_map *em;
6198 struct extent_map *hole_em = NULL;
6199 u64 range_start = start;
6200 u64 end;
6201 u64 found;
6202 u64 found_end;
6203 int err = 0;
6204
6205 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6206 if (IS_ERR(em))
6207 return em;
6208 if (em) {
6209 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006210 * if our em maps to
6211 * - a hole or
6212 * - a pre-alloc extent,
6213 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006214 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006215 if (em->block_start != EXTENT_MAP_HOLE &&
6216 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006217 return em;
6218 else
6219 hole_em = em;
6220 }
6221
6222 /* check to see if we've wrapped (len == -1 or similar) */
6223 end = start + len;
6224 if (end < start)
6225 end = (u64)-1;
6226 else
6227 end -= 1;
6228
6229 em = NULL;
6230
6231 /* ok, we didn't find anything, lets look for delalloc */
6232 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6233 end, len, EXTENT_DELALLOC, 1);
6234 found_end = range_start + found;
6235 if (found_end < range_start)
6236 found_end = (u64)-1;
6237
6238 /*
6239 * we didn't find anything useful, return
6240 * the original results from get_extent()
6241 */
6242 if (range_start > end || found_end <= start) {
6243 em = hole_em;
6244 hole_em = NULL;
6245 goto out;
6246 }
6247
6248 /* adjust the range_start to make sure it doesn't
6249 * go backwards from the start they passed in
6250 */
6251 range_start = max(start,range_start);
6252 found = found_end - range_start;
6253
6254 if (found > 0) {
6255 u64 hole_start = start;
6256 u64 hole_len = len;
6257
David Sterba172ddd62011-04-21 00:48:27 +02006258 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006259 if (!em) {
6260 err = -ENOMEM;
6261 goto out;
6262 }
6263 /*
6264 * when btrfs_get_extent can't find anything it
6265 * returns one huge hole
6266 *
6267 * make sure what it found really fits our range, and
6268 * adjust to make sure it is based on the start from
6269 * the caller
6270 */
6271 if (hole_em) {
6272 u64 calc_end = extent_map_end(hole_em);
6273
6274 if (calc_end <= start || (hole_em->start > end)) {
6275 free_extent_map(hole_em);
6276 hole_em = NULL;
6277 } else {
6278 hole_start = max(hole_em->start, start);
6279 hole_len = calc_end - hole_start;
6280 }
6281 }
6282 em->bdev = NULL;
6283 if (hole_em && range_start > hole_start) {
6284 /* our hole starts before our delalloc, so we
6285 * have to return just the parts of the hole
6286 * that go until the delalloc starts
6287 */
6288 em->len = min(hole_len,
6289 range_start - hole_start);
6290 em->start = hole_start;
6291 em->orig_start = hole_start;
6292 /*
6293 * don't adjust block start at all,
6294 * it is fixed at EXTENT_MAP_HOLE
6295 */
6296 em->block_start = hole_em->block_start;
6297 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006298 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6299 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006300 } else {
6301 em->start = range_start;
6302 em->len = found;
6303 em->orig_start = range_start;
6304 em->block_start = EXTENT_MAP_DELALLOC;
6305 em->block_len = found;
6306 }
6307 } else if (hole_em) {
6308 return hole_em;
6309 }
6310out:
6311
6312 free_extent_map(hole_em);
6313 if (err) {
6314 free_extent_map(em);
6315 return ERR_PTR(err);
6316 }
6317 return em;
6318}
6319
Josef Bacik4b46fce2010-05-23 11:00:55 -04006320static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6321 u64 start, u64 len)
6322{
6323 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006324 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006325 struct btrfs_key ins;
6326 u64 alloc_hint;
6327 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006328
Josef Bacik4b46fce2010-05-23 11:00:55 -04006329 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006330 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006331 alloc_hint, &ins, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006332 if (ret)
6333 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006334
Josef Bacik70c8a912012-10-11 16:54:30 -04006335 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006336 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006337 if (IS_ERR(em)) {
6338 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6339 return em;
6340 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006341
6342 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6343 ins.offset, ins.offset, 0);
6344 if (ret) {
6345 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Josef Bacik00361582013-08-14 14:02:47 -04006346 free_extent_map(em);
6347 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006348 }
Josef Bacik00361582013-08-14 14:02:47 -04006349
Josef Bacik4b46fce2010-05-23 11:00:55 -04006350 return em;
6351}
6352
Chris Mason46bfbb52010-05-26 11:04:10 -04006353/*
6354 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6355 * block must be cow'd
6356 */
Josef Bacik00361582013-08-14 14:02:47 -04006357noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006358 u64 *orig_start, u64 *orig_block_len,
6359 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006360{
Josef Bacik00361582013-08-14 14:02:47 -04006361 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006362 struct btrfs_path *path;
6363 int ret;
6364 struct extent_buffer *leaf;
6365 struct btrfs_root *root = BTRFS_I(inode)->root;
6366 struct btrfs_file_extent_item *fi;
6367 struct btrfs_key key;
6368 u64 disk_bytenr;
6369 u64 backref_offset;
6370 u64 extent_end;
6371 u64 num_bytes;
6372 int slot;
6373 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006374 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Chris Mason46bfbb52010-05-26 11:04:10 -04006375 path = btrfs_alloc_path();
6376 if (!path)
6377 return -ENOMEM;
6378
Josef Bacik00361582013-08-14 14:02:47 -04006379 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006380 offset, 0);
6381 if (ret < 0)
6382 goto out;
6383
6384 slot = path->slots[0];
6385 if (ret == 1) {
6386 if (slot == 0) {
6387 /* can't find the item, must cow */
6388 ret = 0;
6389 goto out;
6390 }
6391 slot--;
6392 }
6393 ret = 0;
6394 leaf = path->nodes[0];
6395 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006396 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006397 key.type != BTRFS_EXTENT_DATA_KEY) {
6398 /* not our file or wrong item type, must cow */
6399 goto out;
6400 }
6401
6402 if (key.offset > offset) {
6403 /* Wrong offset, must cow */
6404 goto out;
6405 }
6406
6407 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6408 found_type = btrfs_file_extent_type(leaf, fi);
6409 if (found_type != BTRFS_FILE_EXTENT_REG &&
6410 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6411 /* not a regular extent, must cow */
6412 goto out;
6413 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006414
6415 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6416 goto out;
6417
Chris Mason46bfbb52010-05-26 11:04:10 -04006418 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006419 if (disk_bytenr == 0)
6420 goto out;
6421
6422 if (btrfs_file_extent_compression(leaf, fi) ||
6423 btrfs_file_extent_encryption(leaf, fi) ||
6424 btrfs_file_extent_other_encoding(leaf, fi))
6425 goto out;
6426
Chris Mason46bfbb52010-05-26 11:04:10 -04006427 backref_offset = btrfs_file_extent_offset(leaf, fi);
6428
Josef Bacik7ee9e442013-06-21 16:37:03 -04006429 if (orig_start) {
6430 *orig_start = key.offset - backref_offset;
6431 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6432 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6433 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006434
Chris Mason46bfbb52010-05-26 11:04:10 -04006435 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason46bfbb52010-05-26 11:04:10 -04006436
6437 if (btrfs_extent_readonly(root, disk_bytenr))
6438 goto out;
Josef Bacik1bda19e2013-10-18 12:10:36 -04006439 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006440
6441 /*
6442 * look for other files referencing this extent, if we
6443 * find any we must cow
6444 */
Josef Bacik00361582013-08-14 14:02:47 -04006445 trans = btrfs_join_transaction(root);
6446 if (IS_ERR(trans)) {
6447 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006448 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006449 }
6450
6451 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6452 key.offset - backref_offset, disk_bytenr);
6453 btrfs_end_transaction(trans, root);
6454 if (ret) {
6455 ret = 0;
6456 goto out;
6457 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006458
6459 /*
6460 * adjust disk_bytenr and num_bytes to cover just the bytes
6461 * in this extent we are about to write. If there
6462 * are any csums in that range we have to cow in order
6463 * to keep the csums correct
6464 */
6465 disk_bytenr += backref_offset;
6466 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006467 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006468 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6469 goto out;
6470 /*
6471 * all of the above have passed, it is safe to overwrite this extent
6472 * without cow
6473 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006474 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006475 ret = 1;
6476out:
6477 btrfs_free_path(path);
6478 return ret;
6479}
6480
Josef Bacikeb838e72012-07-31 16:28:48 -04006481static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6482 struct extent_state **cached_state, int writing)
6483{
6484 struct btrfs_ordered_extent *ordered;
6485 int ret = 0;
6486
6487 while (1) {
6488 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6489 0, cached_state);
6490 /*
6491 * We're concerned with the entire range that we're going to be
6492 * doing DIO to, so we need to make sure theres no ordered
6493 * extents in this range.
6494 */
6495 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6496 lockend - lockstart + 1);
6497
6498 /*
6499 * We need to make sure there are no buffered pages in this
6500 * range either, we could have raced between the invalidate in
6501 * generic_file_direct_write and locking the extent. The
6502 * invalidate needs to happen so that reads after a write do not
6503 * get stale data.
6504 */
6505 if (!ordered && (!writing ||
6506 !test_range_bit(&BTRFS_I(inode)->io_tree,
6507 lockstart, lockend, EXTENT_UPTODATE, 0,
6508 *cached_state)))
6509 break;
6510
6511 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6512 cached_state, GFP_NOFS);
6513
6514 if (ordered) {
6515 btrfs_start_ordered_extent(inode, ordered, 1);
6516 btrfs_put_ordered_extent(ordered);
6517 } else {
6518 /* Screw you mmap */
6519 ret = filemap_write_and_wait_range(inode->i_mapping,
6520 lockstart,
6521 lockend);
6522 if (ret)
6523 break;
6524
6525 /*
6526 * If we found a page that couldn't be invalidated just
6527 * fall back to buffered.
6528 */
6529 ret = invalidate_inode_pages2_range(inode->i_mapping,
6530 lockstart >> PAGE_CACHE_SHIFT,
6531 lockend >> PAGE_CACHE_SHIFT);
6532 if (ret)
6533 break;
6534 }
6535
6536 cond_resched();
6537 }
6538
6539 return ret;
6540}
6541
Josef Bacik69ffb542012-09-11 15:40:07 -04006542static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6543 u64 len, u64 orig_start,
6544 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006545 u64 orig_block_len, u64 ram_bytes,
6546 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006547{
6548 struct extent_map_tree *em_tree;
6549 struct extent_map *em;
6550 struct btrfs_root *root = BTRFS_I(inode)->root;
6551 int ret;
6552
6553 em_tree = &BTRFS_I(inode)->extent_tree;
6554 em = alloc_extent_map();
6555 if (!em)
6556 return ERR_PTR(-ENOMEM);
6557
6558 em->start = start;
6559 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006560 em->mod_start = start;
6561 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006562 em->len = len;
6563 em->block_len = block_len;
6564 em->block_start = block_start;
6565 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006566 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006567 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006568 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006569 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6570 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006571 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006572
6573 do {
6574 btrfs_drop_extent_cache(inode, em->start,
6575 em->start + em->len - 1, 0);
6576 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006577 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006578 write_unlock(&em_tree->lock);
6579 } while (ret == -EEXIST);
6580
6581 if (ret) {
6582 free_extent_map(em);
6583 return ERR_PTR(ret);
6584 }
6585
6586 return em;
6587}
6588
6589
Josef Bacik4b46fce2010-05-23 11:00:55 -04006590static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6591 struct buffer_head *bh_result, int create)
6592{
6593 struct extent_map *em;
6594 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006595 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006596 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006597 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006598 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04006599 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006600 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006601
Miao Xie172a5042013-02-21 02:48:22 -07006602 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006603 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006604 else
Josef Bacikc3298612012-08-03 16:49:19 -04006605 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006606
Josef Bacikc3298612012-08-03 16:49:19 -04006607 lockstart = start;
6608 lockend = start + len - 1;
6609
Josef Bacikeb838e72012-07-31 16:28:48 -04006610 /*
6611 * If this errors out it's because we couldn't invalidate pagecache for
6612 * this range and we need to fallback to buffered.
6613 */
6614 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6615 return -ENOTBLK;
6616
Josef Bacik4b46fce2010-05-23 11:00:55 -04006617 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006618 if (IS_ERR(em)) {
6619 ret = PTR_ERR(em);
6620 goto unlock_err;
6621 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006622
6623 /*
6624 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6625 * io. INLINE is special, and we could probably kludge it in here, but
6626 * it's still buffered so for safety lets just fall back to the generic
6627 * buffered path.
6628 *
6629 * For COMPRESSED we _have_ to read the entire extent in so we can
6630 * decompress it, so there will be buffering required no matter what we
6631 * do, so go ahead and fallback to buffered.
6632 *
6633 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6634 * to buffered IO. Don't blame me, this is the price we pay for using
6635 * the generic code.
6636 */
6637 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6638 em->block_start == EXTENT_MAP_INLINE) {
6639 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006640 ret = -ENOTBLK;
6641 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006642 }
6643
6644 /* Just a good old fashioned hole, return */
6645 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6646 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6647 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006648 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006649 }
6650
6651 /*
6652 * We don't allocate a new extent in the following cases
6653 *
6654 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6655 * existing extent.
6656 * 2) The extent is marked as PREALLOC. We're good to go here and can
6657 * just use the extent.
6658 *
6659 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006660 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006661 len = min(len, em->len - (start - em->start));
6662 lockstart = start + len;
6663 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006664 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006665
6666 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6667 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6668 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006669 int type;
6670 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006671 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006672
6673 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6674 type = BTRFS_ORDERED_PREALLOC;
6675 else
6676 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006677 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006678 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006679
Josef Bacik00361582013-08-14 14:02:47 -04006680 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006681 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006682 if (type == BTRFS_ORDERED_PREALLOC) {
6683 free_extent_map(em);
6684 em = create_pinned_em(inode, start, len,
6685 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006686 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006687 orig_block_len,
6688 ram_bytes, type);
Josef Bacik00361582013-08-14 14:02:47 -04006689 if (IS_ERR(em))
Josef Bacik69ffb542012-09-11 15:40:07 -04006690 goto unlock_err;
Josef Bacik69ffb542012-09-11 15:40:07 -04006691 }
6692
Chris Mason46bfbb52010-05-26 11:04:10 -04006693 ret = btrfs_add_ordered_extent_dio(inode, start,
6694 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04006695 if (ret) {
6696 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006697 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006698 }
6699 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006700 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006701 }
Josef Bacik00361582013-08-14 14:02:47 -04006702
Chris Mason46bfbb52010-05-26 11:04:10 -04006703 /*
6704 * this will cow the extent, reset the len in case we changed
6705 * it above
6706 */
6707 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006708 free_extent_map(em);
6709 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006710 if (IS_ERR(em)) {
6711 ret = PTR_ERR(em);
6712 goto unlock_err;
6713 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006714 len = min(len, em->len - (start - em->start));
6715unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006716 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6717 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006718 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006719 bh_result->b_bdev = em->bdev;
6720 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006721 if (create) {
6722 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6723 set_buffer_new(bh_result);
6724
6725 /*
6726 * Need to update the i_size under the extent lock so buffered
6727 * readers will get the updated i_size when we unlock.
6728 */
6729 if (start + len > i_size_read(inode))
6730 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006731
Miao Xie172a5042013-02-21 02:48:22 -07006732 spin_lock(&BTRFS_I(inode)->lock);
6733 BTRFS_I(inode)->outstanding_extents++;
6734 spin_unlock(&BTRFS_I(inode)->lock);
6735
Miao Xie09348562013-02-07 10:12:07 +00006736 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6737 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6738 &cached_state, GFP_NOFS);
6739 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006740 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006741
Josef Bacikeb838e72012-07-31 16:28:48 -04006742 /*
6743 * In the case of write we need to clear and unlock the entire range,
6744 * in the case of read we need to unlock only the end area that we
6745 * aren't using if there is any left over space.
6746 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006747 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006748 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6749 lockend, unlock_bits, 1, 0,
6750 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006751 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006752 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006753 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006754
Josef Bacik4b46fce2010-05-23 11:00:55 -04006755 free_extent_map(em);
6756
6757 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006758
6759unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006760 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6761 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6762 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006763}
6764
Josef Bacik4b46fce2010-05-23 11:00:55 -04006765static void btrfs_endio_direct_read(struct bio *bio, int err)
6766{
Miao Xiee65e1532010-11-22 03:04:43 +00006767 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006768 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6769 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006770 struct inode *inode = dip->inode;
6771 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006772 struct bio *dio_bio;
Miao Xiefacc8a22013-07-25 19:22:34 +08006773 u32 *csums = (u32 *)dip->csum;
6774 int index = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006775 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006776
6777 start = dip->logical_offset;
6778 do {
6779 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6780 struct page *page = bvec->bv_page;
6781 char *kaddr;
6782 u32 csum = ~(u32)0;
6783 unsigned long flags;
6784
6785 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006786 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006787 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006788 csum, bvec->bv_len);
6789 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006790 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006791 local_irq_restore(flags);
6792
6793 flush_dcache_page(bvec->bv_page);
Miao Xiefacc8a22013-07-25 19:22:34 +08006794 if (csum != csums[index]) {
6795 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006796 btrfs_ino(inode), start, csum,
6797 csums[index]);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006798 err = -EIO;
6799 }
6800 }
6801
6802 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006803 bvec++;
Miao Xiefacc8a22013-07-25 19:22:34 +08006804 index++;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006805 } while (bvec <= bvec_end);
6806
6807 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006808 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04006809 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006810
Josef Bacik4b46fce2010-05-23 11:00:55 -04006811 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006812
6813 /* If we had a csum failure make sure to clear the uptodate flag */
6814 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006815 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6816 dio_end_io(dio_bio, err);
6817 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006818}
6819
6820static void btrfs_endio_direct_write(struct bio *bio, int err)
6821{
6822 struct btrfs_dio_private *dip = bio->bi_private;
6823 struct inode *inode = dip->inode;
6824 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006825 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006826 u64 ordered_offset = dip->logical_offset;
6827 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04006828 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006829 int ret;
6830
6831 if (err)
6832 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006833again:
6834 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6835 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006836 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006837 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006838 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006839
Josef Bacik5fd02042012-05-02 14:00:54 -04006840 ordered->work.func = finish_ordered_fn;
6841 ordered->work.flags = 0;
6842 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6843 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006844out_test:
6845 /*
6846 * our bio might span multiple ordered extents. If we haven't
6847 * completed the accounting for the whole dio, go back and try again
6848 */
6849 if (ordered_offset < dip->logical_offset + dip->bytes) {
6850 ordered_bytes = dip->logical_offset + dip->bytes -
6851 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006852 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006853 goto again;
6854 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006855out_done:
Chris Mason9be33952013-05-17 18:30:14 -04006856 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006857
Josef Bacik4b46fce2010-05-23 11:00:55 -04006858 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006859
6860 /* If we had an error make sure to clear the uptodate flag */
6861 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006862 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6863 dio_end_io(dio_bio, err);
6864 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006865}
6866
Chris Masoneaf25d92010-05-25 09:48:28 -04006867static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6868 struct bio *bio, int mirror_num,
6869 unsigned long bio_flags, u64 offset)
6870{
6871 int ret;
6872 struct btrfs_root *root = BTRFS_I(inode)->root;
6873 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006874 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006875 return 0;
6876}
6877
Miao Xiee65e1532010-11-22 03:04:43 +00006878static void btrfs_end_dio_bio(struct bio *bio, int err)
6879{
6880 struct btrfs_dio_private *dip = bio->bi_private;
6881
6882 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006883 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006884 "sector %#Lx len %u err no %d\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006885 btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006886 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006887 dip->errors = 1;
6888
6889 /*
6890 * before atomic variable goto zero, we must make sure
6891 * dip->errors is perceived to be set.
6892 */
6893 smp_mb__before_atomic_dec();
6894 }
6895
6896 /* if there are more bios still pending for this dio, just exit */
6897 if (!atomic_dec_and_test(&dip->pending_bios))
6898 goto out;
6899
Chris Mason9be33952013-05-17 18:30:14 -04006900 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00006901 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04006902 } else {
6903 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00006904 bio_endio(dip->orig_bio, 0);
6905 }
6906out:
6907 bio_put(bio);
6908}
6909
6910static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6911 u64 first_sector, gfp_t gfp_flags)
6912{
6913 int nr_vecs = bio_get_nr_vecs(bdev);
6914 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6915}
6916
6917static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6918 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006919 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006920{
Miao Xiefacc8a22013-07-25 19:22:34 +08006921 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00006922 int write = rw & REQ_WRITE;
6923 struct btrfs_root *root = BTRFS_I(inode)->root;
6924 int ret;
6925
Josef Bacikb812ce22012-11-16 13:56:32 -05006926 if (async_submit)
6927 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6928
Miao Xiee65e1532010-11-22 03:04:43 +00006929 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006930
6931 if (!write) {
6932 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6933 if (ret)
6934 goto err;
6935 }
Miao Xiee65e1532010-11-22 03:04:43 +00006936
Josef Bacik1ae39932011-04-06 14:41:34 -04006937 if (skip_sum)
6938 goto map;
6939
6940 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006941 ret = btrfs_wq_submit_bio(root->fs_info,
6942 inode, rw, bio, 0, 0,
6943 file_offset,
6944 __btrfs_submit_bio_start_direct_io,
6945 __btrfs_submit_bio_done);
6946 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006947 } else if (write) {
6948 /*
6949 * If we aren't doing async submit, calculate the csum of the
6950 * bio now.
6951 */
6952 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6953 if (ret)
6954 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006955 } else if (!skip_sum) {
Miao Xiefacc8a22013-07-25 19:22:34 +08006956 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
6957 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006958 if (ret)
6959 goto err;
6960 }
Miao Xiee65e1532010-11-22 03:04:43 +00006961
Josef Bacik1ae39932011-04-06 14:41:34 -04006962map:
6963 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006964err:
6965 bio_put(bio);
6966 return ret;
6967}
6968
6969static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6970 int skip_sum)
6971{
6972 struct inode *inode = dip->inode;
6973 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00006974 struct bio *bio;
6975 struct bio *orig_bio = dip->orig_bio;
6976 struct bio_vec *bvec = orig_bio->bi_io_vec;
6977 u64 start_sector = orig_bio->bi_sector;
6978 u64 file_offset = dip->logical_offset;
6979 u64 submit_len = 0;
6980 u64 map_length;
6981 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006982 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006983 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006984
Miao Xiee65e1532010-11-22 03:04:43 +00006985 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05006986 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006987 &map_length, NULL, 0);
6988 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006989 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006990 return -EIO;
6991 }
Miao Xiefacc8a22013-07-25 19:22:34 +08006992
Josef Bacik02f57c72011-04-06 14:25:44 -04006993 if (map_length >= orig_bio->bi_size) {
6994 bio = orig_bio;
6995 goto submit;
6996 }
6997
David Woodhouse53b381b2013-01-29 18:40:14 -05006998 /* async crcs make it difficult to collect full stripe writes. */
6999 if (btrfs_get_alloc_profile(root, 1) &
7000 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7001 async_submit = 0;
7002 else
7003 async_submit = 1;
7004
Josef Bacik02f57c72011-04-06 14:25:44 -04007005 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7006 if (!bio)
7007 return -ENOMEM;
7008 bio->bi_private = dip;
7009 bio->bi_end_io = btrfs_end_dio_bio;
7010 atomic_inc(&dip->pending_bios);
7011
Miao Xiee65e1532010-11-22 03:04:43 +00007012 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7013 if (unlikely(map_length < submit_len + bvec->bv_len ||
7014 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7015 bvec->bv_offset) < bvec->bv_len)) {
7016 /*
7017 * inc the count before we submit the bio so
7018 * we know the end IO handler won't happen before
7019 * we inc the count. Otherwise, the dip might get freed
7020 * before we're done setting it up
7021 */
7022 atomic_inc(&dip->pending_bios);
7023 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7024 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007025 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007026 if (ret) {
7027 bio_put(bio);
7028 atomic_dec(&dip->pending_bios);
7029 goto out_err;
7030 }
7031
Miao Xiee65e1532010-11-22 03:04:43 +00007032 start_sector += submit_len >> 9;
7033 file_offset += submit_len;
7034
7035 submit_len = 0;
7036 nr_pages = 0;
7037
7038 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7039 start_sector, GFP_NOFS);
7040 if (!bio)
7041 goto out_err;
7042 bio->bi_private = dip;
7043 bio->bi_end_io = btrfs_end_dio_bio;
7044
7045 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007046 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007047 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007048 &map_length, NULL, 0);
7049 if (ret) {
7050 bio_put(bio);
7051 goto out_err;
7052 }
7053 } else {
7054 submit_len += bvec->bv_len;
7055 nr_pages ++;
7056 bvec++;
7057 }
7058 }
7059
Josef Bacik02f57c72011-04-06 14:25:44 -04007060submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007061 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007062 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007063 if (!ret)
7064 return 0;
7065
7066 bio_put(bio);
7067out_err:
7068 dip->errors = 1;
7069 /*
7070 * before atomic variable goto zero, we must
7071 * make sure dip->errors is perceived to be set.
7072 */
7073 smp_mb__before_atomic_dec();
7074 if (atomic_dec_and_test(&dip->pending_bios))
7075 bio_io_error(dip->orig_bio);
7076
7077 /* bio_end_io() will handle error, so we needn't return it */
7078 return 0;
7079}
7080
Chris Mason9be33952013-05-17 18:30:14 -04007081static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7082 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007083{
7084 struct btrfs_root *root = BTRFS_I(inode)->root;
7085 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007086 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007087 int skip_sum;
Miao Xiefacc8a22013-07-25 19:22:34 +08007088 int sum_len;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007089 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007090 int ret = 0;
Miao Xiefacc8a22013-07-25 19:22:34 +08007091 u16 csum_size;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007092
7093 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7094
Chris Mason9be33952013-05-17 18:30:14 -04007095 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007096 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007097 ret = -ENOMEM;
7098 goto free_ordered;
7099 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007100
Miao Xiefacc8a22013-07-25 19:22:34 +08007101 if (!skip_sum && !write) {
7102 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7103 sum_len = dio_bio->bi_size >> inode->i_sb->s_blocksize_bits;
7104 sum_len *= csum_size;
7105 } else {
7106 sum_len = 0;
7107 }
7108
7109 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007110 if (!dip) {
7111 ret = -ENOMEM;
7112 goto free_io_bio;
7113 }
7114
7115 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007116 dip->inode = inode;
7117 dip->logical_offset = file_offset;
Miao Xiee6da5d22013-06-19 18:19:17 +08007118 dip->bytes = dio_bio->bi_size;
Chris Mason9be33952013-05-17 18:30:14 -04007119 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7120 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007121 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007122 dip->orig_bio = io_bio;
7123 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007124 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007125
7126 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007127 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007128 else
Chris Mason9be33952013-05-17 18:30:14 -04007129 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007130
Miao Xiee65e1532010-11-22 03:04:43 +00007131 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7132 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007133 return;
Chris Mason9be33952013-05-17 18:30:14 -04007134
7135free_io_bio:
7136 bio_put(io_bio);
7137
Josef Bacik4b46fce2010-05-23 11:00:55 -04007138free_ordered:
7139 /*
7140 * If this is a write, we need to clean up the reserved space and kill
7141 * the ordered extent.
7142 */
7143 if (write) {
7144 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007145 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007146 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7147 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7148 btrfs_free_reserved_extent(root, ordered->start,
7149 ordered->disk_len);
7150 btrfs_put_ordered_extent(ordered);
7151 btrfs_put_ordered_extent(ordered);
7152 }
Chris Mason9be33952013-05-17 18:30:14 -04007153 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007154}
7155
Chris Mason5a5f79b2010-05-26 21:33:37 -04007156static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7157 const struct iovec *iov, loff_t offset,
7158 unsigned long nr_segs)
7159{
7160 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007161 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007162 size_t size;
7163 unsigned long addr;
7164 unsigned blocksize_mask = root->sectorsize - 1;
7165 ssize_t retval = -EINVAL;
7166 loff_t end = offset;
7167
7168 if (offset & blocksize_mask)
7169 goto out;
7170
7171 /* Check the memory alignment. Blocks cannot straddle pages */
7172 for (seg = 0; seg < nr_segs; seg++) {
7173 addr = (unsigned long)iov[seg].iov_base;
7174 size = iov[seg].iov_len;
7175 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007176 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007177 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007178
7179 /* If this is a write we don't need to check anymore */
7180 if (rw & WRITE)
7181 continue;
7182
7183 /*
7184 * Check to make sure we don't have duplicate iov_base's in this
7185 * iovec, if so return EINVAL, otherwise we'll get csum errors
7186 * when reading back.
7187 */
7188 for (i = seg + 1; i < nr_segs; i++) {
7189 if (iov[seg].iov_base == iov[i].iov_base)
7190 goto out;
7191 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007192 }
7193 retval = 0;
7194out:
7195 return retval;
7196}
Josef Bacikeb838e72012-07-31 16:28:48 -04007197
Chris Mason16432982008-04-10 10:23:21 -04007198static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7199 const struct iovec *iov, loff_t offset,
7200 unsigned long nr_segs)
7201{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007202 struct file *file = iocb->ki_filp;
7203 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007204 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007205 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007206 bool wakeup = true;
7207 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007208 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007209
Chris Mason5a5f79b2010-05-26 21:33:37 -04007210 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007211 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007212 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007213
Miao Xie38851cc2013-02-08 07:04:11 +00007214 atomic_inc(&inode->i_dio_count);
7215 smp_mb__after_atomic_inc();
7216
Josef Bacik0e267c42013-07-02 10:38:02 -04007217 /*
7218 * The generic stuff only does filemap_write_and_wait_range, which isn't
7219 * enough if we've written compressed pages to this area, so we need to
7220 * call btrfs_wait_ordered_range to make absolutely sure that any
7221 * outstanding dirty pages are on disk.
7222 */
7223 count = iov_length(iov, nr_segs);
7224 btrfs_wait_ordered_range(inode, offset, count);
7225
Miao Xie09348562013-02-07 10:12:07 +00007226 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007227 /*
7228 * If the write DIO is beyond the EOF, we need update
7229 * the isize, but it is protected by i_mutex. So we can
7230 * not unlock the i_mutex at this case.
7231 */
7232 if (offset + count <= inode->i_size) {
7233 mutex_unlock(&inode->i_mutex);
7234 relock = true;
7235 }
Miao Xie09348562013-02-07 10:12:07 +00007236 ret = btrfs_delalloc_reserve_space(inode, count);
7237 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007238 goto out;
7239 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7240 &BTRFS_I(inode)->runtime_flags))) {
7241 inode_dio_done(inode);
7242 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7243 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007244 }
7245
7246 ret = __blockdev_direct_IO(rw, iocb, inode,
7247 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7248 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007249 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007250 if (rw & WRITE) {
7251 if (ret < 0 && ret != -EIOCBQUEUED)
7252 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007253 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007254 btrfs_delalloc_release_space(inode,
7255 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007256 else
7257 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007258 }
Miao Xie38851cc2013-02-08 07:04:11 +00007259out:
Miao Xie2e60a512013-02-08 07:01:08 +00007260 if (wakeup)
7261 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007262 if (relock)
7263 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007264
7265 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007266}
7267
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007268#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7269
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007270static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7271 __u64 start, __u64 len)
7272{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007273 int ret;
7274
7275 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7276 if (ret)
7277 return ret;
7278
Chris Masonec29ed52011-02-23 16:23:20 -05007279 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007280}
7281
Chris Mason9ebefb182007-06-15 13:50:00 -04007282int btrfs_readpage(struct file *file, struct page *page)
7283{
Chris Masond1310b22008-01-24 16:13:08 -05007284 struct extent_io_tree *tree;
7285 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007286 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007287}
Chris Mason1832a6d2007-12-21 16:27:21 -05007288
Chris Mason39279cc2007-06-12 06:35:45 -04007289static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7290{
Chris Masond1310b22008-01-24 16:13:08 -05007291 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007292
7293
7294 if (current->flags & PF_MEMALLOC) {
7295 redirty_page_for_writepage(wbc, page);
7296 unlock_page(page);
7297 return 0;
7298 }
Chris Masond1310b22008-01-24 16:13:08 -05007299 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007300 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7301}
Chris Mason39279cc2007-06-12 06:35:45 -04007302
Eric Sandeen48a3b632013-04-25 20:41:01 +00007303static int btrfs_writepages(struct address_space *mapping,
7304 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007305{
Chris Masond1310b22008-01-24 16:13:08 -05007306 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007307
Chris Masond1310b22008-01-24 16:13:08 -05007308 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007309 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7310}
7311
Chris Mason3ab2fb52007-11-08 10:59:22 -05007312static int
7313btrfs_readpages(struct file *file, struct address_space *mapping,
7314 struct list_head *pages, unsigned nr_pages)
7315{
Chris Masond1310b22008-01-24 16:13:08 -05007316 struct extent_io_tree *tree;
7317 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007318 return extent_readpages(tree, mapping, pages, nr_pages,
7319 btrfs_get_extent);
7320}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007321static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007322{
Chris Masond1310b22008-01-24 16:13:08 -05007323 struct extent_io_tree *tree;
7324 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007325 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007326
Chris Masond1310b22008-01-24 16:13:08 -05007327 tree = &BTRFS_I(page->mapping->host)->io_tree;
7328 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007329 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007330 if (ret == 1) {
7331 ClearPagePrivate(page);
7332 set_page_private(page, 0);
7333 page_cache_release(page);
7334 }
7335 return ret;
7336}
Chris Mason39279cc2007-06-12 06:35:45 -04007337
Chris Masone6dcd2d2008-07-17 12:53:50 -04007338static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7339{
Chris Mason98509cf2008-09-11 15:51:43 -04007340 if (PageWriteback(page) || PageDirty(page))
7341 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007342 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007343}
7344
Lukas Czernerd47992f2013-05-21 23:17:23 -04007345static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7346 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007347{
Josef Bacik5fd02042012-05-02 14:00:54 -04007348 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007349 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007350 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007351 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007352 u64 page_start = page_offset(page);
7353 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007354
Chris Mason8b62b722009-09-02 16:53:46 -04007355 /*
7356 * we have the page locked, so new writeback can't start,
7357 * and the dirty bit won't be cleared while we are here.
7358 *
7359 * Wait for IO on this page so that we can safely clear
7360 * the PagePrivate2 bit and do ordered accounting
7361 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007362 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007363
Josef Bacik5fd02042012-05-02 14:00:54 -04007364 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007365 if (offset) {
7366 btrfs_releasepage(page, GFP_NOFS);
7367 return;
7368 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007369 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007370 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007371 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007372 /*
7373 * IO on this page will never be started, so we need
7374 * to account for any ordered extents now
7375 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007376 clear_extent_bit(tree, page_start, page_end,
7377 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007378 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7379 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007380 /*
7381 * whoever cleared the private bit is responsible
7382 * for the finish_ordered_io
7383 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04007384 if (TestClearPagePrivate2(page)) {
7385 struct btrfs_ordered_inode_tree *tree;
7386 u64 new_len;
7387
7388 tree = &BTRFS_I(inode)->ordered_tree;
7389
7390 spin_lock_irq(&tree->lock);
7391 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
7392 new_len = page_start - ordered->file_offset;
7393 if (new_len < ordered->truncated_len)
7394 ordered->truncated_len = new_len;
7395 spin_unlock_irq(&tree->lock);
7396
7397 if (btrfs_dec_test_ordered_pending(inode, &ordered,
7398 page_start,
7399 PAGE_CACHE_SIZE, 1))
7400 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007401 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007402 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007403 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007404 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007405 }
7406 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007407 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007408 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7409 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007410 __btrfs_releasepage(page, GFP_NOFS);
7411
Chris Mason4a096752008-07-21 10:29:44 -04007412 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007413 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007414 ClearPagePrivate(page);
7415 set_page_private(page, 0);
7416 page_cache_release(page);
7417 }
Chris Mason39279cc2007-06-12 06:35:45 -04007418}
7419
Chris Mason9ebefb182007-06-15 13:50:00 -04007420/*
7421 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7422 * called from a page fault handler when a page is first dirtied. Hence we must
7423 * be careful to check for EOF conditions here. We set the page up correctly
7424 * for a written page which means we get ENOSPC checking when writing into
7425 * holes and correct delalloc and unwritten extent mapping on filesystems that
7426 * support these features.
7427 *
7428 * We are not allowed to take the i_mutex here so we have to play games to
7429 * protect against truncate races as the page could now be beyond EOF. Because
7430 * vmtruncate() writes the inode size before removing pages, once we have the
7431 * page lock we can determine safely if the page is beyond EOF. If it is not
7432 * beyond EOF, then the page is guaranteed safe against truncation until we
7433 * unlock the page.
7434 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007435int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007436{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007437 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007438 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007439 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007440 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7441 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007442 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007443 char *kaddr;
7444 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007445 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007446 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007447 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007448 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007449 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007450
Jan Karab2b5ef52012-06-12 16:20:45 +02007451 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007452 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007453 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007454 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007455 reserved = 1;
7456 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007457 if (ret) {
7458 if (ret == -ENOMEM)
7459 ret = VM_FAULT_OOM;
7460 else /* -ENOSPC, -EIO, etc */
7461 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007462 if (reserved)
7463 goto out;
7464 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007465 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007466
Nick Piggin56a76f82009-03-31 15:23:23 -07007467 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007468again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007469 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007470 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007471 page_start = page_offset(page);
7472 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007473
Chris Mason9ebefb182007-06-15 13:50:00 -04007474 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007475 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007476 /* page got truncated out from underneath us */
7477 goto out_unlock;
7478 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007479 wait_on_page_writeback(page);
7480
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007481 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007482 set_page_extent_mapped(page);
7483
Chris Masoneb84ae02008-07-17 13:53:27 -04007484 /*
7485 * we can't set the delalloc bits if there are pending ordered
7486 * extents. Drop our locks and wait for them to finish
7487 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007488 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7489 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007490 unlock_extent_cached(io_tree, page_start, page_end,
7491 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007492 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007493 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007494 btrfs_put_ordered_extent(ordered);
7495 goto again;
7496 }
7497
Josef Bacikfbf19082009-10-01 17:10:23 -04007498 /*
7499 * XXX - page_mkwrite gets called every time the page is dirtied, even
7500 * if it was already dirty, so for space accounting reasons we need to
7501 * clear any delalloc bits for the range we are fixing to save. There
7502 * is probably a better way to do this, but for now keep consistent with
7503 * prepare_pages in the normal write path.
7504 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007505 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007506 EXTENT_DIRTY | EXTENT_DELALLOC |
7507 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007508 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007509
Josef Bacik2ac55d42010-02-03 19:33:23 +00007510 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7511 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007512 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007513 unlock_extent_cached(io_tree, page_start, page_end,
7514 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007515 ret = VM_FAULT_SIGBUS;
7516 goto out_unlock;
7517 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007518 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007519
7520 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007521 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007522 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007523 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007524 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007525
Chris Masone6dcd2d2008-07-17 12:53:50 -04007526 if (zero_start != PAGE_CACHE_SIZE) {
7527 kaddr = kmap(page);
7528 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7529 flush_dcache_page(page);
7530 kunmap(page);
7531 }
Chris Mason247e7432008-07-17 12:53:51 -04007532 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007533 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007534 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007535
Chris Mason257c62e2009-10-13 13:21:08 -04007536 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7537 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007538 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007539
Josef Bacik2ac55d42010-02-03 19:33:23 +00007540 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007541
7542out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007543 if (!ret) {
7544 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007545 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007546 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007547 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007548out:
Josef Bacikec39e182012-01-12 19:10:12 -05007549 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007550out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007551 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007552 return ret;
7553}
7554
Josef Bacika41ad392011-01-31 15:30:16 -05007555static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007556{
7557 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007558 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007559 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007560 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007561 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007562 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007563 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007564
Chris Mason4a096752008-07-21 10:29:44 -04007565 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04007566
Josef Bacikfcb80c22011-05-03 10:40:22 -04007567 /*
7568 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7569 * 3 things going on here
7570 *
7571 * 1) We need to reserve space for our orphan item and the space to
7572 * delete our orphan item. Lord knows we don't want to have a dangling
7573 * orphan item because we didn't reserve space to remove it.
7574 *
7575 * 2) We need to reserve space to update our inode.
7576 *
7577 * 3) We need to have something to cache all the space that is going to
7578 * be free'd up by the truncate operation, but also have some slack
7579 * space reserved in case it uses space during the truncate (thank you
7580 * very much snapshotting).
7581 *
7582 * And we need these to all be seperate. The fact is we can use alot of
7583 * space doing the truncate, and we have no earthly idea how much space
7584 * we will use, so we need the truncate reservation to be seperate so it
7585 * doesn't end up using space reserved for updating the inode or
7586 * removing the orphan item. We also need to be able to stop the
7587 * transaction and start a new one, which means we need to be able to
7588 * update the inode several times, and we have no idea of knowing how
7589 * many times that will be, so we can't just reserve 1 item for the
7590 * entirety of the opration, so that has to be done seperately as well.
7591 * Then there is the orphan item, which does indeed need to be held on
7592 * to for the whole operation, and we need nobody to touch this reserved
7593 * space except the orphan code.
7594 *
7595 * So that leaves us with
7596 *
7597 * 1) root->orphan_block_rsv - for the orphan deletion.
7598 * 2) rsv - for the truncate reservation, which we will steal from the
7599 * transaction reservation.
7600 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7601 * updating the inode.
7602 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007603 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007604 if (!rsv)
7605 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007606 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007607 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007608
Josef Bacik907cbce2011-08-08 13:46:15 -04007609 /*
Josef Bacik07127182011-08-19 10:29:59 -04007610 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007611 * 1 for updating the inode.
7612 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007613 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007614 if (IS_ERR(trans)) {
7615 err = PTR_ERR(trans);
7616 goto out;
7617 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007618
Josef Bacik907cbce2011-08-08 13:46:15 -04007619 /* Migrate the slack space for the truncate to our reserve */
7620 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7621 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007622 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007623
Chris Mason5a3f23d2009-03-31 13:27:11 -04007624 /*
7625 * setattr is responsible for setting the ordered_data_close flag,
7626 * but that is only tested during the last file release. That
7627 * could happen well after the next commit, leaving a great big
7628 * window where new writes may get lost if someone chooses to write
7629 * to this file after truncating to zero
7630 *
7631 * The inode doesn't have any dirty data here, and so if we commit
7632 * this is a noop. If someone immediately starts writing to the inode
7633 * it is very likely we'll catch some of their writes in this
7634 * transaction, and the commit will find this file on the ordered
7635 * data list with good things to send down.
7636 *
7637 * This is a best effort solution, there is still a window where
7638 * using truncate to replace the contents of the file will
7639 * end up with a zero length file after a crash.
7640 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007641 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7642 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007643 btrfs_add_ordered_operation(trans, root, inode);
7644
Josef Bacik5dc562c2012-08-17 13:14:17 -04007645 /*
7646 * So if we truncate and then write and fsync we normally would just
7647 * write the extents that changed, which is a problem if we need to
7648 * first truncate that entire inode. So set this flag so we write out
7649 * all of the extents in the inode to the sync log so we're completely
7650 * safe.
7651 */
7652 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007653 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007654
Yan, Zheng80825102009-11-12 09:35:36 +00007655 while (1) {
7656 ret = btrfs_truncate_inode_items(trans, root, inode,
7657 inode->i_size,
7658 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007659 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007660 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007661 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007662 }
Chris Mason39279cc2007-06-12 06:35:45 -04007663
Josef Bacikfcb80c22011-05-03 10:40:22 -04007664 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007665 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007666 if (ret) {
7667 err = ret;
7668 break;
7669 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007670
Yan, Zheng80825102009-11-12 09:35:36 +00007671 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007672 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007673
7674 trans = btrfs_start_transaction(root, 2);
7675 if (IS_ERR(trans)) {
7676 ret = err = PTR_ERR(trans);
7677 trans = NULL;
7678 break;
7679 }
7680
7681 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7682 rsv, min_size);
7683 BUG_ON(ret); /* shouldn't happen */
7684 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007685 }
7686
7687 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007688 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007689 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007690 if (ret)
7691 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007692 }
7693
Chris Mason917c16b2011-11-08 14:49:59 -05007694 if (trans) {
7695 trans->block_rsv = &root->fs_info->trans_block_rsv;
7696 ret = btrfs_update_inode(trans, root, inode);
7697 if (ret && !err)
7698 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007699
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007700 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007701 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007702 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007703
7704out:
7705 btrfs_free_block_rsv(root, rsv);
7706
Josef Bacik3893e332011-01-31 16:03:11 -05007707 if (ret && !err)
7708 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007709
Josef Bacik3893e332011-01-31 16:03:11 -05007710 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007711}
7712
Sven Wegener3b963622008-06-09 21:57:42 -04007713/*
Chris Masond352ac62008-09-29 15:18:18 -04007714 * create a new subvolume directory/inode (helper for the ioctl).
7715 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007716int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007717 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007718{
Chris Mason39279cc2007-06-12 06:35:45 -04007719 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007720 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007721 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007722
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007723 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7724 new_dirid, new_dirid,
7725 S_IFDIR | (~current_umask() & S_IRWXUGO),
7726 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007727 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007728 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007729 inode->i_op = &btrfs_dir_inode_operations;
7730 inode->i_fop = &btrfs_dir_file_operations;
7731
Miklos Szeredibfe86842011-10-28 14:13:29 +02007732 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007733 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007734
Yan, Zheng76dda932009-09-21 16:00:26 -04007735 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007736
Yan, Zheng76dda932009-09-21 16:00:26 -04007737 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007738 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007739}
7740
Chris Mason39279cc2007-06-12 06:35:45 -04007741struct inode *btrfs_alloc_inode(struct super_block *sb)
7742{
7743 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007744 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007745
7746 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7747 if (!ei)
7748 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007749
7750 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007751 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007752 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007753 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007754 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007755 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007756 ei->disk_i_size = 0;
7757 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007758 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007759 ei->index_cnt = (u64)-1;
7760 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007761 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007762
Josef Bacik9e0baf62011-07-15 15:16:44 +00007763 spin_lock_init(&ei->lock);
7764 ei->outstanding_extents = 0;
7765 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007766
Josef Bacik72ac3c02012-05-23 14:13:11 -04007767 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007768 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007769
Miao Xie16cdcec2011-04-22 18:12:22 +08007770 ei->delayed_node = NULL;
7771
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007772 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007773 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007774 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7775 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007776 ei->io_tree.track_uptodate = 1;
7777 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007778 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007779 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007780 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007781 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007782 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007783 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007784 RB_CLEAR_NODE(&ei->rb_node);
7785
7786 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007787}
7788
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007789static void btrfs_i_callback(struct rcu_head *head)
7790{
7791 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007792 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7793}
7794
Chris Mason39279cc2007-06-12 06:35:45 -04007795void btrfs_destroy_inode(struct inode *inode)
7796{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007797 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007798 struct btrfs_root *root = BTRFS_I(inode)->root;
7799
Al Virob3d9b7a2012-06-09 13:51:19 -04007800 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007801 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007802 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7803 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007804 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7805 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007806
Chris Mason5a3f23d2009-03-31 13:27:11 -04007807 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007808 * This can happen where we create an inode, but somebody else also
7809 * created the same inode and we need to destroy the one we already
7810 * created.
7811 */
7812 if (!root)
7813 goto free;
7814
7815 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007816 * Make sure we're properly removed from the ordered operation
7817 * lists.
7818 */
7819 smp_mb();
7820 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
Miao Xie199c2a92013-05-15 07:48:23 +00007821 spin_lock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007822 list_del_init(&BTRFS_I(inode)->ordered_operations);
Miao Xie199c2a92013-05-15 07:48:23 +00007823 spin_unlock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007824 }
7825
Josef Bacik8a35d952012-05-23 14:26:42 -04007826 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7827 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007828 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007829 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007830 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007831 }
Josef Bacik7b128762008-07-24 12:17:14 -04007832
Chris Masond3977122009-01-05 21:25:51 -05007833 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007834 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7835 if (!ordered)
7836 break;
7837 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007838 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007839 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007840 btrfs_remove_ordered_extent(inode, ordered);
7841 btrfs_put_ordered_extent(ordered);
7842 btrfs_put_ordered_extent(ordered);
7843 }
7844 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007845 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007846 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007847free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007848 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007849}
7850
Al Viro45321ac2010-06-07 13:43:19 -04007851int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007852{
7853 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007854
Naohiro Aota6379ef92013-06-06 09:56:34 +00007855 if (root == NULL)
7856 return 1;
7857
Liu Bofa6ac872013-02-20 14:10:23 +00007858 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02007859 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04007860 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007861 else
Al Viro45321ac2010-06-07 13:43:19 -04007862 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007863}
7864
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007865static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007866{
7867 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7868
7869 inode_init_once(&ei->vfs_inode);
7870}
7871
7872void btrfs_destroy_cachep(void)
7873{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007874 /*
7875 * Make sure all delayed rcu free inodes are flushed before we
7876 * destroy cache.
7877 */
7878 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007879 if (btrfs_inode_cachep)
7880 kmem_cache_destroy(btrfs_inode_cachep);
7881 if (btrfs_trans_handle_cachep)
7882 kmem_cache_destroy(btrfs_trans_handle_cachep);
7883 if (btrfs_transaction_cachep)
7884 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007885 if (btrfs_path_cachep)
7886 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007887 if (btrfs_free_space_cachep)
7888 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007889 if (btrfs_delalloc_work_cachep)
7890 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007891}
7892
7893int btrfs_init_cachep(void)
7894{
David Sterba837e1972012-09-07 03:00:48 -06007895 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007896 sizeof(struct btrfs_inode), 0,
7897 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007898 if (!btrfs_inode_cachep)
7899 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007900
David Sterba837e1972012-09-07 03:00:48 -06007901 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007902 sizeof(struct btrfs_trans_handle), 0,
7903 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007904 if (!btrfs_trans_handle_cachep)
7905 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007906
David Sterba837e1972012-09-07 03:00:48 -06007907 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007908 sizeof(struct btrfs_transaction), 0,
7909 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007910 if (!btrfs_transaction_cachep)
7911 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007912
David Sterba837e1972012-09-07 03:00:48 -06007913 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007914 sizeof(struct btrfs_path), 0,
7915 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007916 if (!btrfs_path_cachep)
7917 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007918
David Sterba837e1972012-09-07 03:00:48 -06007919 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007920 sizeof(struct btrfs_free_space), 0,
7921 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7922 if (!btrfs_free_space_cachep)
7923 goto fail;
7924
Miao Xie8ccf6f192012-10-25 09:28:04 +00007925 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7926 sizeof(struct btrfs_delalloc_work), 0,
7927 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7928 NULL);
7929 if (!btrfs_delalloc_work_cachep)
7930 goto fail;
7931
Chris Mason39279cc2007-06-12 06:35:45 -04007932 return 0;
7933fail:
7934 btrfs_destroy_cachep();
7935 return -ENOMEM;
7936}
7937
7938static int btrfs_getattr(struct vfsmount *mnt,
7939 struct dentry *dentry, struct kstat *stat)
7940{
Miao Xiedf0af1a52013-01-29 10:11:59 +00007941 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04007942 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007943 u32 blocksize = inode->i_sb->s_blocksize;
7944
Chris Mason39279cc2007-06-12 06:35:45 -04007945 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007946 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007947 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a52013-01-29 10:11:59 +00007948
7949 spin_lock(&BTRFS_I(inode)->lock);
7950 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
7951 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05007952 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a52013-01-29 10:11:59 +00007953 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007954 return 0;
7955}
7956
Chris Masond3977122009-01-05 21:25:51 -05007957static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7958 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007959{
7960 struct btrfs_trans_handle *trans;
7961 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007962 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007963 struct inode *new_inode = new_dentry->d_inode;
7964 struct inode *old_inode = old_dentry->d_inode;
7965 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007966 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007967 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007968 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007969 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007970
Li Zefan33345d012011-04-20 10:31:50 +08007971 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007972 return -EPERM;
7973
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007974 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007975 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007976 return -EXDEV;
7977
Li Zefan33345d012011-04-20 10:31:50 +08007978 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7979 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007980 return -ENOTEMPTY;
7981
Chris Mason39279cc2007-06-12 06:35:45 -04007982 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007983 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007984 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05007985
7986
7987 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04007988 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05007989 new_dentry->d_name.name,
7990 new_dentry->d_name.len);
7991
7992 if (ret) {
7993 if (ret == -EEXIST) {
7994 /* we shouldn't get
7995 * eexist without a new_inode */
7996 if (!new_inode) {
7997 WARN_ON(1);
7998 return ret;
7999 }
8000 } else {
8001 /* maybe -EOVERFLOW */
8002 return ret;
8003 }
8004 }
8005 ret = 0;
8006
Chris Mason5a3f23d2009-03-31 13:27:11 -04008007 /*
8008 * we're using rename to replace one file with another.
8009 * and the replacement file is large. Start IO on it now so
8010 * we don't add too much work to the end of the transaction
8011 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008012 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008013 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8014 filemap_flush(old_inode->i_mapping);
8015
Yan, Zheng76dda932009-09-21 16:00:26 -04008016 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008017 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008018 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008019 /*
8020 * We want to reserve the absolute worst case amount of items. So if
8021 * both inodes are subvols and we need to unlink them then that would
8022 * require 4 item modifications, but if they are both normal inodes it
8023 * would require 5 item modifications, so we'll assume their normal
8024 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8025 * should cover the worst case number of items we'll modify.
8026 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008027 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008028 if (IS_ERR(trans)) {
8029 ret = PTR_ERR(trans);
8030 goto out_notrans;
8031 }
Chris Mason5f39d392007-10-15 16:14:19 -04008032
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008033 if (dest != root)
8034 btrfs_record_root_in_trans(trans, dest);
8035
Yan, Zhenga5719522009-09-24 09:17:31 -04008036 ret = btrfs_set_inode_index(new_dir, &index);
8037 if (ret)
8038 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008039
Li Zefan33345d012011-04-20 10:31:50 +08008040 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008041 /* force full log commit if subvolume involved. */
8042 root->fs_info->last_trans_log_full_commit = trans->transid;
8043 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008044 ret = btrfs_insert_inode_ref(trans, dest,
8045 new_dentry->d_name.name,
8046 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008047 old_ino,
8048 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008049 if (ret)
8050 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008051 /*
8052 * this is an ugly little race, but the rename is required
8053 * to make sure that if we crash, the inode is either at the
8054 * old name or the new one. pinning the log transaction lets
8055 * us make sure we don't allow a log commit to come in after
8056 * we unlink the name but before we add the new name back in.
8057 */
8058 btrfs_pin_log_trans(root);
8059 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008060 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008061 * make sure the inode gets flushed if it is replacing
8062 * something.
8063 */
Li Zefan33345d012011-04-20 10:31:50 +08008064 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008065 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008066
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008067 inode_inc_iversion(old_dir);
8068 inode_inc_iversion(new_dir);
8069 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008070 old_dir->i_ctime = old_dir->i_mtime = ctime;
8071 new_dir->i_ctime = new_dir->i_mtime = ctime;
8072 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008073
Chris Mason12fcfd22009-03-24 10:24:20 -04008074 if (old_dentry->d_parent != new_dentry->d_parent)
8075 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8076
Li Zefan33345d012011-04-20 10:31:50 +08008077 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008078 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8079 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8080 old_dentry->d_name.name,
8081 old_dentry->d_name.len);
8082 } else {
Al Viro92986792011-03-04 17:14:37 +00008083 ret = __btrfs_unlink_inode(trans, root, old_dir,
8084 old_dentry->d_inode,
8085 old_dentry->d_name.name,
8086 old_dentry->d_name.len);
8087 if (!ret)
8088 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008089 }
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
8095 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008096 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008097 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008098 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008099 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8100 root_objectid = BTRFS_I(new_inode)->location.objectid;
8101 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8102 root_objectid,
8103 new_dentry->d_name.name,
8104 new_dentry->d_name.len);
8105 BUG_ON(new_inode->i_nlink == 0);
8106 } else {
8107 ret = btrfs_unlink_inode(trans, dest, new_dir,
8108 new_dentry->d_inode,
8109 new_dentry->d_name.name,
8110 new_dentry->d_name.len);
8111 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008112 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008113 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008114 if (ret) {
8115 btrfs_abort_transaction(trans, root, ret);
8116 goto out_fail;
8117 }
Chris Mason39279cc2007-06-12 06:35:45 -04008118 }
Josef Bacikaec74772008-07-24 12:12:38 -04008119
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008120 ret = btrfs_add_link(trans, new_dir, old_inode,
8121 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008122 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008123 if (ret) {
8124 btrfs_abort_transaction(trans, root, ret);
8125 goto out_fail;
8126 }
Chris Mason39279cc2007-06-12 06:35:45 -04008127
Li Zefan33345d012011-04-20 10:31:50 +08008128 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008129 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008130 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008131 btrfs_end_log_trans(root);
8132 }
Chris Mason39279cc2007-06-12 06:35:45 -04008133out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008134 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008135out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008136 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008137 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008138
Chris Mason39279cc2007-06-12 06:35:45 -04008139 return ret;
8140}
8141
Miao Xie8ccf6f192012-10-25 09:28:04 +00008142static void btrfs_run_delalloc_work(struct btrfs_work *work)
8143{
8144 struct btrfs_delalloc_work *delalloc_work;
8145
8146 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8147 work);
8148 if (delalloc_work->wait)
8149 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8150 else
8151 filemap_flush(delalloc_work->inode->i_mapping);
8152
8153 if (delalloc_work->delay_iput)
8154 btrfs_add_delayed_iput(delalloc_work->inode);
8155 else
8156 iput(delalloc_work->inode);
8157 complete(&delalloc_work->completion);
8158}
8159
8160struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8161 int wait, int delay_iput)
8162{
8163 struct btrfs_delalloc_work *work;
8164
8165 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8166 if (!work)
8167 return NULL;
8168
8169 init_completion(&work->completion);
8170 INIT_LIST_HEAD(&work->list);
8171 work->inode = inode;
8172 work->wait = wait;
8173 work->delay_iput = delay_iput;
8174 work->work.func = btrfs_run_delalloc_work;
8175
8176 return work;
8177}
8178
8179void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8180{
8181 wait_for_completion(&work->completion);
8182 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8183}
8184
Chris Masond352ac62008-09-29 15:18:18 -04008185/*
8186 * some fairly slow code that needs optimization. This walks the list
8187 * of all the inodes with pending delalloc and forces them to disk.
8188 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00008189static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008190{
Chris Masonea8c2812008-08-04 23:17:27 -04008191 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008192 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008193 struct btrfs_delalloc_work *work, *next;
8194 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008195 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008196 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008197
Miao Xie8ccf6f192012-10-25 09:28:04 +00008198 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008199 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008200
Miao Xieeb73c1b2013-05-15 07:48:22 +00008201 spin_lock(&root->delalloc_lock);
8202 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008203 while (!list_empty(&splice)) {
8204 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008205 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008206
Miao Xieeb73c1b2013-05-15 07:48:22 +00008207 list_move_tail(&binode->delalloc_inodes,
8208 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008209 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a52013-01-29 10:11:59 +00008210 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008211 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008212 continue;
Miao Xiedf0af1a52013-01-29 10:11:59 +00008213 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008214 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008215
8216 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8217 if (unlikely(!work)) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04008218 if (delay_iput)
8219 btrfs_add_delayed_iput(inode);
8220 else
8221 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008222 ret = -ENOMEM;
8223 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008224 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008225 list_add_tail(&work->list, &works);
8226 btrfs_queue_worker(&root->fs_info->flush_workers,
8227 &work->work);
8228
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008229 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008230 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008231 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008232 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008233
Miao Xie1eafa6c2013-01-22 10:49:00 +00008234 list_for_each_entry_safe(work, next, &works, list) {
8235 list_del_init(&work->list);
8236 btrfs_wait_and_free_delalloc_work(work);
8237 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008238 return 0;
8239out:
8240 list_for_each_entry_safe(work, next, &works, list) {
8241 list_del_init(&work->list);
8242 btrfs_wait_and_free_delalloc_work(work);
8243 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008244
Miao Xieeb73c1b2013-05-15 07:48:22 +00008245 if (!list_empty_careful(&splice)) {
8246 spin_lock(&root->delalloc_lock);
8247 list_splice_tail(&splice, &root->delalloc_inodes);
8248 spin_unlock(&root->delalloc_lock);
8249 }
8250 return ret;
8251}
8252
8253int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8254{
8255 int ret;
8256
8257 if (root->fs_info->sb->s_flags & MS_RDONLY)
8258 return -EROFS;
8259
8260 ret = __start_delalloc_inodes(root, delay_iput);
8261 /*
8262 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008263 * we have to make sure the IO is actually started and that
8264 * ordered extents get created before we return
8265 */
8266 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008267 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008268 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008269 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008270 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8271 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008272 }
8273 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008274 return ret;
8275}
8276
8277int btrfs_start_all_delalloc_inodes(struct btrfs_fs_info *fs_info,
8278 int delay_iput)
8279{
8280 struct btrfs_root *root;
8281 struct list_head splice;
8282 int ret;
8283
8284 if (fs_info->sb->s_flags & MS_RDONLY)
8285 return -EROFS;
8286
8287 INIT_LIST_HEAD(&splice);
8288
8289 spin_lock(&fs_info->delalloc_root_lock);
8290 list_splice_init(&fs_info->delalloc_roots, &splice);
8291 while (!list_empty(&splice)) {
8292 root = list_first_entry(&splice, struct btrfs_root,
8293 delalloc_root);
8294 root = btrfs_grab_fs_root(root);
8295 BUG_ON(!root);
8296 list_move_tail(&root->delalloc_root,
8297 &fs_info->delalloc_roots);
8298 spin_unlock(&fs_info->delalloc_root_lock);
8299
8300 ret = __start_delalloc_inodes(root, delay_iput);
8301 btrfs_put_fs_root(root);
8302 if (ret)
8303 goto out;
8304
8305 spin_lock(&fs_info->delalloc_root_lock);
8306 }
8307 spin_unlock(&fs_info->delalloc_root_lock);
8308
8309 atomic_inc(&fs_info->async_submit_draining);
8310 while (atomic_read(&fs_info->nr_async_submits) ||
8311 atomic_read(&fs_info->async_delalloc_pages)) {
8312 wait_event(fs_info->async_submit_wait,
8313 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8314 atomic_read(&fs_info->async_delalloc_pages) == 0));
8315 }
8316 atomic_dec(&fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008317 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008318out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008319 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008320 spin_lock(&fs_info->delalloc_root_lock);
8321 list_splice_tail(&splice, &fs_info->delalloc_roots);
8322 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008323 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008324 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008325}
8326
Chris Mason39279cc2007-06-12 06:35:45 -04008327static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8328 const char *symname)
8329{
8330 struct btrfs_trans_handle *trans;
8331 struct btrfs_root *root = BTRFS_I(dir)->root;
8332 struct btrfs_path *path;
8333 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008334 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008335 int err;
8336 int drop_inode = 0;
8337 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008338 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008339 int name_len;
8340 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008341 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008342 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008343 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008344
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008345 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04008346 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8347 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008348
Josef Bacik9ed74f22009-09-11 16:12:44 -04008349 /*
8350 * 2 items for inode item and ref
8351 * 2 items for dir items
8352 * 1 item for xattr if selinux is on
8353 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008354 trans = btrfs_start_transaction(root, 5);
8355 if (IS_ERR(trans))
8356 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008357
Li Zefan581bb052011-04-20 10:06:11 +08008358 err = btrfs_find_free_ino(root, &objectid);
8359 if (err)
8360 goto out_unlock;
8361
Josef Bacikaec74772008-07-24 12:12:38 -04008362 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008363 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008364 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008365 if (IS_ERR(inode)) {
8366 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008367 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008368 }
Chris Mason39279cc2007-06-12 06:35:45 -04008369
Eric Paris2a7dba32011-02-01 11:05:39 -05008370 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008371 if (err) {
8372 drop_inode = 1;
8373 goto out_unlock;
8374 }
8375
Casey Schauflerad19db72011-12-15 10:09:07 -05008376 /*
8377 * If the active LSM wants to access the inode during
8378 * d_instantiate it needs these. Smack checks to see
8379 * if the filesystem supports xattrs by looking at the
8380 * ops vector.
8381 */
8382 inode->i_fop = &btrfs_file_operations;
8383 inode->i_op = &btrfs_file_inode_operations;
8384
Josef Bacika1b075d2010-11-19 20:36:11 +00008385 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008386 if (err)
8387 drop_inode = 1;
8388 else {
8389 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008390 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008391 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008392 }
Chris Mason39279cc2007-06-12 06:35:45 -04008393 if (drop_inode)
8394 goto out_unlock;
8395
8396 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008397 if (!path) {
8398 err = -ENOMEM;
8399 drop_inode = 1;
8400 goto out_unlock;
8401 }
Li Zefan33345d012011-04-20 10:31:50 +08008402 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008403 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008404 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8405 datasize = btrfs_file_extent_calc_inline_size(name_len);
8406 err = btrfs_insert_empty_item(trans, root, path, &key,
8407 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008408 if (err) {
8409 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008410 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008411 goto out_unlock;
8412 }
Chris Mason5f39d392007-10-15 16:14:19 -04008413 leaf = path->nodes[0];
8414 ei = btrfs_item_ptr(leaf, path->slots[0],
8415 struct btrfs_file_extent_item);
8416 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8417 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008418 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008419 btrfs_set_file_extent_encryption(leaf, ei, 0);
8420 btrfs_set_file_extent_compression(leaf, ei, 0);
8421 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8422 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8423
Chris Mason39279cc2007-06-12 06:35:45 -04008424 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008425 write_extent_buffer(leaf, symname, ptr, name_len);
8426 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008427 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008428
Chris Mason39279cc2007-06-12 06:35:45 -04008429 inode->i_op = &btrfs_symlink_inode_operations;
8430 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008431 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008432 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008433 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04008434 err = btrfs_update_inode(trans, root, inode);
8435 if (err)
8436 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008437
8438out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008439 if (!err)
8440 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008441 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008442 if (drop_inode) {
8443 inode_dec_link_count(inode);
8444 iput(inode);
8445 }
Liu Bob53d3f52012-11-14 14:34:34 +00008446 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008447 return err;
8448}
Chris Mason16432982008-04-10 10:23:21 -04008449
Josef Bacik0af3d002010-06-21 14:48:16 -04008450static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8451 u64 start, u64 num_bytes, u64 min_size,
8452 loff_t actual_len, u64 *alloc_hint,
8453 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008454{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008455 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8456 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008457 struct btrfs_root *root = BTRFS_I(inode)->root;
8458 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008459 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008460 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008461 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008462 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008463 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008464
Josef Bacik0af3d002010-06-21 14:48:16 -04008465 if (trans)
8466 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008467 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008468 if (own_trans) {
8469 trans = btrfs_start_transaction(root, 3);
8470 if (IS_ERR(trans)) {
8471 ret = PTR_ERR(trans);
8472 break;
8473 }
Yan Zhengd899e052008-10-30 14:25:28 -04008474 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008475
Chris Mason154ea282013-03-05 11:11:26 -05008476 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8477 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04008478 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
8479 *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008480 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008481 if (own_trans)
8482 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008483 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008484 }
8485
Yan Zhengd899e052008-10-30 14:25:28 -04008486 ret = insert_reserved_file_extent(trans, inode,
8487 cur_offset, ins.objectid,
8488 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008489 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008490 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008491 if (ret) {
8492 btrfs_abort_transaction(trans, root, ret);
8493 if (own_trans)
8494 btrfs_end_transaction(trans, root);
8495 break;
8496 }
Chris Masona1ed8352009-09-11 12:27:37 -04008497 btrfs_drop_extent_cache(inode, cur_offset,
8498 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008499
Josef Bacik5dc562c2012-08-17 13:14:17 -04008500 em = alloc_extent_map();
8501 if (!em) {
8502 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8503 &BTRFS_I(inode)->runtime_flags);
8504 goto next;
8505 }
8506
8507 em->start = cur_offset;
8508 em->orig_start = cur_offset;
8509 em->len = ins.offset;
8510 em->block_start = ins.objectid;
8511 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008512 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008513 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008514 em->bdev = root->fs_info->fs_devices->latest_bdev;
8515 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8516 em->generation = trans->transid;
8517
8518 while (1) {
8519 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008520 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008521 write_unlock(&em_tree->lock);
8522 if (ret != -EEXIST)
8523 break;
8524 btrfs_drop_extent_cache(inode, cur_offset,
8525 cur_offset + ins.offset - 1,
8526 0);
8527 }
8528 free_extent_map(em);
8529next:
Yan Zhengd899e052008-10-30 14:25:28 -04008530 num_bytes -= ins.offset;
8531 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008532 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008533
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008534 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008535 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008536 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008537 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008538 (actual_len > inode->i_size) &&
8539 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008540 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008541 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008542 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008543 i_size = cur_offset;
8544 i_size_write(inode, i_size);
8545 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008546 }
8547
Yan Zhengd899e052008-10-30 14:25:28 -04008548 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008549
8550 if (ret) {
8551 btrfs_abort_transaction(trans, root, ret);
8552 if (own_trans)
8553 btrfs_end_transaction(trans, root);
8554 break;
8555 }
Yan Zhengd899e052008-10-30 14:25:28 -04008556
Josef Bacik0af3d002010-06-21 14:48:16 -04008557 if (own_trans)
8558 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008559 }
Yan Zhengd899e052008-10-30 14:25:28 -04008560 return ret;
8561}
8562
Josef Bacik0af3d002010-06-21 14:48:16 -04008563int btrfs_prealloc_file_range(struct inode *inode, int mode,
8564 u64 start, u64 num_bytes, u64 min_size,
8565 loff_t actual_len, u64 *alloc_hint)
8566{
8567 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8568 min_size, actual_len, alloc_hint,
8569 NULL);
8570}
8571
8572int btrfs_prealloc_file_range_trans(struct inode *inode,
8573 struct btrfs_trans_handle *trans, int mode,
8574 u64 start, u64 num_bytes, u64 min_size,
8575 loff_t actual_len, u64 *alloc_hint)
8576{
8577 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8578 min_size, actual_len, alloc_hint, trans);
8579}
8580
Chris Masone6dcd2d2008-07-17 12:53:50 -04008581static int btrfs_set_page_dirty(struct page *page)
8582{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008583 return __set_page_dirty_nobuffers(page);
8584}
8585
Al Viro10556cb2011-06-20 19:28:19 -04008586static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008587{
Li Zefanb83cc962010-12-20 16:04:08 +08008588 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008589 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008590
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008591 if (mask & MAY_WRITE &&
8592 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8593 if (btrfs_root_readonly(root))
8594 return -EROFS;
8595 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8596 return -EACCES;
8597 }
Al Viro2830ba72011-06-20 19:16:29 -04008598 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008599}
Chris Mason39279cc2007-06-12 06:35:45 -04008600
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008601static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008602 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008603 .lookup = btrfs_lookup,
8604 .create = btrfs_create,
8605 .unlink = btrfs_unlink,
8606 .link = btrfs_link,
8607 .mkdir = btrfs_mkdir,
8608 .rmdir = btrfs_rmdir,
8609 .rename = btrfs_rename,
8610 .symlink = btrfs_symlink,
8611 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008612 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008613 .setxattr = btrfs_setxattr,
8614 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008615 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008616 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008617 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008618 .get_acl = btrfs_get_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07008619 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008620};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008621static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008622 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008623 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008624 .get_acl = btrfs_get_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07008625 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008626};
Yan, Zheng76dda932009-09-21 16:00:26 -04008627
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008628static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008629 .llseek = generic_file_llseek,
8630 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04008631 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008632 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008633#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008634 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008635#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008636 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008637 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008638};
8639
Chris Masond1310b22008-01-24 16:13:08 -05008640static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008641 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008642 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008643 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008644 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008645 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008646 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008647 .set_bit_hook = btrfs_set_bit_hook,
8648 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008649 .merge_extent_hook = btrfs_merge_extent_hook,
8650 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008651};
8652
Chris Mason35054392009-01-21 13:11:13 -05008653/*
8654 * btrfs doesn't support the bmap operation because swapfiles
8655 * use bmap to make a mapping of extents in the file. They assume
8656 * these extents won't change over the life of the file and they
8657 * use the bmap result to do IO directly to the drive.
8658 *
8659 * the btrfs bmap call would return logical addresses that aren't
8660 * suitable for IO and they also will change frequently as COW
8661 * operations happen. So, swapfile + btrfs == corruption.
8662 *
8663 * For now we're avoiding this by dropping bmap.
8664 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008665static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008666 .readpage = btrfs_readpage,
8667 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008668 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008669 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008670 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008671 .invalidatepage = btrfs_invalidatepage,
8672 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008673 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008674 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008675};
8676
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008677static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008678 .readpage = btrfs_readpage,
8679 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008680 .invalidatepage = btrfs_invalidatepage,
8681 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008682};
8683
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008684static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008685 .getattr = btrfs_getattr,
8686 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008687 .setxattr = btrfs_setxattr,
8688 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008689 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008690 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008691 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008692 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008693 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008694 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008695};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008696static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008697 .getattr = btrfs_getattr,
8698 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008699 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008700 .setxattr = btrfs_setxattr,
8701 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008702 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008703 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008704 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008705 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008706};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008707static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008708 .readlink = generic_readlink,
8709 .follow_link = page_follow_link_light,
8710 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008711 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008712 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008713 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008714 .setxattr = btrfs_setxattr,
8715 .getxattr = btrfs_getxattr,
8716 .listxattr = btrfs_listxattr,
8717 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008718 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008719 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008720};
Yan, Zheng76dda932009-09-21 16:00:26 -04008721
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008722const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008723 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008724 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008725};