blob: 1ca49eaba3bb4aee7e18aba92e38bbc500e71e69 [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, Zhengc2167752009-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, Zhengc2167752009-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 Masonf03d9301f2009-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 Masonf03d9301f2009-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, Zhengc2167752009-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, Zhengc2167752009-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 Bacik7a7eaa42011-04-13 12:54:33 -04001158 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001159 else
Josef Bacik7a7eaa42011-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 Xiedf0af1a2013-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 Xiedf0af1a2013-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 Xiedf0af1a2013-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
Josef Bacikb6d08f02013-09-27 14:57:43 -04001554 /*
1555 * We don't reserve metadata space for space cache inodes so we
1556 * don't need to call dellalloc_release_metadata if there is an
1557 * error.
1558 */
1559 if (*bits & EXTENT_DO_ACCOUNTING &&
1560 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001561 btrfs_delalloc_release_metadata(inode, len);
1562
Josef Bacik0cb59c92010-07-02 12:14:14 -04001563 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001564 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001565 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001566
Miao Xie963d6782013-01-29 10:10:51 +00001567 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1568 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001569 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001570 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001571 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001572 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001573 &BTRFS_I(inode)->runtime_flags))
1574 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001575 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001576 }
Chris Mason291d6732008-01-29 15:55:23 -05001577}
1578
Chris Masond352ac62008-09-29 15:18:18 -04001579/*
1580 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1581 * we don't create bios that span stripes or chunks
1582 */
David Woodhouse64a16702009-07-15 23:29:37 +01001583int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001584 size_t size, struct bio *bio,
1585 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001586{
1587 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001588 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001589 u64 length = 0;
1590 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001591 int ret;
1592
Chris Mason771ed682008-11-06 22:02:51 -05001593 if (bio_flags & EXTENT_BIO_COMPRESSED)
1594 return 0;
1595
Chris Masonf2d8d742008-04-21 10:03:05 -04001596 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001597 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001598 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001599 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001600 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001601 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001602 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001603 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001604 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001605}
1606
Chris Masond352ac62008-09-29 15:18:18 -04001607/*
1608 * in order to insert checksums into the metadata in large chunks,
1609 * we wait until bio submission time. All the pages in the bio are
1610 * checksummed and sums are attached onto the ordered extent record.
1611 *
1612 * At IO completion time the cums attached on the ordered extent record
1613 * are inserted into the btree
1614 */
Chris Masond3977122009-01-05 21:25:51 -05001615static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1616 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001617 unsigned long bio_flags,
1618 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001619{
Chris Mason065631f2008-02-20 12:07:25 -05001620 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001621 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001622
Chris Masond20f7042008-12-08 16:58:54 -05001623 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001624 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001625 return 0;
1626}
Chris Masone0156402008-04-16 11:15:20 -04001627
Chris Mason4a69a412008-11-06 22:03:00 -05001628/*
1629 * in order to insert checksums into the metadata in large chunks,
1630 * we wait until bio submission time. All the pages in the bio are
1631 * checksummed and sums are attached onto the ordered extent record.
1632 *
1633 * At IO completion time the cums attached on the ordered extent record
1634 * are inserted into the btree
1635 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001636static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001637 int mirror_num, unsigned long bio_flags,
1638 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001639{
1640 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001641 int ret;
1642
1643 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1644 if (ret)
1645 bio_endio(bio, ret);
1646 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001647}
1648
Chris Masond352ac62008-09-29 15:18:18 -04001649/*
Chris Masoncad321a2008-12-17 14:51:42 -05001650 * extent_io.c submission hook. This does the right thing for csum calculation
1651 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001652 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001653static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001654 int mirror_num, unsigned long bio_flags,
1655 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001656{
1657 struct btrfs_root *root = BTRFS_I(inode)->root;
1658 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001659 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001660 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001661 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001662
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001663 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001664
Liu Bo83eea1f2012-07-10 05:28:39 -06001665 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001666 metadata = 2;
1667
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001668 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001669 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1670 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001671 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001672
Chris Masond20f7042008-12-08 16:58:54 -05001673 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001674 ret = btrfs_submit_compressed_read(inode, bio,
1675 mirror_num,
1676 bio_flags);
1677 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001678 } else if (!skip_sum) {
1679 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1680 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001681 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001682 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001683 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001684 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001685 /* csum items have already been cloned */
1686 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1687 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001688 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001689 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001690 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001691 bio_flags, bio_offset,
1692 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001693 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001694 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001695 } else if (!skip_sum) {
1696 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1697 if (ret)
1698 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001699 }
1700
Chris Mason0b86a832008-03-24 15:01:56 -04001701mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001702 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1703
1704out:
1705 if (ret < 0)
1706 bio_endio(bio, ret);
1707 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001708}
Chris Mason6885f302008-02-20 16:11:05 -05001709
Chris Masond352ac62008-09-29 15:18:18 -04001710/*
1711 * given a list of ordered sums record them in the inode. This happens
1712 * at IO completion time based on sums calculated at bio submission time.
1713 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001714static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001715 struct inode *inode, u64 file_offset,
1716 struct list_head *list)
1717{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001718 struct btrfs_ordered_sum *sum;
1719
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001720 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001721 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001722 btrfs_csum_file_blocks(trans,
1723 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001724 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001725 }
1726 return 0;
1727}
1728
Josef Bacik2ac55d42010-02-03 19:33:23 +00001729int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1730 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001731{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001732 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001733 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001734 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001735}
1736
Chris Masond352ac62008-09-29 15:18:18 -04001737/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001738struct btrfs_writepage_fixup {
1739 struct page *page;
1740 struct btrfs_work work;
1741};
1742
Christoph Hellwigb2950862008-12-02 09:54:17 -05001743static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001744{
1745 struct btrfs_writepage_fixup *fixup;
1746 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001747 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001748 struct page *page;
1749 struct inode *inode;
1750 u64 page_start;
1751 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001752 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001753
1754 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1755 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001756again:
Chris Mason247e7432008-07-17 12:53:51 -04001757 lock_page(page);
1758 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1759 ClearPageChecked(page);
1760 goto out_page;
1761 }
1762
1763 inode = page->mapping->host;
1764 page_start = page_offset(page);
1765 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1766
Josef Bacik2ac55d42010-02-03 19:33:23 +00001767 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001768 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001769
1770 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001771 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001772 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001773
1774 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1775 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001776 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1777 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001778 unlock_page(page);
1779 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001780 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001781 goto again;
1782 }
Chris Mason247e7432008-07-17 12:53:51 -04001783
Jeff Mahoney87826df2012-02-15 16:23:57 +01001784 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1785 if (ret) {
1786 mapping_set_error(page->mapping, ret);
1787 end_extent_writepage(page, ret, page_start, page_end);
1788 ClearPageChecked(page);
1789 goto out;
1790 }
1791
Josef Bacik2ac55d42010-02-03 19:33:23 +00001792 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001793 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001794 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001795out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001796 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1797 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001798out_page:
1799 unlock_page(page);
1800 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001801 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001802}
1803
1804/*
1805 * There are a few paths in the higher layers of the kernel that directly
1806 * set the page dirty bit without asking the filesystem if it is a
1807 * good idea. This causes problems because we want to make sure COW
1808 * properly happens and the data=ordered rules are followed.
1809 *
Chris Masonc8b97812008-10-29 14:49:59 -04001810 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001811 * hasn't been properly setup for IO. We kick off an async process
1812 * to fix it up. The async helper will wait for ordered extents, set
1813 * the delalloc bit and make it safe to write the page.
1814 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001815static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001816{
1817 struct inode *inode = page->mapping->host;
1818 struct btrfs_writepage_fixup *fixup;
1819 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001820
Chris Mason8b62b722009-09-02 16:53:46 -04001821 /* this page is properly in the ordered list */
1822 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001823 return 0;
1824
1825 if (PageChecked(page))
1826 return -EAGAIN;
1827
1828 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1829 if (!fixup)
1830 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001831
Chris Mason247e7432008-07-17 12:53:51 -04001832 SetPageChecked(page);
1833 page_cache_get(page);
1834 fixup->work.func = btrfs_writepage_fixup_worker;
1835 fixup->page = page;
1836 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001837 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001838}
1839
Yan Zhengd899e052008-10-30 14:25:28 -04001840static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1841 struct inode *inode, u64 file_pos,
1842 u64 disk_bytenr, u64 disk_num_bytes,
1843 u64 num_bytes, u64 ram_bytes,
1844 u8 compression, u8 encryption,
1845 u16 other_encoding, int extent_type)
1846{
1847 struct btrfs_root *root = BTRFS_I(inode)->root;
1848 struct btrfs_file_extent_item *fi;
1849 struct btrfs_path *path;
1850 struct extent_buffer *leaf;
1851 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001852 int ret;
1853
1854 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001855 if (!path)
1856 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001857
Chris Masonb9473432009-03-13 11:00:37 -04001858 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001859
1860 /*
1861 * we may be replacing one extent in the tree with another.
1862 * The new extent is pinned in the extent map, and we don't want
1863 * to drop it from the cache until it is completely in the btree.
1864 *
1865 * So, tell btrfs_drop_extents to leave this extent in the cache.
1866 * the caller is expected to unpin it and allow it to be merged
1867 * with the others.
1868 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001869 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001870 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001871 if (ret)
1872 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001873
Li Zefan33345d012011-04-20 10:31:50 +08001874 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001875 ins.offset = file_pos;
1876 ins.type = BTRFS_EXTENT_DATA_KEY;
1877 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001878 if (ret)
1879 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001880 leaf = path->nodes[0];
1881 fi = btrfs_item_ptr(leaf, path->slots[0],
1882 struct btrfs_file_extent_item);
1883 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1884 btrfs_set_file_extent_type(leaf, fi, extent_type);
1885 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1886 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1887 btrfs_set_file_extent_offset(leaf, fi, 0);
1888 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1889 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1890 btrfs_set_file_extent_compression(leaf, fi, compression);
1891 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1892 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001893
Yan Zhengd899e052008-10-30 14:25:28 -04001894 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001895 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001896
1897 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001898
1899 ins.objectid = disk_bytenr;
1900 ins.offset = disk_num_bytes;
1901 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001902 ret = btrfs_alloc_reserved_file_extent(trans, root,
1903 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001904 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001905out:
Yan Zhengd899e052008-10-30 14:25:28 -04001906 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001907
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001908 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001909}
1910
Liu Bo38c227d2013-01-29 03:18:40 +00001911/* snapshot-aware defrag */
1912struct sa_defrag_extent_backref {
1913 struct rb_node node;
1914 struct old_sa_defrag_extent *old;
1915 u64 root_id;
1916 u64 inum;
1917 u64 file_pos;
1918 u64 extent_offset;
1919 u64 num_bytes;
1920 u64 generation;
1921};
1922
1923struct old_sa_defrag_extent {
1924 struct list_head list;
1925 struct new_sa_defrag_extent *new;
1926
1927 u64 extent_offset;
1928 u64 bytenr;
1929 u64 offset;
1930 u64 len;
1931 int count;
1932};
1933
1934struct new_sa_defrag_extent {
1935 struct rb_root root;
1936 struct list_head head;
1937 struct btrfs_path *path;
1938 struct inode *inode;
1939 u64 file_pos;
1940 u64 len;
1941 u64 bytenr;
1942 u64 disk_len;
1943 u8 compress_type;
1944};
1945
1946static int backref_comp(struct sa_defrag_extent_backref *b1,
1947 struct sa_defrag_extent_backref *b2)
1948{
1949 if (b1->root_id < b2->root_id)
1950 return -1;
1951 else if (b1->root_id > b2->root_id)
1952 return 1;
1953
1954 if (b1->inum < b2->inum)
1955 return -1;
1956 else if (b1->inum > b2->inum)
1957 return 1;
1958
1959 if (b1->file_pos < b2->file_pos)
1960 return -1;
1961 else if (b1->file_pos > b2->file_pos)
1962 return 1;
1963
1964 /*
1965 * [------------------------------] ===> (a range of space)
1966 * |<--->| |<---->| =============> (fs/file tree A)
1967 * |<---------------------------->| ===> (fs/file tree B)
1968 *
1969 * A range of space can refer to two file extents in one tree while
1970 * refer to only one file extent in another tree.
1971 *
1972 * So we may process a disk offset more than one time(two extents in A)
1973 * and locate at the same extent(one extent in B), then insert two same
1974 * backrefs(both refer to the extent in B).
1975 */
1976 return 0;
1977}
1978
1979static void backref_insert(struct rb_root *root,
1980 struct sa_defrag_extent_backref *backref)
1981{
1982 struct rb_node **p = &root->rb_node;
1983 struct rb_node *parent = NULL;
1984 struct sa_defrag_extent_backref *entry;
1985 int ret;
1986
1987 while (*p) {
1988 parent = *p;
1989 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
1990
1991 ret = backref_comp(backref, entry);
1992 if (ret < 0)
1993 p = &(*p)->rb_left;
1994 else
1995 p = &(*p)->rb_right;
1996 }
1997
1998 rb_link_node(&backref->node, parent, p);
1999 rb_insert_color(&backref->node, root);
2000}
2001
2002/*
2003 * Note the backref might has changed, and in this case we just return 0.
2004 */
2005static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2006 void *ctx)
2007{
2008 struct btrfs_file_extent_item *extent;
2009 struct btrfs_fs_info *fs_info;
2010 struct old_sa_defrag_extent *old = ctx;
2011 struct new_sa_defrag_extent *new = old->new;
2012 struct btrfs_path *path = new->path;
2013 struct btrfs_key key;
2014 struct btrfs_root *root;
2015 struct sa_defrag_extent_backref *backref;
2016 struct extent_buffer *leaf;
2017 struct inode *inode = new->inode;
2018 int slot;
2019 int ret;
2020 u64 extent_offset;
2021 u64 num_bytes;
2022
2023 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2024 inum == btrfs_ino(inode))
2025 return 0;
2026
2027 key.objectid = root_id;
2028 key.type = BTRFS_ROOT_ITEM_KEY;
2029 key.offset = (u64)-1;
2030
2031 fs_info = BTRFS_I(inode)->root->fs_info;
2032 root = btrfs_read_fs_root_no_name(fs_info, &key);
2033 if (IS_ERR(root)) {
2034 if (PTR_ERR(root) == -ENOENT)
2035 return 0;
2036 WARN_ON(1);
2037 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2038 inum, offset, root_id);
2039 return PTR_ERR(root);
2040 }
2041
2042 key.objectid = inum;
2043 key.type = BTRFS_EXTENT_DATA_KEY;
2044 if (offset > (u64)-1 << 32)
2045 key.offset = 0;
2046 else
2047 key.offset = offset;
2048
2049 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2050 if (ret < 0) {
2051 WARN_ON(1);
2052 return ret;
2053 }
Josef Bacik50f13192013-07-22 12:50:37 -04002054 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002055
2056 while (1) {
2057 cond_resched();
2058
2059 leaf = path->nodes[0];
2060 slot = path->slots[0];
2061
2062 if (slot >= btrfs_header_nritems(leaf)) {
2063 ret = btrfs_next_leaf(root, path);
2064 if (ret < 0) {
2065 goto out;
2066 } else if (ret > 0) {
2067 ret = 0;
2068 goto out;
2069 }
2070 continue;
2071 }
2072
2073 path->slots[0]++;
2074
2075 btrfs_item_key_to_cpu(leaf, &key, slot);
2076
2077 if (key.objectid > inum)
2078 goto out;
2079
2080 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2081 continue;
2082
2083 extent = btrfs_item_ptr(leaf, slot,
2084 struct btrfs_file_extent_item);
2085
2086 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2087 continue;
2088
Liu Boe68afa42013-07-01 22:13:26 +08002089 /*
2090 * 'offset' refers to the exact key.offset,
2091 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2092 * (key.offset - extent_offset).
2093 */
2094 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002095 continue;
2096
Liu Boe68afa42013-07-01 22:13:26 +08002097 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002098 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002099
Liu Bo38c227d2013-01-29 03:18:40 +00002100 if (extent_offset >= old->extent_offset + old->offset +
2101 old->len || extent_offset + num_bytes <=
2102 old->extent_offset + old->offset)
2103 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002104 break;
2105 }
2106
2107 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2108 if (!backref) {
2109 ret = -ENOENT;
2110 goto out;
2111 }
2112
2113 backref->root_id = root_id;
2114 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002115 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002116 backref->num_bytes = num_bytes;
2117 backref->extent_offset = extent_offset;
2118 backref->generation = btrfs_file_extent_generation(leaf, extent);
2119 backref->old = old;
2120 backref_insert(&new->root, backref);
2121 old->count++;
2122out:
2123 btrfs_release_path(path);
2124 WARN_ON(ret);
2125 return ret;
2126}
2127
2128static noinline bool record_extent_backrefs(struct btrfs_path *path,
2129 struct new_sa_defrag_extent *new)
2130{
2131 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2132 struct old_sa_defrag_extent *old, *tmp;
2133 int ret;
2134
2135 new->path = path;
2136
2137 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002138 ret = iterate_inodes_from_logical(old->bytenr +
2139 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002140 path, record_one_backref,
2141 old);
2142 BUG_ON(ret < 0 && ret != -ENOENT);
2143
2144 /* no backref to be processed for this extent */
2145 if (!old->count) {
2146 list_del(&old->list);
2147 kfree(old);
2148 }
2149 }
2150
2151 if (list_empty(&new->head))
2152 return false;
2153
2154 return true;
2155}
2156
2157static int relink_is_mergable(struct extent_buffer *leaf,
2158 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002159 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002160{
Liu Bo116e0022013-08-02 16:30:40 +08002161 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002162 return 0;
2163
2164 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2165 return 0;
2166
Liu Bo116e0022013-08-02 16:30:40 +08002167 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2168 return 0;
2169
2170 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002171 btrfs_file_extent_other_encoding(leaf, fi))
2172 return 0;
2173
2174 return 1;
2175}
2176
2177/*
2178 * Note the backref might has changed, and in this case we just return 0.
2179 */
2180static noinline int relink_extent_backref(struct btrfs_path *path,
2181 struct sa_defrag_extent_backref *prev,
2182 struct sa_defrag_extent_backref *backref)
2183{
2184 struct btrfs_file_extent_item *extent;
2185 struct btrfs_file_extent_item *item;
2186 struct btrfs_ordered_extent *ordered;
2187 struct btrfs_trans_handle *trans;
2188 struct btrfs_fs_info *fs_info;
2189 struct btrfs_root *root;
2190 struct btrfs_key key;
2191 struct extent_buffer *leaf;
2192 struct old_sa_defrag_extent *old = backref->old;
2193 struct new_sa_defrag_extent *new = old->new;
2194 struct inode *src_inode = new->inode;
2195 struct inode *inode;
2196 struct extent_state *cached = NULL;
2197 int ret = 0;
2198 u64 start;
2199 u64 len;
2200 u64 lock_start;
2201 u64 lock_end;
2202 bool merge = false;
2203 int index;
2204
2205 if (prev && prev->root_id == backref->root_id &&
2206 prev->inum == backref->inum &&
2207 prev->file_pos + prev->num_bytes == backref->file_pos)
2208 merge = true;
2209
2210 /* step 1: get root */
2211 key.objectid = backref->root_id;
2212 key.type = BTRFS_ROOT_ITEM_KEY;
2213 key.offset = (u64)-1;
2214
2215 fs_info = BTRFS_I(src_inode)->root->fs_info;
2216 index = srcu_read_lock(&fs_info->subvol_srcu);
2217
2218 root = btrfs_read_fs_root_no_name(fs_info, &key);
2219 if (IS_ERR(root)) {
2220 srcu_read_unlock(&fs_info->subvol_srcu, index);
2221 if (PTR_ERR(root) == -ENOENT)
2222 return 0;
2223 return PTR_ERR(root);
2224 }
Liu Bo38c227d2013-01-29 03:18:40 +00002225
2226 /* step 2: get inode */
2227 key.objectid = backref->inum;
2228 key.type = BTRFS_INODE_ITEM_KEY;
2229 key.offset = 0;
2230
2231 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2232 if (IS_ERR(inode)) {
2233 srcu_read_unlock(&fs_info->subvol_srcu, index);
2234 return 0;
2235 }
2236
2237 srcu_read_unlock(&fs_info->subvol_srcu, index);
2238
2239 /* step 3: relink backref */
2240 lock_start = backref->file_pos;
2241 lock_end = backref->file_pos + backref->num_bytes - 1;
2242 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2243 0, &cached);
2244
2245 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2246 if (ordered) {
2247 btrfs_put_ordered_extent(ordered);
2248 goto out_unlock;
2249 }
2250
2251 trans = btrfs_join_transaction(root);
2252 if (IS_ERR(trans)) {
2253 ret = PTR_ERR(trans);
2254 goto out_unlock;
2255 }
2256
2257 key.objectid = backref->inum;
2258 key.type = BTRFS_EXTENT_DATA_KEY;
2259 key.offset = backref->file_pos;
2260
2261 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2262 if (ret < 0) {
2263 goto out_free_path;
2264 } else if (ret > 0) {
2265 ret = 0;
2266 goto out_free_path;
2267 }
2268
2269 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2270 struct btrfs_file_extent_item);
2271
2272 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2273 backref->generation)
2274 goto out_free_path;
2275
2276 btrfs_release_path(path);
2277
2278 start = backref->file_pos;
2279 if (backref->extent_offset < old->extent_offset + old->offset)
2280 start += old->extent_offset + old->offset -
2281 backref->extent_offset;
2282
2283 len = min(backref->extent_offset + backref->num_bytes,
2284 old->extent_offset + old->offset + old->len);
2285 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2286
2287 ret = btrfs_drop_extents(trans, root, inode, start,
2288 start + len, 1);
2289 if (ret)
2290 goto out_free_path;
2291again:
2292 key.objectid = btrfs_ino(inode);
2293 key.type = BTRFS_EXTENT_DATA_KEY;
2294 key.offset = start;
2295
Liu Boa09a0a72013-03-11 09:20:58 +00002296 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002297 if (merge) {
2298 struct btrfs_file_extent_item *fi;
2299 u64 extent_len;
2300 struct btrfs_key found_key;
2301
2302 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2303 if (ret < 0)
2304 goto out_free_path;
2305
2306 path->slots[0]--;
2307 leaf = path->nodes[0];
2308 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2309
2310 fi = btrfs_item_ptr(leaf, path->slots[0],
2311 struct btrfs_file_extent_item);
2312 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2313
Liu Bo116e0022013-08-02 16:30:40 +08002314 if (extent_len + found_key.offset == start &&
2315 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002316 btrfs_set_file_extent_num_bytes(leaf, fi,
2317 extent_len + len);
2318 btrfs_mark_buffer_dirty(leaf);
2319 inode_add_bytes(inode, len);
2320
2321 ret = 1;
2322 goto out_free_path;
2323 } else {
2324 merge = false;
2325 btrfs_release_path(path);
2326 goto again;
2327 }
2328 }
2329
2330 ret = btrfs_insert_empty_item(trans, root, path, &key,
2331 sizeof(*extent));
2332 if (ret) {
2333 btrfs_abort_transaction(trans, root, ret);
2334 goto out_free_path;
2335 }
2336
2337 leaf = path->nodes[0];
2338 item = btrfs_item_ptr(leaf, path->slots[0],
2339 struct btrfs_file_extent_item);
2340 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2341 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2342 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2343 btrfs_set_file_extent_num_bytes(leaf, item, len);
2344 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2345 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2346 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2347 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2348 btrfs_set_file_extent_encryption(leaf, item, 0);
2349 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2350
2351 btrfs_mark_buffer_dirty(leaf);
2352 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002353 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002354
2355 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2356 new->disk_len, 0,
2357 backref->root_id, backref->inum,
2358 new->file_pos, 0); /* start - extent_offset */
2359 if (ret) {
2360 btrfs_abort_transaction(trans, root, ret);
2361 goto out_free_path;
2362 }
2363
2364 ret = 1;
2365out_free_path:
2366 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002367 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002368 btrfs_end_transaction(trans, root);
2369out_unlock:
2370 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2371 &cached, GFP_NOFS);
2372 iput(inode);
2373 return ret;
2374}
2375
2376static void relink_file_extents(struct new_sa_defrag_extent *new)
2377{
2378 struct btrfs_path *path;
2379 struct old_sa_defrag_extent *old, *tmp;
2380 struct sa_defrag_extent_backref *backref;
2381 struct sa_defrag_extent_backref *prev = NULL;
2382 struct inode *inode;
2383 struct btrfs_root *root;
2384 struct rb_node *node;
2385 int ret;
2386
2387 inode = new->inode;
2388 root = BTRFS_I(inode)->root;
2389
2390 path = btrfs_alloc_path();
2391 if (!path)
2392 return;
2393
2394 if (!record_extent_backrefs(path, new)) {
2395 btrfs_free_path(path);
2396 goto out;
2397 }
2398 btrfs_release_path(path);
2399
2400 while (1) {
2401 node = rb_first(&new->root);
2402 if (!node)
2403 break;
2404 rb_erase(node, &new->root);
2405
2406 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2407
2408 ret = relink_extent_backref(path, prev, backref);
2409 WARN_ON(ret < 0);
2410
2411 kfree(prev);
2412
2413 if (ret == 1)
2414 prev = backref;
2415 else
2416 prev = NULL;
2417 cond_resched();
2418 }
2419 kfree(prev);
2420
2421 btrfs_free_path(path);
2422
2423 list_for_each_entry_safe(old, tmp, &new->head, list) {
2424 list_del(&old->list);
2425 kfree(old);
2426 }
2427out:
2428 atomic_dec(&root->fs_info->defrag_running);
2429 wake_up(&root->fs_info->transaction_wait);
2430
2431 kfree(new);
2432}
2433
2434static struct new_sa_defrag_extent *
2435record_old_file_extents(struct inode *inode,
2436 struct btrfs_ordered_extent *ordered)
2437{
2438 struct btrfs_root *root = BTRFS_I(inode)->root;
2439 struct btrfs_path *path;
2440 struct btrfs_key key;
2441 struct old_sa_defrag_extent *old, *tmp;
2442 struct new_sa_defrag_extent *new;
2443 int ret;
2444
2445 new = kmalloc(sizeof(*new), GFP_NOFS);
2446 if (!new)
2447 return NULL;
2448
2449 new->inode = inode;
2450 new->file_pos = ordered->file_offset;
2451 new->len = ordered->len;
2452 new->bytenr = ordered->start;
2453 new->disk_len = ordered->disk_len;
2454 new->compress_type = ordered->compress_type;
2455 new->root = RB_ROOT;
2456 INIT_LIST_HEAD(&new->head);
2457
2458 path = btrfs_alloc_path();
2459 if (!path)
2460 goto out_kfree;
2461
2462 key.objectid = btrfs_ino(inode);
2463 key.type = BTRFS_EXTENT_DATA_KEY;
2464 key.offset = new->file_pos;
2465
2466 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2467 if (ret < 0)
2468 goto out_free_path;
2469 if (ret > 0 && path->slots[0] > 0)
2470 path->slots[0]--;
2471
2472 /* find out all the old extents for the file range */
2473 while (1) {
2474 struct btrfs_file_extent_item *extent;
2475 struct extent_buffer *l;
2476 int slot;
2477 u64 num_bytes;
2478 u64 offset;
2479 u64 end;
2480 u64 disk_bytenr;
2481 u64 extent_offset;
2482
2483 l = path->nodes[0];
2484 slot = path->slots[0];
2485
2486 if (slot >= btrfs_header_nritems(l)) {
2487 ret = btrfs_next_leaf(root, path);
2488 if (ret < 0)
2489 goto out_free_list;
2490 else if (ret > 0)
2491 break;
2492 continue;
2493 }
2494
2495 btrfs_item_key_to_cpu(l, &key, slot);
2496
2497 if (key.objectid != btrfs_ino(inode))
2498 break;
2499 if (key.type != BTRFS_EXTENT_DATA_KEY)
2500 break;
2501 if (key.offset >= new->file_pos + new->len)
2502 break;
2503
2504 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2505
2506 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2507 if (key.offset + num_bytes < new->file_pos)
2508 goto next;
2509
2510 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2511 if (!disk_bytenr)
2512 goto next;
2513
2514 extent_offset = btrfs_file_extent_offset(l, extent);
2515
2516 old = kmalloc(sizeof(*old), GFP_NOFS);
2517 if (!old)
2518 goto out_free_list;
2519
2520 offset = max(new->file_pos, key.offset);
2521 end = min(new->file_pos + new->len, key.offset + num_bytes);
2522
2523 old->bytenr = disk_bytenr;
2524 old->extent_offset = extent_offset;
2525 old->offset = offset - key.offset;
2526 old->len = end - offset;
2527 old->new = new;
2528 old->count = 0;
2529 list_add_tail(&old->list, &new->head);
2530next:
2531 path->slots[0]++;
2532 cond_resched();
2533 }
2534
2535 btrfs_free_path(path);
2536 atomic_inc(&root->fs_info->defrag_running);
2537
2538 return new;
2539
2540out_free_list:
2541 list_for_each_entry_safe(old, tmp, &new->head, list) {
2542 list_del(&old->list);
2543 kfree(old);
2544 }
2545out_free_path:
2546 btrfs_free_path(path);
2547out_kfree:
2548 kfree(new);
2549 return NULL;
2550}
2551
Chris Mason5d13a982009-03-13 11:41:46 -04002552/*
2553 * helper function for btrfs_finish_ordered_io, this
2554 * just reads in some of the csum leaves to prime them into ram
2555 * before we start the transaction. It limits the amount of btree
2556 * reads required while inside the transaction.
2557 */
Chris Masond352ac62008-09-29 15:18:18 -04002558/* as ordered data IO finishes, this gets called so we can finish
2559 * an ordered extent if the range of bytes in the file it covers are
2560 * fully written.
2561 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002562static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002563{
Josef Bacik5fd02042012-05-02 14:00:54 -04002564 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002565 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002566 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002567 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002568 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002569 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002570 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002571 int ret = 0;
2572 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002573 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002574 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002575
Liu Bo83eea1f2012-07-10 05:28:39 -06002576 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002577
Josef Bacik5fd02042012-05-02 14:00:54 -04002578 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2579 ret = -EIO;
2580 goto out;
2581 }
2582
Josef Bacik77cef2e2013-08-29 13:57:21 -04002583 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2584 truncated = true;
2585 logical_len = ordered_extent->truncated_len;
2586 /* Truncated the entire extent, don't bother adding */
2587 if (!logical_len)
2588 goto out;
2589 }
2590
Yan, Zhengc2167752009-11-12 09:34:21 +00002591 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002592 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002593 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2594 if (nolock)
2595 trans = btrfs_join_transaction_nolock(root);
2596 else
2597 trans = btrfs_join_transaction(root);
2598 if (IS_ERR(trans)) {
2599 ret = PTR_ERR(trans);
2600 trans = NULL;
2601 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002602 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002603 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2604 ret = btrfs_update_inode_fallback(trans, root, inode);
2605 if (ret) /* -ENOMEM or corruption */
2606 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002607 goto out;
2608 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002609
Josef Bacik2ac55d42010-02-03 19:33:23 +00002610 lock_extent_bits(io_tree, ordered_extent->file_offset,
2611 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002612 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002613
Liu Bo38c227d2013-01-29 03:18:40 +00002614 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2615 ordered_extent->file_offset + ordered_extent->len - 1,
2616 EXTENT_DEFRAG, 1, cached_state);
2617 if (ret) {
2618 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2619 if (last_snapshot >= BTRFS_I(inode)->generation)
2620 /* the inode is shared */
2621 new = record_old_file_extents(inode, ordered_extent);
2622
2623 clear_extent_bit(io_tree, ordered_extent->file_offset,
2624 ordered_extent->file_offset + ordered_extent->len - 1,
2625 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2626 }
2627
Josef Bacik0cb59c92010-07-02 12:14:14 -04002628 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002629 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002630 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002631 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002632 if (IS_ERR(trans)) {
2633 ret = PTR_ERR(trans);
2634 trans = NULL;
2635 goto out_unlock;
2636 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002637 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002638
Chris Masonc8b97812008-10-29 14:49:59 -04002639 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002640 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002641 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002642 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002643 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002644 ordered_extent->file_offset,
2645 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002646 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002647 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002648 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002649 ret = insert_reserved_file_extent(trans, inode,
2650 ordered_extent->file_offset,
2651 ordered_extent->start,
2652 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002653 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002654 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002655 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002656 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002657 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2658 ordered_extent->file_offset, ordered_extent->len,
2659 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002660 if (ret < 0) {
2661 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002662 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002663 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002664
Chris Masone6dcd2d2008-07-17 12:53:50 -04002665 add_pending_csums(trans, inode, ordered_extent->file_offset,
2666 &ordered_extent->list);
2667
Josef Bacik6c760c02012-11-09 10:53:21 -05002668 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2669 ret = btrfs_update_inode_fallback(trans, root, inode);
2670 if (ret) { /* -ENOMEM or corruption */
2671 btrfs_abort_transaction(trans, root, ret);
2672 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002673 }
2674 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002675out_unlock:
2676 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2677 ordered_extent->file_offset +
2678 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002679out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002680 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002681 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002682 if (trans)
2683 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002684
Josef Bacik77cef2e2013-08-29 13:57:21 -04002685 if (ret || truncated) {
2686 u64 start, end;
2687
2688 if (truncated)
2689 start = ordered_extent->file_offset + logical_len;
2690 else
2691 start = ordered_extent->file_offset;
2692 end = ordered_extent->file_offset + ordered_extent->len - 1;
2693 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2694
2695 /* Drop the cache for the part of the extent we didn't write. */
2696 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002697
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002698 /*
2699 * If the ordered extent had an IOERR or something else went
2700 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002701 * back to the allocator. We only free the extent in the
2702 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002703 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002704 if ((ret || !logical_len) &&
2705 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002706 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2707 btrfs_free_reserved_extent(root, ordered_extent->start,
2708 ordered_extent->disk_len);
2709 }
2710
2711
Josef Bacik5fd02042012-05-02 14:00:54 -04002712 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002713 * This needs to be done to make sure anybody waiting knows we are done
2714 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002715 */
2716 btrfs_remove_ordered_extent(inode, ordered_extent);
2717
Liu Bo38c227d2013-01-29 03:18:40 +00002718 /* for snapshot-aware defrag */
2719 if (new)
2720 relink_file_extents(new);
2721
Chris Masone6dcd2d2008-07-17 12:53:50 -04002722 /* once for us */
2723 btrfs_put_ordered_extent(ordered_extent);
2724 /* once for the tree */
2725 btrfs_put_ordered_extent(ordered_extent);
2726
Josef Bacik5fd02042012-05-02 14:00:54 -04002727 return ret;
2728}
2729
2730static void finish_ordered_fn(struct btrfs_work *work)
2731{
2732 struct btrfs_ordered_extent *ordered_extent;
2733 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2734 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002735}
2736
Christoph Hellwigb2950862008-12-02 09:54:17 -05002737static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002738 struct extent_state *state, int uptodate)
2739{
Josef Bacik5fd02042012-05-02 14:00:54 -04002740 struct inode *inode = page->mapping->host;
2741 struct btrfs_root *root = BTRFS_I(inode)->root;
2742 struct btrfs_ordered_extent *ordered_extent = NULL;
2743 struct btrfs_workers *workers;
2744
liubo1abe9b82011-03-24 11:18:59 +00002745 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2746
Chris Mason8b62b722009-09-02 16:53:46 -04002747 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002748 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2749 end - start + 1, uptodate))
2750 return 0;
2751
2752 ordered_extent->work.func = finish_ordered_fn;
2753 ordered_extent->work.flags = 0;
2754
Liu Bo83eea1f2012-07-10 05:28:39 -06002755 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002756 workers = &root->fs_info->endio_freespace_worker;
2757 else
2758 workers = &root->fs_info->endio_write_workers;
2759 btrfs_queue_worker(workers, &ordered_extent->work);
2760
2761 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002762}
2763
Chris Masond352ac62008-09-29 15:18:18 -04002764/*
Chris Masond352ac62008-09-29 15:18:18 -04002765 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002766 * if there's a match, we allow the bio to finish. If not, the code in
2767 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002768 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002769static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2770 u64 phy_offset, struct page *page,
2771 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002772{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002773 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002774 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002775 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002776 char *kaddr;
Chris Masonff79f812007-10-15 16:22:25 -04002777 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiefacc8a222013-07-25 19:22:34 +08002778 u32 csum_expected;
Chris Masonff79f812007-10-15 16:22:25 -04002779 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002780 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2781 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002782
Chris Masond20f7042008-12-08 16:58:54 -05002783 if (PageChecked(page)) {
2784 ClearPageChecked(page);
2785 goto good;
2786 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002787
2788 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002789 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002790
Yan Zheng17d217f2008-12-12 10:03:38 -05002791 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002792 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002793 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2794 GFP_NOFS);
2795 return 0;
2796 }
2797
Miao Xiefacc8a222013-07-25 19:22:34 +08002798 phy_offset >>= inode->i_sb->s_blocksize_bits;
2799 csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
Chris Masond3977122009-01-05 21:25:51 -05002800
Miao Xiefacc8a222013-07-25 19:22:34 +08002801 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00002802 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002803 btrfs_csum_final(csum, (char *)&csum);
Miao Xiefacc8a222013-07-25 19:22:34 +08002804 if (csum != csum_expected)
Chris Mason07157aa2007-08-30 08:50:51 -04002805 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002806
Cong Wang7ac687d2011-11-25 23:14:28 +08002807 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002808good:
Chris Mason07157aa2007-08-30 08:50:51 -04002809 return 0;
2810
2811zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002812 if (__ratelimit(&_rs))
Miao Xiefacc8a222013-07-25 19:22:34 +08002813 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002814 btrfs_ino(page->mapping->host), start, csum, csum_expected);
Chris Masondb945352007-10-15 16:15:53 -04002815 memset(kaddr + offset, 1, end - start + 1);
2816 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002817 kunmap_atomic(kaddr);
Miao Xiefacc8a222013-07-25 19:22:34 +08002818 if (csum_expected == 0)
Chris Mason3b951512008-04-17 11:29:12 -04002819 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002820 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002821}
Chris Masonb888db22007-08-27 16:49:44 -04002822
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002823struct delayed_iput {
2824 struct list_head list;
2825 struct inode *inode;
2826};
2827
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002828/* JDM: If this is fs-wide, why can't we add a pointer to
2829 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002830void btrfs_add_delayed_iput(struct inode *inode)
2831{
2832 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2833 struct delayed_iput *delayed;
2834
2835 if (atomic_add_unless(&inode->i_count, -1, 1))
2836 return;
2837
2838 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2839 delayed->inode = inode;
2840
2841 spin_lock(&fs_info->delayed_iput_lock);
2842 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2843 spin_unlock(&fs_info->delayed_iput_lock);
2844}
2845
2846void btrfs_run_delayed_iputs(struct btrfs_root *root)
2847{
2848 LIST_HEAD(list);
2849 struct btrfs_fs_info *fs_info = root->fs_info;
2850 struct delayed_iput *delayed;
2851 int empty;
2852
2853 spin_lock(&fs_info->delayed_iput_lock);
2854 empty = list_empty(&fs_info->delayed_iputs);
2855 spin_unlock(&fs_info->delayed_iput_lock);
2856 if (empty)
2857 return;
2858
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002859 spin_lock(&fs_info->delayed_iput_lock);
2860 list_splice_init(&fs_info->delayed_iputs, &list);
2861 spin_unlock(&fs_info->delayed_iput_lock);
2862
2863 while (!list_empty(&list)) {
2864 delayed = list_entry(list.next, struct delayed_iput, list);
2865 list_del(&delayed->list);
2866 iput(delayed->inode);
2867 kfree(delayed);
2868 }
Yan, Zheng24bbcf042009-11-12 09:36:34 +00002869}
2870
Yan, Zhengd68fc572010-05-16 10:49:58 -04002871/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002872 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002873 * files in the subvolume, it removes orphan item and frees block_rsv
2874 * structure.
2875 */
2876void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2877 struct btrfs_root *root)
2878{
Josef Bacik90290e12011-12-02 15:44:12 -05002879 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002880 int ret;
2881
Josef Bacik8a35d952012-05-23 14:26:42 -04002882 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002883 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2884 return;
2885
Josef Bacik90290e12011-12-02 15:44:12 -05002886 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002887 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002888 spin_unlock(&root->orphan_lock);
2889 return;
2890 }
2891
2892 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2893 spin_unlock(&root->orphan_lock);
2894 return;
2895 }
2896
2897 block_rsv = root->orphan_block_rsv;
2898 root->orphan_block_rsv = NULL;
2899 spin_unlock(&root->orphan_lock);
2900
Yan, Zhengd68fc572010-05-16 10:49:58 -04002901 if (root->orphan_item_inserted &&
2902 btrfs_root_refs(&root->root_item) > 0) {
2903 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2904 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002905 if (ret)
2906 btrfs_abort_transaction(trans, root, ret);
2907 else
2908 root->orphan_item_inserted = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002909 }
2910
Josef Bacik90290e12011-12-02 15:44:12 -05002911 if (block_rsv) {
2912 WARN_ON(block_rsv->size > 0);
2913 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002914 }
2915}
2916
2917/*
Josef Bacik7b128762008-07-24 12:17:14 -04002918 * This creates an orphan entry for the given inode in case something goes
2919 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002920 *
2921 * NOTE: caller of this function should reserve 5 units of metadata for
2922 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002923 */
2924int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2925{
2926 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002927 struct btrfs_block_rsv *block_rsv = NULL;
2928 int reserve = 0;
2929 int insert = 0;
2930 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002931
Yan, Zhengd68fc572010-05-16 10:49:58 -04002932 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002933 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002934 if (!block_rsv)
2935 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002936 }
2937
Yan, Zhengd68fc572010-05-16 10:49:58 -04002938 spin_lock(&root->orphan_lock);
2939 if (!root->orphan_block_rsv) {
2940 root->orphan_block_rsv = block_rsv;
2941 } else if (block_rsv) {
2942 btrfs_free_block_rsv(root, block_rsv);
2943 block_rsv = NULL;
2944 }
Josef Bacik7b128762008-07-24 12:17:14 -04002945
Josef Bacik8a35d952012-05-23 14:26:42 -04002946 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2947 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002948#if 0
2949 /*
2950 * For proper ENOSPC handling, we should do orphan
2951 * cleanup when mounting. But this introduces backward
2952 * compatibility issue.
2953 */
2954 if (!xchg(&root->orphan_item_inserted, 1))
2955 insert = 2;
2956 else
2957 insert = 1;
2958#endif
2959 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002960 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002961 }
Josef Bacik7b128762008-07-24 12:17:14 -04002962
Josef Bacik72ac3c02012-05-23 14:13:11 -04002963 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2964 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002965 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002966 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002967
Yan, Zhengd68fc572010-05-16 10:49:58 -04002968 /* grab metadata reservation from transaction handle */
2969 if (reserve) {
2970 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002971 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002972 }
2973
2974 /* insert an orphan item to track this unlinked/truncated file */
2975 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002976 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04002977 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01002978 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002979 if (reserve) {
2980 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2981 &BTRFS_I(inode)->runtime_flags);
2982 btrfs_orphan_release_metadata(inode);
2983 }
2984 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04002985 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2986 &BTRFS_I(inode)->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04002987 btrfs_abort_transaction(trans, root, ret);
2988 return ret;
2989 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002990 }
2991 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002992 }
2993
2994 /* insert an orphan item to track subvolume contains orphan files */
2995 if (insert >= 2) {
2996 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2997 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002998 if (ret && ret != -EEXIST) {
2999 btrfs_abort_transaction(trans, root, ret);
3000 return ret;
3001 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003002 }
3003 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003004}
3005
3006/*
3007 * We have done the truncate/delete so we can go ahead and remove the orphan
3008 * item for this particular inode.
3009 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003010static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3011 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003012{
3013 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003014 int delete_item = 0;
3015 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003016 int ret = 0;
3017
Yan, Zhengd68fc572010-05-16 10:49:58 -04003018 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003019 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3020 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003021 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003022
Josef Bacik72ac3c02012-05-23 14:13:11 -04003023 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3024 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003025 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003026 spin_unlock(&root->orphan_lock);
3027
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003028 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003029 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003030 if (trans)
3031 ret = btrfs_del_orphan_item(trans, root,
3032 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003033 }
Josef Bacik7b128762008-07-24 12:17:14 -04003034
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003035 if (release_rsv)
3036 btrfs_orphan_release_metadata(inode);
3037
Josef Bacik4ef31a42013-08-13 14:10:08 -04003038 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003039}
3040
3041/*
3042 * this cleans up any orphans that may be left on the list from the last use
3043 * of this root.
3044 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003045int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003046{
3047 struct btrfs_path *path;
3048 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003049 struct btrfs_key key, found_key;
3050 struct btrfs_trans_handle *trans;
3051 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003052 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003053 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3054
Yan, Zhengd68fc572010-05-16 10:49:58 -04003055 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003056 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003057
3058 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003059 if (!path) {
3060 ret = -ENOMEM;
3061 goto out;
3062 }
Josef Bacik7b128762008-07-24 12:17:14 -04003063 path->reada = -1;
3064
3065 key.objectid = BTRFS_ORPHAN_OBJECTID;
3066 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3067 key.offset = (u64)-1;
3068
Josef Bacik7b128762008-07-24 12:17:14 -04003069 while (1) {
3070 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003071 if (ret < 0)
3072 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003073
3074 /*
3075 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003076 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003077 * find the key and see if we have stuff that matches
3078 */
3079 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003080 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003081 if (path->slots[0] == 0)
3082 break;
3083 path->slots[0]--;
3084 }
3085
3086 /* pull out the item */
3087 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003088 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3089
3090 /* make sure the item matches what we want */
3091 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3092 break;
3093 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3094 break;
3095
3096 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003097 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003098
3099 /*
3100 * this is where we are basically btrfs_lookup, without the
3101 * crossing root thing. we store the inode number in the
3102 * offset of the orphan item.
3103 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003104
3105 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003106 btrfs_err(root->fs_info,
3107 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003108 ret = -EINVAL;
3109 goto out;
3110 }
3111
3112 last_objectid = found_key.offset;
3113
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003114 found_key.objectid = found_key.offset;
3115 found_key.type = BTRFS_INODE_ITEM_KEY;
3116 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003117 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303118 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003119 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003120 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003121
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003122 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3123 struct btrfs_root *dead_root;
3124 struct btrfs_fs_info *fs_info = root->fs_info;
3125 int is_dead_root = 0;
3126
3127 /*
3128 * this is an orphan in the tree root. Currently these
3129 * could come from 2 sources:
3130 * a) a snapshot deletion in progress
3131 * b) a free space cache inode
3132 * We need to distinguish those two, as the snapshot
3133 * orphan must not get deleted.
3134 * find_dead_roots already ran before us, so if this
3135 * is a snapshot deletion, we should find the root
3136 * in the dead_roots list
3137 */
3138 spin_lock(&fs_info->trans_lock);
3139 list_for_each_entry(dead_root, &fs_info->dead_roots,
3140 root_list) {
3141 if (dead_root->root_key.objectid ==
3142 found_key.objectid) {
3143 is_dead_root = 1;
3144 break;
3145 }
3146 }
3147 spin_unlock(&fs_info->trans_lock);
3148 if (is_dead_root) {
3149 /* prevent this orphan from being found again */
3150 key.offset = found_key.objectid - 1;
3151 continue;
3152 }
3153 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003154 /*
3155 * Inode is already gone but the orphan item is still there,
3156 * kill the orphan item.
3157 */
3158 if (ret == -ESTALE) {
3159 trans = btrfs_start_transaction(root, 1);
3160 if (IS_ERR(trans)) {
3161 ret = PTR_ERR(trans);
3162 goto out;
3163 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003164 btrfs_debug(root->fs_info, "auto deleting %Lu",
3165 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003166 ret = btrfs_del_orphan_item(trans, root,
3167 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003168 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003169 if (ret)
3170 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003171 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003172 }
Josef Bacik7b128762008-07-24 12:17:14 -04003173
Josef Bacik7b128762008-07-24 12:17:14 -04003174 /*
3175 * add this inode to the orphan list so btrfs_orphan_del does
3176 * the proper thing when we hit it
3177 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003178 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3179 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003180 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003181
Josef Bacik7b128762008-07-24 12:17:14 -04003182 /* if we have links, this was a truncate, lets do that */
3183 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003184 if (!S_ISREG(inode->i_mode)) {
3185 WARN_ON(1);
3186 iput(inode);
3187 continue;
3188 }
Josef Bacik7b128762008-07-24 12:17:14 -04003189 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003190
3191 /* 1 for the orphan item deletion. */
3192 trans = btrfs_start_transaction(root, 1);
3193 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003194 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003195 ret = PTR_ERR(trans);
3196 goto out;
3197 }
3198 ret = btrfs_orphan_add(trans, inode);
3199 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003200 if (ret) {
3201 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003202 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003203 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003204
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003205 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003206 if (ret)
3207 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003208 } else {
3209 nr_unlink++;
3210 }
3211
3212 /* this will do delete_inode and everything for us */
3213 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003214 if (ret)
3215 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003216 }
Miao Xie3254c872011-11-10 20:45:05 -05003217 /* release the path since we're done with it */
3218 btrfs_release_path(path);
3219
Yan, Zhengd68fc572010-05-16 10:49:58 -04003220 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3221
3222 if (root->orphan_block_rsv)
3223 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3224 (u64)-1);
3225
3226 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003227 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003228 if (!IS_ERR(trans))
3229 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003230 }
Josef Bacik7b128762008-07-24 12:17:14 -04003231
3232 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003233 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003234 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003235 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003236
3237out:
3238 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003239 btrfs_crit(root->fs_info,
3240 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003241 btrfs_free_path(path);
3242 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003243}
3244
Chris Masond352ac62008-09-29 15:18:18 -04003245/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003246 * very simple check to peek ahead in the leaf looking for xattrs. If we
3247 * don't find any xattrs, we know there can't be any acls.
3248 *
3249 * slot is the slot the inode is in, objectid is the objectid of the inode
3250 */
3251static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3252 int slot, u64 objectid)
3253{
3254 u32 nritems = btrfs_header_nritems(leaf);
3255 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003256 static u64 xattr_access = 0;
3257 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003258 int scanned = 0;
3259
Josef Bacikf23b5a52013-06-19 10:16:26 -04003260 if (!xattr_access) {
3261 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3262 strlen(POSIX_ACL_XATTR_ACCESS));
3263 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3264 strlen(POSIX_ACL_XATTR_DEFAULT));
3265 }
3266
Chris Mason46a53cc2009-04-27 11:47:50 -04003267 slot++;
3268 while (slot < nritems) {
3269 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3270
3271 /* we found a different objectid, there must not be acls */
3272 if (found_key.objectid != objectid)
3273 return 0;
3274
3275 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003276 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3277 if (found_key.offset == xattr_access ||
3278 found_key.offset == xattr_default)
3279 return 1;
3280 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003281
3282 /*
3283 * we found a key greater than an xattr key, there can't
3284 * be any acls later on
3285 */
3286 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3287 return 0;
3288
3289 slot++;
3290 scanned++;
3291
3292 /*
3293 * it goes inode, inode backrefs, xattrs, extents,
3294 * so if there are a ton of hard links to an inode there can
3295 * be a lot of backrefs. Don't waste time searching too hard,
3296 * this is just an optimization
3297 */
3298 if (scanned >= 8)
3299 break;
3300 }
3301 /* we hit the end of the leaf before we found an xattr or
3302 * something larger than an xattr. We have to assume the inode
3303 * has acls
3304 */
3305 return 1;
3306}
3307
3308/*
Chris Masond352ac62008-09-29 15:18:18 -04003309 * read an inode from the btree into the in-memory inode
3310 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003311static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003312{
3313 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003314 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003315 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003316 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003317 struct btrfs_root *root = BTRFS_I(inode)->root;
3318 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003319 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003320 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003321 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003322 bool filled = false;
3323
3324 ret = btrfs_fill_inode(inode, &rdev);
3325 if (!ret)
3326 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003327
3328 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003329 if (!path)
3330 goto make_bad;
3331
Josef Bacikd90c7322011-05-17 09:50:54 -04003332 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003333 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003334
Chris Mason39279cc2007-06-12 06:35:45 -04003335 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003336 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003337 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003338
Chris Mason5f39d392007-10-15 16:14:19 -04003339 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003340
3341 if (filled)
3342 goto cache_acl;
3343
Chris Mason5f39d392007-10-15 16:14:19 -04003344 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3345 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003346 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003347 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003348 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3349 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003350 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003351
3352 tspec = btrfs_inode_atime(inode_item);
3353 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3354 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3355
3356 tspec = btrfs_inode_mtime(inode_item);
3357 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3358 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3359
3360 tspec = btrfs_inode_ctime(inode_item);
3361 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3362 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3363
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003364 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003365 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003366 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3367
3368 /*
3369 * If we were modified in the current generation and evicted from memory
3370 * and then re-read we need to do a full sync since we don't have any
3371 * idea about which extents were modified before we were evicted from
3372 * cache.
3373 */
3374 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3375 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3376 &BTRFS_I(inode)->runtime_flags);
3377
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003378 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003379 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003380 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003381 rdev = btrfs_inode_rdev(leaf, inode_item);
3382
Josef Bacikaec74772008-07-24 12:12:38 -04003383 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003384 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003385cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003386 /*
3387 * try to precache a NULL acl entry for files that don't have
3388 * any xattrs or acls
3389 */
Li Zefan33345d012011-04-20 10:31:50 +08003390 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3391 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003392 if (!maybe_acls)
3393 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003394
Chris Mason39279cc2007-06-12 06:35:45 -04003395 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003396
Chris Mason39279cc2007-06-12 06:35:45 -04003397 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003398 case S_IFREG:
3399 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003400 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003401 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003402 inode->i_fop = &btrfs_file_operations;
3403 inode->i_op = &btrfs_file_inode_operations;
3404 break;
3405 case S_IFDIR:
3406 inode->i_fop = &btrfs_dir_file_operations;
3407 if (root == root->fs_info->tree_root)
3408 inode->i_op = &btrfs_dir_ro_inode_operations;
3409 else
3410 inode->i_op = &btrfs_dir_inode_operations;
3411 break;
3412 case S_IFLNK:
3413 inode->i_op = &btrfs_symlink_inode_operations;
3414 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003415 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003416 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003417 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003418 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003419 init_special_inode(inode, inode->i_mode, rdev);
3420 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003421 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003422
3423 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003424 return;
3425
3426make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003427 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003428 make_bad_inode(inode);
3429}
3430
Chris Masond352ac62008-09-29 15:18:18 -04003431/*
3432 * given a leaf and an inode, copy the inode fields into the leaf
3433 */
Chris Masone02119d2008-09-05 16:13:11 -04003434static void fill_inode_item(struct btrfs_trans_handle *trans,
3435 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003436 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003437 struct inode *inode)
3438{
Liu Bo51fab692012-12-27 09:01:21 +00003439 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003440
Liu Bo51fab692012-12-27 09:01:21 +00003441 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003442
Liu Bo51fab692012-12-27 09:01:21 +00003443 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3444 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3445 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3446 &token);
3447 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3448 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003449
Liu Bo51fab692012-12-27 09:01:21 +00003450 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3451 inode->i_atime.tv_sec, &token);
3452 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3453 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003454
Liu Bo51fab692012-12-27 09:01:21 +00003455 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3456 inode->i_mtime.tv_sec, &token);
3457 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3458 inode->i_mtime.tv_nsec, &token);
3459
3460 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3461 inode->i_ctime.tv_sec, &token);
3462 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3463 inode->i_ctime.tv_nsec, &token);
3464
3465 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3466 &token);
3467 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3468 &token);
3469 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3470 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3471 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3472 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3473 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003474}
3475
Chris Masond352ac62008-09-29 15:18:18 -04003476/*
3477 * copy everything in the in-memory inode into the btree.
3478 */
Chris Mason21151332011-11-10 20:39:08 -05003479static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003480 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003481{
3482 struct btrfs_inode_item *inode_item;
3483 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003484 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003485 int ret;
3486
3487 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003488 if (!path)
3489 return -ENOMEM;
3490
Chris Masonb9473432009-03-13 11:00:37 -04003491 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003492 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3493 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003494 if (ret) {
3495 if (ret > 0)
3496 ret = -ENOENT;
3497 goto failed;
3498 }
3499
Chris Masonb4ce94d2009-02-04 09:25:08 -05003500 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003501 leaf = path->nodes[0];
3502 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003503 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003504
Chris Masone02119d2008-09-05 16:13:11 -04003505 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003506 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003507 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003508 ret = 0;
3509failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003510 btrfs_free_path(path);
3511 return ret;
3512}
3513
Chris Masond352ac62008-09-29 15:18:18 -04003514/*
Chris Mason21151332011-11-10 20:39:08 -05003515 * copy everything in the in-memory inode into the btree.
3516 */
3517noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3518 struct btrfs_root *root, struct inode *inode)
3519{
3520 int ret;
3521
3522 /*
3523 * If the inode is a free space inode, we can deadlock during commit
3524 * if we put it into the delayed code.
3525 *
3526 * The data relocation inode should also be directly updated
3527 * without delay
3528 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003529 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003530 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003531 btrfs_update_root_times(trans, root);
3532
Chris Mason21151332011-11-10 20:39:08 -05003533 ret = btrfs_delayed_update_inode(trans, root, inode);
3534 if (!ret)
3535 btrfs_set_inode_last_trans(trans, inode);
3536 return ret;
3537 }
3538
3539 return btrfs_update_inode_item(trans, root, inode);
3540}
3541
Josef Bacikbe6aef62012-10-22 15:43:12 -04003542noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3543 struct btrfs_root *root,
3544 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003545{
3546 int ret;
3547
3548 ret = btrfs_update_inode(trans, root, inode);
3549 if (ret == -ENOSPC)
3550 return btrfs_update_inode_item(trans, root, inode);
3551 return ret;
3552}
3553
3554/*
Chris Masond352ac62008-09-29 15:18:18 -04003555 * unlink helper that gets used here in inode.c and in the tree logging
3556 * recovery code. It remove a link in a directory with a given name, and
3557 * also drops the back refs in the inode to the directory
3558 */
Al Viro92986792011-03-04 17:14:37 +00003559static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3560 struct btrfs_root *root,
3561 struct inode *dir, struct inode *inode,
3562 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003563{
3564 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003565 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003566 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003567 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003568 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003569 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003570 u64 ino = btrfs_ino(inode);
3571 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003572
3573 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003574 if (!path) {
3575 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003576 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003577 }
3578
Chris Masonb9473432009-03-13 11:00:37 -04003579 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003580 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003581 name, name_len, -1);
3582 if (IS_ERR(di)) {
3583 ret = PTR_ERR(di);
3584 goto err;
3585 }
3586 if (!di) {
3587 ret = -ENOENT;
3588 goto err;
3589 }
Chris Mason5f39d392007-10-15 16:14:19 -04003590 leaf = path->nodes[0];
3591 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003592 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003593 if (ret)
3594 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003595 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003596
Li Zefan33345d012011-04-20 10:31:50 +08003597 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3598 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003599 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003600 btrfs_info(root->fs_info,
3601 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003602 name_len, name, ino, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003603 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003604 goto err;
3605 }
3606
Miao Xie16cdcec2011-04-22 18:12:22 +08003607 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003608 if (ret) {
3609 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003610 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003611 }
Chris Mason39279cc2007-06-12 06:35:45 -04003612
Chris Masone02119d2008-09-05 16:13:11 -04003613 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003614 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003615 if (ret != 0 && ret != -ENOENT) {
3616 btrfs_abort_transaction(trans, root, ret);
3617 goto err;
3618 }
Chris Masone02119d2008-09-05 16:13:11 -04003619
3620 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3621 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003622 if (ret == -ENOENT)
3623 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003624 else if (ret)
3625 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003626err:
3627 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003628 if (ret)
3629 goto out;
3630
3631 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003632 inode_inc_iversion(inode);
3633 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003634 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003635 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003636out:
Chris Mason39279cc2007-06-12 06:35:45 -04003637 return ret;
3638}
3639
Al Viro92986792011-03-04 17:14:37 +00003640int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3641 struct btrfs_root *root,
3642 struct inode *dir, struct inode *inode,
3643 const char *name, int name_len)
3644{
3645 int ret;
3646 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3647 if (!ret) {
3648 btrfs_drop_nlink(inode);
3649 ret = btrfs_update_inode(trans, root, inode);
3650 }
3651 return ret;
3652}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003653
3654/*
3655 * helper to start transaction for unlink and rmdir.
3656 *
Josef Bacikd52be812013-05-29 14:54:47 -04003657 * unlink and rmdir are special in btrfs, they do not always free space, so
3658 * if we cannot make our reservations the normal way try and see if there is
3659 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3660 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003661 */
Josef Bacikd52be812013-05-29 14:54:47 -04003662static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003663{
3664 struct btrfs_trans_handle *trans;
3665 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003666 int ret;
3667
Josef Bacike70bea52011-10-11 14:18:24 -04003668 /*
3669 * 1 for the possible orphan item
3670 * 1 for the dir item
3671 * 1 for the dir index
3672 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003673 * 1 for the inode
3674 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003675 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003676 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3677 return trans;
3678
Josef Bacikd52be812013-05-29 14:54:47 -04003679 if (PTR_ERR(trans) == -ENOSPC) {
3680 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003681
Josef Bacikd52be812013-05-29 14:54:47 -04003682 trans = btrfs_start_transaction(root, 0);
3683 if (IS_ERR(trans))
3684 return trans;
3685 ret = btrfs_cond_migrate_bytes(root->fs_info,
3686 &root->fs_info->trans_block_rsv,
3687 num_bytes, 5);
3688 if (ret) {
3689 btrfs_end_transaction(trans, root);
3690 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003691 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003692 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003693 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003694 }
Josef Bacikd52be812013-05-29 14:54:47 -04003695 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003696}
3697
Chris Mason39279cc2007-06-12 06:35:45 -04003698static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3699{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003700 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003701 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003702 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003703 int ret;
3704
Josef Bacikd52be812013-05-29 14:54:47 -04003705 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003706 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003707 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003708
Chris Mason12fcfd22009-03-24 10:24:20 -04003709 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3710
Chris Masone02119d2008-09-05 16:13:11 -04003711 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3712 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003713 if (ret)
3714 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003715
Yan, Zhenga22285a2010-05-16 10:48:46 -04003716 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003717 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003718 if (ret)
3719 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003720 }
Josef Bacik7b128762008-07-24 12:17:14 -04003721
Tsutomu Itohb5324022011-07-19 07:27:20 +00003722out:
Josef Bacikd52be812013-05-29 14:54:47 -04003723 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003724 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003725 return ret;
3726}
3727
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003728int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3729 struct btrfs_root *root,
3730 struct inode *dir, u64 objectid,
3731 const char *name, int name_len)
3732{
3733 struct btrfs_path *path;
3734 struct extent_buffer *leaf;
3735 struct btrfs_dir_item *di;
3736 struct btrfs_key key;
3737 u64 index;
3738 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003739 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003740
3741 path = btrfs_alloc_path();
3742 if (!path)
3743 return -ENOMEM;
3744
Li Zefan33345d012011-04-20 10:31:50 +08003745 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003746 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003747 if (IS_ERR_OR_NULL(di)) {
3748 if (!di)
3749 ret = -ENOENT;
3750 else
3751 ret = PTR_ERR(di);
3752 goto out;
3753 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003754
3755 leaf = path->nodes[0];
3756 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3757 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3758 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003759 if (ret) {
3760 btrfs_abort_transaction(trans, root, ret);
3761 goto out;
3762 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003763 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003764
3765 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3766 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003767 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003768 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003769 if (ret != -ENOENT) {
3770 btrfs_abort_transaction(trans, root, ret);
3771 goto out;
3772 }
Li Zefan33345d012011-04-20 10:31:50 +08003773 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003774 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003775 if (IS_ERR_OR_NULL(di)) {
3776 if (!di)
3777 ret = -ENOENT;
3778 else
3779 ret = PTR_ERR(di);
3780 btrfs_abort_transaction(trans, root, ret);
3781 goto out;
3782 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003783
3784 leaf = path->nodes[0];
3785 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003786 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003787 index = key.offset;
3788 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003789 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003790
Miao Xie16cdcec2011-04-22 18:12:22 +08003791 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003792 if (ret) {
3793 btrfs_abort_transaction(trans, root, ret);
3794 goto out;
3795 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003796
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003797 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003798 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003799 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003800 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003801 if (ret)
3802 btrfs_abort_transaction(trans, root, ret);
3803out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003804 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003805 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003806}
3807
Chris Mason39279cc2007-06-12 06:35:45 -04003808static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3809{
3810 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003811 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003812 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003813 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003814
David Sterbab3ae2442012-09-13 16:04:34 -06003815 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003816 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003817 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3818 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003819
Josef Bacikd52be812013-05-29 14:54:47 -04003820 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003821 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003822 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003823
Li Zefan33345d012011-04-20 10:31:50 +08003824 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003825 err = btrfs_unlink_subvol(trans, root, dir,
3826 BTRFS_I(inode)->location.objectid,
3827 dentry->d_name.name,
3828 dentry->d_name.len);
3829 goto out;
3830 }
3831
Josef Bacik7b128762008-07-24 12:17:14 -04003832 err = btrfs_orphan_add(trans, inode);
3833 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003834 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003835
Chris Mason39279cc2007-06-12 06:35:45 -04003836 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003837 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3838 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003839 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003840 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003841out:
Josef Bacikd52be812013-05-29 14:54:47 -04003842 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003843 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003844
Chris Mason39279cc2007-06-12 06:35:45 -04003845 return err;
3846}
3847
Chris Mason323ac952008-10-01 19:05:46 -04003848/*
Chris Mason39279cc2007-06-12 06:35:45 -04003849 * this can truncate away extent items, csum items and directory items.
3850 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003851 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003852 *
3853 * csum items that cross the new i_size are truncated to the new size
3854 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003855 *
3856 * min_type is the minimum key type to truncate down to. If set to 0, this
3857 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003858 */
Yan, Zheng80825102009-11-12 09:35:36 +00003859int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3860 struct btrfs_root *root,
3861 struct inode *inode,
3862 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003863{
Chris Mason39279cc2007-06-12 06:35:45 -04003864 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003865 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003866 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003867 struct btrfs_key key;
3868 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003869 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003870 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003871 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003872 u64 item_end = 0;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04003873 u64 last_size = (u64)-1;
Yan, Zheng80825102009-11-12 09:35:36 +00003874 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003875 int found_extent;
3876 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003877 int pending_del_nr = 0;
3878 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003879 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003880 int ret;
3881 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003882 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003883
3884 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003885
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003886 path = btrfs_alloc_path();
3887 if (!path)
3888 return -ENOMEM;
3889 path->reada = -1;
3890
Josef Bacik5dc562c2012-08-17 13:14:17 -04003891 /*
3892 * We want to drop from the next block forward in case this new size is
3893 * not block aligned since we will be keeping the last block of the
3894 * extent just the way it is.
3895 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003896 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00003897 btrfs_drop_extent_cache(inode, ALIGN(new_size,
3898 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003899
Miao Xie16cdcec2011-04-22 18:12:22 +08003900 /*
3901 * This function is also used to drop the items in the log tree before
3902 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3903 * it is used to drop the loged items. So we shouldn't kill the delayed
3904 * items.
3905 */
3906 if (min_type == 0 && root == BTRFS_I(inode)->root)
3907 btrfs_kill_delayed_inode_items(inode);
3908
Li Zefan33345d012011-04-20 10:31:50 +08003909 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003910 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003911 key.type = (u8)-1;
3912
Chris Mason85e21ba2008-01-29 15:11:36 -05003913search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003914 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003915 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003916 if (ret < 0) {
3917 err = ret;
3918 goto out;
3919 }
Chris Masond3977122009-01-05 21:25:51 -05003920
Chris Mason85e21ba2008-01-29 15:11:36 -05003921 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003922 /* there are no items in the tree for us to truncate, we're
3923 * done
3924 */
Yan, Zheng80825102009-11-12 09:35:36 +00003925 if (path->slots[0] == 0)
3926 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003927 path->slots[0]--;
3928 }
3929
Chris Masond3977122009-01-05 21:25:51 -05003930 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003931 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003932 leaf = path->nodes[0];
3933 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3934 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003935
Li Zefan33345d012011-04-20 10:31:50 +08003936 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003937 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003938
Chris Mason85e21ba2008-01-29 15:11:36 -05003939 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003940 break;
3941
Chris Mason5f39d392007-10-15 16:14:19 -04003942 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003943 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003944 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003945 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003946 extent_type = btrfs_file_extent_type(leaf, fi);
3947 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003948 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003949 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003950 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003951 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003952 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003953 }
Yan008630c2007-11-07 13:31:09 -05003954 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003955 }
Yan, Zheng80825102009-11-12 09:35:36 +00003956 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003957 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003958 } else {
3959 if (item_end < new_size)
3960 break;
3961 if (found_key.offset >= new_size)
3962 del_item = 1;
3963 else
3964 del_item = 0;
3965 }
Chris Mason39279cc2007-06-12 06:35:45 -04003966 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003967 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003968 if (found_type != BTRFS_EXTENT_DATA_KEY)
3969 goto delete;
3970
Josef Bacik7f4f6e02013-08-29 16:43:28 -04003971 if (del_item)
3972 last_size = found_key.offset;
3973 else
3974 last_size = new_size;
3975
Chris Mason179e29e2007-11-01 11:28:41 -04003976 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003977 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003978 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003979 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003980 u64 orig_num_bytes =
3981 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00003982 extent_num_bytes = ALIGN(new_size -
3983 found_key.offset,
3984 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04003985 btrfs_set_file_extent_num_bytes(leaf, fi,
3986 extent_num_bytes);
3987 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003988 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003989 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003990 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003991 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003992 } else {
Chris Masondb945352007-10-15 16:15:53 -04003993 extent_num_bytes =
3994 btrfs_file_extent_disk_num_bytes(leaf,
3995 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003996 extent_offset = found_key.offset -
3997 btrfs_file_extent_offset(leaf, fi);
3998
Chris Mason39279cc2007-06-12 06:35:45 -04003999 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004000 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004001 if (extent_start != 0) {
4002 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004003 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004004 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004005 }
4006 }
Chris Mason90692182008-02-08 13:49:28 -05004007 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004008 /*
4009 * we can't truncate inline items that have had
4010 * special encodings
4011 */
4012 if (!del_item &&
4013 btrfs_file_extent_compression(leaf, fi) == 0 &&
4014 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4015 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004016 u32 size = new_size - found_key.offset;
4017
4018 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004019 inode_sub_bytes(inode, item_end + 1 -
4020 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004021 }
4022 size =
4023 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004024 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004025 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004026 inode_sub_bytes(inode, item_end + 1 -
4027 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004028 }
Chris Mason39279cc2007-06-12 06:35:45 -04004029 }
Chris Mason179e29e2007-11-01 11:28:41 -04004030delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004031 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004032 if (!pending_del_nr) {
4033 /* no pending yet, add ourselves */
4034 pending_del_slot = path->slots[0];
4035 pending_del_nr = 1;
4036 } else if (pending_del_nr &&
4037 path->slots[0] + 1 == pending_del_slot) {
4038 /* hop on the pending chunk */
4039 pending_del_nr++;
4040 pending_del_slot = path->slots[0];
4041 } else {
Chris Masond3977122009-01-05 21:25:51 -05004042 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004043 }
Chris Mason39279cc2007-06-12 06:35:45 -04004044 } else {
4045 break;
4046 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004047 if (found_extent && (root->ref_cows ||
4048 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004049 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004050 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004051 extent_num_bytes, 0,
4052 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004053 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004054 BUG_ON(ret);
4055 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004056
Yan, Zheng80825102009-11-12 09:35:36 +00004057 if (found_type == BTRFS_INODE_ITEM_KEY)
4058 break;
4059
4060 if (path->slots[0] == 0 ||
4061 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004062 if (pending_del_nr) {
4063 ret = btrfs_del_items(trans, root, path,
4064 pending_del_slot,
4065 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004066 if (ret) {
4067 btrfs_abort_transaction(trans,
4068 root, ret);
4069 goto error;
4070 }
Yan, Zheng80825102009-11-12 09:35:36 +00004071 pending_del_nr = 0;
4072 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004073 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004074 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004075 } else {
4076 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004077 }
Chris Mason39279cc2007-06-12 06:35:45 -04004078 }
Yan, Zheng80825102009-11-12 09:35:36 +00004079out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004080 if (pending_del_nr) {
4081 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4082 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004083 if (ret)
4084 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004085 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004086error:
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004087 if (last_size != (u64)-1)
4088 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004089 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004090 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004091}
4092
Chris Masona52d9a82007-08-27 16:49:44 -04004093/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004094 * btrfs_truncate_page - read, zero a chunk and write a page
4095 * @inode - inode that we're zeroing
4096 * @from - the offset to start zeroing
4097 * @len - the length to zero, 0 to zero the entire range respective to the
4098 * offset
4099 * @front - zero up to the offset instead of from the offset on
4100 *
4101 * This will find the page for the "from" offset and cow the page and zero the
4102 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004103 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004104int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4105 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004106{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004107 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004108 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004109 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4110 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004111 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004112 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004113 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004114 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4115 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4116 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004117 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004118 int ret = 0;
4119 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004120 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004121
Josef Bacik2aaa6652012-08-29 14:27:18 -04004122 if ((offset & (blocksize - 1)) == 0 &&
4123 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004124 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004125 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004126 if (ret)
4127 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004128
Chris Mason211c17f2008-05-15 09:13:45 -04004129again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004130 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004131 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004132 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004133 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004134 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004135 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004136
4137 page_start = page_offset(page);
4138 page_end = page_start + PAGE_CACHE_SIZE - 1;
4139
Chris Masona52d9a82007-08-27 16:49:44 -04004140 if (!PageUptodate(page)) {
4141 ret = btrfs_readpage(NULL, page);
4142 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004143 if (page->mapping != mapping) {
4144 unlock_page(page);
4145 page_cache_release(page);
4146 goto again;
4147 }
Chris Masona52d9a82007-08-27 16:49:44 -04004148 if (!PageUptodate(page)) {
4149 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004150 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004151 }
4152 }
Chris Mason211c17f2008-05-15 09:13:45 -04004153 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004154
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004155 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004156 set_page_extent_mapped(page);
4157
4158 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4159 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004160 unlock_extent_cached(io_tree, page_start, page_end,
4161 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004162 unlock_page(page);
4163 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004164 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004165 btrfs_put_ordered_extent(ordered);
4166 goto again;
4167 }
4168
Josef Bacik2ac55d42010-02-03 19:33:23 +00004169 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004170 EXTENT_DIRTY | EXTENT_DELALLOC |
4171 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004172 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004173
Josef Bacik2ac55d42010-02-03 19:33:23 +00004174 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4175 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004176 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004177 unlock_extent_cached(io_tree, page_start, page_end,
4178 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004179 goto out_unlock;
4180 }
4181
Chris Masone6dcd2d2008-07-17 12:53:50 -04004182 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004183 if (!len)
4184 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004185 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004186 if (front)
4187 memset(kaddr, 0, offset);
4188 else
4189 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004190 flush_dcache_page(page);
4191 kunmap(page);
4192 }
Chris Mason247e7432008-07-17 12:53:51 -04004193 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004194 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004195 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4196 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004197
Chris Mason89642222008-07-24 09:41:53 -04004198out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004199 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004200 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004201 unlock_page(page);
4202 page_cache_release(page);
4203out:
4204 return ret;
4205}
4206
Josef Bacik695a0d02011-03-04 15:46:53 -05004207/*
4208 * This function puts in dummy file extents for the area we're creating a hole
4209 * for. So if we are truncating this file to a larger size we need to insert
4210 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4211 * the range between oldsize and size
4212 */
Josef Bacika41ad392011-01-31 15:30:16 -05004213int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004214{
4215 struct btrfs_trans_handle *trans;
4216 struct btrfs_root *root = BTRFS_I(inode)->root;
4217 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004218 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004219 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004220 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004221 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4222 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004223 u64 last_byte;
4224 u64 cur_offset;
4225 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004226 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004227
Josef Bacika71754f2013-06-17 17:14:39 -04004228 /*
4229 * If our size started in the middle of a page we need to zero out the
4230 * rest of the page before we expand the i_size, otherwise we could
4231 * expose stale data.
4232 */
4233 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4234 if (err)
4235 return err;
4236
Yan Zheng9036c102008-10-30 14:19:41 -04004237 if (size <= hole_start)
4238 return 0;
4239
Yan Zheng9036c102008-10-30 14:19:41 -04004240 while (1) {
4241 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004242
Josef Bacik2ac55d42010-02-03 19:33:23 +00004243 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004244 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004245 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4246 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004247 if (!ordered)
4248 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004249 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4250 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004251 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004252 btrfs_put_ordered_extent(ordered);
4253 }
4254
Yan Zheng9036c102008-10-30 14:19:41 -04004255 cur_offset = hole_start;
4256 while (1) {
4257 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4258 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004259 if (IS_ERR(em)) {
4260 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004261 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004262 break;
4263 }
Yan Zheng9036c102008-10-30 14:19:41 -04004264 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004265 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004266 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004267 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004268 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004269
Miao Xie36423202011-12-14 20:12:02 -05004270 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004271 if (IS_ERR(trans)) {
4272 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004273 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004274 }
Yan, Zheng80825102009-11-12 09:35:36 +00004275
Josef Bacik5dc562c2012-08-17 13:14:17 -04004276 err = btrfs_drop_extents(trans, root, inode,
4277 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004278 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004279 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004280 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004281 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004282 break;
Miao Xie5b397372011-09-11 10:52:24 -04004283 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004284
Yan Zheng9036c102008-10-30 14:19:41 -04004285 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004286 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004287 0, hole_size, 0, hole_size,
4288 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004289 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004290 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004291 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004292 break;
Miao Xie5b397372011-09-11 10:52:24 -04004293 }
Yan, Zheng80825102009-11-12 09:35:36 +00004294
Josef Bacik5dc562c2012-08-17 13:14:17 -04004295 btrfs_drop_extent_cache(inode, cur_offset,
4296 cur_offset + hole_size - 1, 0);
4297 hole_em = alloc_extent_map();
4298 if (!hole_em) {
4299 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4300 &BTRFS_I(inode)->runtime_flags);
4301 goto next;
4302 }
4303 hole_em->start = cur_offset;
4304 hole_em->len = hole_size;
4305 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004306
Josef Bacik5dc562c2012-08-17 13:14:17 -04004307 hole_em->block_start = EXTENT_MAP_HOLE;
4308 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004309 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004310 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004311 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4312 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4313 hole_em->generation = trans->transid;
4314
4315 while (1) {
4316 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004317 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004318 write_unlock(&em_tree->lock);
4319 if (err != -EEXIST)
4320 break;
4321 btrfs_drop_extent_cache(inode, cur_offset,
4322 cur_offset +
4323 hole_size - 1, 0);
4324 }
4325 free_extent_map(hole_em);
4326next:
Miao Xie36423202011-12-14 20:12:02 -05004327 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004328 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004329 }
4330 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004331 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004332 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004333 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004334 break;
4335 }
4336
Yan, Zhenga22285a2010-05-16 10:48:46 -04004337 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004338 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4339 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004340 return err;
4341}
4342
Eric Sandeen3972f262013-01-12 02:57:22 +00004343static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004344{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004345 struct btrfs_root *root = BTRFS_I(inode)->root;
4346 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004347 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004348 loff_t newsize = attr->ia_size;
4349 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004350 int ret;
4351
Eric Sandeen3972f262013-01-12 02:57:22 +00004352 /*
4353 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4354 * special case where we need to update the times despite not having
4355 * these flags set. For all other operations the VFS set these flags
4356 * explicitly if it wants a timestamp update.
4357 */
4358 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4359 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4360
Josef Bacika41ad392011-01-31 15:30:16 -05004361 if (newsize > oldsize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004362 truncate_pagecache(inode, newsize);
Josef Bacika41ad392011-01-31 15:30:16 -05004363 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004364 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004365 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004366
Miao Xief4a2f4c2011-12-14 20:12:01 -05004367 trans = btrfs_start_transaction(root, 1);
4368 if (IS_ERR(trans))
4369 return PTR_ERR(trans);
4370
4371 i_size_write(inode, newsize);
4372 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4373 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004374 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004375 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004376
Josef Bacika41ad392011-01-31 15:30:16 -05004377 /*
4378 * We're truncating a file that used to have good data down to
4379 * zero. Make sure it gets into the ordered flush list so that
4380 * any new writes get down to disk quickly.
4381 */
4382 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004383 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4384 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004385
Josef Bacikf3fe8202013-01-07 17:03:21 -05004386 /*
4387 * 1 for the orphan item we're going to add
4388 * 1 for the orphan item deletion.
4389 */
4390 trans = btrfs_start_transaction(root, 2);
4391 if (IS_ERR(trans))
4392 return PTR_ERR(trans);
4393
4394 /*
4395 * We need to do this in case we fail at _any_ point during the
4396 * actual truncate. Once we do the truncate_setsize we could
4397 * invalidate pages which forces any outstanding ordered io to
4398 * be instantly completed which will give us extents that need
4399 * to be truncated. If we fail to get an orphan inode down we
4400 * could have left over extents that were never meant to live,
4401 * so we need to garuntee from this point on that everything
4402 * will be consistent.
4403 */
4404 ret = btrfs_orphan_add(trans, inode);
4405 btrfs_end_transaction(trans, root);
4406 if (ret)
4407 return ret;
4408
Josef Bacika41ad392011-01-31 15:30:16 -05004409 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4410 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004411
4412 /* Disable nonlocked read DIO to avoid the end less truncate */
4413 btrfs_inode_block_unlocked_dio(inode);
4414 inode_dio_wait(inode);
4415 btrfs_inode_resume_unlocked_dio(inode);
4416
Josef Bacika41ad392011-01-31 15:30:16 -05004417 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004418 if (ret && inode->i_nlink) {
4419 int err;
4420
4421 /*
4422 * failed to truncate, disk_i_size is only adjusted down
4423 * as we remove extents, so it should represent the true
4424 * size of the inode, so reset the in memory size and
4425 * delete our orphan entry.
4426 */
4427 trans = btrfs_join_transaction(root);
4428 if (IS_ERR(trans)) {
4429 btrfs_orphan_del(NULL, inode);
4430 return ret;
4431 }
4432 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4433 err = btrfs_orphan_del(trans, inode);
4434 if (err)
4435 btrfs_abort_transaction(trans, root, err);
4436 btrfs_end_transaction(trans, root);
4437 }
Yan, Zheng80825102009-11-12 09:35:36 +00004438 }
4439
Josef Bacika41ad392011-01-31 15:30:16 -05004440 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004441}
4442
Chris Mason39279cc2007-06-12 06:35:45 -04004443static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4444{
4445 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004446 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004447 int err;
4448
Li Zefanb83cc962010-12-20 16:04:08 +08004449 if (btrfs_root_readonly(root))
4450 return -EROFS;
4451
Chris Mason39279cc2007-06-12 06:35:45 -04004452 err = inode_change_ok(inode, attr);
4453 if (err)
4454 return err;
4455
Chris Mason5a3f23d2009-03-31 13:27:11 -04004456 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004457 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004458 if (err)
4459 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004460 }
Yan Zheng9036c102008-10-30 14:19:41 -04004461
Christoph Hellwig10257742010-06-04 11:30:02 +02004462 if (attr->ia_valid) {
4463 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004464 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004465 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004466
Josef Bacik22c44fe2011-11-30 10:45:38 -05004467 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004468 err = btrfs_acl_chmod(inode);
4469 }
4470
Chris Mason39279cc2007-06-12 06:35:45 -04004471 return err;
4472}
Chris Mason61295eb2008-01-14 16:24:38 -05004473
Al Virobd555972010-06-07 11:35:40 -04004474void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004475{
4476 struct btrfs_trans_handle *trans;
4477 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004478 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004479 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004480 int ret;
4481
liubo1abe9b82011-03-24 11:18:59 +00004482 trace_btrfs_inode_evict(inode);
4483
Chris Mason39279cc2007-06-12 06:35:45 -04004484 truncate_inode_pages(&inode->i_data, 0);
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004485 if (inode->i_nlink &&
4486 ((btrfs_root_refs(&root->root_item) != 0 &&
4487 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4488 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004489 goto no_delete;
4490
Chris Mason39279cc2007-06-12 06:35:45 -04004491 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004492 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004493 goto no_delete;
4494 }
Al Virobd555972010-06-07 11:35:40 -04004495 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004496 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004497
Yan, Zhengc71bf092009-11-12 09:34:40 +00004498 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004499 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004500 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004501 goto no_delete;
4502 }
4503
Yan, Zheng76dda932009-09-21 16:00:26 -04004504 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004505 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4506 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004507 goto no_delete;
4508 }
4509
Miao Xie0e8c36a2012-12-19 06:59:51 +00004510 ret = btrfs_commit_inode_delayed_inode(inode);
4511 if (ret) {
4512 btrfs_orphan_del(NULL, inode);
4513 goto no_delete;
4514 }
4515
Miao Xie66d8f3d2012-09-06 04:02:28 -06004516 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004517 if (!rsv) {
4518 btrfs_orphan_del(NULL, inode);
4519 goto no_delete;
4520 }
Josef Bacik4a338542011-08-29 11:01:31 -04004521 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004522 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004523 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004524
Chris Masondbe674a2008-07-17 12:54:05 -04004525 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004526
Josef Bacik4289a662011-08-05 13:22:24 -04004527 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004528 * This is a bit simpler than btrfs_truncate since we've already
4529 * reserved our space for our orphan item in the unlink, so we just
4530 * need to reserve some slack space in case we add bytes and update
4531 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004532 */
Yan, Zheng80825102009-11-12 09:35:36 +00004533 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004534 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4535 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004536
Josef Bacik726c35f2011-09-26 15:46:06 -04004537 /*
4538 * Try and steal from the global reserve since we will
4539 * likely not use this space anyway, we want to try as
4540 * hard as possible to get this to work.
4541 */
4542 if (ret)
4543 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4544
Yan, Zhengd68fc572010-05-16 10:49:58 -04004545 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004546 btrfs_warn(root->fs_info,
4547 "Could not get space for a delete, will truncate on mount %d",
4548 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004549 btrfs_orphan_del(NULL, inode);
4550 btrfs_free_block_rsv(root, rsv);
4551 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004552 }
4553
Miao Xie0e8c36a2012-12-19 06:59:51 +00004554 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004555 if (IS_ERR(trans)) {
4556 btrfs_orphan_del(NULL, inode);
4557 btrfs_free_block_rsv(root, rsv);
4558 goto no_delete;
4559 }
4560
4561 trans->block_rsv = rsv;
4562
Yan, Zhengd68fc572010-05-16 10:49:58 -04004563 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004564 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004565 break;
4566
Miao Xie8407aa42012-09-07 01:43:32 -06004567 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004568 btrfs_end_transaction(trans, root);
4569 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004570 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004571 }
4572
Josef Bacik4289a662011-08-05 13:22:24 -04004573 btrfs_free_block_rsv(root, rsv);
4574
Josef Bacik4ef31a42013-08-13 14:10:08 -04004575 /*
4576 * Errors here aren't a big deal, it just means we leave orphan items
4577 * in the tree. They will be cleaned up on the next mount.
4578 */
Yan, Zheng80825102009-11-12 09:35:36 +00004579 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004580 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04004581 btrfs_orphan_del(trans, inode);
4582 } else {
4583 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004584 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004585
Josef Bacik4289a662011-08-05 13:22:24 -04004586 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004587 if (!(root == root->fs_info->tree_root ||
4588 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004589 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004590
Chris Mason54aa1f42007-06-22 14:16:25 -04004591 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004592 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004593no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004594 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004595 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004596 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004597}
4598
4599/*
4600 * this returns the key found in the dir entry in the location pointer.
4601 * If no dir entries were found, location->objectid is 0.
4602 */
4603static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4604 struct btrfs_key *location)
4605{
4606 const char *name = dentry->d_name.name;
4607 int namelen = dentry->d_name.len;
4608 struct btrfs_dir_item *di;
4609 struct btrfs_path *path;
4610 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004611 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004612
4613 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004614 if (!path)
4615 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004616
Li Zefan33345d012011-04-20 10:31:50 +08004617 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004618 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004619 if (IS_ERR(di))
4620 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004621
David Sterbac7040052011-04-19 18:00:01 +02004622 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004623 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004624
Chris Mason5f39d392007-10-15 16:14:19 -04004625 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004626out:
Chris Mason39279cc2007-06-12 06:35:45 -04004627 btrfs_free_path(path);
4628 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004629out_err:
4630 location->objectid = 0;
4631 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004632}
4633
4634/*
4635 * when we hit a tree root in a directory, the btrfs part of the inode
4636 * needs to be changed to reflect the root directory of the tree root. This
4637 * is kind of like crossing a mount point.
4638 */
4639static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004640 struct inode *dir,
4641 struct dentry *dentry,
4642 struct btrfs_key *location,
4643 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004644{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004645 struct btrfs_path *path;
4646 struct btrfs_root *new_root;
4647 struct btrfs_root_ref *ref;
4648 struct extent_buffer *leaf;
4649 int ret;
4650 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004651
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004652 path = btrfs_alloc_path();
4653 if (!path) {
4654 err = -ENOMEM;
4655 goto out;
4656 }
Chris Mason39279cc2007-06-12 06:35:45 -04004657
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004658 err = -ENOENT;
4659 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4660 BTRFS_I(dir)->root->root_key.objectid,
4661 location->objectid);
4662 if (ret) {
4663 if (ret < 0)
4664 err = ret;
4665 goto out;
4666 }
Chris Mason39279cc2007-06-12 06:35:45 -04004667
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004668 leaf = path->nodes[0];
4669 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004670 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004671 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4672 goto out;
4673
4674 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4675 (unsigned long)(ref + 1),
4676 dentry->d_name.len);
4677 if (ret)
4678 goto out;
4679
David Sterbab3b4aa72011-04-21 01:20:15 +02004680 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004681
4682 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4683 if (IS_ERR(new_root)) {
4684 err = PTR_ERR(new_root);
4685 goto out;
4686 }
4687
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004688 *sub_root = new_root;
4689 location->objectid = btrfs_root_dirid(&new_root->root_item);
4690 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004691 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004692 err = 0;
4693out:
4694 btrfs_free_path(path);
4695 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004696}
4697
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004698static void inode_tree_add(struct inode *inode)
4699{
4700 struct btrfs_root *root = BTRFS_I(inode)->root;
4701 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004702 struct rb_node **p;
4703 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004704 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08004705 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004706
Al Viro1d3382cb2010-10-23 15:19:20 -04004707 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004708 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004709 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004710 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004711 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004712 while (*p) {
4713 parent = *p;
4714 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4715
Li Zefan33345d012011-04-20 10:31:50 +08004716 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004717 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004718 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004719 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004720 else {
4721 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004722 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004723 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004724 RB_CLEAR_NODE(parent);
4725 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004726 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004727 }
4728 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01004729 rb_link_node(new, parent, p);
4730 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004731 spin_unlock(&root->inode_lock);
4732}
4733
4734static void inode_tree_del(struct inode *inode)
4735{
4736 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004737 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004738
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004739 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004740 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004741 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004742 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004743 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004744 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004745 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004746
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004747 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004748 synchronize_srcu(&root->fs_info->subvol_srcu);
4749 spin_lock(&root->inode_lock);
4750 empty = RB_EMPTY_ROOT(&root->inode_tree);
4751 spin_unlock(&root->inode_lock);
4752 if (empty)
4753 btrfs_add_dead_root(root);
4754 }
4755}
4756
Jeff Mahoney143bede2012-03-01 14:56:26 +01004757void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004758{
4759 struct rb_node *node;
4760 struct rb_node *prev;
4761 struct btrfs_inode *entry;
4762 struct inode *inode;
4763 u64 objectid = 0;
4764
4765 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4766
4767 spin_lock(&root->inode_lock);
4768again:
4769 node = root->inode_tree.rb_node;
4770 prev = NULL;
4771 while (node) {
4772 prev = node;
4773 entry = rb_entry(node, struct btrfs_inode, rb_node);
4774
Li Zefan33345d012011-04-20 10:31:50 +08004775 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004776 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004777 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004778 node = node->rb_right;
4779 else
4780 break;
4781 }
4782 if (!node) {
4783 while (prev) {
4784 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004785 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004786 node = prev;
4787 break;
4788 }
4789 prev = rb_next(prev);
4790 }
4791 }
4792 while (node) {
4793 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004794 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004795 inode = igrab(&entry->vfs_inode);
4796 if (inode) {
4797 spin_unlock(&root->inode_lock);
4798 if (atomic_read(&inode->i_count) > 1)
4799 d_prune_aliases(inode);
4800 /*
Al Viro45321ac2010-06-07 13:43:19 -04004801 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004802 * the inode cache when its usage count
4803 * hits zero.
4804 */
4805 iput(inode);
4806 cond_resched();
4807 spin_lock(&root->inode_lock);
4808 goto again;
4809 }
4810
4811 if (cond_resched_lock(&root->inode_lock))
4812 goto again;
4813
4814 node = rb_next(node);
4815 }
4816 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004817}
4818
Chris Masone02119d2008-09-05 16:13:11 -04004819static int btrfs_init_locked_inode(struct inode *inode, void *p)
4820{
4821 struct btrfs_iget_args *args = p;
4822 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004823 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004824 return 0;
4825}
4826
4827static int btrfs_find_actor(struct inode *inode, void *opaque)
4828{
4829 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004830 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004831 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004832}
4833
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004834static struct inode *btrfs_iget_locked(struct super_block *s,
4835 u64 objectid,
4836 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004837{
4838 struct inode *inode;
4839 struct btrfs_iget_args args;
4840 args.ino = objectid;
4841 args.root = root;
4842
4843 inode = iget5_locked(s, objectid, btrfs_find_actor,
4844 btrfs_init_locked_inode,
4845 (void *)&args);
4846 return inode;
4847}
4848
Balaji Rao1a54ef82008-07-21 02:01:04 +05304849/* Get an inode object given its location and corresponding root.
4850 * Returns in *is_new if the inode was read from disk
4851 */
4852struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004853 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304854{
4855 struct inode *inode;
4856
4857 inode = btrfs_iget_locked(s, location->objectid, root);
4858 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004859 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304860
4861 if (inode->i_state & I_NEW) {
4862 BTRFS_I(inode)->root = root;
4863 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4864 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004865 if (!is_bad_inode(inode)) {
4866 inode_tree_add(inode);
4867 unlock_new_inode(inode);
4868 if (new)
4869 *new = 1;
4870 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004871 unlock_new_inode(inode);
4872 iput(inode);
4873 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004874 }
4875 }
4876
Balaji Rao1a54ef82008-07-21 02:01:04 +05304877 return inode;
4878}
4879
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004880static struct inode *new_simple_dir(struct super_block *s,
4881 struct btrfs_key *key,
4882 struct btrfs_root *root)
4883{
4884 struct inode *inode = new_inode(s);
4885
4886 if (!inode)
4887 return ERR_PTR(-ENOMEM);
4888
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004889 BTRFS_I(inode)->root = root;
4890 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004891 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004892
4893 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004894 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004895 inode->i_fop = &simple_dir_operations;
4896 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4897 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4898
4899 return inode;
4900}
4901
Chris Mason3de45862008-11-17 21:02:50 -05004902struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004903{
Chris Masond3977122009-01-05 21:25:51 -05004904 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004905 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004906 struct btrfs_root *sub_root = root;
4907 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004908 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004909 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004910
4911 if (dentry->d_name.len > BTRFS_NAME_LEN)
4912 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004913
Jeff Layton39e3c952012-11-28 11:30:53 -05004914 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04004915 if (ret < 0)
4916 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004917
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004918 if (location.objectid == 0)
4919 return NULL;
4920
4921 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004922 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004923 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004924 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004925
4926 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4927
Yan, Zheng76dda932009-09-21 16:00:26 -04004928 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004929 ret = fixup_tree_root_location(root, dir, dentry,
4930 &location, &sub_root);
4931 if (ret < 0) {
4932 if (ret != -ENOENT)
4933 inode = ERR_PTR(ret);
4934 else
4935 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4936 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004937 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004938 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004939 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4940
Julia Lawall34d19ba2011-01-24 19:55:19 +00004941 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004942 down_read(&root->fs_info->cleanup_work_sem);
4943 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004944 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004945 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04004946 if (ret) {
4947 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004948 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04004949 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00004950 }
4951
Chris Mason3de45862008-11-17 21:02:50 -05004952 return inode;
4953}
4954
Nick Pigginfe15ce42011-01-07 17:49:23 +11004955static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004956{
4957 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004958 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004959
Li Zefan848cce02012-02-21 17:04:28 +08004960 if (!inode && !IS_ROOT(dentry))
4961 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004962
Li Zefan848cce02012-02-21 17:04:28 +08004963 if (inode) {
4964 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004965 if (btrfs_root_refs(&root->root_item) == 0)
4966 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004967
4968 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4969 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004970 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004971 return 0;
4972}
4973
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004974static void btrfs_dentry_release(struct dentry *dentry)
4975{
4976 if (dentry->d_fsdata)
4977 kfree(dentry->d_fsdata);
4978}
4979
Chris Mason3de45862008-11-17 21:02:50 -05004980static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004981 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004982{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004983 struct dentry *ret;
4984
4985 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04004986 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004987}
4988
Miao Xie16cdcec2011-04-22 18:12:22 +08004989unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004990 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4991};
4992
Al Viro9cdda8d2013-05-22 16:48:09 -04004993static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04004994{
Al Viro9cdda8d2013-05-22 16:48:09 -04004995 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04004996 struct btrfs_root *root = BTRFS_I(inode)->root;
4997 struct btrfs_item *item;
4998 struct btrfs_dir_item *di;
4999 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005000 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005001 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005002 struct list_head ins_list;
5003 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005004 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005005 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005006 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005007 unsigned char d_type;
5008 int over = 0;
5009 u32 di_cur;
5010 u32 di_total;
5011 u32 di_len;
5012 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005013 char tmp_name[32];
5014 char *name_ptr;
5015 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005016 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005017
5018 /* FIXME, use a real flag for deciding about the key type */
5019 if (root->fs_info->tree_root == root)
5020 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005021
Al Viro9cdda8d2013-05-22 16:48:09 -04005022 if (!dir_emit_dots(file, ctx))
5023 return 0;
5024
David Woodhouse49593bf2008-08-17 17:08:36 +01005025 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005026 if (!path)
5027 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005028
Josef Bacik026fd312011-05-13 10:32:11 -04005029 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005030
Miao Xie16cdcec2011-04-22 18:12:22 +08005031 if (key_type == BTRFS_DIR_INDEX_KEY) {
5032 INIT_LIST_HEAD(&ins_list);
5033 INIT_LIST_HEAD(&del_list);
5034 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5035 }
5036
Chris Mason39279cc2007-06-12 06:35:45 -04005037 btrfs_set_key_type(&key, key_type);
Al Viro9cdda8d2013-05-22 16:48:09 -04005038 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005039 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005040
Chris Mason39279cc2007-06-12 06:35:45 -04005041 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5042 if (ret < 0)
5043 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005044
5045 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005046 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005047 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005048 if (slot >= btrfs_header_nritems(leaf)) {
5049 ret = btrfs_next_leaf(root, path);
5050 if (ret < 0)
5051 goto err;
5052 else if (ret > 0)
5053 break;
5054 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005055 }
Chris Mason3de45862008-11-17 21:02:50 -05005056
Ross Kirkdd3cc162013-09-16 15:58:09 +01005057 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005058 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5059
5060 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005061 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005062 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005063 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005064 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005065 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005066 if (key_type == BTRFS_DIR_INDEX_KEY &&
5067 btrfs_should_delete_dir_index(&del_list,
5068 found_key.offset))
5069 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005070
Al Viro9cdda8d2013-05-22 16:48:09 -04005071 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005072 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005073
Chris Mason39279cc2007-06-12 06:35:45 -04005074 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5075 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005076 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005077
5078 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005079 struct btrfs_key location;
5080
Josef Bacik22a94d42011-03-16 16:47:17 -04005081 if (verify_dir_item(root, leaf, di))
5082 break;
5083
Chris Mason5f39d392007-10-15 16:14:19 -04005084 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005085 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005086 name_ptr = tmp_name;
5087 } else {
5088 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005089 if (!name_ptr) {
5090 ret = -ENOMEM;
5091 goto err;
5092 }
Chris Mason5f39d392007-10-15 16:14:19 -04005093 }
5094 read_extent_buffer(leaf, name_ptr,
5095 (unsigned long)(di + 1), name_len);
5096
5097 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5098 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005099
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005100
Chris Mason3de45862008-11-17 21:02:50 -05005101 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005102 * skip it.
5103 *
5104 * In contrast to old kernels, we insert the snapshot's
5105 * dir item and dir index after it has been created, so
5106 * we won't find a reference to our own snapshot. We
5107 * still keep the following code for backward
5108 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005109 */
5110 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5111 location.objectid == root->root_key.objectid) {
5112 over = 0;
5113 goto skip;
5114 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005115 over = !dir_emit(ctx, name_ptr, name_len,
5116 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005117
Chris Mason3de45862008-11-17 21:02:50 -05005118skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005119 if (name_ptr != tmp_name)
5120 kfree(name_ptr);
5121
Chris Mason39279cc2007-06-12 06:35:45 -04005122 if (over)
5123 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005124 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005125 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005126 di_cur += di_len;
5127 di = (struct btrfs_dir_item *)((char *)di + di_len);
5128 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005129next:
5130 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005131 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005132
Miao Xie16cdcec2011-04-22 18:12:22 +08005133 if (key_type == BTRFS_DIR_INDEX_KEY) {
5134 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005135 ctx->pos++;
5136 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005137 if (ret)
5138 goto nopos;
5139 }
5140
David Woodhouse49593bf2008-08-17 17:08:36 +01005141 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005142 ctx->pos++;
5143
5144 /*
5145 * Stop new entries from being returned after we return the last
5146 * entry.
5147 *
5148 * New directory entries are assigned a strictly increasing
5149 * offset. This means that new entries created during readdir
5150 * are *guaranteed* to be seen in the future by that readdir.
5151 * This has broken buggy programs which operate on names as
5152 * they're returned by readdir. Until we re-use freed offsets
5153 * we have this hack to stop new entries from being returned
5154 * under the assumption that they'll never reach this huge
5155 * offset.
5156 *
5157 * This is being careful not to overflow 32bit loff_t unless the
5158 * last entry requires it because doing so has broken 32bit apps
5159 * in the past.
5160 */
5161 if (key_type == BTRFS_DIR_INDEX_KEY) {
5162 if (ctx->pos >= INT_MAX)
5163 ctx->pos = LLONG_MAX;
5164 else
5165 ctx->pos = INT_MAX;
5166 }
Chris Mason39279cc2007-06-12 06:35:45 -04005167nopos:
5168 ret = 0;
5169err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005170 if (key_type == BTRFS_DIR_INDEX_KEY)
5171 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005172 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005173 return ret;
5174}
5175
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005176int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005177{
5178 struct btrfs_root *root = BTRFS_I(inode)->root;
5179 struct btrfs_trans_handle *trans;
5180 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005181 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005182
Josef Bacik72ac3c02012-05-23 14:13:11 -04005183 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005184 return 0;
5185
Liu Bo83eea1f2012-07-10 05:28:39 -06005186 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005187 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005188
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005189 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005190 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005191 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005192 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005193 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005194 if (IS_ERR(trans))
5195 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005196 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005197 }
5198 return ret;
5199}
5200
5201/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005202 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005203 * inode changes. But, it is most likely to find the inode in cache.
5204 * FIXME, needs more benchmarking...there are no reasons other than performance
5205 * to keep or drop this code.
5206 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005207static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005208{
5209 struct btrfs_root *root = BTRFS_I(inode)->root;
5210 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005211 int ret;
5212
Josef Bacik72ac3c02012-05-23 14:13:11 -04005213 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005214 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005215
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005216 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005217 if (IS_ERR(trans))
5218 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005219
5220 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005221 if (ret && ret == -ENOSPC) {
5222 /* whoops, lets try again with the full transaction */
5223 btrfs_end_transaction(trans, root);
5224 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005225 if (IS_ERR(trans))
5226 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005227
Chris Mason94b60442010-05-26 11:02:00 -04005228 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005229 }
Chris Mason39279cc2007-06-12 06:35:45 -04005230 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005231 if (BTRFS_I(inode)->delayed_node)
5232 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005233
5234 return ret;
5235}
5236
5237/*
5238 * This is a copy of file_update_time. We need this so we can return error on
5239 * ENOSPC for updating the inode in the case of file write and mmap writes.
5240 */
Josef Bacike41f9412012-03-26 09:46:47 -04005241static int btrfs_update_time(struct inode *inode, struct timespec *now,
5242 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005243{
Alexander Block2bc5565282012-06-15 09:49:33 +02005244 struct btrfs_root *root = BTRFS_I(inode)->root;
5245
5246 if (btrfs_root_readonly(root))
5247 return -EROFS;
5248
Josef Bacike41f9412012-03-26 09:46:47 -04005249 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005250 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005251 if (flags & S_CTIME)
5252 inode->i_ctime = *now;
5253 if (flags & S_MTIME)
5254 inode->i_mtime = *now;
5255 if (flags & S_ATIME)
5256 inode->i_atime = *now;
5257 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005258}
5259
Chris Masond352ac62008-09-29 15:18:18 -04005260/*
5261 * find the highest existing sequence number in a directory
5262 * and then set the in-memory index_cnt variable to reflect
5263 * free sequence numbers
5264 */
Josef Bacikaec74772008-07-24 12:12:38 -04005265static int btrfs_set_inode_index_count(struct inode *inode)
5266{
5267 struct btrfs_root *root = BTRFS_I(inode)->root;
5268 struct btrfs_key key, found_key;
5269 struct btrfs_path *path;
5270 struct extent_buffer *leaf;
5271 int ret;
5272
Li Zefan33345d012011-04-20 10:31:50 +08005273 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005274 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5275 key.offset = (u64)-1;
5276
5277 path = btrfs_alloc_path();
5278 if (!path)
5279 return -ENOMEM;
5280
5281 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5282 if (ret < 0)
5283 goto out;
5284 /* FIXME: we should be able to handle this */
5285 if (ret == 0)
5286 goto out;
5287 ret = 0;
5288
5289 /*
5290 * MAGIC NUMBER EXPLANATION:
5291 * since we search a directory based on f_pos we have to start at 2
5292 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5293 * else has to start at 2
5294 */
5295 if (path->slots[0] == 0) {
5296 BTRFS_I(inode)->index_cnt = 2;
5297 goto out;
5298 }
5299
5300 path->slots[0]--;
5301
5302 leaf = path->nodes[0];
5303 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5304
Li Zefan33345d012011-04-20 10:31:50 +08005305 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005306 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5307 BTRFS_I(inode)->index_cnt = 2;
5308 goto out;
5309 }
5310
5311 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5312out:
5313 btrfs_free_path(path);
5314 return ret;
5315}
5316
Chris Masond352ac62008-09-29 15:18:18 -04005317/*
5318 * helper to find a free sequence number in a given directory. This current
5319 * code is very simple, later versions will do smarter things in the btree
5320 */
Chris Mason3de45862008-11-17 21:02:50 -05005321int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005322{
5323 int ret = 0;
5324
5325 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005326 ret = btrfs_inode_delayed_dir_index_count(dir);
5327 if (ret) {
5328 ret = btrfs_set_inode_index_count(dir);
5329 if (ret)
5330 return ret;
5331 }
Josef Bacikaec74772008-07-24 12:12:38 -04005332 }
5333
Chris Mason00e4e6b2008-08-05 11:18:09 -04005334 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005335 BTRFS_I(dir)->index_cnt++;
5336
5337 return ret;
5338}
5339
Chris Mason39279cc2007-06-12 06:35:45 -04005340static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5341 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005342 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005343 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005344 u64 ref_objectid, u64 objectid,
5345 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005346{
5347 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005348 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005349 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005350 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005351 struct btrfs_inode_ref *ref;
5352 struct btrfs_key key[2];
5353 u32 sizes[2];
5354 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005355 int ret;
5356 int owner;
5357
Chris Mason5f39d392007-10-15 16:14:19 -04005358 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005359 if (!path)
5360 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005361
Chris Mason39279cc2007-06-12 06:35:45 -04005362 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005363 if (!inode) {
5364 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005365 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005366 }
Chris Mason39279cc2007-06-12 06:35:45 -04005367
Li Zefan581bb052011-04-20 10:06:11 +08005368 /*
5369 * we have to initialize this early, so we can reclaim the inode
5370 * number if we fail afterwards in this function.
5371 */
5372 inode->i_ino = objectid;
5373
Josef Bacikaec74772008-07-24 12:12:38 -04005374 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005375 trace_btrfs_inode_request(dir);
5376
Chris Mason3de45862008-11-17 21:02:50 -05005377 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005378 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005379 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005380 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005381 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005382 }
Josef Bacikaec74772008-07-24 12:12:38 -04005383 }
5384 /*
5385 * index_cnt is ignored for everything but a dir,
5386 * btrfs_get_inode_index_count has an explanation for the magic
5387 * number
5388 */
5389 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005390 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005391 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005392 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005393
Josef Bacik5dc562c2012-08-17 13:14:17 -04005394 /*
5395 * We could have gotten an inode number from somebody who was fsynced
5396 * and then removed in this same transaction, so let's just set full
5397 * sync since it will be a full sync anyway and this will blow away the
5398 * old info in the log.
5399 */
5400 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5401
Al Viro569254b2011-07-24 17:08:40 -04005402 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005403 owner = 0;
5404 else
5405 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005406
5407 key[0].objectid = objectid;
5408 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5409 key[0].offset = 0;
5410
Mark Fashehf1863732012-08-08 11:32:27 -07005411 /*
5412 * Start new inodes with an inode_ref. This is slightly more
5413 * efficient for small numbers of hard links since they will
5414 * be packed into one item. Extended refs will kick in if we
5415 * add more hard links than can fit in the ref item.
5416 */
Chris Mason9c583092008-01-29 15:15:18 -05005417 key[1].objectid = objectid;
5418 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5419 key[1].offset = ref_objectid;
5420
5421 sizes[0] = sizeof(struct btrfs_inode_item);
5422 sizes[1] = name_len + sizeof(*ref);
5423
Chris Masonb9473432009-03-13 11:00:37 -04005424 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005425 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5426 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005427 goto fail;
5428
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005429 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005430 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005431 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005432 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5433 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005434 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5435 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005436 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005437
5438 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5439 struct btrfs_inode_ref);
5440 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005441 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005442 ptr = (unsigned long)(ref + 1);
5443 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5444
Chris Mason5f39d392007-10-15 16:14:19 -04005445 btrfs_mark_buffer_dirty(path->nodes[0]);
5446 btrfs_free_path(path);
5447
Chris Mason39279cc2007-06-12 06:35:45 -04005448 location = &BTRFS_I(inode)->location;
5449 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005450 location->offset = 0;
5451 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5452
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005453 btrfs_inherit_iflags(inode, dir);
5454
Al Viro569254b2011-07-24 17:08:40 -04005455 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005456 if (btrfs_test_opt(root, NODATASUM))
5457 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005458 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005459 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5460 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005461 }
5462
Chris Mason39279cc2007-06-12 06:35:45 -04005463 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005464 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005465
5466 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005467 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005468
Alexander Block8ea05e32012-07-25 17:35:53 +02005469 btrfs_update_root_times(trans, root);
5470
Chris Mason39279cc2007-06-12 06:35:45 -04005471 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005472fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005473 if (dir)
5474 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005475 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005476 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005477 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005478}
5479
5480static inline u8 btrfs_inode_type(struct inode *inode)
5481{
5482 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5483}
5484
Chris Masond352ac62008-09-29 15:18:18 -04005485/*
5486 * utility function to add 'inode' into 'parent_inode' with
5487 * a give name and a given sequence number.
5488 * if 'add_backref' is true, also insert a backref from the
5489 * inode to the parent directory.
5490 */
Chris Masone02119d2008-09-05 16:13:11 -04005491int btrfs_add_link(struct btrfs_trans_handle *trans,
5492 struct inode *parent_inode, struct inode *inode,
5493 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005494{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005495 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005496 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005497 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005498 u64 ino = btrfs_ino(inode);
5499 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005500
Li Zefan33345d012011-04-20 10:31:50 +08005501 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005502 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5503 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005504 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005505 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5506 key.offset = 0;
5507 }
Chris Mason39279cc2007-06-12 06:35:45 -04005508
Li Zefan33345d012011-04-20 10:31:50 +08005509 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005510 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5511 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005512 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005513 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005514 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5515 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005516 }
5517
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005518 /* Nothing to clean up yet */
5519 if (ret)
5520 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005521
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005522 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5523 parent_inode, &key,
5524 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005525 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005526 goto fail_dir_item;
5527 else if (ret) {
5528 btrfs_abort_transaction(trans, root, ret);
5529 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005530 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005531
5532 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5533 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005534 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005535 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5536 ret = btrfs_update_inode(trans, root, parent_inode);
5537 if (ret)
5538 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005539 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005540
5541fail_dir_item:
5542 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5543 u64 local_index;
5544 int err;
5545 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5546 key.objectid, root->root_key.objectid,
5547 parent_ino, &local_index, name, name_len);
5548
5549 } else if (add_backref) {
5550 u64 local_index;
5551 int err;
5552
5553 err = btrfs_del_inode_ref(trans, root, name, name_len,
5554 ino, parent_ino, &local_index);
5555 }
5556 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005557}
5558
5559static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005560 struct inode *dir, struct dentry *dentry,
5561 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005562{
Josef Bacika1b075d2010-11-19 20:36:11 +00005563 int err = btrfs_add_link(trans, dir, inode,
5564 dentry->d_name.name, dentry->d_name.len,
5565 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005566 if (err > 0)
5567 err = -EEXIST;
5568 return err;
5569}
5570
Josef Bacik618e21d2007-07-11 10:18:17 -04005571static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005572 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005573{
5574 struct btrfs_trans_handle *trans;
5575 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005576 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005577 int err;
5578 int drop_inode = 0;
5579 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005580 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005581
5582 if (!new_valid_dev(rdev))
5583 return -EINVAL;
5584
Josef Bacik9ed74f22009-09-11 16:12:44 -04005585 /*
5586 * 2 for inode item and ref
5587 * 2 for dir items
5588 * 1 for xattr if selinux is on
5589 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005590 trans = btrfs_start_transaction(root, 5);
5591 if (IS_ERR(trans))
5592 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005593
Li Zefan581bb052011-04-20 10:06:11 +08005594 err = btrfs_find_free_ino(root, &objectid);
5595 if (err)
5596 goto out_unlock;
5597
Josef Bacikaec74772008-07-24 12:12:38 -04005598 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005599 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005600 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005601 if (IS_ERR(inode)) {
5602 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005603 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005604 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005605
Eric Paris2a7dba32011-02-01 11:05:39 -05005606 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005607 if (err) {
5608 drop_inode = 1;
5609 goto out_unlock;
5610 }
5611
Casey Schauflerad19db72011-12-15 10:09:07 -05005612 /*
5613 * If the active LSM wants to access the inode during
5614 * d_instantiate it needs these. Smack checks to see
5615 * if the filesystem supports xattrs by looking at the
5616 * ops vector.
5617 */
5618
5619 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005620 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005621 if (err)
5622 drop_inode = 1;
5623 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005624 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005625 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005626 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005627 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005628out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005629 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005630 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005631 if (drop_inode) {
5632 inode_dec_link_count(inode);
5633 iput(inode);
5634 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005635 return err;
5636}
5637
Chris Mason39279cc2007-06-12 06:35:45 -04005638static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005639 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005640{
5641 struct btrfs_trans_handle *trans;
5642 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005643 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005644 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005645 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005646 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005647 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005648
Josef Bacik9ed74f22009-09-11 16:12:44 -04005649 /*
5650 * 2 for inode item and ref
5651 * 2 for dir items
5652 * 1 for xattr if selinux is on
5653 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005654 trans = btrfs_start_transaction(root, 5);
5655 if (IS_ERR(trans))
5656 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005657
Li Zefan581bb052011-04-20 10:06:11 +08005658 err = btrfs_find_free_ino(root, &objectid);
5659 if (err)
5660 goto out_unlock;
5661
Josef Bacikaec74772008-07-24 12:12:38 -04005662 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005663 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005664 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005665 if (IS_ERR(inode)) {
5666 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005667 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005668 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005669 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005670
Eric Paris2a7dba32011-02-01 11:05:39 -05005671 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005672 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005673 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005674
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005675 err = btrfs_update_inode(trans, root, inode);
5676 if (err)
5677 goto out_unlock;
5678
Casey Schauflerad19db72011-12-15 10:09:07 -05005679 /*
5680 * If the active LSM wants to access the inode during
5681 * d_instantiate it needs these. Smack checks to see
5682 * if the filesystem supports xattrs by looking at the
5683 * ops vector.
5684 */
5685 inode->i_fop = &btrfs_file_operations;
5686 inode->i_op = &btrfs_file_inode_operations;
5687
Josef Bacika1b075d2010-11-19 20:36:11 +00005688 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005689 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005690 goto out_unlock;
5691
5692 inode->i_mapping->a_ops = &btrfs_aops;
5693 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5694 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5695 d_instantiate(dentry, inode);
5696
Chris Mason39279cc2007-06-12 06:35:45 -04005697out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005698 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005699 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005700 inode_dec_link_count(inode);
5701 iput(inode);
5702 }
Liu Bob53d3f52012-11-14 14:34:34 +00005703 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005704 return err;
5705}
5706
5707static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5708 struct dentry *dentry)
5709{
5710 struct btrfs_trans_handle *trans;
5711 struct btrfs_root *root = BTRFS_I(dir)->root;
5712 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005713 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005714 int err;
5715 int drop_inode = 0;
5716
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005717 /* do not allow sys_link's with other subvols of the same device */
5718 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005719 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005720
Mark Fashehf1863732012-08-08 11:32:27 -07005721 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005722 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005723
Chris Mason3de45862008-11-17 21:02:50 -05005724 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005725 if (err)
5726 goto fail;
5727
Yan, Zhenga22285a2010-05-16 10:48:46 -04005728 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005729 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005730 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005731 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005732 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005733 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005734 if (IS_ERR(trans)) {
5735 err = PTR_ERR(trans);
5736 goto fail;
5737 }
Chris Mason5f39d392007-10-15 16:14:19 -04005738
Miao Xie31534952011-04-13 13:19:21 +08005739 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005740 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005741 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005742 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005743 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005744
Josef Bacika1b075d2010-11-19 20:36:11 +00005745 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005746
Yan, Zhenga5719522009-09-24 09:17:31 -04005747 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005748 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005749 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005750 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005751 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005752 if (err)
5753 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005754 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005755 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005756 }
Chris Mason39279cc2007-06-12 06:35:45 -04005757
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005758 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005759fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005760 if (drop_inode) {
5761 inode_dec_link_count(inode);
5762 iput(inode);
5763 }
Liu Bob53d3f52012-11-14 14:34:34 +00005764 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005765 return err;
5766}
5767
Al Viro18bb1db2011-07-26 01:41:39 -04005768static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005769{
Chris Masonb9d86662008-05-02 16:13:49 -04005770 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005771 struct btrfs_trans_handle *trans;
5772 struct btrfs_root *root = BTRFS_I(dir)->root;
5773 int err = 0;
5774 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005775 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005776 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005777
Josef Bacik9ed74f22009-09-11 16:12:44 -04005778 /*
5779 * 2 items for inode and ref
5780 * 2 items for dir items
5781 * 1 for xattr if selinux is on
5782 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005783 trans = btrfs_start_transaction(root, 5);
5784 if (IS_ERR(trans))
5785 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005786
Li Zefan581bb052011-04-20 10:06:11 +08005787 err = btrfs_find_free_ino(root, &objectid);
5788 if (err)
5789 goto out_fail;
5790
Josef Bacikaec74772008-07-24 12:12:38 -04005791 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005792 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005793 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005794 if (IS_ERR(inode)) {
5795 err = PTR_ERR(inode);
5796 goto out_fail;
5797 }
Chris Mason5f39d392007-10-15 16:14:19 -04005798
Chris Mason39279cc2007-06-12 06:35:45 -04005799 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005800
Eric Paris2a7dba32011-02-01 11:05:39 -05005801 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005802 if (err)
5803 goto out_fail;
5804
Chris Mason39279cc2007-06-12 06:35:45 -04005805 inode->i_op = &btrfs_dir_inode_operations;
5806 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005807
Chris Masondbe674a2008-07-17 12:54:05 -04005808 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005809 err = btrfs_update_inode(trans, root, inode);
5810 if (err)
5811 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005812
Josef Bacika1b075d2010-11-19 20:36:11 +00005813 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5814 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005815 if (err)
5816 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005817
Chris Mason39279cc2007-06-12 06:35:45 -04005818 d_instantiate(dentry, inode);
5819 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005820
5821out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005822 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005823 if (drop_on_err)
5824 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005825 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005826 return err;
5827}
5828
Chris Masond352ac62008-09-29 15:18:18 -04005829/* helper for btfs_get_extent. Given an existing extent in the tree,
5830 * and an extent that you want to insert, deal with overlap and insert
5831 * the new extent into the tree.
5832 */
Chris Mason3b951512008-04-17 11:29:12 -04005833static int merge_extent_mapping(struct extent_map_tree *em_tree,
5834 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005835 struct extent_map *em,
5836 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005837{
5838 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005839
Chris Masone6dcd2d2008-07-17 12:53:50 -04005840 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5841 start_diff = map_start - em->start;
5842 em->start = map_start;
5843 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005844 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5845 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005846 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005847 em->block_len -= start_diff;
5848 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005849 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005850}
5851
Chris Masonc8b97812008-10-29 14:49:59 -04005852static noinline int uncompress_inline(struct btrfs_path *path,
5853 struct inode *inode, struct page *page,
5854 size_t pg_offset, u64 extent_offset,
5855 struct btrfs_file_extent_item *item)
5856{
5857 int ret;
5858 struct extent_buffer *leaf = path->nodes[0];
5859 char *tmp;
5860 size_t max_size;
5861 unsigned long inline_size;
5862 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005863 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005864
5865 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005866 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005867 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5868 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01005869 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04005870 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005871 if (!tmp)
5872 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005873 ptr = btrfs_file_extent_inline_start(item);
5874
5875 read_extent_buffer(leaf, tmp, ptr, inline_size);
5876
Chris Mason5b050f02008-11-11 09:34:41 -05005877 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005878 ret = btrfs_decompress(compress_type, tmp, page,
5879 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005880 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005881 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005882 unsigned long copy_size = min_t(u64,
5883 PAGE_CACHE_SIZE - pg_offset,
5884 max_size - extent_offset);
5885 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005886 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005887 }
5888 kfree(tmp);
5889 return 0;
5890}
5891
Chris Masond352ac62008-09-29 15:18:18 -04005892/*
5893 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005894 * the ugly parts come from merging extents from the disk with the in-ram
5895 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005896 * where the in-ram extents might be locked pending data=ordered completion.
5897 *
5898 * This also copies inline extents directly into the page.
5899 */
Chris Masond3977122009-01-05 21:25:51 -05005900
Chris Masona52d9a82007-08-27 16:49:44 -04005901struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005902 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005903 int create)
5904{
5905 int ret;
5906 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005907 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005908 u64 extent_start = 0;
5909 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005910 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005911 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005912 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005913 struct btrfs_root *root = BTRFS_I(inode)->root;
5914 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005915 struct extent_buffer *leaf;
5916 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005917 struct extent_map *em = NULL;
5918 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005919 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005920 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005921 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005922
Chris Masona52d9a82007-08-27 16:49:44 -04005923again:
Chris Mason890871b2009-09-02 16:24:52 -04005924 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005925 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005926 if (em)
5927 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005928 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005929
Chris Masona52d9a82007-08-27 16:49:44 -04005930 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005931 if (em->start > start || em->start + em->len <= start)
5932 free_extent_map(em);
5933 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005934 free_extent_map(em);
5935 else
5936 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005937 }
David Sterba172ddd62011-04-21 00:48:27 +02005938 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005939 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005940 err = -ENOMEM;
5941 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005942 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005943 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005944 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005945 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005946 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005947 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005948
5949 if (!path) {
5950 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005951 if (!path) {
5952 err = -ENOMEM;
5953 goto out;
5954 }
5955 /*
5956 * Chances are we'll be called again, so go ahead and do
5957 * readahead
5958 */
5959 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005960 }
5961
Chris Mason179e29e2007-11-01 11:28:41 -04005962 ret = btrfs_lookup_file_extent(trans, root, path,
5963 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005964 if (ret < 0) {
5965 err = ret;
5966 goto out;
5967 }
5968
5969 if (ret != 0) {
5970 if (path->slots[0] == 0)
5971 goto not_found;
5972 path->slots[0]--;
5973 }
5974
Chris Mason5f39d392007-10-15 16:14:19 -04005975 leaf = path->nodes[0];
5976 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005977 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005978 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005979 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5980 found_type = btrfs_key_type(&found_key);
5981 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005982 found_type != BTRFS_EXTENT_DATA_KEY) {
5983 goto not_found;
5984 }
5985
Chris Mason5f39d392007-10-15 16:14:19 -04005986 found_type = btrfs_file_extent_type(leaf, item);
5987 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005988 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005989 if (found_type == BTRFS_FILE_EXTENT_REG ||
5990 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005991 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005992 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005993 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5994 size_t size;
5995 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00005996 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005997 }
5998
5999 if (start >= extent_end) {
6000 path->slots[0]++;
6001 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6002 ret = btrfs_next_leaf(root, path);
6003 if (ret < 0) {
6004 err = ret;
6005 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006006 }
Yan Zheng9036c102008-10-30 14:19:41 -04006007 if (ret > 0)
6008 goto not_found;
6009 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006010 }
Yan Zheng9036c102008-10-30 14:19:41 -04006011 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6012 if (found_key.objectid != objectid ||
6013 found_key.type != BTRFS_EXTENT_DATA_KEY)
6014 goto not_found;
6015 if (start + len <= found_key.offset)
6016 goto not_found;
6017 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006018 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006019 em->len = found_key.offset - start;
6020 goto not_found_em;
6021 }
6022
Josef Bacikcc95bef2013-04-04 14:31:27 -04006023 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006024 if (found_type == BTRFS_FILE_EXTENT_REG ||
6025 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006026 em->start = extent_start;
6027 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006028 em->orig_start = extent_start -
6029 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006030 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6031 item);
Chris Masondb945352007-10-15 16:15:53 -04006032 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6033 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006034 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006035 goto insert;
6036 }
Li Zefan261507a02010-12-17 14:21:50 +08006037 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006038 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006039 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006040 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006041 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006042 } else {
6043 bytenr += btrfs_file_extent_offset(leaf, item);
6044 em->block_start = bytenr;
6045 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006046 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6047 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006048 }
Chris Masona52d9a82007-08-27 16:49:44 -04006049 goto insert;
6050 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006051 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006052 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006053 size_t size;
6054 size_t extent_offset;
6055 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006056
Chris Masona52d9a82007-08-27 16:49:44 -04006057 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006058 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006059 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006060 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006061 goto out;
6062 }
6063
Yan Zheng9036c102008-10-30 14:19:41 -04006064 size = btrfs_file_extent_inline_len(leaf, item);
6065 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006066 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006067 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006068 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006069 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006070 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006071 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006072 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006073 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006074 em->compress_type = compress_type;
6075 }
Yan689f9342007-10-29 11:41:07 -04006076 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006077 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006078 if (btrfs_file_extent_compression(leaf, item) !=
6079 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006080 ret = uncompress_inline(path, inode, page,
6081 pg_offset,
6082 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006083 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006084 } else {
6085 map = kmap(page);
6086 read_extent_buffer(leaf, map + pg_offset, ptr,
6087 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006088 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6089 memset(map + pg_offset + copy_size, 0,
6090 PAGE_CACHE_SIZE - pg_offset -
6091 copy_size);
6092 }
Chris Masonc8b97812008-10-29 14:49:59 -04006093 kunmap(page);
6094 }
Chris Mason179e29e2007-11-01 11:28:41 -04006095 flush_dcache_page(page);
6096 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006097 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006098 if (!trans) {
6099 kunmap(page);
6100 free_extent_map(em);
6101 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006102
David Sterbab3b4aa72011-04-21 01:20:15 +02006103 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006104 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006105
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006106 if (IS_ERR(trans))
6107 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006108 goto again;
6109 }
Chris Masonc8b97812008-10-29 14:49:59 -04006110 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006111 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006112 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006113 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006114 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006115 }
Chris Masond1310b22008-01-24 16:13:08 -05006116 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006117 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006118 goto insert;
6119 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006120 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006121 }
6122not_found:
6123 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006124 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006125 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006126not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006127 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006128 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006129insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006130 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006131 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006132 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006133 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006134 err = -EIO;
6135 goto out;
6136 }
Chris Masond1310b22008-01-24 16:13:08 -05006137
6138 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006139 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006140 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006141 /* it is possible that someone inserted the extent into the tree
6142 * while we had the lock dropped. It is also possible that
6143 * an overlapping map exists in the tree
6144 */
Chris Masona52d9a82007-08-27 16:49:44 -04006145 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006146 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006147
6148 ret = 0;
6149
Chris Mason3b951512008-04-17 11:29:12 -04006150 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006151 if (existing && (existing->start > start ||
6152 existing->start + existing->len <= start)) {
6153 free_extent_map(existing);
6154 existing = NULL;
6155 }
Chris Mason3b951512008-04-17 11:29:12 -04006156 if (!existing) {
6157 existing = lookup_extent_mapping(em_tree, em->start,
6158 em->len);
6159 if (existing) {
6160 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006161 em, start,
6162 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006163 free_extent_map(existing);
6164 if (err) {
6165 free_extent_map(em);
6166 em = NULL;
6167 }
6168 } else {
6169 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006170 free_extent_map(em);
6171 em = NULL;
6172 }
6173 } else {
6174 free_extent_map(em);
6175 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006176 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006177 }
Chris Masona52d9a82007-08-27 16:49:44 -04006178 }
Chris Mason890871b2009-09-02 16:24:52 -04006179 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006180out:
liubo1abe9b82011-03-24 11:18:59 +00006181
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006182 if (em)
6183 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006184
Chris Masonf4219502008-07-22 11:18:09 -04006185 if (path)
6186 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006187 if (trans) {
6188 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006189 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006190 err = ret;
6191 }
Chris Masona52d9a82007-08-27 16:49:44 -04006192 if (err) {
6193 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006194 return ERR_PTR(err);
6195 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006196 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006197 return em;
6198}
6199
Chris Masonec29ed52011-02-23 16:23:20 -05006200struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6201 size_t pg_offset, u64 start, u64 len,
6202 int create)
6203{
6204 struct extent_map *em;
6205 struct extent_map *hole_em = NULL;
6206 u64 range_start = start;
6207 u64 end;
6208 u64 found;
6209 u64 found_end;
6210 int err = 0;
6211
6212 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6213 if (IS_ERR(em))
6214 return em;
6215 if (em) {
6216 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006217 * if our em maps to
6218 * - a hole or
6219 * - a pre-alloc extent,
6220 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006221 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006222 if (em->block_start != EXTENT_MAP_HOLE &&
6223 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006224 return em;
6225 else
6226 hole_em = em;
6227 }
6228
6229 /* check to see if we've wrapped (len == -1 or similar) */
6230 end = start + len;
6231 if (end < start)
6232 end = (u64)-1;
6233 else
6234 end -= 1;
6235
6236 em = NULL;
6237
6238 /* ok, we didn't find anything, lets look for delalloc */
6239 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6240 end, len, EXTENT_DELALLOC, 1);
6241 found_end = range_start + found;
6242 if (found_end < range_start)
6243 found_end = (u64)-1;
6244
6245 /*
6246 * we didn't find anything useful, return
6247 * the original results from get_extent()
6248 */
6249 if (range_start > end || found_end <= start) {
6250 em = hole_em;
6251 hole_em = NULL;
6252 goto out;
6253 }
6254
6255 /* adjust the range_start to make sure it doesn't
6256 * go backwards from the start they passed in
6257 */
6258 range_start = max(start,range_start);
6259 found = found_end - range_start;
6260
6261 if (found > 0) {
6262 u64 hole_start = start;
6263 u64 hole_len = len;
6264
David Sterba172ddd62011-04-21 00:48:27 +02006265 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006266 if (!em) {
6267 err = -ENOMEM;
6268 goto out;
6269 }
6270 /*
6271 * when btrfs_get_extent can't find anything it
6272 * returns one huge hole
6273 *
6274 * make sure what it found really fits our range, and
6275 * adjust to make sure it is based on the start from
6276 * the caller
6277 */
6278 if (hole_em) {
6279 u64 calc_end = extent_map_end(hole_em);
6280
6281 if (calc_end <= start || (hole_em->start > end)) {
6282 free_extent_map(hole_em);
6283 hole_em = NULL;
6284 } else {
6285 hole_start = max(hole_em->start, start);
6286 hole_len = calc_end - hole_start;
6287 }
6288 }
6289 em->bdev = NULL;
6290 if (hole_em && range_start > hole_start) {
6291 /* our hole starts before our delalloc, so we
6292 * have to return just the parts of the hole
6293 * that go until the delalloc starts
6294 */
6295 em->len = min(hole_len,
6296 range_start - hole_start);
6297 em->start = hole_start;
6298 em->orig_start = hole_start;
6299 /*
6300 * don't adjust block start at all,
6301 * it is fixed at EXTENT_MAP_HOLE
6302 */
6303 em->block_start = hole_em->block_start;
6304 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006305 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6306 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006307 } else {
6308 em->start = range_start;
6309 em->len = found;
6310 em->orig_start = range_start;
6311 em->block_start = EXTENT_MAP_DELALLOC;
6312 em->block_len = found;
6313 }
6314 } else if (hole_em) {
6315 return hole_em;
6316 }
6317out:
6318
6319 free_extent_map(hole_em);
6320 if (err) {
6321 free_extent_map(em);
6322 return ERR_PTR(err);
6323 }
6324 return em;
6325}
6326
Josef Bacik4b46fce2010-05-23 11:00:55 -04006327static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6328 u64 start, u64 len)
6329{
6330 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006331 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006332 struct btrfs_key ins;
6333 u64 alloc_hint;
6334 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006335
Josef Bacik4b46fce2010-05-23 11:00:55 -04006336 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04006337 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006338 alloc_hint, &ins, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006339 if (ret)
6340 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006341
Josef Bacik70c8a912012-10-11 16:54:30 -04006342 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006343 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik00361582013-08-14 14:02:47 -04006344 if (IS_ERR(em)) {
6345 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6346 return em;
6347 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006348
6349 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6350 ins.offset, ins.offset, 0);
6351 if (ret) {
6352 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Josef Bacik00361582013-08-14 14:02:47 -04006353 free_extent_map(em);
6354 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006355 }
Josef Bacik00361582013-08-14 14:02:47 -04006356
Josef Bacik4b46fce2010-05-23 11:00:55 -04006357 return em;
6358}
6359
Chris Mason46bfbb52010-05-26 11:04:10 -04006360/*
6361 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6362 * block must be cow'd
6363 */
Josef Bacik00361582013-08-14 14:02:47 -04006364noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006365 u64 *orig_start, u64 *orig_block_len,
6366 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006367{
Josef Bacik00361582013-08-14 14:02:47 -04006368 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04006369 struct btrfs_path *path;
6370 int ret;
6371 struct extent_buffer *leaf;
6372 struct btrfs_root *root = BTRFS_I(inode)->root;
6373 struct btrfs_file_extent_item *fi;
6374 struct btrfs_key key;
6375 u64 disk_bytenr;
6376 u64 backref_offset;
6377 u64 extent_end;
6378 u64 num_bytes;
6379 int slot;
6380 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006381 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Chris Mason46bfbb52010-05-26 11:04:10 -04006382 path = btrfs_alloc_path();
6383 if (!path)
6384 return -ENOMEM;
6385
Josef Bacik00361582013-08-14 14:02:47 -04006386 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006387 offset, 0);
6388 if (ret < 0)
6389 goto out;
6390
6391 slot = path->slots[0];
6392 if (ret == 1) {
6393 if (slot == 0) {
6394 /* can't find the item, must cow */
6395 ret = 0;
6396 goto out;
6397 }
6398 slot--;
6399 }
6400 ret = 0;
6401 leaf = path->nodes[0];
6402 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006403 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006404 key.type != BTRFS_EXTENT_DATA_KEY) {
6405 /* not our file or wrong item type, must cow */
6406 goto out;
6407 }
6408
6409 if (key.offset > offset) {
6410 /* Wrong offset, must cow */
6411 goto out;
6412 }
6413
6414 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6415 found_type = btrfs_file_extent_type(leaf, fi);
6416 if (found_type != BTRFS_FILE_EXTENT_REG &&
6417 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6418 /* not a regular extent, must cow */
6419 goto out;
6420 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006421
6422 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6423 goto out;
6424
Chris Mason46bfbb52010-05-26 11:04:10 -04006425 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006426 if (disk_bytenr == 0)
6427 goto out;
6428
6429 if (btrfs_file_extent_compression(leaf, fi) ||
6430 btrfs_file_extent_encryption(leaf, fi) ||
6431 btrfs_file_extent_other_encoding(leaf, fi))
6432 goto out;
6433
Chris Mason46bfbb52010-05-26 11:04:10 -04006434 backref_offset = btrfs_file_extent_offset(leaf, fi);
6435
Josef Bacik7ee9e442013-06-21 16:37:03 -04006436 if (orig_start) {
6437 *orig_start = key.offset - backref_offset;
6438 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6439 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6440 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006441
Chris Mason46bfbb52010-05-26 11:04:10 -04006442 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason46bfbb52010-05-26 11:04:10 -04006443
6444 if (btrfs_extent_readonly(root, disk_bytenr))
6445 goto out;
Josef Bacik1bda19e2013-10-18 12:10:36 -04006446 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006447
6448 /*
6449 * look for other files referencing this extent, if we
6450 * find any we must cow
6451 */
Josef Bacik00361582013-08-14 14:02:47 -04006452 trans = btrfs_join_transaction(root);
6453 if (IS_ERR(trans)) {
6454 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04006455 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04006456 }
6457
6458 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6459 key.offset - backref_offset, disk_bytenr);
6460 btrfs_end_transaction(trans, root);
6461 if (ret) {
6462 ret = 0;
6463 goto out;
6464 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006465
6466 /*
6467 * adjust disk_bytenr and num_bytes to cover just the bytes
6468 * in this extent we are about to write. If there
6469 * are any csums in that range we have to cow in order
6470 * to keep the csums correct
6471 */
6472 disk_bytenr += backref_offset;
6473 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006474 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006475 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6476 goto out;
6477 /*
6478 * all of the above have passed, it is safe to overwrite this extent
6479 * without cow
6480 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006481 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006482 ret = 1;
6483out:
6484 btrfs_free_path(path);
6485 return ret;
6486}
6487
Josef Bacikeb838e72012-07-31 16:28:48 -04006488static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6489 struct extent_state **cached_state, int writing)
6490{
6491 struct btrfs_ordered_extent *ordered;
6492 int ret = 0;
6493
6494 while (1) {
6495 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6496 0, cached_state);
6497 /*
6498 * We're concerned with the entire range that we're going to be
6499 * doing DIO to, so we need to make sure theres no ordered
6500 * extents in this range.
6501 */
6502 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6503 lockend - lockstart + 1);
6504
6505 /*
6506 * We need to make sure there are no buffered pages in this
6507 * range either, we could have raced between the invalidate in
6508 * generic_file_direct_write and locking the extent. The
6509 * invalidate needs to happen so that reads after a write do not
6510 * get stale data.
6511 */
6512 if (!ordered && (!writing ||
6513 !test_range_bit(&BTRFS_I(inode)->io_tree,
6514 lockstart, lockend, EXTENT_UPTODATE, 0,
6515 *cached_state)))
6516 break;
6517
6518 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6519 cached_state, GFP_NOFS);
6520
6521 if (ordered) {
6522 btrfs_start_ordered_extent(inode, ordered, 1);
6523 btrfs_put_ordered_extent(ordered);
6524 } else {
6525 /* Screw you mmap */
6526 ret = filemap_write_and_wait_range(inode->i_mapping,
6527 lockstart,
6528 lockend);
6529 if (ret)
6530 break;
6531
6532 /*
6533 * If we found a page that couldn't be invalidated just
6534 * fall back to buffered.
6535 */
6536 ret = invalidate_inode_pages2_range(inode->i_mapping,
6537 lockstart >> PAGE_CACHE_SHIFT,
6538 lockend >> PAGE_CACHE_SHIFT);
6539 if (ret)
6540 break;
6541 }
6542
6543 cond_resched();
6544 }
6545
6546 return ret;
6547}
6548
Josef Bacik69ffb542012-09-11 15:40:07 -04006549static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6550 u64 len, u64 orig_start,
6551 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006552 u64 orig_block_len, u64 ram_bytes,
6553 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006554{
6555 struct extent_map_tree *em_tree;
6556 struct extent_map *em;
6557 struct btrfs_root *root = BTRFS_I(inode)->root;
6558 int ret;
6559
6560 em_tree = &BTRFS_I(inode)->extent_tree;
6561 em = alloc_extent_map();
6562 if (!em)
6563 return ERR_PTR(-ENOMEM);
6564
6565 em->start = start;
6566 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006567 em->mod_start = start;
6568 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006569 em->len = len;
6570 em->block_len = block_len;
6571 em->block_start = block_start;
6572 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006573 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006574 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006575 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006576 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6577 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006578 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006579
6580 do {
6581 btrfs_drop_extent_cache(inode, em->start,
6582 em->start + em->len - 1, 0);
6583 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006584 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006585 write_unlock(&em_tree->lock);
6586 } while (ret == -EEXIST);
6587
6588 if (ret) {
6589 free_extent_map(em);
6590 return ERR_PTR(ret);
6591 }
6592
6593 return em;
6594}
6595
6596
Josef Bacik4b46fce2010-05-23 11:00:55 -04006597static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6598 struct buffer_head *bh_result, int create)
6599{
6600 struct extent_map *em;
6601 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006602 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006603 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006604 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006605 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04006606 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006607 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006608
Miao Xie172a5042013-02-21 02:48:22 -07006609 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006610 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006611 else
Josef Bacikc3298612012-08-03 16:49:19 -04006612 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006613
Josef Bacikc3298612012-08-03 16:49:19 -04006614 lockstart = start;
6615 lockend = start + len - 1;
6616
Josef Bacikeb838e72012-07-31 16:28:48 -04006617 /*
6618 * If this errors out it's because we couldn't invalidate pagecache for
6619 * this range and we need to fallback to buffered.
6620 */
6621 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6622 return -ENOTBLK;
6623
Josef Bacik4b46fce2010-05-23 11:00:55 -04006624 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006625 if (IS_ERR(em)) {
6626 ret = PTR_ERR(em);
6627 goto unlock_err;
6628 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006629
6630 /*
6631 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6632 * io. INLINE is special, and we could probably kludge it in here, but
6633 * it's still buffered so for safety lets just fall back to the generic
6634 * buffered path.
6635 *
6636 * For COMPRESSED we _have_ to read the entire extent in so we can
6637 * decompress it, so there will be buffering required no matter what we
6638 * do, so go ahead and fallback to buffered.
6639 *
6640 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6641 * to buffered IO. Don't blame me, this is the price we pay for using
6642 * the generic code.
6643 */
6644 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6645 em->block_start == EXTENT_MAP_INLINE) {
6646 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006647 ret = -ENOTBLK;
6648 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006649 }
6650
6651 /* Just a good old fashioned hole, return */
6652 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6653 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6654 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006655 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006656 }
6657
6658 /*
6659 * We don't allocate a new extent in the following cases
6660 *
6661 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6662 * existing extent.
6663 * 2) The extent is marked as PREALLOC. We're good to go here and can
6664 * just use the extent.
6665 *
6666 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006667 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006668 len = min(len, em->len - (start - em->start));
6669 lockstart = start + len;
6670 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006671 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006672
6673 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6674 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6675 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006676 int type;
6677 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006678 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006679
6680 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6681 type = BTRFS_ORDERED_PREALLOC;
6682 else
6683 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006684 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006685 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006686
Josef Bacik00361582013-08-14 14:02:47 -04006687 if (can_nocow_extent(inode, start, &len, &orig_start,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006688 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006689 if (type == BTRFS_ORDERED_PREALLOC) {
6690 free_extent_map(em);
6691 em = create_pinned_em(inode, start, len,
6692 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006693 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006694 orig_block_len,
6695 ram_bytes, type);
Josef Bacik00361582013-08-14 14:02:47 -04006696 if (IS_ERR(em))
Josef Bacik69ffb542012-09-11 15:40:07 -04006697 goto unlock_err;
Josef Bacik69ffb542012-09-11 15:40:07 -04006698 }
6699
Chris Mason46bfbb52010-05-26 11:04:10 -04006700 ret = btrfs_add_ordered_extent_dio(inode, start,
6701 block_start, len, len, type);
Chris Mason46bfbb52010-05-26 11:04:10 -04006702 if (ret) {
6703 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006704 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006705 }
6706 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006707 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006708 }
Josef Bacik00361582013-08-14 14:02:47 -04006709
Chris Mason46bfbb52010-05-26 11:04:10 -04006710 /*
6711 * this will cow the extent, reset the len in case we changed
6712 * it above
6713 */
6714 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006715 free_extent_map(em);
6716 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006717 if (IS_ERR(em)) {
6718 ret = PTR_ERR(em);
6719 goto unlock_err;
6720 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006721 len = min(len, em->len - (start - em->start));
6722unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006723 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6724 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006725 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006726 bh_result->b_bdev = em->bdev;
6727 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006728 if (create) {
6729 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6730 set_buffer_new(bh_result);
6731
6732 /*
6733 * Need to update the i_size under the extent lock so buffered
6734 * readers will get the updated i_size when we unlock.
6735 */
6736 if (start + len > i_size_read(inode))
6737 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006738
Miao Xie172a5042013-02-21 02:48:22 -07006739 spin_lock(&BTRFS_I(inode)->lock);
6740 BTRFS_I(inode)->outstanding_extents++;
6741 spin_unlock(&BTRFS_I(inode)->lock);
6742
Miao Xie09348562013-02-07 10:12:07 +00006743 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6744 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6745 &cached_state, GFP_NOFS);
6746 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006747 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006748
Josef Bacikeb838e72012-07-31 16:28:48 -04006749 /*
6750 * In the case of write we need to clear and unlock the entire range,
6751 * in the case of read we need to unlock only the end area that we
6752 * aren't using if there is any left over space.
6753 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006754 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006755 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6756 lockend, unlock_bits, 1, 0,
6757 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006758 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006759 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006760 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006761
Josef Bacik4b46fce2010-05-23 11:00:55 -04006762 free_extent_map(em);
6763
6764 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006765
6766unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006767 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6768 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6769 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006770}
6771
Josef Bacik4b46fce2010-05-23 11:00:55 -04006772static void btrfs_endio_direct_read(struct bio *bio, int err)
6773{
Miao Xiee65e1532010-11-22 03:04:43 +00006774 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006775 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6776 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006777 struct inode *inode = dip->inode;
6778 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006779 struct bio *dio_bio;
Miao Xiefacc8a222013-07-25 19:22:34 +08006780 u32 *csums = (u32 *)dip->csum;
6781 int index = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006782 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006783
6784 start = dip->logical_offset;
6785 do {
6786 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6787 struct page *page = bvec->bv_page;
6788 char *kaddr;
6789 u32 csum = ~(u32)0;
6790 unsigned long flags;
6791
6792 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006793 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006794 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006795 csum, bvec->bv_len);
6796 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006797 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006798 local_irq_restore(flags);
6799
6800 flush_dcache_page(bvec->bv_page);
Miao Xiefacc8a222013-07-25 19:22:34 +08006801 if (csum != csums[index]) {
6802 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006803 btrfs_ino(inode), start, csum,
6804 csums[index]);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006805 err = -EIO;
6806 }
6807 }
6808
6809 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006810 bvec++;
Miao Xiefacc8a222013-07-25 19:22:34 +08006811 index++;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006812 } while (bvec <= bvec_end);
6813
6814 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006815 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04006816 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006817
Josef Bacik4b46fce2010-05-23 11:00:55 -04006818 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006819
6820 /* If we had a csum failure make sure to clear the uptodate flag */
6821 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006822 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6823 dio_end_io(dio_bio, err);
6824 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006825}
6826
6827static void btrfs_endio_direct_write(struct bio *bio, int err)
6828{
6829 struct btrfs_dio_private *dip = bio->bi_private;
6830 struct inode *inode = dip->inode;
6831 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006832 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006833 u64 ordered_offset = dip->logical_offset;
6834 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04006835 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006836 int ret;
6837
6838 if (err)
6839 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006840again:
6841 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6842 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006843 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006844 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006845 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006846
Josef Bacik5fd02042012-05-02 14:00:54 -04006847 ordered->work.func = finish_ordered_fn;
6848 ordered->work.flags = 0;
6849 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6850 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006851out_test:
6852 /*
6853 * our bio might span multiple ordered extents. If we haven't
6854 * completed the accounting for the whole dio, go back and try again
6855 */
6856 if (ordered_offset < dip->logical_offset + dip->bytes) {
6857 ordered_bytes = dip->logical_offset + dip->bytes -
6858 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006859 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006860 goto again;
6861 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006862out_done:
Chris Mason9be33952013-05-17 18:30:14 -04006863 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006864
Josef Bacik4b46fce2010-05-23 11:00:55 -04006865 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006866
6867 /* If we had an error make sure to clear the uptodate flag */
6868 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006869 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6870 dio_end_io(dio_bio, err);
6871 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006872}
6873
Chris Masoneaf25d92010-05-25 09:48:28 -04006874static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6875 struct bio *bio, int mirror_num,
6876 unsigned long bio_flags, u64 offset)
6877{
6878 int ret;
6879 struct btrfs_root *root = BTRFS_I(inode)->root;
6880 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006881 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006882 return 0;
6883}
6884
Miao Xiee65e1532010-11-22 03:04:43 +00006885static void btrfs_end_dio_bio(struct bio *bio, int err)
6886{
6887 struct btrfs_dio_private *dip = bio->bi_private;
6888
6889 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006890 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006891 "sector %#Lx len %u err no %d\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006892 btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006893 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006894 dip->errors = 1;
6895
6896 /*
6897 * before atomic variable goto zero, we must make sure
6898 * dip->errors is perceived to be set.
6899 */
6900 smp_mb__before_atomic_dec();
6901 }
6902
6903 /* if there are more bios still pending for this dio, just exit */
6904 if (!atomic_dec_and_test(&dip->pending_bios))
6905 goto out;
6906
Chris Mason9be33952013-05-17 18:30:14 -04006907 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00006908 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04006909 } else {
6910 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00006911 bio_endio(dip->orig_bio, 0);
6912 }
6913out:
6914 bio_put(bio);
6915}
6916
6917static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6918 u64 first_sector, gfp_t gfp_flags)
6919{
6920 int nr_vecs = bio_get_nr_vecs(bdev);
6921 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6922}
6923
6924static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6925 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006926 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006927{
Miao Xiefacc8a222013-07-25 19:22:34 +08006928 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00006929 int write = rw & REQ_WRITE;
6930 struct btrfs_root *root = BTRFS_I(inode)->root;
6931 int ret;
6932
Josef Bacikb812ce22012-11-16 13:56:32 -05006933 if (async_submit)
6934 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6935
Miao Xiee65e1532010-11-22 03:04:43 +00006936 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006937
6938 if (!write) {
6939 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6940 if (ret)
6941 goto err;
6942 }
Miao Xiee65e1532010-11-22 03:04:43 +00006943
Josef Bacik1ae39932011-04-06 14:41:34 -04006944 if (skip_sum)
6945 goto map;
6946
6947 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006948 ret = btrfs_wq_submit_bio(root->fs_info,
6949 inode, rw, bio, 0, 0,
6950 file_offset,
6951 __btrfs_submit_bio_start_direct_io,
6952 __btrfs_submit_bio_done);
6953 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006954 } else if (write) {
6955 /*
6956 * If we aren't doing async submit, calculate the csum of the
6957 * bio now.
6958 */
6959 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6960 if (ret)
6961 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006962 } else if (!skip_sum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08006963 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
6964 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006965 if (ret)
6966 goto err;
6967 }
Miao Xiee65e1532010-11-22 03:04:43 +00006968
Josef Bacik1ae39932011-04-06 14:41:34 -04006969map:
6970 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006971err:
6972 bio_put(bio);
6973 return ret;
6974}
6975
6976static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6977 int skip_sum)
6978{
6979 struct inode *inode = dip->inode;
6980 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00006981 struct bio *bio;
6982 struct bio *orig_bio = dip->orig_bio;
6983 struct bio_vec *bvec = orig_bio->bi_io_vec;
6984 u64 start_sector = orig_bio->bi_sector;
6985 u64 file_offset = dip->logical_offset;
6986 u64 submit_len = 0;
6987 u64 map_length;
6988 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006989 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006990 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006991
Miao Xiee65e1532010-11-22 03:04:43 +00006992 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05006993 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006994 &map_length, NULL, 0);
6995 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006996 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006997 return -EIO;
6998 }
Miao Xiefacc8a222013-07-25 19:22:34 +08006999
Josef Bacik02f57c72011-04-06 14:25:44 -04007000 if (map_length >= orig_bio->bi_size) {
7001 bio = orig_bio;
7002 goto submit;
7003 }
7004
David Woodhouse53b381b2013-01-29 18:40:14 -05007005 /* async crcs make it difficult to collect full stripe writes. */
7006 if (btrfs_get_alloc_profile(root, 1) &
7007 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7008 async_submit = 0;
7009 else
7010 async_submit = 1;
7011
Josef Bacik02f57c72011-04-06 14:25:44 -04007012 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7013 if (!bio)
7014 return -ENOMEM;
7015 bio->bi_private = dip;
7016 bio->bi_end_io = btrfs_end_dio_bio;
7017 atomic_inc(&dip->pending_bios);
7018
Miao Xiee65e1532010-11-22 03:04:43 +00007019 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7020 if (unlikely(map_length < submit_len + bvec->bv_len ||
7021 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7022 bvec->bv_offset) < bvec->bv_len)) {
7023 /*
7024 * inc the count before we submit the bio so
7025 * we know the end IO handler won't happen before
7026 * we inc the count. Otherwise, the dip might get freed
7027 * before we're done setting it up
7028 */
7029 atomic_inc(&dip->pending_bios);
7030 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7031 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007032 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007033 if (ret) {
7034 bio_put(bio);
7035 atomic_dec(&dip->pending_bios);
7036 goto out_err;
7037 }
7038
Miao Xiee65e1532010-11-22 03:04:43 +00007039 start_sector += submit_len >> 9;
7040 file_offset += submit_len;
7041
7042 submit_len = 0;
7043 nr_pages = 0;
7044
7045 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7046 start_sector, GFP_NOFS);
7047 if (!bio)
7048 goto out_err;
7049 bio->bi_private = dip;
7050 bio->bi_end_io = btrfs_end_dio_bio;
7051
7052 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007053 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007054 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007055 &map_length, NULL, 0);
7056 if (ret) {
7057 bio_put(bio);
7058 goto out_err;
7059 }
7060 } else {
7061 submit_len += bvec->bv_len;
7062 nr_pages ++;
7063 bvec++;
7064 }
7065 }
7066
Josef Bacik02f57c72011-04-06 14:25:44 -04007067submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007068 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007069 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007070 if (!ret)
7071 return 0;
7072
7073 bio_put(bio);
7074out_err:
7075 dip->errors = 1;
7076 /*
7077 * before atomic variable goto zero, we must
7078 * make sure dip->errors is perceived to be set.
7079 */
7080 smp_mb__before_atomic_dec();
7081 if (atomic_dec_and_test(&dip->pending_bios))
7082 bio_io_error(dip->orig_bio);
7083
7084 /* bio_end_io() will handle error, so we needn't return it */
7085 return 0;
7086}
7087
Chris Mason9be33952013-05-17 18:30:14 -04007088static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7089 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007090{
7091 struct btrfs_root *root = BTRFS_I(inode)->root;
7092 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007093 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007094 int skip_sum;
Miao Xiefacc8a222013-07-25 19:22:34 +08007095 int sum_len;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007096 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007097 int ret = 0;
Miao Xiefacc8a222013-07-25 19:22:34 +08007098 u16 csum_size;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007099
7100 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7101
Chris Mason9be33952013-05-17 18:30:14 -04007102 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007103 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007104 ret = -ENOMEM;
7105 goto free_ordered;
7106 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007107
Miao Xiefacc8a222013-07-25 19:22:34 +08007108 if (!skip_sum && !write) {
7109 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7110 sum_len = dio_bio->bi_size >> inode->i_sb->s_blocksize_bits;
7111 sum_len *= csum_size;
7112 } else {
7113 sum_len = 0;
7114 }
7115
7116 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007117 if (!dip) {
7118 ret = -ENOMEM;
7119 goto free_io_bio;
7120 }
7121
7122 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007123 dip->inode = inode;
7124 dip->logical_offset = file_offset;
Miao Xiee6da5d22013-06-19 18:19:17 +08007125 dip->bytes = dio_bio->bi_size;
Chris Mason9be33952013-05-17 18:30:14 -04007126 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7127 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007128 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007129 dip->orig_bio = io_bio;
7130 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007131 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007132
7133 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007134 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007135 else
Chris Mason9be33952013-05-17 18:30:14 -04007136 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007137
Miao Xiee65e1532010-11-22 03:04:43 +00007138 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7139 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007140 return;
Chris Mason9be33952013-05-17 18:30:14 -04007141
7142free_io_bio:
7143 bio_put(io_bio);
7144
Josef Bacik4b46fce2010-05-23 11:00:55 -04007145free_ordered:
7146 /*
7147 * If this is a write, we need to clean up the reserved space and kill
7148 * the ordered extent.
7149 */
7150 if (write) {
7151 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007152 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007153 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7154 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7155 btrfs_free_reserved_extent(root, ordered->start,
7156 ordered->disk_len);
7157 btrfs_put_ordered_extent(ordered);
7158 btrfs_put_ordered_extent(ordered);
7159 }
Chris Mason9be33952013-05-17 18:30:14 -04007160 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007161}
7162
Chris Mason5a5f79b2010-05-26 21:33:37 -04007163static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7164 const struct iovec *iov, loff_t offset,
7165 unsigned long nr_segs)
7166{
7167 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007168 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007169 size_t size;
7170 unsigned long addr;
7171 unsigned blocksize_mask = root->sectorsize - 1;
7172 ssize_t retval = -EINVAL;
7173 loff_t end = offset;
7174
7175 if (offset & blocksize_mask)
7176 goto out;
7177
7178 /* Check the memory alignment. Blocks cannot straddle pages */
7179 for (seg = 0; seg < nr_segs; seg++) {
7180 addr = (unsigned long)iov[seg].iov_base;
7181 size = iov[seg].iov_len;
7182 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007183 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007184 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007185
7186 /* If this is a write we don't need to check anymore */
7187 if (rw & WRITE)
7188 continue;
7189
7190 /*
7191 * Check to make sure we don't have duplicate iov_base's in this
7192 * iovec, if so return EINVAL, otherwise we'll get csum errors
7193 * when reading back.
7194 */
7195 for (i = seg + 1; i < nr_segs; i++) {
7196 if (iov[seg].iov_base == iov[i].iov_base)
7197 goto out;
7198 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007199 }
7200 retval = 0;
7201out:
7202 return retval;
7203}
Josef Bacikeb838e72012-07-31 16:28:48 -04007204
Chris Mason16432982008-04-10 10:23:21 -04007205static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7206 const struct iovec *iov, loff_t offset,
7207 unsigned long nr_segs)
7208{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007209 struct file *file = iocb->ki_filp;
7210 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007211 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007212 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007213 bool wakeup = true;
7214 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007215 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007216
Chris Mason5a5f79b2010-05-26 21:33:37 -04007217 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007218 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007219 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007220
Miao Xie38851cc2013-02-08 07:04:11 +00007221 atomic_inc(&inode->i_dio_count);
7222 smp_mb__after_atomic_inc();
7223
Josef Bacik0e267c42013-07-02 10:38:02 -04007224 /*
7225 * The generic stuff only does filemap_write_and_wait_range, which isn't
7226 * enough if we've written compressed pages to this area, so we need to
7227 * call btrfs_wait_ordered_range to make absolutely sure that any
7228 * outstanding dirty pages are on disk.
7229 */
7230 count = iov_length(iov, nr_segs);
7231 btrfs_wait_ordered_range(inode, offset, count);
7232
Miao Xie09348562013-02-07 10:12:07 +00007233 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007234 /*
7235 * If the write DIO is beyond the EOF, we need update
7236 * the isize, but it is protected by i_mutex. So we can
7237 * not unlock the i_mutex at this case.
7238 */
7239 if (offset + count <= inode->i_size) {
7240 mutex_unlock(&inode->i_mutex);
7241 relock = true;
7242 }
Miao Xie09348562013-02-07 10:12:07 +00007243 ret = btrfs_delalloc_reserve_space(inode, count);
7244 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007245 goto out;
7246 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7247 &BTRFS_I(inode)->runtime_flags))) {
7248 inode_dio_done(inode);
7249 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7250 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007251 }
7252
7253 ret = __blockdev_direct_IO(rw, iocb, inode,
7254 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7255 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007256 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007257 if (rw & WRITE) {
7258 if (ret < 0 && ret != -EIOCBQUEUED)
7259 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007260 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007261 btrfs_delalloc_release_space(inode,
7262 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007263 else
7264 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007265 }
Miao Xie38851cc2013-02-08 07:04:11 +00007266out:
Miao Xie2e60a512013-02-08 07:01:08 +00007267 if (wakeup)
7268 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007269 if (relock)
7270 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007271
7272 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007273}
7274
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007275#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7276
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007277static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7278 __u64 start, __u64 len)
7279{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007280 int ret;
7281
7282 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7283 if (ret)
7284 return ret;
7285
Chris Masonec29ed52011-02-23 16:23:20 -05007286 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007287}
7288
Chris Mason9ebefb182007-06-15 13:50:00 -04007289int btrfs_readpage(struct file *file, struct page *page)
7290{
Chris Masond1310b22008-01-24 16:13:08 -05007291 struct extent_io_tree *tree;
7292 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007293 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007294}
Chris Mason1832a6d2007-12-21 16:27:21 -05007295
Chris Mason39279cc2007-06-12 06:35:45 -04007296static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7297{
Chris Masond1310b22008-01-24 16:13:08 -05007298 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007299
7300
7301 if (current->flags & PF_MEMALLOC) {
7302 redirty_page_for_writepage(wbc, page);
7303 unlock_page(page);
7304 return 0;
7305 }
Chris Masond1310b22008-01-24 16:13:08 -05007306 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007307 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7308}
Chris Mason39279cc2007-06-12 06:35:45 -04007309
Eric Sandeen48a3b632013-04-25 20:41:01 +00007310static int btrfs_writepages(struct address_space *mapping,
7311 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007312{
Chris Masond1310b22008-01-24 16:13:08 -05007313 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007314
Chris Masond1310b22008-01-24 16:13:08 -05007315 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007316 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7317}
7318
Chris Mason3ab2fb52007-11-08 10:59:22 -05007319static int
7320btrfs_readpages(struct file *file, struct address_space *mapping,
7321 struct list_head *pages, unsigned nr_pages)
7322{
Chris Masond1310b22008-01-24 16:13:08 -05007323 struct extent_io_tree *tree;
7324 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007325 return extent_readpages(tree, mapping, pages, nr_pages,
7326 btrfs_get_extent);
7327}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007328static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007329{
Chris Masond1310b22008-01-24 16:13:08 -05007330 struct extent_io_tree *tree;
7331 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007332 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007333
Chris Masond1310b22008-01-24 16:13:08 -05007334 tree = &BTRFS_I(page->mapping->host)->io_tree;
7335 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007336 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007337 if (ret == 1) {
7338 ClearPagePrivate(page);
7339 set_page_private(page, 0);
7340 page_cache_release(page);
7341 }
7342 return ret;
7343}
Chris Mason39279cc2007-06-12 06:35:45 -04007344
Chris Masone6dcd2d2008-07-17 12:53:50 -04007345static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7346{
Chris Mason98509cfc2008-09-11 15:51:43 -04007347 if (PageWriteback(page) || PageDirty(page))
7348 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007349 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007350}
7351
Lukas Czernerd47992f2013-05-21 23:17:23 -04007352static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7353 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007354{
Josef Bacik5fd02042012-05-02 14:00:54 -04007355 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007356 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007357 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007358 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007359 u64 page_start = page_offset(page);
7360 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007361
Chris Mason8b62b722009-09-02 16:53:46 -04007362 /*
7363 * we have the page locked, so new writeback can't start,
7364 * and the dirty bit won't be cleared while we are here.
7365 *
7366 * Wait for IO on this page so that we can safely clear
7367 * the PagePrivate2 bit and do ordered accounting
7368 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007369 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007370
Josef Bacik5fd02042012-05-02 14:00:54 -04007371 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007372 if (offset) {
7373 btrfs_releasepage(page, GFP_NOFS);
7374 return;
7375 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007376 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007377 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007378 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007379 /*
7380 * IO on this page will never be started, so we need
7381 * to account for any ordered extents now
7382 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007383 clear_extent_bit(tree, page_start, page_end,
7384 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007385 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7386 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007387 /*
7388 * whoever cleared the private bit is responsible
7389 * for the finish_ordered_io
7390 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04007391 if (TestClearPagePrivate2(page)) {
7392 struct btrfs_ordered_inode_tree *tree;
7393 u64 new_len;
7394
7395 tree = &BTRFS_I(inode)->ordered_tree;
7396
7397 spin_lock_irq(&tree->lock);
7398 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
7399 new_len = page_start - ordered->file_offset;
7400 if (new_len < ordered->truncated_len)
7401 ordered->truncated_len = new_len;
7402 spin_unlock_irq(&tree->lock);
7403
7404 if (btrfs_dec_test_ordered_pending(inode, &ordered,
7405 page_start,
7406 PAGE_CACHE_SIZE, 1))
7407 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007408 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007409 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007410 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007411 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007412 }
7413 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007414 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007415 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7416 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007417 __btrfs_releasepage(page, GFP_NOFS);
7418
Chris Mason4a096752008-07-21 10:29:44 -04007419 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007420 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007421 ClearPagePrivate(page);
7422 set_page_private(page, 0);
7423 page_cache_release(page);
7424 }
Chris Mason39279cc2007-06-12 06:35:45 -04007425}
7426
Chris Mason9ebefb182007-06-15 13:50:00 -04007427/*
7428 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7429 * called from a page fault handler when a page is first dirtied. Hence we must
7430 * be careful to check for EOF conditions here. We set the page up correctly
7431 * for a written page which means we get ENOSPC checking when writing into
7432 * holes and correct delalloc and unwritten extent mapping on filesystems that
7433 * support these features.
7434 *
7435 * We are not allowed to take the i_mutex here so we have to play games to
7436 * protect against truncate races as the page could now be beyond EOF. Because
7437 * vmtruncate() writes the inode size before removing pages, once we have the
7438 * page lock we can determine safely if the page is beyond EOF. If it is not
7439 * beyond EOF, then the page is guaranteed safe against truncation until we
7440 * unlock the page.
7441 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007442int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007443{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007444 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007445 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007446 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007447 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7448 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007449 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007450 char *kaddr;
7451 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007452 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007453 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007454 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007455 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007456 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007457
Jan Karab2b5ef52012-06-12 16:20:45 +02007458 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007459 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007460 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007461 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007462 reserved = 1;
7463 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007464 if (ret) {
7465 if (ret == -ENOMEM)
7466 ret = VM_FAULT_OOM;
7467 else /* -ENOSPC, -EIO, etc */
7468 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007469 if (reserved)
7470 goto out;
7471 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007472 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007473
Nick Piggin56a76f82009-03-31 15:23:23 -07007474 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007475again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007476 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007477 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007478 page_start = page_offset(page);
7479 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007480
Chris Mason9ebefb182007-06-15 13:50:00 -04007481 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007482 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007483 /* page got truncated out from underneath us */
7484 goto out_unlock;
7485 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007486 wait_on_page_writeback(page);
7487
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007488 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007489 set_page_extent_mapped(page);
7490
Chris Masoneb84ae02008-07-17 13:53:27 -04007491 /*
7492 * we can't set the delalloc bits if there are pending ordered
7493 * extents. Drop our locks and wait for them to finish
7494 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007495 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7496 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007497 unlock_extent_cached(io_tree, page_start, page_end,
7498 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007499 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007500 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007501 btrfs_put_ordered_extent(ordered);
7502 goto again;
7503 }
7504
Josef Bacikfbf19082009-10-01 17:10:23 -04007505 /*
7506 * XXX - page_mkwrite gets called every time the page is dirtied, even
7507 * if it was already dirty, so for space accounting reasons we need to
7508 * clear any delalloc bits for the range we are fixing to save. There
7509 * is probably a better way to do this, but for now keep consistent with
7510 * prepare_pages in the normal write path.
7511 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007512 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007513 EXTENT_DIRTY | EXTENT_DELALLOC |
7514 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007515 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007516
Josef Bacik2ac55d42010-02-03 19:33:23 +00007517 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7518 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007519 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007520 unlock_extent_cached(io_tree, page_start, page_end,
7521 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007522 ret = VM_FAULT_SIGBUS;
7523 goto out_unlock;
7524 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007525 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007526
7527 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007528 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007529 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007530 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007531 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007532
Chris Masone6dcd2d2008-07-17 12:53:50 -04007533 if (zero_start != PAGE_CACHE_SIZE) {
7534 kaddr = kmap(page);
7535 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7536 flush_dcache_page(page);
7537 kunmap(page);
7538 }
Chris Mason247e7432008-07-17 12:53:51 -04007539 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007540 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007541 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007542
Chris Mason257c62e2009-10-13 13:21:08 -04007543 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7544 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007545 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007546
Josef Bacik2ac55d42010-02-03 19:33:23 +00007547 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007548
7549out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007550 if (!ret) {
7551 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007552 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007553 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007554 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007555out:
Josef Bacikec39e182012-01-12 19:10:12 -05007556 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007557out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007558 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007559 return ret;
7560}
7561
Josef Bacika41ad392011-01-31 15:30:16 -05007562static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007563{
7564 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007565 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007566 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007567 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007568 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007569 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007570 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007571
Chris Mason4a096752008-07-21 10:29:44 -04007572 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04007573
Josef Bacikfcb80c22011-05-03 10:40:22 -04007574 /*
7575 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7576 * 3 things going on here
7577 *
7578 * 1) We need to reserve space for our orphan item and the space to
7579 * delete our orphan item. Lord knows we don't want to have a dangling
7580 * orphan item because we didn't reserve space to remove it.
7581 *
7582 * 2) We need to reserve space to update our inode.
7583 *
7584 * 3) We need to have something to cache all the space that is going to
7585 * be free'd up by the truncate operation, but also have some slack
7586 * space reserved in case it uses space during the truncate (thank you
7587 * very much snapshotting).
7588 *
7589 * And we need these to all be seperate. The fact is we can use alot of
7590 * space doing the truncate, and we have no earthly idea how much space
7591 * we will use, so we need the truncate reservation to be seperate so it
7592 * doesn't end up using space reserved for updating the inode or
7593 * removing the orphan item. We also need to be able to stop the
7594 * transaction and start a new one, which means we need to be able to
7595 * update the inode several times, and we have no idea of knowing how
7596 * many times that will be, so we can't just reserve 1 item for the
7597 * entirety of the opration, so that has to be done seperately as well.
7598 * Then there is the orphan item, which does indeed need to be held on
7599 * to for the whole operation, and we need nobody to touch this reserved
7600 * space except the orphan code.
7601 *
7602 * So that leaves us with
7603 *
7604 * 1) root->orphan_block_rsv - for the orphan deletion.
7605 * 2) rsv - for the truncate reservation, which we will steal from the
7606 * transaction reservation.
7607 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7608 * updating the inode.
7609 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007610 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007611 if (!rsv)
7612 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007613 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007614 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007615
Josef Bacik907cbce2011-08-08 13:46:15 -04007616 /*
Josef Bacik07127182011-08-19 10:29:59 -04007617 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007618 * 1 for updating the inode.
7619 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007620 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007621 if (IS_ERR(trans)) {
7622 err = PTR_ERR(trans);
7623 goto out;
7624 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007625
Josef Bacik907cbce2011-08-08 13:46:15 -04007626 /* Migrate the slack space for the truncate to our reserve */
7627 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7628 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007629 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007630
Chris Mason5a3f23d2009-03-31 13:27:11 -04007631 /*
7632 * setattr is responsible for setting the ordered_data_close flag,
7633 * but that is only tested during the last file release. That
7634 * could happen well after the next commit, leaving a great big
7635 * window where new writes may get lost if someone chooses to write
7636 * to this file after truncating to zero
7637 *
7638 * The inode doesn't have any dirty data here, and so if we commit
7639 * this is a noop. If someone immediately starts writing to the inode
7640 * it is very likely we'll catch some of their writes in this
7641 * transaction, and the commit will find this file on the ordered
7642 * data list with good things to send down.
7643 *
7644 * This is a best effort solution, there is still a window where
7645 * using truncate to replace the contents of the file will
7646 * end up with a zero length file after a crash.
7647 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007648 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7649 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007650 btrfs_add_ordered_operation(trans, root, inode);
7651
Josef Bacik5dc562c2012-08-17 13:14:17 -04007652 /*
7653 * So if we truncate and then write and fsync we normally would just
7654 * write the extents that changed, which is a problem if we need to
7655 * first truncate that entire inode. So set this flag so we write out
7656 * all of the extents in the inode to the sync log so we're completely
7657 * safe.
7658 */
7659 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007660 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007661
Yan, Zheng80825102009-11-12 09:35:36 +00007662 while (1) {
7663 ret = btrfs_truncate_inode_items(trans, root, inode,
7664 inode->i_size,
7665 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007666 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007667 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007668 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007669 }
Chris Mason39279cc2007-06-12 06:35:45 -04007670
Josef Bacikfcb80c22011-05-03 10:40:22 -04007671 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007672 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007673 if (ret) {
7674 err = ret;
7675 break;
7676 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007677
Yan, Zheng80825102009-11-12 09:35:36 +00007678 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007679 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007680
7681 trans = btrfs_start_transaction(root, 2);
7682 if (IS_ERR(trans)) {
7683 ret = err = PTR_ERR(trans);
7684 trans = NULL;
7685 break;
7686 }
7687
7688 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7689 rsv, min_size);
7690 BUG_ON(ret); /* shouldn't happen */
7691 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007692 }
7693
7694 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007695 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007696 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007697 if (ret)
7698 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007699 }
7700
Chris Mason917c16b2011-11-08 14:49:59 -05007701 if (trans) {
7702 trans->block_rsv = &root->fs_info->trans_block_rsv;
7703 ret = btrfs_update_inode(trans, root, inode);
7704 if (ret && !err)
7705 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007706
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007707 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007708 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007709 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007710
7711out:
7712 btrfs_free_block_rsv(root, rsv);
7713
Josef Bacik3893e332011-01-31 16:03:11 -05007714 if (ret && !err)
7715 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007716
Josef Bacik3893e332011-01-31 16:03:11 -05007717 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007718}
7719
Sven Wegener3b963622008-06-09 21:57:42 -04007720/*
Chris Masond352ac62008-09-29 15:18:18 -04007721 * create a new subvolume directory/inode (helper for the ioctl).
7722 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007723int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007724 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007725{
Chris Mason39279cc2007-06-12 06:35:45 -04007726 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007727 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007728 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007729
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007730 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7731 new_dirid, new_dirid,
7732 S_IFDIR | (~current_umask() & S_IRWXUGO),
7733 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007734 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007735 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007736 inode->i_op = &btrfs_dir_inode_operations;
7737 inode->i_fop = &btrfs_dir_file_operations;
7738
Miklos Szeredibfe86842011-10-28 14:13:29 +02007739 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007740 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007741
Yan, Zheng76dda932009-09-21 16:00:26 -04007742 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007743
Yan, Zheng76dda932009-09-21 16:00:26 -04007744 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007745 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007746}
7747
Chris Mason39279cc2007-06-12 06:35:45 -04007748struct inode *btrfs_alloc_inode(struct super_block *sb)
7749{
7750 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007751 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007752
7753 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7754 if (!ei)
7755 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007756
7757 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007758 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007759 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007760 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007761 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007762 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007763 ei->disk_i_size = 0;
7764 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007765 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007766 ei->index_cnt = (u64)-1;
7767 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007768 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007769
Josef Bacik9e0baf62011-07-15 15:16:44 +00007770 spin_lock_init(&ei->lock);
7771 ei->outstanding_extents = 0;
7772 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007773
Josef Bacik72ac3c02012-05-23 14:13:11 -04007774 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007775 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007776
Miao Xie16cdcec2011-04-22 18:12:22 +08007777 ei->delayed_node = NULL;
7778
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007779 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007780 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007781 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7782 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007783 ei->io_tree.track_uptodate = 1;
7784 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007785 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007786 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007787 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007788 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007789 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007790 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007791 RB_CLEAR_NODE(&ei->rb_node);
7792
7793 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007794}
7795
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007796static void btrfs_i_callback(struct rcu_head *head)
7797{
7798 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007799 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7800}
7801
Chris Mason39279cc2007-06-12 06:35:45 -04007802void btrfs_destroy_inode(struct inode *inode)
7803{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007804 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007805 struct btrfs_root *root = BTRFS_I(inode)->root;
7806
Al Virob3d9b7a2012-06-09 13:51:19 -04007807 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007808 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007809 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7810 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007811 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7812 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007813
Chris Mason5a3f23d2009-03-31 13:27:11 -04007814 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007815 * This can happen where we create an inode, but somebody else also
7816 * created the same inode and we need to destroy the one we already
7817 * created.
7818 */
7819 if (!root)
7820 goto free;
7821
7822 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007823 * Make sure we're properly removed from the ordered operation
7824 * lists.
7825 */
7826 smp_mb();
7827 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
Miao Xie199c2a92013-05-15 07:48:23 +00007828 spin_lock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007829 list_del_init(&BTRFS_I(inode)->ordered_operations);
Miao Xie199c2a92013-05-15 07:48:23 +00007830 spin_unlock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007831 }
7832
Josef Bacik8a35d952012-05-23 14:26:42 -04007833 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7834 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007835 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007836 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007837 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007838 }
Josef Bacik7b128762008-07-24 12:17:14 -04007839
Chris Masond3977122009-01-05 21:25:51 -05007840 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007841 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7842 if (!ordered)
7843 break;
7844 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007845 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007846 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007847 btrfs_remove_ordered_extent(inode, ordered);
7848 btrfs_put_ordered_extent(ordered);
7849 btrfs_put_ordered_extent(ordered);
7850 }
7851 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007852 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007853 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007854free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007855 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007856}
7857
Al Viro45321ac2010-06-07 13:43:19 -04007858int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007859{
7860 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007861
Naohiro Aota6379ef92013-06-06 09:56:34 +00007862 if (root == NULL)
7863 return 1;
7864
Liu Bofa6ac872013-02-20 14:10:23 +00007865 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02007866 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04007867 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007868 else
Al Viro45321ac2010-06-07 13:43:19 -04007869 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007870}
7871
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007872static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007873{
7874 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7875
7876 inode_init_once(&ei->vfs_inode);
7877}
7878
7879void btrfs_destroy_cachep(void)
7880{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007881 /*
7882 * Make sure all delayed rcu free inodes are flushed before we
7883 * destroy cache.
7884 */
7885 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007886 if (btrfs_inode_cachep)
7887 kmem_cache_destroy(btrfs_inode_cachep);
7888 if (btrfs_trans_handle_cachep)
7889 kmem_cache_destroy(btrfs_trans_handle_cachep);
7890 if (btrfs_transaction_cachep)
7891 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007892 if (btrfs_path_cachep)
7893 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007894 if (btrfs_free_space_cachep)
7895 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007896 if (btrfs_delalloc_work_cachep)
7897 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007898}
7899
7900int btrfs_init_cachep(void)
7901{
David Sterba837e1972012-09-07 03:00:48 -06007902 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007903 sizeof(struct btrfs_inode), 0,
7904 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007905 if (!btrfs_inode_cachep)
7906 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007907
David Sterba837e1972012-09-07 03:00:48 -06007908 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007909 sizeof(struct btrfs_trans_handle), 0,
7910 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007911 if (!btrfs_trans_handle_cachep)
7912 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007913
David Sterba837e1972012-09-07 03:00:48 -06007914 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007915 sizeof(struct btrfs_transaction), 0,
7916 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007917 if (!btrfs_transaction_cachep)
7918 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007919
David Sterba837e1972012-09-07 03:00:48 -06007920 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007921 sizeof(struct btrfs_path), 0,
7922 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007923 if (!btrfs_path_cachep)
7924 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007925
David Sterba837e1972012-09-07 03:00:48 -06007926 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007927 sizeof(struct btrfs_free_space), 0,
7928 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7929 if (!btrfs_free_space_cachep)
7930 goto fail;
7931
Miao Xie8ccf6f192012-10-25 09:28:04 +00007932 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7933 sizeof(struct btrfs_delalloc_work), 0,
7934 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7935 NULL);
7936 if (!btrfs_delalloc_work_cachep)
7937 goto fail;
7938
Chris Mason39279cc2007-06-12 06:35:45 -04007939 return 0;
7940fail:
7941 btrfs_destroy_cachep();
7942 return -ENOMEM;
7943}
7944
7945static int btrfs_getattr(struct vfsmount *mnt,
7946 struct dentry *dentry, struct kstat *stat)
7947{
Miao Xiedf0af1a2013-01-29 10:11:59 +00007948 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04007949 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007950 u32 blocksize = inode->i_sb->s_blocksize;
7951
Chris Mason39279cc2007-06-12 06:35:45 -04007952 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007953 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007954 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00007955
7956 spin_lock(&BTRFS_I(inode)->lock);
7957 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
7958 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05007959 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00007960 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007961 return 0;
7962}
7963
Chris Masond3977122009-01-05 21:25:51 -05007964static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7965 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007966{
7967 struct btrfs_trans_handle *trans;
7968 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007969 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007970 struct inode *new_inode = new_dentry->d_inode;
7971 struct inode *old_inode = old_dentry->d_inode;
7972 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007973 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007974 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007975 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007976 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007977
Li Zefan33345d012011-04-20 10:31:50 +08007978 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007979 return -EPERM;
7980
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007981 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007982 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007983 return -EXDEV;
7984
Li Zefan33345d012011-04-20 10:31:50 +08007985 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7986 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007987 return -ENOTEMPTY;
7988
Chris Mason39279cc2007-06-12 06:35:45 -04007989 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007990 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007991 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05007992
7993
7994 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04007995 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05007996 new_dentry->d_name.name,
7997 new_dentry->d_name.len);
7998
7999 if (ret) {
8000 if (ret == -EEXIST) {
8001 /* we shouldn't get
8002 * eexist without a new_inode */
8003 if (!new_inode) {
8004 WARN_ON(1);
8005 return ret;
8006 }
8007 } else {
8008 /* maybe -EOVERFLOW */
8009 return ret;
8010 }
8011 }
8012 ret = 0;
8013
Chris Mason5a3f23d2009-03-31 13:27:11 -04008014 /*
8015 * we're using rename to replace one file with another.
8016 * and the replacement file is large. Start IO on it now so
8017 * we don't add too much work to the end of the transaction
8018 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008019 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008020 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8021 filemap_flush(old_inode->i_mapping);
8022
Yan, Zheng76dda932009-09-21 16:00:26 -04008023 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008024 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008025 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008026 /*
8027 * We want to reserve the absolute worst case amount of items. So if
8028 * both inodes are subvols and we need to unlink them then that would
8029 * require 4 item modifications, but if they are both normal inodes it
8030 * would require 5 item modifications, so we'll assume their normal
8031 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8032 * should cover the worst case number of items we'll modify.
8033 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008034 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008035 if (IS_ERR(trans)) {
8036 ret = PTR_ERR(trans);
8037 goto out_notrans;
8038 }
Chris Mason5f39d392007-10-15 16:14:19 -04008039
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008040 if (dest != root)
8041 btrfs_record_root_in_trans(trans, dest);
8042
Yan, Zhenga5719522009-09-24 09:17:31 -04008043 ret = btrfs_set_inode_index(new_dir, &index);
8044 if (ret)
8045 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008046
Li Zefan33345d012011-04-20 10:31:50 +08008047 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008048 /* force full log commit if subvolume involved. */
8049 root->fs_info->last_trans_log_full_commit = trans->transid;
8050 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008051 ret = btrfs_insert_inode_ref(trans, dest,
8052 new_dentry->d_name.name,
8053 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008054 old_ino,
8055 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008056 if (ret)
8057 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008058 /*
8059 * this is an ugly little race, but the rename is required
8060 * to make sure that if we crash, the inode is either at the
8061 * old name or the new one. pinning the log transaction lets
8062 * us make sure we don't allow a log commit to come in after
8063 * we unlink the name but before we add the new name back in.
8064 */
8065 btrfs_pin_log_trans(root);
8066 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008067 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008068 * make sure the inode gets flushed if it is replacing
8069 * something.
8070 */
Li Zefan33345d012011-04-20 10:31:50 +08008071 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008072 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008073
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008074 inode_inc_iversion(old_dir);
8075 inode_inc_iversion(new_dir);
8076 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008077 old_dir->i_ctime = old_dir->i_mtime = ctime;
8078 new_dir->i_ctime = new_dir->i_mtime = ctime;
8079 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008080
Chris Mason12fcfd22009-03-24 10:24:20 -04008081 if (old_dentry->d_parent != new_dentry->d_parent)
8082 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8083
Li Zefan33345d012011-04-20 10:31:50 +08008084 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008085 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8086 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8087 old_dentry->d_name.name,
8088 old_dentry->d_name.len);
8089 } else {
Al Viro92986792011-03-04 17:14:37 +00008090 ret = __btrfs_unlink_inode(trans, root, old_dir,
8091 old_dentry->d_inode,
8092 old_dentry->d_name.name,
8093 old_dentry->d_name.len);
8094 if (!ret)
8095 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008096 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008097 if (ret) {
8098 btrfs_abort_transaction(trans, root, ret);
8099 goto out_fail;
8100 }
Chris Mason39279cc2007-06-12 06:35:45 -04008101
8102 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008103 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008104 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008105 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008106 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8107 root_objectid = BTRFS_I(new_inode)->location.objectid;
8108 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8109 root_objectid,
8110 new_dentry->d_name.name,
8111 new_dentry->d_name.len);
8112 BUG_ON(new_inode->i_nlink == 0);
8113 } else {
8114 ret = btrfs_unlink_inode(trans, dest, new_dir,
8115 new_dentry->d_inode,
8116 new_dentry->d_name.name,
8117 new_dentry->d_name.len);
8118 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04008119 if (!ret && new_inode->i_nlink == 0)
Chris Masone02119d2008-09-05 16:13:11 -04008120 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008121 if (ret) {
8122 btrfs_abort_transaction(trans, root, ret);
8123 goto out_fail;
8124 }
Chris Mason39279cc2007-06-12 06:35:45 -04008125 }
Josef Bacikaec74772008-07-24 12:12:38 -04008126
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008127 ret = btrfs_add_link(trans, new_dir, old_inode,
8128 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008129 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008130 if (ret) {
8131 btrfs_abort_transaction(trans, root, ret);
8132 goto out_fail;
8133 }
Chris Mason39279cc2007-06-12 06:35:45 -04008134
Li Zefan33345d012011-04-20 10:31:50 +08008135 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008136 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008137 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008138 btrfs_end_log_trans(root);
8139 }
Chris Mason39279cc2007-06-12 06:35:45 -04008140out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008141 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008142out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008143 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008144 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008145
Chris Mason39279cc2007-06-12 06:35:45 -04008146 return ret;
8147}
8148
Miao Xie8ccf6f192012-10-25 09:28:04 +00008149static void btrfs_run_delalloc_work(struct btrfs_work *work)
8150{
8151 struct btrfs_delalloc_work *delalloc_work;
8152
8153 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8154 work);
8155 if (delalloc_work->wait)
8156 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8157 else
8158 filemap_flush(delalloc_work->inode->i_mapping);
8159
8160 if (delalloc_work->delay_iput)
8161 btrfs_add_delayed_iput(delalloc_work->inode);
8162 else
8163 iput(delalloc_work->inode);
8164 complete(&delalloc_work->completion);
8165}
8166
8167struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8168 int wait, int delay_iput)
8169{
8170 struct btrfs_delalloc_work *work;
8171
8172 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8173 if (!work)
8174 return NULL;
8175
8176 init_completion(&work->completion);
8177 INIT_LIST_HEAD(&work->list);
8178 work->inode = inode;
8179 work->wait = wait;
8180 work->delay_iput = delay_iput;
8181 work->work.func = btrfs_run_delalloc_work;
8182
8183 return work;
8184}
8185
8186void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8187{
8188 wait_for_completion(&work->completion);
8189 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8190}
8191
Chris Masond352ac62008-09-29 15:18:18 -04008192/*
8193 * some fairly slow code that needs optimization. This walks the list
8194 * of all the inodes with pending delalloc and forces them to disk.
8195 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00008196static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008197{
Chris Masonea8c2812008-08-04 23:17:27 -04008198 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008199 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008200 struct btrfs_delalloc_work *work, *next;
8201 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008202 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008203 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008204
Miao Xie8ccf6f192012-10-25 09:28:04 +00008205 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008206 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008207
Miao Xieeb73c1b2013-05-15 07:48:22 +00008208 spin_lock(&root->delalloc_lock);
8209 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008210 while (!list_empty(&splice)) {
8211 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008212 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008213
Miao Xieeb73c1b2013-05-15 07:48:22 +00008214 list_move_tail(&binode->delalloc_inodes,
8215 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008216 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008217 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008218 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008219 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008220 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008221 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008222
8223 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8224 if (unlikely(!work)) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -04008225 if (delay_iput)
8226 btrfs_add_delayed_iput(inode);
8227 else
8228 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008229 ret = -ENOMEM;
8230 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008231 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008232 list_add_tail(&work->list, &works);
8233 btrfs_queue_worker(&root->fs_info->flush_workers,
8234 &work->work);
8235
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008236 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008237 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008238 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008239 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008240
Miao Xie1eafa6c2013-01-22 10:49:00 +00008241 list_for_each_entry_safe(work, next, &works, list) {
8242 list_del_init(&work->list);
8243 btrfs_wait_and_free_delalloc_work(work);
8244 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008245 return 0;
8246out:
8247 list_for_each_entry_safe(work, next, &works, list) {
8248 list_del_init(&work->list);
8249 btrfs_wait_and_free_delalloc_work(work);
8250 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008251
Miao Xieeb73c1b2013-05-15 07:48:22 +00008252 if (!list_empty_careful(&splice)) {
8253 spin_lock(&root->delalloc_lock);
8254 list_splice_tail(&splice, &root->delalloc_inodes);
8255 spin_unlock(&root->delalloc_lock);
8256 }
8257 return ret;
8258}
8259
8260int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8261{
8262 int ret;
8263
8264 if (root->fs_info->sb->s_flags & MS_RDONLY)
8265 return -EROFS;
8266
8267 ret = __start_delalloc_inodes(root, delay_iput);
8268 /*
8269 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008270 * we have to make sure the IO is actually started and that
8271 * ordered extents get created before we return
8272 */
8273 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008274 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008275 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008276 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008277 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8278 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008279 }
8280 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008281 return ret;
8282}
8283
8284int btrfs_start_all_delalloc_inodes(struct btrfs_fs_info *fs_info,
8285 int delay_iput)
8286{
8287 struct btrfs_root *root;
8288 struct list_head splice;
8289 int ret;
8290
8291 if (fs_info->sb->s_flags & MS_RDONLY)
8292 return -EROFS;
8293
8294 INIT_LIST_HEAD(&splice);
8295
8296 spin_lock(&fs_info->delalloc_root_lock);
8297 list_splice_init(&fs_info->delalloc_roots, &splice);
8298 while (!list_empty(&splice)) {
8299 root = list_first_entry(&splice, struct btrfs_root,
8300 delalloc_root);
8301 root = btrfs_grab_fs_root(root);
8302 BUG_ON(!root);
8303 list_move_tail(&root->delalloc_root,
8304 &fs_info->delalloc_roots);
8305 spin_unlock(&fs_info->delalloc_root_lock);
8306
8307 ret = __start_delalloc_inodes(root, delay_iput);
8308 btrfs_put_fs_root(root);
8309 if (ret)
8310 goto out;
8311
8312 spin_lock(&fs_info->delalloc_root_lock);
8313 }
8314 spin_unlock(&fs_info->delalloc_root_lock);
8315
8316 atomic_inc(&fs_info->async_submit_draining);
8317 while (atomic_read(&fs_info->nr_async_submits) ||
8318 atomic_read(&fs_info->async_delalloc_pages)) {
8319 wait_event(fs_info->async_submit_wait,
8320 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8321 atomic_read(&fs_info->async_delalloc_pages) == 0));
8322 }
8323 atomic_dec(&fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008324 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008325out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008326 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008327 spin_lock(&fs_info->delalloc_root_lock);
8328 list_splice_tail(&splice, &fs_info->delalloc_roots);
8329 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008330 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008331 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008332}
8333
Chris Mason39279cc2007-06-12 06:35:45 -04008334static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8335 const char *symname)
8336{
8337 struct btrfs_trans_handle *trans;
8338 struct btrfs_root *root = BTRFS_I(dir)->root;
8339 struct btrfs_path *path;
8340 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008341 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008342 int err;
8343 int drop_inode = 0;
8344 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008345 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008346 int name_len;
8347 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008348 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008349 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008350 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008351
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008352 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -04008353 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8354 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008355
Josef Bacik9ed74f22009-09-11 16:12:44 -04008356 /*
8357 * 2 items for inode item and ref
8358 * 2 items for dir items
8359 * 1 item for xattr if selinux is on
8360 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008361 trans = btrfs_start_transaction(root, 5);
8362 if (IS_ERR(trans))
8363 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008364
Li Zefan581bb052011-04-20 10:06:11 +08008365 err = btrfs_find_free_ino(root, &objectid);
8366 if (err)
8367 goto out_unlock;
8368
Josef Bacikaec74772008-07-24 12:12:38 -04008369 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008370 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008371 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008372 if (IS_ERR(inode)) {
8373 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008374 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008375 }
Chris Mason39279cc2007-06-12 06:35:45 -04008376
Eric Paris2a7dba32011-02-01 11:05:39 -05008377 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008378 if (err) {
8379 drop_inode = 1;
8380 goto out_unlock;
8381 }
8382
Casey Schauflerad19db72011-12-15 10:09:07 -05008383 /*
8384 * If the active LSM wants to access the inode during
8385 * d_instantiate it needs these. Smack checks to see
8386 * if the filesystem supports xattrs by looking at the
8387 * ops vector.
8388 */
8389 inode->i_fop = &btrfs_file_operations;
8390 inode->i_op = &btrfs_file_inode_operations;
8391
Josef Bacika1b075d2010-11-19 20:36:11 +00008392 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008393 if (err)
8394 drop_inode = 1;
8395 else {
8396 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008397 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008398 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008399 }
Chris Mason39279cc2007-06-12 06:35:45 -04008400 if (drop_inode)
8401 goto out_unlock;
8402
8403 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008404 if (!path) {
8405 err = -ENOMEM;
8406 drop_inode = 1;
8407 goto out_unlock;
8408 }
Li Zefan33345d012011-04-20 10:31:50 +08008409 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008410 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008411 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8412 datasize = btrfs_file_extent_calc_inline_size(name_len);
8413 err = btrfs_insert_empty_item(trans, root, path, &key,
8414 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008415 if (err) {
8416 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008417 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008418 goto out_unlock;
8419 }
Chris Mason5f39d392007-10-15 16:14:19 -04008420 leaf = path->nodes[0];
8421 ei = btrfs_item_ptr(leaf, path->slots[0],
8422 struct btrfs_file_extent_item);
8423 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8424 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008425 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008426 btrfs_set_file_extent_encryption(leaf, ei, 0);
8427 btrfs_set_file_extent_compression(leaf, ei, 0);
8428 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8429 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8430
Chris Mason39279cc2007-06-12 06:35:45 -04008431 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008432 write_extent_buffer(leaf, symname, ptr, name_len);
8433 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008434 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008435
Chris Mason39279cc2007-06-12 06:35:45 -04008436 inode->i_op = &btrfs_symlink_inode_operations;
8437 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008438 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008439 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01008440 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04008441 err = btrfs_update_inode(trans, root, inode);
8442 if (err)
8443 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008444
8445out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008446 if (!err)
8447 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008448 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008449 if (drop_inode) {
8450 inode_dec_link_count(inode);
8451 iput(inode);
8452 }
Liu Bob53d3f52012-11-14 14:34:34 +00008453 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008454 return err;
8455}
Chris Mason16432982008-04-10 10:23:21 -04008456
Josef Bacik0af3d002010-06-21 14:48:16 -04008457static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8458 u64 start, u64 num_bytes, u64 min_size,
8459 loff_t actual_len, u64 *alloc_hint,
8460 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008461{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008462 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8463 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008464 struct btrfs_root *root = BTRFS_I(inode)->root;
8465 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008466 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008467 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008468 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008469 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008470 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008471
Josef Bacik0af3d002010-06-21 14:48:16 -04008472 if (trans)
8473 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008474 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008475 if (own_trans) {
8476 trans = btrfs_start_transaction(root, 3);
8477 if (IS_ERR(trans)) {
8478 ret = PTR_ERR(trans);
8479 break;
8480 }
Yan Zhengd899e052008-10-30 14:25:28 -04008481 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008482
Chris Mason154ea282013-03-05 11:11:26 -05008483 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8484 cur_bytes = max(cur_bytes, min_size);
Josef Bacik00361582013-08-14 14:02:47 -04008485 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
8486 *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008487 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008488 if (own_trans)
8489 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008490 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008491 }
8492
Yan Zhengd899e052008-10-30 14:25:28 -04008493 ret = insert_reserved_file_extent(trans, inode,
8494 cur_offset, ins.objectid,
8495 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008496 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008497 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008498 if (ret) {
8499 btrfs_abort_transaction(trans, root, ret);
8500 if (own_trans)
8501 btrfs_end_transaction(trans, root);
8502 break;
8503 }
Chris Masona1ed8352009-09-11 12:27:37 -04008504 btrfs_drop_extent_cache(inode, cur_offset,
8505 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008506
Josef Bacik5dc562c2012-08-17 13:14:17 -04008507 em = alloc_extent_map();
8508 if (!em) {
8509 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8510 &BTRFS_I(inode)->runtime_flags);
8511 goto next;
8512 }
8513
8514 em->start = cur_offset;
8515 em->orig_start = cur_offset;
8516 em->len = ins.offset;
8517 em->block_start = ins.objectid;
8518 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008519 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008520 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008521 em->bdev = root->fs_info->fs_devices->latest_bdev;
8522 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8523 em->generation = trans->transid;
8524
8525 while (1) {
8526 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008527 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008528 write_unlock(&em_tree->lock);
8529 if (ret != -EEXIST)
8530 break;
8531 btrfs_drop_extent_cache(inode, cur_offset,
8532 cur_offset + ins.offset - 1,
8533 0);
8534 }
8535 free_extent_map(em);
8536next:
Yan Zhengd899e052008-10-30 14:25:28 -04008537 num_bytes -= ins.offset;
8538 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008539 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008540
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008541 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008542 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008543 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008544 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008545 (actual_len > inode->i_size) &&
8546 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008547 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008548 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008549 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008550 i_size = cur_offset;
8551 i_size_write(inode, i_size);
8552 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008553 }
8554
Yan Zhengd899e052008-10-30 14:25:28 -04008555 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008556
8557 if (ret) {
8558 btrfs_abort_transaction(trans, root, ret);
8559 if (own_trans)
8560 btrfs_end_transaction(trans, root);
8561 break;
8562 }
Yan Zhengd899e052008-10-30 14:25:28 -04008563
Josef Bacik0af3d002010-06-21 14:48:16 -04008564 if (own_trans)
8565 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008566 }
Yan Zhengd899e052008-10-30 14:25:28 -04008567 return ret;
8568}
8569
Josef Bacik0af3d002010-06-21 14:48:16 -04008570int btrfs_prealloc_file_range(struct inode *inode, int mode,
8571 u64 start, u64 num_bytes, u64 min_size,
8572 loff_t actual_len, u64 *alloc_hint)
8573{
8574 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8575 min_size, actual_len, alloc_hint,
8576 NULL);
8577}
8578
8579int btrfs_prealloc_file_range_trans(struct inode *inode,
8580 struct btrfs_trans_handle *trans, int mode,
8581 u64 start, u64 num_bytes, u64 min_size,
8582 loff_t actual_len, u64 *alloc_hint)
8583{
8584 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8585 min_size, actual_len, alloc_hint, trans);
8586}
8587
Chris Masone6dcd2d2008-07-17 12:53:50 -04008588static int btrfs_set_page_dirty(struct page *page)
8589{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008590 return __set_page_dirty_nobuffers(page);
8591}
8592
Al Viro10556cb2011-06-20 19:28:19 -04008593static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008594{
Li Zefanb83cc962010-12-20 16:04:08 +08008595 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008596 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008597
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008598 if (mask & MAY_WRITE &&
8599 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8600 if (btrfs_root_readonly(root))
8601 return -EROFS;
8602 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8603 return -EACCES;
8604 }
Al Viro2830ba72011-06-20 19:16:29 -04008605 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008606}
Chris Mason39279cc2007-06-12 06:35:45 -04008607
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008608static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008609 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008610 .lookup = btrfs_lookup,
8611 .create = btrfs_create,
8612 .unlink = btrfs_unlink,
8613 .link = btrfs_link,
8614 .mkdir = btrfs_mkdir,
8615 .rmdir = btrfs_rmdir,
8616 .rename = btrfs_rename,
8617 .symlink = btrfs_symlink,
8618 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008619 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008620 .setxattr = btrfs_setxattr,
8621 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008622 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008623 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008624 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008625 .get_acl = btrfs_get_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07008626 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008627};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008628static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008629 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008630 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008631 .get_acl = btrfs_get_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -07008632 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008633};
Yan, Zheng76dda932009-09-21 16:00:26 -04008634
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008635static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008636 .llseek = generic_file_llseek,
8637 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04008638 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008639 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008640#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008641 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008642#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008643 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008644 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008645};
8646
Chris Masond1310b22008-01-24 16:13:08 -05008647static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008648 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008649 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008650 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008651 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008652 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008653 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008654 .set_bit_hook = btrfs_set_bit_hook,
8655 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008656 .merge_extent_hook = btrfs_merge_extent_hook,
8657 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008658};
8659
Chris Mason35054392009-01-21 13:11:13 -05008660/*
8661 * btrfs doesn't support the bmap operation because swapfiles
8662 * use bmap to make a mapping of extents in the file. They assume
8663 * these extents won't change over the life of the file and they
8664 * use the bmap result to do IO directly to the drive.
8665 *
8666 * the btrfs bmap call would return logical addresses that aren't
8667 * suitable for IO and they also will change frequently as COW
8668 * operations happen. So, swapfile + btrfs == corruption.
8669 *
8670 * For now we're avoiding this by dropping bmap.
8671 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008672static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008673 .readpage = btrfs_readpage,
8674 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008675 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008676 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008677 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008678 .invalidatepage = btrfs_invalidatepage,
8679 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008680 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008681 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008682};
8683
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008684static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008685 .readpage = btrfs_readpage,
8686 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008687 .invalidatepage = btrfs_invalidatepage,
8688 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008689};
8690
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008691static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008692 .getattr = btrfs_getattr,
8693 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008694 .setxattr = btrfs_setxattr,
8695 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008696 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008697 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008698 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008699 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008700 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008701 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008702};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008703static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008704 .getattr = btrfs_getattr,
8705 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008706 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008707 .setxattr = btrfs_setxattr,
8708 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008709 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008710 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008711 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008712 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008713};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008714static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008715 .readlink = generic_readlink,
8716 .follow_link = page_follow_link_light,
8717 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008718 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008719 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008720 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008721 .setxattr = btrfs_setxattr,
8722 .getxattr = btrfs_getxattr,
8723 .listxattr = btrfs_listxattr,
8724 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008725 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008726 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008727};
Yan, Zheng76dda932009-09-21 16:00:26 -04008728
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008729const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008730 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008731 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008732};