blob: 27fa81977f862038997f19f76b8d8555437c7760 [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 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400233static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400234 struct btrfs_root *root,
235 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000236 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400237 struct page **compressed_pages)
238{
239 u64 isize = i_size_read(inode);
240 u64 actual_end = min(end + 1, isize);
241 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000242 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400243 u64 data_len = inline_len;
244 int ret;
245
246 if (compressed_size)
247 data_len = compressed_size;
248
249 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400250 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400251 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
252 (!compressed_size &&
253 (actual_end & (root->sectorsize - 1)) == 0) ||
254 end + 1 < isize ||
255 data_len > root->fs_info->max_inline) {
256 return 1;
257 }
258
Josef Bacik26714852012-08-29 12:24:27 -0400259 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100260 if (ret)
261 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400262
263 if (isize > actual_end)
264 inline_len = min_t(u64, isize, actual_end);
265 ret = insert_inline_extent(trans, root, inode, start,
266 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000267 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400268 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100269 btrfs_abort_transaction(trans, root, ret);
270 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400271 } else if (ret == -ENOSPC) {
272 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100273 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400274
Josef Bacikbdc20e62013-02-28 13:23:38 -0500275 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400276 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400277 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400278 return 0;
279}
280
Chris Mason771ed682008-11-06 22:02:51 -0500281struct async_extent {
282 u64 start;
283 u64 ram_size;
284 u64 compressed_size;
285 struct page **pages;
286 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800287 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500288 struct list_head list;
289};
290
291struct async_cow {
292 struct inode *inode;
293 struct btrfs_root *root;
294 struct page *locked_page;
295 u64 start;
296 u64 end;
297 struct list_head extents;
298 struct btrfs_work work;
299};
300
301static noinline int add_async_extent(struct async_cow *cow,
302 u64 start, u64 ram_size,
303 u64 compressed_size,
304 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800305 unsigned long nr_pages,
306 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500307{
308 struct async_extent *async_extent;
309
310 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100311 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500312 async_extent->start = start;
313 async_extent->ram_size = ram_size;
314 async_extent->compressed_size = compressed_size;
315 async_extent->pages = pages;
316 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800317 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500318 list_add_tail(&async_extent->list, &cow->extents);
319 return 0;
320}
321
Chris Masonc8b97812008-10-29 14:49:59 -0400322/*
Chris Mason771ed682008-11-06 22:02:51 -0500323 * we create compressed extents in two phases. The first
324 * phase compresses a range of pages that have already been
325 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400326 *
Chris Mason771ed682008-11-06 22:02:51 -0500327 * This is done inside an ordered work queue, and the compression
328 * is spread across many cpus. The actual IO submission is step
329 * two, and the ordered work queue takes care of making sure that
330 * happens in the same order things were put onto the queue by
331 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400332 *
Chris Mason771ed682008-11-06 22:02:51 -0500333 * If this code finds it can't get good compression, it puts an
334 * entry onto the work queue to write the uncompressed bytes. This
335 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300336 * are written in the same order that the flusher thread sent them
337 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400338 */
Chris Mason771ed682008-11-06 22:02:51 -0500339static noinline int compress_file_range(struct inode *inode,
340 struct page *locked_page,
341 u64 start, u64 end,
342 struct async_cow *async_cow,
343 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400344{
345 struct btrfs_root *root = BTRFS_I(inode)->root;
346 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400347 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400348 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400349 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500350 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400351 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400352 struct page **pages = NULL;
353 unsigned long nr_pages;
354 unsigned long nr_pages_ret = 0;
355 unsigned long total_compressed = 0;
356 unsigned long total_in = 0;
357 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500358 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400359 int i;
360 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800361 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400362 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400363
Liu Bo4cb13e52012-03-29 09:57:45 -0400364 /* if this is a small write inside eof, kick off a defrag */
365 if ((end - start + 1) < 16 * 1024 &&
366 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400367 btrfs_add_inode_defrag(NULL, inode);
368
Chris Mason42dc7ba2008-12-15 11:44:56 -0500369 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400370again:
371 will_compress = 0;
372 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
373 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
374
Chris Masonf03d9301f2009-02-04 09:31:06 -0500375 /*
376 * we don't want to send crud past the end of i_size through
377 * compression, that's just a waste of CPU time. So, if the
378 * end of the file is before the start of our current
379 * requested range of bytes, we bail out to the uncompressed
380 * cleanup code that can deal with all of this.
381 *
382 * It isn't really the fastest way to fix things, but this is a
383 * very uncommon corner.
384 */
385 if (actual_end <= start)
386 goto cleanup_and_bail_uncompressed;
387
Chris Masonc8b97812008-10-29 14:49:59 -0400388 total_compressed = actual_end - start;
389
390 /* we want to make sure that amount of ram required to uncompress
391 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500392 * of a compressed extent to 128k. This is a crucial number
393 * because it also controls how easily we can spread reads across
394 * cpus for decompression.
395 *
396 * We also want to make sure the amount of IO required to do
397 * a random read is reasonably small, so we limit the size of
398 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400399 */
400 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000401 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500402 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400403 total_in = 0;
404 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400405
Chris Mason771ed682008-11-06 22:02:51 -0500406 /*
407 * we do compression for mount -o compress and when the
408 * inode has not been flagged as nocompress. This flag can
409 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400410 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200411 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500412 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000413 (BTRFS_I(inode)->force_compress) ||
414 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400415 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400416 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800417 if (!pages) {
418 /* just bail out to the uncompressed code */
419 goto cont;
420 }
Chris Mason179e29e2007-11-01 11:28:41 -0400421
Li Zefan261507a02010-12-17 14:21:50 +0800422 if (BTRFS_I(inode)->force_compress)
423 compress_type = BTRFS_I(inode)->force_compress;
424
Chris Mason4adaa612013-03-26 13:07:00 -0400425 /*
426 * we need to call clear_page_dirty_for_io on each
427 * page in the range. Otherwise applications with the file
428 * mmap'd can wander in and change the page contents while
429 * we are compressing them.
430 *
431 * If the compression fails for any reason, we set the pages
432 * dirty again later on.
433 */
434 extent_range_clear_dirty_for_io(inode, start, end);
435 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800436 ret = btrfs_compress_pages(compress_type,
437 inode->i_mapping, start,
438 total_compressed, pages,
439 nr_pages, &nr_pages_ret,
440 &total_in,
441 &total_compressed,
442 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400443
444 if (!ret) {
445 unsigned long offset = total_compressed &
446 (PAGE_CACHE_SIZE - 1);
447 struct page *page = pages[nr_pages_ret - 1];
448 char *kaddr;
449
450 /* zero the tail end of the last page, we might be
451 * sending it down to disk
452 */
453 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800454 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400455 memset(kaddr + offset, 0,
456 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800457 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400458 }
459 will_compress = 1;
460 }
461 }
Li Zefan560f7d72011-09-08 10:22:01 +0800462cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400463 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400464 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100465 if (IS_ERR(trans)) {
466 ret = PTR_ERR(trans);
467 trans = NULL;
468 goto cleanup_and_out;
469 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400470 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500471
Chris Masonc8b97812008-10-29 14:49:59 -0400472 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500473 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400474 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500475 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400476 */
477 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000478 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400479 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500480 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400481 ret = cow_file_range_inline(trans, root, inode,
482 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000483 total_compressed,
484 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400485 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100486 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400487 unsigned long clear_flags = EXTENT_DELALLOC |
488 EXTENT_DEFRAG;
489 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
490
Chris Mason771ed682008-11-06 22:02:51 -0500491 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100492 * inline extent creation worked or returned error,
493 * we don't need to create any more async work items.
494 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500495 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400496 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400497 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400498 PAGE_CLEAR_DIRTY |
499 PAGE_SET_WRITEBACK |
500 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000501 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400502 goto free_pages_out;
503 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000504 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400505 }
506
507 if (will_compress) {
508 /*
509 * we aren't doing an inline extent round the compressed size
510 * up to a block size boundary so the allocator does sane
511 * things
512 */
Qu Wenruofda28322013-02-26 08:10:22 +0000513 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400514
515 /*
516 * one last check to make sure the compression is really a
517 * win, compare the page count read with the blocks on disk
518 */
Qu Wenruofda28322013-02-26 08:10:22 +0000519 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400520 if (total_compressed >= total_in) {
521 will_compress = 0;
522 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400523 num_bytes = total_in;
524 }
525 }
526 if (!will_compress && pages) {
527 /*
528 * the compression code ran but failed to make things smaller,
529 * free any pages it allocated and our page pointer array
530 */
531 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400532 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400533 page_cache_release(pages[i]);
534 }
535 kfree(pages);
536 pages = NULL;
537 total_compressed = 0;
538 nr_pages_ret = 0;
539
540 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500541 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
542 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500543 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500544 }
Chris Masonc8b97812008-10-29 14:49:59 -0400545 }
Chris Mason771ed682008-11-06 22:02:51 -0500546 if (will_compress) {
547 *num_added += 1;
548
549 /* the async work queues will take care of doing actual
550 * allocation on disk for these compressed pages,
551 * and will submit them to the elevator.
552 */
553 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800554 total_compressed, pages, nr_pages_ret,
555 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500556
Yan, Zheng24ae6362010-12-06 07:02:36 +0000557 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500558 start += num_bytes;
559 pages = NULL;
560 cond_resched();
561 goto again;
562 }
563 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500564cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500565 /*
566 * No compression, but we still need to write the pages in
567 * the file we've been given so far. redirty the locked
568 * page if it corresponds to our extent and set things up
569 * for the async work queue to run cow_file_range to do
570 * the normal delalloc dance
571 */
572 if (page_offset(locked_page) >= start &&
573 page_offset(locked_page) <= end) {
574 __set_page_dirty_nobuffers(locked_page);
575 /* unlocked later on in the async handlers */
576 }
Chris Mason4adaa612013-03-26 13:07:00 -0400577 if (redirty)
578 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800579 add_async_extent(async_cow, start, end - start + 1,
580 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500581 *num_added += 1;
582 }
583
584out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100585 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500586
587free_pages_out:
588 for (i = 0; i < nr_pages_ret; i++) {
589 WARN_ON(pages[i]->mapping);
590 page_cache_release(pages[i]);
591 }
Chris Masond3977122009-01-05 21:25:51 -0500592 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500593
594 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100595
596cleanup_and_out:
Josef Bacikc2790a22013-07-29 11:20:47 -0400597 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400598 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
599 EXTENT_DEFRAG, PAGE_UNLOCK |
600 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
601 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100602 if (!trans || IS_ERR(trans))
603 btrfs_error(root->fs_info, ret, "Failed to join transaction");
604 else
605 btrfs_abort_transaction(trans, root, ret);
606 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500607}
608
609/*
610 * phase two of compressed writeback. This is the ordered portion
611 * of the code, which only gets called in the order the work was
612 * queued. We walk all the async extents created by compress_file_range
613 * and send them down to the disk.
614 */
615static noinline int submit_compressed_extents(struct inode *inode,
616 struct async_cow *async_cow)
617{
618 struct async_extent *async_extent;
619 u64 alloc_hint = 0;
620 struct btrfs_trans_handle *trans;
621 struct btrfs_key ins;
622 struct extent_map *em;
623 struct btrfs_root *root = BTRFS_I(inode)->root;
624 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
625 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500626 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500627
628 if (list_empty(&async_cow->extents))
629 return 0;
630
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500631again:
Chris Masond3977122009-01-05 21:25:51 -0500632 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500633 async_extent = list_entry(async_cow->extents.next,
634 struct async_extent, list);
635 list_del(&async_extent->list);
636
637 io_tree = &BTRFS_I(inode)->io_tree;
638
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500639retry:
Chris Mason771ed682008-11-06 22:02:51 -0500640 /* did the compression code fall back to uncompressed IO? */
641 if (!async_extent->pages) {
642 int page_started = 0;
643 unsigned long nr_written = 0;
644
645 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000646 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100647 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500648
649 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500650 ret = cow_file_range(inode, async_cow->locked_page,
651 async_extent->start,
652 async_extent->start +
653 async_extent->ram_size - 1,
654 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500655
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100656 /* JDM XXX */
657
Chris Mason771ed682008-11-06 22:02:51 -0500658 /*
659 * if page_started, cow_file_range inserted an
660 * inline extent and took care of all the unlocking
661 * and IO for us. Otherwise, we need to submit
662 * all those pages down to the drive.
663 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500664 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500665 extent_write_locked_range(io_tree,
666 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500667 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500668 async_extent->ram_size - 1,
669 btrfs_get_extent,
670 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500671 else if (ret)
672 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500673 kfree(async_extent);
674 cond_resched();
675 continue;
676 }
677
678 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100679 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500680
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400681 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100682 if (IS_ERR(trans)) {
683 ret = PTR_ERR(trans);
684 } else {
685 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
686 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500687 async_extent->compressed_size,
688 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500689 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600690 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100691 btrfs_abort_transaction(trans, root, ret);
692 btrfs_end_transaction(trans, root);
693 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000694
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500695 if (ret) {
696 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500697
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500698 for (i = 0; i < async_extent->nr_pages; i++) {
699 WARN_ON(async_extent->pages[i]->mapping);
700 page_cache_release(async_extent->pages[i]);
701 }
702 kfree(async_extent->pages);
703 async_extent->nr_pages = 0;
704 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500705
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400706 if (ret == -ENOSPC) {
707 unlock_extent(io_tree, async_extent->start,
708 async_extent->start +
709 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100710 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400711 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500712 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500713 }
714
Yan, Zhengc2167752009-11-12 09:34:21 +0000715 /*
716 * here we're doing allocation and writeback of the
717 * compressed pages
718 */
719 btrfs_drop_extent_cache(inode, async_extent->start,
720 async_extent->start +
721 async_extent->ram_size - 1, 0);
722
David Sterba172ddd62011-04-21 00:48:27 +0200723 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000724 if (!em) {
725 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500726 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000727 }
Chris Mason771ed682008-11-06 22:02:51 -0500728 em->start = async_extent->start;
729 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500730 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400731 em->mod_start = em->start;
732 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500733
734 em->block_start = ins.objectid;
735 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500736 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400737 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500738 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800739 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500740 set_bit(EXTENT_FLAG_PINNED, &em->flags);
741 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400742 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500743
Chris Masond3977122009-01-05 21:25:51 -0500744 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400745 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400746 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400747 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500748 if (ret != -EEXIST) {
749 free_extent_map(em);
750 break;
751 }
752 btrfs_drop_extent_cache(inode, async_extent->start,
753 async_extent->start +
754 async_extent->ram_size - 1, 0);
755 }
756
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500757 if (ret)
758 goto out_free_reserve;
759
Li Zefan261507a02010-12-17 14:21:50 +0800760 ret = btrfs_add_ordered_extent_compress(inode,
761 async_extent->start,
762 ins.objectid,
763 async_extent->ram_size,
764 ins.offset,
765 BTRFS_ORDERED_COMPRESSED,
766 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500767 if (ret)
768 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500769
Chris Mason771ed682008-11-06 22:02:51 -0500770 /*
771 * clear dirty, set writeback and unlock the pages.
772 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400773 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400774 async_extent->start +
775 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400776 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
777 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400778 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500779 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500780 async_extent->start,
781 async_extent->ram_size,
782 ins.objectid,
783 ins.offset, async_extent->pages,
784 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500785 alloc_hint = ins.objectid + ins.offset;
786 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500787 if (ret)
788 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500789 cond_resched();
790 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100791 ret = 0;
792out:
793 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500794out_free_reserve:
795 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100796out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400797 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500798 async_extent->start +
799 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400800 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400801 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
802 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
803 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100804 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500805 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500806}
807
Josef Bacik4b46fce2010-05-23 11:00:55 -0400808static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
809 u64 num_bytes)
810{
811 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
812 struct extent_map *em;
813 u64 alloc_hint = 0;
814
815 read_lock(&em_tree->lock);
816 em = search_extent_mapping(em_tree, start, num_bytes);
817 if (em) {
818 /*
819 * if block start isn't an actual block number then find the
820 * first block in this inode and use that as a hint. If that
821 * block is also bogus then just don't worry about it.
822 */
823 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
824 free_extent_map(em);
825 em = search_extent_mapping(em_tree, 0, 0);
826 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
827 alloc_hint = em->block_start;
828 if (em)
829 free_extent_map(em);
830 } else {
831 alloc_hint = em->block_start;
832 free_extent_map(em);
833 }
834 }
835 read_unlock(&em_tree->lock);
836
837 return alloc_hint;
838}
839
Chris Mason771ed682008-11-06 22:02:51 -0500840/*
841 * when extent_io.c finds a delayed allocation range in the file,
842 * the call backs end up in this code. The basic idea is to
843 * allocate extents on disk for the range, and create ordered data structs
844 * in ram to track those extents.
845 *
846 * locked_page is the page that writepage had locked already. We use
847 * it to make sure we don't do extra locks or unlocks.
848 *
849 * *page_started is set to one if we unlock locked_page and do everything
850 * required to start IO on it. It may be clean and already done with
851 * IO when we return.
852 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000853static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
854 struct inode *inode,
855 struct btrfs_root *root,
856 struct page *locked_page,
857 u64 start, u64 end, int *page_started,
858 unsigned long *nr_written,
859 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500860{
Chris Mason771ed682008-11-06 22:02:51 -0500861 u64 alloc_hint = 0;
862 u64 num_bytes;
863 unsigned long ram_size;
864 u64 disk_num_bytes;
865 u64 cur_alloc_size;
866 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500867 struct btrfs_key ins;
868 struct extent_map *em;
869 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
870 int ret = 0;
871
Liu Bo83eea1f2012-07-10 05:28:39 -0600872 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500873
Qu Wenruofda28322013-02-26 08:10:22 +0000874 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500875 num_bytes = max(blocksize, num_bytes);
876 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500877
Chris Mason4cb53002011-05-24 15:35:30 -0400878 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400879 if (num_bytes < 64 * 1024 &&
880 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400881 btrfs_add_inode_defrag(trans, inode);
882
Chris Mason771ed682008-11-06 22:02:51 -0500883 if (start == 0) {
884 /* lets try to make an inline extent */
885 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000886 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500887 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400888 extent_clear_unlock_delalloc(inode, start, end, NULL,
889 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400890 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400891 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
892 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000893
Chris Mason771ed682008-11-06 22:02:51 -0500894 *nr_written = *nr_written +
895 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
896 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500897 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100898 } else if (ret < 0) {
899 btrfs_abort_transaction(trans, root, ret);
900 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500901 }
902 }
Chris Masonc8b97812008-10-29 14:49:59 -0400903
904 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200905 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400906
Josef Bacik4b46fce2010-05-23 11:00:55 -0400907 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400908 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400909
Chris Masond3977122009-01-05 21:25:51 -0500910 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400911 unsigned long op;
912
Josef Bacik287a0ab2010-03-19 18:07:23 +0000913 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400914 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500915 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500916 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100917 if (ret < 0) {
918 btrfs_abort_transaction(trans, root, ret);
919 goto out_unlock;
920 }
Chris Masond3977122009-01-05 21:25:51 -0500921
David Sterba172ddd62011-04-21 00:48:27 +0200922 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000923 if (!em) {
924 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000925 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000926 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400927 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500928 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500929 ram_size = ins.offset;
930 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400931 em->mod_start = em->start;
932 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400933
Chris Masone6dcd2d2008-07-17 12:53:50 -0400934 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400935 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500936 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400937 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400938 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400939 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400940 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400941
Chris Masond3977122009-01-05 21:25:51 -0500942 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400943 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400944 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400945 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400946 if (ret != -EEXIST) {
947 free_extent_map(em);
948 break;
949 }
950 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400951 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400952 }
Liu Boace68ba2013-04-22 10:53:47 +0000953 if (ret)
954 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400955
Chris Mason98d20f62008-04-14 09:46:10 -0400956 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400957 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500958 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000959 if (ret)
960 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400961
Yan Zheng17d217f2008-12-12 10:03:38 -0500962 if (root->root_key.objectid ==
963 BTRFS_DATA_RELOC_TREE_OBJECTID) {
964 ret = btrfs_reloc_clone_csums(inode, start,
965 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100966 if (ret) {
967 btrfs_abort_transaction(trans, root, ret);
Liu Boace68ba2013-04-22 10:53:47 +0000968 goto out_reserve;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100969 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500970 }
971
Chris Masond3977122009-01-05 21:25:51 -0500972 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400973 break;
Chris Masond3977122009-01-05 21:25:51 -0500974
Chris Masonc8b97812008-10-29 14:49:59 -0400975 /* we're not doing compressed IO, don't unlock the first
976 * page (which the caller expects to stay locked), don't
977 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400978 *
979 * Do set the Private2 bit so we know this page was properly
980 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400981 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400982 op = unlock ? PAGE_UNLOCK : 0;
983 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -0400984
Josef Bacikc2790a22013-07-29 11:20:47 -0400985 extent_clear_unlock_delalloc(inode, start,
986 start + ram_size - 1, locked_page,
987 EXTENT_LOCKED | EXTENT_DELALLOC,
988 op);
Chris Masonc8b97812008-10-29 14:49:59 -0400989 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500990 num_bytes -= cur_alloc_size;
991 alloc_hint = ins.objectid + ins.offset;
992 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -0400993 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100994out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500995 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000996
Liu Boace68ba2013-04-22 10:53:47 +0000997out_reserve:
998 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100999out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001000 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001001 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1002 EXTENT_DELALLOC | EXTENT_DEFRAG,
1003 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1004 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001005 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001006}
Chris Masonc8b97812008-10-29 14:49:59 -04001007
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001008static noinline int cow_file_range(struct inode *inode,
1009 struct page *locked_page,
1010 u64 start, u64 end, int *page_started,
1011 unsigned long *nr_written,
1012 int unlock)
1013{
1014 struct btrfs_trans_handle *trans;
1015 struct btrfs_root *root = BTRFS_I(inode)->root;
1016 int ret;
1017
1018 trans = btrfs_join_transaction(root);
1019 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001020 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1021 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001022 EXTENT_DO_ACCOUNTING |
1023 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001024 PAGE_CLEAR_DIRTY |
1025 PAGE_SET_WRITEBACK |
1026 PAGE_END_WRITEBACK);
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001027 return PTR_ERR(trans);
1028 }
1029 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1030
1031 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1032 page_started, nr_written, unlock);
1033
1034 btrfs_end_transaction(trans, root);
1035
1036 return ret;
1037}
1038
Chris Mason771ed682008-11-06 22:02:51 -05001039/*
1040 * work queue call back to started compression on a file and pages
1041 */
1042static noinline void async_cow_start(struct btrfs_work *work)
1043{
1044 struct async_cow *async_cow;
1045 int num_added = 0;
1046 async_cow = container_of(work, struct async_cow, work);
1047
1048 compress_file_range(async_cow->inode, async_cow->locked_page,
1049 async_cow->start, async_cow->end, async_cow,
1050 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001051 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001052 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001053 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001054 }
Chris Mason771ed682008-11-06 22:02:51 -05001055}
1056
1057/*
1058 * work queue call back to submit previously compressed pages
1059 */
1060static noinline void async_cow_submit(struct btrfs_work *work)
1061{
1062 struct async_cow *async_cow;
1063 struct btrfs_root *root;
1064 unsigned long nr_pages;
1065
1066 async_cow = container_of(work, struct async_cow, work);
1067
1068 root = async_cow->root;
1069 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1070 PAGE_CACHE_SHIFT;
1071
Josef Bacik66657b32012-08-01 15:36:24 -04001072 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001073 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001074 waitqueue_active(&root->fs_info->async_submit_wait))
1075 wake_up(&root->fs_info->async_submit_wait);
1076
Chris Masond3977122009-01-05 21:25:51 -05001077 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001078 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001079}
Chris Masonc8b97812008-10-29 14:49:59 -04001080
Chris Mason771ed682008-11-06 22:02:51 -05001081static noinline void async_cow_free(struct btrfs_work *work)
1082{
1083 struct async_cow *async_cow;
1084 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001085 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001086 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001087 kfree(async_cow);
1088}
1089
1090static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1091 u64 start, u64 end, int *page_started,
1092 unsigned long *nr_written)
1093{
1094 struct async_cow *async_cow;
1095 struct btrfs_root *root = BTRFS_I(inode)->root;
1096 unsigned long nr_pages;
1097 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001098 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001099
Chris Masona3429ab2009-10-08 12:30:20 -04001100 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1101 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001102 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001103 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001104 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001105 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001106 async_cow->root = root;
1107 async_cow->locked_page = locked_page;
1108 async_cow->start = start;
1109
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001110 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001111 cur_end = end;
1112 else
1113 cur_end = min(end, start + 512 * 1024 - 1);
1114
1115 async_cow->end = cur_end;
1116 INIT_LIST_HEAD(&async_cow->extents);
1117
1118 async_cow->work.func = async_cow_start;
1119 async_cow->work.ordered_func = async_cow_submit;
1120 async_cow->work.ordered_free = async_cow_free;
1121 async_cow->work.flags = 0;
1122
Chris Mason771ed682008-11-06 22:02:51 -05001123 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1124 PAGE_CACHE_SHIFT;
1125 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1126
1127 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1128 &async_cow->work);
1129
1130 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1131 wait_event(root->fs_info->async_submit_wait,
1132 (atomic_read(&root->fs_info->async_delalloc_pages) <
1133 limit));
1134 }
1135
Chris Masond3977122009-01-05 21:25:51 -05001136 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001137 atomic_read(&root->fs_info->async_delalloc_pages)) {
1138 wait_event(root->fs_info->async_submit_wait,
1139 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1140 0));
1141 }
1142
1143 *nr_written += nr_pages;
1144 start = cur_end + 1;
1145 }
1146 *page_started = 1;
1147 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001148}
1149
Chris Masond3977122009-01-05 21:25:51 -05001150static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001151 u64 bytenr, u64 num_bytes)
1152{
1153 int ret;
1154 struct btrfs_ordered_sum *sums;
1155 LIST_HEAD(list);
1156
Yan Zheng07d400a2009-01-06 11:42:00 -05001157 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001158 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001159 if (ret == 0 && list_empty(&list))
1160 return 0;
1161
1162 while (!list_empty(&list)) {
1163 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1164 list_del(&sums->list);
1165 kfree(sums);
1166 }
1167 return 1;
1168}
1169
Chris Masond352ac62008-09-29 15:18:18 -04001170/*
1171 * when nowcow writeback call back. This checks for snapshots or COW copies
1172 * of the extents that exist in the file, and COWs the file as required.
1173 *
1174 * If no cow copies or snapshots exist, we write directly to the existing
1175 * blocks on disk
1176 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001177static noinline int run_delalloc_nocow(struct inode *inode,
1178 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001179 u64 start, u64 end, int *page_started, int force,
1180 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001181{
Chris Masonbe20aa92007-12-17 20:14:01 -05001182 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001183 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001184 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001185 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001186 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001187 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001188 u64 cow_start;
1189 u64 cur_offset;
1190 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001191 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001192 u64 disk_bytenr;
1193 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001194 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001195 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001196 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001197 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001198 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001199 int nocow;
1200 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001201 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001202 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001203
1204 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001205 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001206 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1207 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001208 EXTENT_DO_ACCOUNTING |
1209 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001210 PAGE_CLEAR_DIRTY |
1211 PAGE_SET_WRITEBACK |
1212 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001213 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001214 }
Li Zefan82d59022011-04-20 10:33:24 +08001215
Liu Bo83eea1f2012-07-10 05:28:39 -06001216 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001217
1218 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001219 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001220 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001221 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001222
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001223 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001224 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1225 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001226 EXTENT_DO_ACCOUNTING |
1227 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001228 PAGE_CLEAR_DIRTY |
1229 PAGE_SET_WRITEBACK |
1230 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001231 btrfs_free_path(path);
1232 return PTR_ERR(trans);
1233 }
1234
Josef Bacik74b21072011-04-13 12:02:53 -04001235 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001236
Yan Zheng80ff3852008-10-30 14:20:02 -04001237 cow_start = (u64)-1;
1238 cur_offset = start;
1239 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001240 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001241 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001242 if (ret < 0) {
1243 btrfs_abort_transaction(trans, root, ret);
1244 goto error;
1245 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001246 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1247 leaf = path->nodes[0];
1248 btrfs_item_key_to_cpu(leaf, &found_key,
1249 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001250 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001251 found_key.type == BTRFS_EXTENT_DATA_KEY)
1252 path->slots[0]--;
1253 }
1254 check_prev = 0;
1255next_slot:
1256 leaf = path->nodes[0];
1257 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1258 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001259 if (ret < 0) {
1260 btrfs_abort_transaction(trans, root, ret);
1261 goto error;
1262 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001263 if (ret > 0)
1264 break;
1265 leaf = path->nodes[0];
1266 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001267
Yan Zheng80ff3852008-10-30 14:20:02 -04001268 nocow = 0;
1269 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001270 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001271 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001272
Li Zefan33345d012011-04-20 10:31:50 +08001273 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001274 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1275 found_key.offset > end)
1276 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001277
Yan Zheng80ff3852008-10-30 14:20:02 -04001278 if (found_key.offset > cur_offset) {
1279 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001280 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001281 goto out_check;
1282 }
Chris Masonc31f8832008-01-08 15:46:31 -05001283
Yan Zheng80ff3852008-10-30 14:20:02 -04001284 fi = btrfs_item_ptr(leaf, path->slots[0],
1285 struct btrfs_file_extent_item);
1286 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001287
Josef Bacikcc95bef2013-04-04 14:31:27 -04001288 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001289 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1290 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001291 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001292 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 extent_end = found_key.offset +
1294 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001295 disk_num_bytes =
1296 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 if (extent_end <= start) {
1298 path->slots[0]++;
1299 goto next_slot;
1300 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001301 if (disk_bytenr == 0)
1302 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001303 if (btrfs_file_extent_compression(leaf, fi) ||
1304 btrfs_file_extent_encryption(leaf, fi) ||
1305 btrfs_file_extent_other_encoding(leaf, fi))
1306 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001307 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1308 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001309 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001310 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001311 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001312 found_key.offset -
1313 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001314 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001315 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001316 disk_bytenr += cur_offset - found_key.offset;
1317 num_bytes = min(end + 1, extent_end) - cur_offset;
1318 /*
1319 * force cow if csum exists in the range.
1320 * this ensure that csum for a given extent are
1321 * either valid or do not exist.
1322 */
1323 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1324 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 nocow = 1;
1326 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1327 extent_end = found_key.offset +
1328 btrfs_file_extent_inline_len(leaf, fi);
1329 extent_end = ALIGN(extent_end, root->sectorsize);
1330 } else {
1331 BUG_ON(1);
1332 }
1333out_check:
1334 if (extent_end <= start) {
1335 path->slots[0]++;
1336 goto next_slot;
1337 }
1338 if (!nocow) {
1339 if (cow_start == (u64)-1)
1340 cow_start = cur_offset;
1341 cur_offset = extent_end;
1342 if (cur_offset > end)
1343 break;
1344 path->slots[0]++;
1345 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001346 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001347
David Sterbab3b4aa72011-04-21 01:20:15 +02001348 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001349 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001350 ret = __cow_file_range(trans, inode, root, locked_page,
1351 cow_start, found_key.offset - 1,
1352 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001353 if (ret) {
1354 btrfs_abort_transaction(trans, root, ret);
1355 goto error;
1356 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001358 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001359
Yan Zhengd899e052008-10-30 14:25:28 -04001360 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1361 struct extent_map *em;
1362 struct extent_map_tree *em_tree;
1363 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001364 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001365 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001366 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001367 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001368 em->len = num_bytes;
1369 em->block_len = num_bytes;
1370 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001371 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001372 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001373 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001374 em->mod_start = em->start;
1375 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001376 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001377 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001378 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001379 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001380 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001381 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001382 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001383 if (ret != -EEXIST) {
1384 free_extent_map(em);
1385 break;
1386 }
1387 btrfs_drop_extent_cache(inode, em->start,
1388 em->start + em->len - 1, 0);
1389 }
1390 type = BTRFS_ORDERED_PREALLOC;
1391 } else {
1392 type = BTRFS_ORDERED_NOCOW;
1393 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001394
1395 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001396 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001397 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001398
Yan, Zhengefa56462010-05-16 10:49:59 -04001399 if (root->root_key.objectid ==
1400 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1401 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1402 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001403 if (ret) {
1404 btrfs_abort_transaction(trans, root, ret);
1405 goto error;
1406 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001407 }
1408
Josef Bacikc2790a22013-07-29 11:20:47 -04001409 extent_clear_unlock_delalloc(inode, cur_offset,
1410 cur_offset + num_bytes - 1,
1411 locked_page, EXTENT_LOCKED |
1412 EXTENT_DELALLOC, PAGE_UNLOCK |
1413 PAGE_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001414 cur_offset = extent_end;
1415 if (cur_offset > end)
1416 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001417 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001418 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001419
Josef Bacik17ca04a2012-05-31 15:58:55 -04001420 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001421 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001422 cur_offset = end;
1423 }
1424
Yan Zheng80ff3852008-10-30 14:20:02 -04001425 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001426 ret = __cow_file_range(trans, inode, root, locked_page,
1427 cow_start, end,
1428 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001429 if (ret) {
1430 btrfs_abort_transaction(trans, root, ret);
1431 goto error;
1432 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001433 }
1434
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001435error:
Miao Xiea698d0752012-09-20 01:51:59 -06001436 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001437 if (!ret)
1438 ret = err;
1439
Josef Bacik17ca04a2012-05-31 15:58:55 -04001440 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001441 extent_clear_unlock_delalloc(inode, cur_offset, end,
1442 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001443 EXTENT_DELALLOC | EXTENT_DEFRAG |
1444 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1445 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001446 PAGE_SET_WRITEBACK |
1447 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001448 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001449 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001450}
1451
Chris Masond352ac62008-09-29 15:18:18 -04001452/*
1453 * extent_io.c call back to do delayed allocation processing
1454 */
Chris Masonc8b97812008-10-29 14:49:59 -04001455static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001456 u64 start, u64 end, int *page_started,
1457 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001458{
Chris Masonbe20aa92007-12-17 20:14:01 -05001459 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001460 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001461
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001462 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001463 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001464 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001465 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001466 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001467 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001468 } else if (!btrfs_test_opt(root, COMPRESS) &&
1469 !(BTRFS_I(inode)->force_compress) &&
1470 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001471 ret = cow_file_range(inode, locked_page, start, end,
1472 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001473 } else {
1474 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1475 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001476 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001477 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001478 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001479 return ret;
1480}
1481
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001482static void btrfs_split_extent_hook(struct inode *inode,
1483 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001484{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001485 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001486 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001487 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001488
Josef Bacik9e0baf62011-07-15 15:16:44 +00001489 spin_lock(&BTRFS_I(inode)->lock);
1490 BTRFS_I(inode)->outstanding_extents++;
1491 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001492}
1493
1494/*
1495 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1496 * extents so we can keep track of new extents that are just merged onto old
1497 * extents, such as when we are doing sequential writes, so we can properly
1498 * account for the metadata space we'll need.
1499 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001500static void btrfs_merge_extent_hook(struct inode *inode,
1501 struct extent_state *new,
1502 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001503{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001504 /* not delalloc, ignore it */
1505 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001506 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001507
Josef Bacik9e0baf62011-07-15 15:16:44 +00001508 spin_lock(&BTRFS_I(inode)->lock);
1509 BTRFS_I(inode)->outstanding_extents--;
1510 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001511}
1512
Miao Xieeb73c1b2013-05-15 07:48:22 +00001513static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1514 struct inode *inode)
1515{
1516 spin_lock(&root->delalloc_lock);
1517 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1518 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1519 &root->delalloc_inodes);
1520 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1521 &BTRFS_I(inode)->runtime_flags);
1522 root->nr_delalloc_inodes++;
1523 if (root->nr_delalloc_inodes == 1) {
1524 spin_lock(&root->fs_info->delalloc_root_lock);
1525 BUG_ON(!list_empty(&root->delalloc_root));
1526 list_add_tail(&root->delalloc_root,
1527 &root->fs_info->delalloc_roots);
1528 spin_unlock(&root->fs_info->delalloc_root_lock);
1529 }
1530 }
1531 spin_unlock(&root->delalloc_lock);
1532}
1533
1534static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1535 struct inode *inode)
1536{
1537 spin_lock(&root->delalloc_lock);
1538 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1539 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1540 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1541 &BTRFS_I(inode)->runtime_flags);
1542 root->nr_delalloc_inodes--;
1543 if (!root->nr_delalloc_inodes) {
1544 spin_lock(&root->fs_info->delalloc_root_lock);
1545 BUG_ON(list_empty(&root->delalloc_root));
1546 list_del_init(&root->delalloc_root);
1547 spin_unlock(&root->fs_info->delalloc_root_lock);
1548 }
1549 }
1550 spin_unlock(&root->delalloc_lock);
1551}
1552
Chris Masond352ac62008-09-29 15:18:18 -04001553/*
1554 * extent_io.c set_bit_hook, used to track delayed allocation
1555 * bytes in this file, and to maintain the list of inodes that
1556 * have pending delalloc work to be done.
1557 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001558static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001559 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001560{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001561
Chris Mason75eff682008-12-15 15:54:40 -05001562 /*
1563 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001564 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001565 * bit, which is only set or cleared with irqs on
1566 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001567 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001568 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001569 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001570 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001571
Josef Bacik9e0baf62011-07-15 15:16:44 +00001572 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001573 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001574 } else {
1575 spin_lock(&BTRFS_I(inode)->lock);
1576 BTRFS_I(inode)->outstanding_extents++;
1577 spin_unlock(&BTRFS_I(inode)->lock);
1578 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001579
Miao Xie963d6782013-01-29 10:10:51 +00001580 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1581 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001582 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001583 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001584 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001585 &BTRFS_I(inode)->runtime_flags))
1586 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001587 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001588 }
Chris Mason291d6732008-01-29 15:55:23 -05001589}
1590
Chris Masond352ac62008-09-29 15:18:18 -04001591/*
1592 * extent_io.c clear_bit_hook, see set_bit_hook for why
1593 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001594static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001595 struct extent_state *state,
1596 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001597{
Chris Mason75eff682008-12-15 15:54:40 -05001598 /*
1599 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001600 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001601 * bit, which is only set or cleared with irqs on
1602 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001603 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001604 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001605 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001606 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001607
Josef Bacik9e0baf62011-07-15 15:16:44 +00001608 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001609 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001610 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1611 spin_lock(&BTRFS_I(inode)->lock);
1612 BTRFS_I(inode)->outstanding_extents--;
1613 spin_unlock(&BTRFS_I(inode)->lock);
1614 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001615
1616 if (*bits & EXTENT_DO_ACCOUNTING)
1617 btrfs_delalloc_release_metadata(inode, len);
1618
Josef Bacik0cb59c92010-07-02 12:14:14 -04001619 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001620 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001621 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001622
Miao Xie963d6782013-01-29 10:10:51 +00001623 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1624 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001625 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001626 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001627 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001628 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001629 &BTRFS_I(inode)->runtime_flags))
1630 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001631 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001632 }
Chris Mason291d6732008-01-29 15:55:23 -05001633}
1634
Chris Masond352ac62008-09-29 15:18:18 -04001635/*
1636 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1637 * we don't create bios that span stripes or chunks
1638 */
David Woodhouse64a16702009-07-15 23:29:37 +01001639int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001640 size_t size, struct bio *bio,
1641 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001642{
1643 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001644 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001645 u64 length = 0;
1646 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001647 int ret;
1648
Chris Mason771ed682008-11-06 22:02:51 -05001649 if (bio_flags & EXTENT_BIO_COMPRESSED)
1650 return 0;
1651
Chris Masonf2d8d742008-04-21 10:03:05 -04001652 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001653 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001654 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001655 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001656 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001657 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001658 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001659 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001660 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001661}
1662
Chris Masond352ac62008-09-29 15:18:18 -04001663/*
1664 * in order to insert checksums into the metadata in large chunks,
1665 * we wait until bio submission time. All the pages in the bio are
1666 * checksummed and sums are attached onto the ordered extent record.
1667 *
1668 * At IO completion time the cums attached on the ordered extent record
1669 * are inserted into the btree
1670 */
Chris Masond3977122009-01-05 21:25:51 -05001671static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1672 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001673 unsigned long bio_flags,
1674 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001675{
Chris Mason065631f2008-02-20 12:07:25 -05001676 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001677 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001678
Chris Masond20f7042008-12-08 16:58:54 -05001679 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001680 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001681 return 0;
1682}
Chris Masone0156402008-04-16 11:15:20 -04001683
Chris Mason4a69a412008-11-06 22:03:00 -05001684/*
1685 * in order to insert checksums into the metadata in large chunks,
1686 * we wait until bio submission time. All the pages in the bio are
1687 * checksummed and sums are attached onto the ordered extent record.
1688 *
1689 * At IO completion time the cums attached on the ordered extent record
1690 * are inserted into the btree
1691 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001692static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001693 int mirror_num, unsigned long bio_flags,
1694 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001695{
1696 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001697 int ret;
1698
1699 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1700 if (ret)
1701 bio_endio(bio, ret);
1702 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001703}
1704
Chris Masond352ac62008-09-29 15:18:18 -04001705/*
Chris Masoncad321a2008-12-17 14:51:42 -05001706 * extent_io.c submission hook. This does the right thing for csum calculation
1707 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001708 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001709static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001710 int mirror_num, unsigned long bio_flags,
1711 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001712{
1713 struct btrfs_root *root = BTRFS_I(inode)->root;
1714 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001715 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001716 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001717 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001718
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001719 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001720
Liu Bo83eea1f2012-07-10 05:28:39 -06001721 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001722 metadata = 2;
1723
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001724 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001725 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1726 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001727 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001728
Chris Masond20f7042008-12-08 16:58:54 -05001729 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001730 ret = btrfs_submit_compressed_read(inode, bio,
1731 mirror_num,
1732 bio_flags);
1733 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001734 } else if (!skip_sum) {
1735 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1736 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001737 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001738 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001739 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001740 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001741 /* csum items have already been cloned */
1742 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1743 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001744 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001745 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001746 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001747 bio_flags, bio_offset,
1748 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001749 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001750 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001751 } else if (!skip_sum) {
1752 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1753 if (ret)
1754 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001755 }
1756
Chris Mason0b86a832008-03-24 15:01:56 -04001757mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001758 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1759
1760out:
1761 if (ret < 0)
1762 bio_endio(bio, ret);
1763 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001764}
Chris Mason6885f302008-02-20 16:11:05 -05001765
Chris Masond352ac62008-09-29 15:18:18 -04001766/*
1767 * given a list of ordered sums record them in the inode. This happens
1768 * at IO completion time based on sums calculated at bio submission time.
1769 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001770static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001771 struct inode *inode, u64 file_offset,
1772 struct list_head *list)
1773{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001774 struct btrfs_ordered_sum *sum;
1775
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001776 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001777 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001778 btrfs_csum_file_blocks(trans,
1779 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001780 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001781 }
1782 return 0;
1783}
1784
Josef Bacik2ac55d42010-02-03 19:33:23 +00001785int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1786 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001787{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001788 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001789 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001790 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001791}
1792
Chris Masond352ac62008-09-29 15:18:18 -04001793/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001794struct btrfs_writepage_fixup {
1795 struct page *page;
1796 struct btrfs_work work;
1797};
1798
Christoph Hellwigb2950862008-12-02 09:54:17 -05001799static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001800{
1801 struct btrfs_writepage_fixup *fixup;
1802 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001803 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001804 struct page *page;
1805 struct inode *inode;
1806 u64 page_start;
1807 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001808 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001809
1810 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1811 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001812again:
Chris Mason247e7432008-07-17 12:53:51 -04001813 lock_page(page);
1814 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1815 ClearPageChecked(page);
1816 goto out_page;
1817 }
1818
1819 inode = page->mapping->host;
1820 page_start = page_offset(page);
1821 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1822
Josef Bacik2ac55d42010-02-03 19:33:23 +00001823 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001824 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001825
1826 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001827 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001828 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001829
1830 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1831 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001832 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1833 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001834 unlock_page(page);
1835 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001836 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001837 goto again;
1838 }
Chris Mason247e7432008-07-17 12:53:51 -04001839
Jeff Mahoney87826df2012-02-15 16:23:57 +01001840 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1841 if (ret) {
1842 mapping_set_error(page->mapping, ret);
1843 end_extent_writepage(page, ret, page_start, page_end);
1844 ClearPageChecked(page);
1845 goto out;
1846 }
1847
Josef Bacik2ac55d42010-02-03 19:33:23 +00001848 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001849 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001850 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001851out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001852 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1853 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001854out_page:
1855 unlock_page(page);
1856 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001857 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001858}
1859
1860/*
1861 * There are a few paths in the higher layers of the kernel that directly
1862 * set the page dirty bit without asking the filesystem if it is a
1863 * good idea. This causes problems because we want to make sure COW
1864 * properly happens and the data=ordered rules are followed.
1865 *
Chris Masonc8b97812008-10-29 14:49:59 -04001866 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001867 * hasn't been properly setup for IO. We kick off an async process
1868 * to fix it up. The async helper will wait for ordered extents, set
1869 * the delalloc bit and make it safe to write the page.
1870 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001871static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001872{
1873 struct inode *inode = page->mapping->host;
1874 struct btrfs_writepage_fixup *fixup;
1875 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001876
Chris Mason8b62b722009-09-02 16:53:46 -04001877 /* this page is properly in the ordered list */
1878 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001879 return 0;
1880
1881 if (PageChecked(page))
1882 return -EAGAIN;
1883
1884 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1885 if (!fixup)
1886 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001887
Chris Mason247e7432008-07-17 12:53:51 -04001888 SetPageChecked(page);
1889 page_cache_get(page);
1890 fixup->work.func = btrfs_writepage_fixup_worker;
1891 fixup->page = page;
1892 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001893 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001894}
1895
Yan Zhengd899e052008-10-30 14:25:28 -04001896static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1897 struct inode *inode, u64 file_pos,
1898 u64 disk_bytenr, u64 disk_num_bytes,
1899 u64 num_bytes, u64 ram_bytes,
1900 u8 compression, u8 encryption,
1901 u16 other_encoding, int extent_type)
1902{
1903 struct btrfs_root *root = BTRFS_I(inode)->root;
1904 struct btrfs_file_extent_item *fi;
1905 struct btrfs_path *path;
1906 struct extent_buffer *leaf;
1907 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001908 int ret;
1909
1910 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001911 if (!path)
1912 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001913
Chris Masonb9473432009-03-13 11:00:37 -04001914 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001915
1916 /*
1917 * we may be replacing one extent in the tree with another.
1918 * The new extent is pinned in the extent map, and we don't want
1919 * to drop it from the cache until it is completely in the btree.
1920 *
1921 * So, tell btrfs_drop_extents to leave this extent in the cache.
1922 * the caller is expected to unpin it and allow it to be merged
1923 * with the others.
1924 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001925 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001926 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001927 if (ret)
1928 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001929
Li Zefan33345d012011-04-20 10:31:50 +08001930 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001931 ins.offset = file_pos;
1932 ins.type = BTRFS_EXTENT_DATA_KEY;
1933 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001934 if (ret)
1935 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001936 leaf = path->nodes[0];
1937 fi = btrfs_item_ptr(leaf, path->slots[0],
1938 struct btrfs_file_extent_item);
1939 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1940 btrfs_set_file_extent_type(leaf, fi, extent_type);
1941 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1942 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1943 btrfs_set_file_extent_offset(leaf, fi, 0);
1944 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1945 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1946 btrfs_set_file_extent_compression(leaf, fi, compression);
1947 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1948 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001949
Yan Zhengd899e052008-10-30 14:25:28 -04001950 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001951 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001952
1953 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001954
1955 ins.objectid = disk_bytenr;
1956 ins.offset = disk_num_bytes;
1957 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001958 ret = btrfs_alloc_reserved_file_extent(trans, root,
1959 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001960 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001961out:
Yan Zhengd899e052008-10-30 14:25:28 -04001962 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001963
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001964 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001965}
1966
Liu Bo38c227d2013-01-29 03:18:40 +00001967/* snapshot-aware defrag */
1968struct sa_defrag_extent_backref {
1969 struct rb_node node;
1970 struct old_sa_defrag_extent *old;
1971 u64 root_id;
1972 u64 inum;
1973 u64 file_pos;
1974 u64 extent_offset;
1975 u64 num_bytes;
1976 u64 generation;
1977};
1978
1979struct old_sa_defrag_extent {
1980 struct list_head list;
1981 struct new_sa_defrag_extent *new;
1982
1983 u64 extent_offset;
1984 u64 bytenr;
1985 u64 offset;
1986 u64 len;
1987 int count;
1988};
1989
1990struct new_sa_defrag_extent {
1991 struct rb_root root;
1992 struct list_head head;
1993 struct btrfs_path *path;
1994 struct inode *inode;
1995 u64 file_pos;
1996 u64 len;
1997 u64 bytenr;
1998 u64 disk_len;
1999 u8 compress_type;
2000};
2001
2002static int backref_comp(struct sa_defrag_extent_backref *b1,
2003 struct sa_defrag_extent_backref *b2)
2004{
2005 if (b1->root_id < b2->root_id)
2006 return -1;
2007 else if (b1->root_id > b2->root_id)
2008 return 1;
2009
2010 if (b1->inum < b2->inum)
2011 return -1;
2012 else if (b1->inum > b2->inum)
2013 return 1;
2014
2015 if (b1->file_pos < b2->file_pos)
2016 return -1;
2017 else if (b1->file_pos > b2->file_pos)
2018 return 1;
2019
2020 /*
2021 * [------------------------------] ===> (a range of space)
2022 * |<--->| |<---->| =============> (fs/file tree A)
2023 * |<---------------------------->| ===> (fs/file tree B)
2024 *
2025 * A range of space can refer to two file extents in one tree while
2026 * refer to only one file extent in another tree.
2027 *
2028 * So we may process a disk offset more than one time(two extents in A)
2029 * and locate at the same extent(one extent in B), then insert two same
2030 * backrefs(both refer to the extent in B).
2031 */
2032 return 0;
2033}
2034
2035static void backref_insert(struct rb_root *root,
2036 struct sa_defrag_extent_backref *backref)
2037{
2038 struct rb_node **p = &root->rb_node;
2039 struct rb_node *parent = NULL;
2040 struct sa_defrag_extent_backref *entry;
2041 int ret;
2042
2043 while (*p) {
2044 parent = *p;
2045 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2046
2047 ret = backref_comp(backref, entry);
2048 if (ret < 0)
2049 p = &(*p)->rb_left;
2050 else
2051 p = &(*p)->rb_right;
2052 }
2053
2054 rb_link_node(&backref->node, parent, p);
2055 rb_insert_color(&backref->node, root);
2056}
2057
2058/*
2059 * Note the backref might has changed, and in this case we just return 0.
2060 */
2061static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2062 void *ctx)
2063{
2064 struct btrfs_file_extent_item *extent;
2065 struct btrfs_fs_info *fs_info;
2066 struct old_sa_defrag_extent *old = ctx;
2067 struct new_sa_defrag_extent *new = old->new;
2068 struct btrfs_path *path = new->path;
2069 struct btrfs_key key;
2070 struct btrfs_root *root;
2071 struct sa_defrag_extent_backref *backref;
2072 struct extent_buffer *leaf;
2073 struct inode *inode = new->inode;
2074 int slot;
2075 int ret;
2076 u64 extent_offset;
2077 u64 num_bytes;
2078
2079 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2080 inum == btrfs_ino(inode))
2081 return 0;
2082
2083 key.objectid = root_id;
2084 key.type = BTRFS_ROOT_ITEM_KEY;
2085 key.offset = (u64)-1;
2086
2087 fs_info = BTRFS_I(inode)->root->fs_info;
2088 root = btrfs_read_fs_root_no_name(fs_info, &key);
2089 if (IS_ERR(root)) {
2090 if (PTR_ERR(root) == -ENOENT)
2091 return 0;
2092 WARN_ON(1);
2093 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2094 inum, offset, root_id);
2095 return PTR_ERR(root);
2096 }
2097
2098 key.objectid = inum;
2099 key.type = BTRFS_EXTENT_DATA_KEY;
2100 if (offset > (u64)-1 << 32)
2101 key.offset = 0;
2102 else
2103 key.offset = offset;
2104
2105 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2106 if (ret < 0) {
2107 WARN_ON(1);
2108 return ret;
2109 }
Josef Bacik50f13192013-07-22 12:50:37 -04002110 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002111
2112 while (1) {
2113 cond_resched();
2114
2115 leaf = path->nodes[0];
2116 slot = path->slots[0];
2117
2118 if (slot >= btrfs_header_nritems(leaf)) {
2119 ret = btrfs_next_leaf(root, path);
2120 if (ret < 0) {
2121 goto out;
2122 } else if (ret > 0) {
2123 ret = 0;
2124 goto out;
2125 }
2126 continue;
2127 }
2128
2129 path->slots[0]++;
2130
2131 btrfs_item_key_to_cpu(leaf, &key, slot);
2132
2133 if (key.objectid > inum)
2134 goto out;
2135
2136 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2137 continue;
2138
2139 extent = btrfs_item_ptr(leaf, slot,
2140 struct btrfs_file_extent_item);
2141
2142 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2143 continue;
2144
Liu Boe68afa42013-07-01 22:13:26 +08002145 /*
2146 * 'offset' refers to the exact key.offset,
2147 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2148 * (key.offset - extent_offset).
2149 */
2150 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002151 continue;
2152
Liu Boe68afa42013-07-01 22:13:26 +08002153 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002154 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002155
Liu Bo38c227d2013-01-29 03:18:40 +00002156 if (extent_offset >= old->extent_offset + old->offset +
2157 old->len || extent_offset + num_bytes <=
2158 old->extent_offset + old->offset)
2159 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002160 break;
2161 }
2162
2163 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2164 if (!backref) {
2165 ret = -ENOENT;
2166 goto out;
2167 }
2168
2169 backref->root_id = root_id;
2170 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002171 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002172 backref->num_bytes = num_bytes;
2173 backref->extent_offset = extent_offset;
2174 backref->generation = btrfs_file_extent_generation(leaf, extent);
2175 backref->old = old;
2176 backref_insert(&new->root, backref);
2177 old->count++;
2178out:
2179 btrfs_release_path(path);
2180 WARN_ON(ret);
2181 return ret;
2182}
2183
2184static noinline bool record_extent_backrefs(struct btrfs_path *path,
2185 struct new_sa_defrag_extent *new)
2186{
2187 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2188 struct old_sa_defrag_extent *old, *tmp;
2189 int ret;
2190
2191 new->path = path;
2192
2193 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002194 ret = iterate_inodes_from_logical(old->bytenr +
2195 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002196 path, record_one_backref,
2197 old);
2198 BUG_ON(ret < 0 && ret != -ENOENT);
2199
2200 /* no backref to be processed for this extent */
2201 if (!old->count) {
2202 list_del(&old->list);
2203 kfree(old);
2204 }
2205 }
2206
2207 if (list_empty(&new->head))
2208 return false;
2209
2210 return true;
2211}
2212
2213static int relink_is_mergable(struct extent_buffer *leaf,
2214 struct btrfs_file_extent_item *fi,
2215 u64 disk_bytenr)
2216{
2217 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2218 return 0;
2219
2220 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2221 return 0;
2222
2223 if (btrfs_file_extent_compression(leaf, fi) ||
2224 btrfs_file_extent_encryption(leaf, fi) ||
2225 btrfs_file_extent_other_encoding(leaf, fi))
2226 return 0;
2227
2228 return 1;
2229}
2230
2231/*
2232 * Note the backref might has changed, and in this case we just return 0.
2233 */
2234static noinline int relink_extent_backref(struct btrfs_path *path,
2235 struct sa_defrag_extent_backref *prev,
2236 struct sa_defrag_extent_backref *backref)
2237{
2238 struct btrfs_file_extent_item *extent;
2239 struct btrfs_file_extent_item *item;
2240 struct btrfs_ordered_extent *ordered;
2241 struct btrfs_trans_handle *trans;
2242 struct btrfs_fs_info *fs_info;
2243 struct btrfs_root *root;
2244 struct btrfs_key key;
2245 struct extent_buffer *leaf;
2246 struct old_sa_defrag_extent *old = backref->old;
2247 struct new_sa_defrag_extent *new = old->new;
2248 struct inode *src_inode = new->inode;
2249 struct inode *inode;
2250 struct extent_state *cached = NULL;
2251 int ret = 0;
2252 u64 start;
2253 u64 len;
2254 u64 lock_start;
2255 u64 lock_end;
2256 bool merge = false;
2257 int index;
2258
2259 if (prev && prev->root_id == backref->root_id &&
2260 prev->inum == backref->inum &&
2261 prev->file_pos + prev->num_bytes == backref->file_pos)
2262 merge = true;
2263
2264 /* step 1: get root */
2265 key.objectid = backref->root_id;
2266 key.type = BTRFS_ROOT_ITEM_KEY;
2267 key.offset = (u64)-1;
2268
2269 fs_info = BTRFS_I(src_inode)->root->fs_info;
2270 index = srcu_read_lock(&fs_info->subvol_srcu);
2271
2272 root = btrfs_read_fs_root_no_name(fs_info, &key);
2273 if (IS_ERR(root)) {
2274 srcu_read_unlock(&fs_info->subvol_srcu, index);
2275 if (PTR_ERR(root) == -ENOENT)
2276 return 0;
2277 return PTR_ERR(root);
2278 }
Liu Bo38c227d2013-01-29 03:18:40 +00002279
2280 /* step 2: get inode */
2281 key.objectid = backref->inum;
2282 key.type = BTRFS_INODE_ITEM_KEY;
2283 key.offset = 0;
2284
2285 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2286 if (IS_ERR(inode)) {
2287 srcu_read_unlock(&fs_info->subvol_srcu, index);
2288 return 0;
2289 }
2290
2291 srcu_read_unlock(&fs_info->subvol_srcu, index);
2292
2293 /* step 3: relink backref */
2294 lock_start = backref->file_pos;
2295 lock_end = backref->file_pos + backref->num_bytes - 1;
2296 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2297 0, &cached);
2298
2299 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2300 if (ordered) {
2301 btrfs_put_ordered_extent(ordered);
2302 goto out_unlock;
2303 }
2304
2305 trans = btrfs_join_transaction(root);
2306 if (IS_ERR(trans)) {
2307 ret = PTR_ERR(trans);
2308 goto out_unlock;
2309 }
2310
2311 key.objectid = backref->inum;
2312 key.type = BTRFS_EXTENT_DATA_KEY;
2313 key.offset = backref->file_pos;
2314
2315 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2316 if (ret < 0) {
2317 goto out_free_path;
2318 } else if (ret > 0) {
2319 ret = 0;
2320 goto out_free_path;
2321 }
2322
2323 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2324 struct btrfs_file_extent_item);
2325
2326 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2327 backref->generation)
2328 goto out_free_path;
2329
2330 btrfs_release_path(path);
2331
2332 start = backref->file_pos;
2333 if (backref->extent_offset < old->extent_offset + old->offset)
2334 start += old->extent_offset + old->offset -
2335 backref->extent_offset;
2336
2337 len = min(backref->extent_offset + backref->num_bytes,
2338 old->extent_offset + old->offset + old->len);
2339 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2340
2341 ret = btrfs_drop_extents(trans, root, inode, start,
2342 start + len, 1);
2343 if (ret)
2344 goto out_free_path;
2345again:
2346 key.objectid = btrfs_ino(inode);
2347 key.type = BTRFS_EXTENT_DATA_KEY;
2348 key.offset = start;
2349
Liu Boa09a0a72013-03-11 09:20:58 +00002350 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002351 if (merge) {
2352 struct btrfs_file_extent_item *fi;
2353 u64 extent_len;
2354 struct btrfs_key found_key;
2355
2356 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2357 if (ret < 0)
2358 goto out_free_path;
2359
2360 path->slots[0]--;
2361 leaf = path->nodes[0];
2362 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2363
2364 fi = btrfs_item_ptr(leaf, path->slots[0],
2365 struct btrfs_file_extent_item);
2366 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2367
2368 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2369 extent_len + found_key.offset == start) {
2370 btrfs_set_file_extent_num_bytes(leaf, fi,
2371 extent_len + len);
2372 btrfs_mark_buffer_dirty(leaf);
2373 inode_add_bytes(inode, len);
2374
2375 ret = 1;
2376 goto out_free_path;
2377 } else {
2378 merge = false;
2379 btrfs_release_path(path);
2380 goto again;
2381 }
2382 }
2383
2384 ret = btrfs_insert_empty_item(trans, root, path, &key,
2385 sizeof(*extent));
2386 if (ret) {
2387 btrfs_abort_transaction(trans, root, ret);
2388 goto out_free_path;
2389 }
2390
2391 leaf = path->nodes[0];
2392 item = btrfs_item_ptr(leaf, path->slots[0],
2393 struct btrfs_file_extent_item);
2394 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2395 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2396 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2397 btrfs_set_file_extent_num_bytes(leaf, item, len);
2398 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2399 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2400 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2401 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2402 btrfs_set_file_extent_encryption(leaf, item, 0);
2403 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2404
2405 btrfs_mark_buffer_dirty(leaf);
2406 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002407 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002408
2409 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2410 new->disk_len, 0,
2411 backref->root_id, backref->inum,
2412 new->file_pos, 0); /* start - extent_offset */
2413 if (ret) {
2414 btrfs_abort_transaction(trans, root, ret);
2415 goto out_free_path;
2416 }
2417
2418 ret = 1;
2419out_free_path:
2420 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002421 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002422 btrfs_end_transaction(trans, root);
2423out_unlock:
2424 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2425 &cached, GFP_NOFS);
2426 iput(inode);
2427 return ret;
2428}
2429
2430static void relink_file_extents(struct new_sa_defrag_extent *new)
2431{
2432 struct btrfs_path *path;
2433 struct old_sa_defrag_extent *old, *tmp;
2434 struct sa_defrag_extent_backref *backref;
2435 struct sa_defrag_extent_backref *prev = NULL;
2436 struct inode *inode;
2437 struct btrfs_root *root;
2438 struct rb_node *node;
2439 int ret;
2440
2441 inode = new->inode;
2442 root = BTRFS_I(inode)->root;
2443
2444 path = btrfs_alloc_path();
2445 if (!path)
2446 return;
2447
2448 if (!record_extent_backrefs(path, new)) {
2449 btrfs_free_path(path);
2450 goto out;
2451 }
2452 btrfs_release_path(path);
2453
2454 while (1) {
2455 node = rb_first(&new->root);
2456 if (!node)
2457 break;
2458 rb_erase(node, &new->root);
2459
2460 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2461
2462 ret = relink_extent_backref(path, prev, backref);
2463 WARN_ON(ret < 0);
2464
2465 kfree(prev);
2466
2467 if (ret == 1)
2468 prev = backref;
2469 else
2470 prev = NULL;
2471 cond_resched();
2472 }
2473 kfree(prev);
2474
2475 btrfs_free_path(path);
2476
2477 list_for_each_entry_safe(old, tmp, &new->head, list) {
2478 list_del(&old->list);
2479 kfree(old);
2480 }
2481out:
2482 atomic_dec(&root->fs_info->defrag_running);
2483 wake_up(&root->fs_info->transaction_wait);
2484
2485 kfree(new);
2486}
2487
2488static struct new_sa_defrag_extent *
2489record_old_file_extents(struct inode *inode,
2490 struct btrfs_ordered_extent *ordered)
2491{
2492 struct btrfs_root *root = BTRFS_I(inode)->root;
2493 struct btrfs_path *path;
2494 struct btrfs_key key;
2495 struct old_sa_defrag_extent *old, *tmp;
2496 struct new_sa_defrag_extent *new;
2497 int ret;
2498
2499 new = kmalloc(sizeof(*new), GFP_NOFS);
2500 if (!new)
2501 return NULL;
2502
2503 new->inode = inode;
2504 new->file_pos = ordered->file_offset;
2505 new->len = ordered->len;
2506 new->bytenr = ordered->start;
2507 new->disk_len = ordered->disk_len;
2508 new->compress_type = ordered->compress_type;
2509 new->root = RB_ROOT;
2510 INIT_LIST_HEAD(&new->head);
2511
2512 path = btrfs_alloc_path();
2513 if (!path)
2514 goto out_kfree;
2515
2516 key.objectid = btrfs_ino(inode);
2517 key.type = BTRFS_EXTENT_DATA_KEY;
2518 key.offset = new->file_pos;
2519
2520 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2521 if (ret < 0)
2522 goto out_free_path;
2523 if (ret > 0 && path->slots[0] > 0)
2524 path->slots[0]--;
2525
2526 /* find out all the old extents for the file range */
2527 while (1) {
2528 struct btrfs_file_extent_item *extent;
2529 struct extent_buffer *l;
2530 int slot;
2531 u64 num_bytes;
2532 u64 offset;
2533 u64 end;
2534 u64 disk_bytenr;
2535 u64 extent_offset;
2536
2537 l = path->nodes[0];
2538 slot = path->slots[0];
2539
2540 if (slot >= btrfs_header_nritems(l)) {
2541 ret = btrfs_next_leaf(root, path);
2542 if (ret < 0)
2543 goto out_free_list;
2544 else if (ret > 0)
2545 break;
2546 continue;
2547 }
2548
2549 btrfs_item_key_to_cpu(l, &key, slot);
2550
2551 if (key.objectid != btrfs_ino(inode))
2552 break;
2553 if (key.type != BTRFS_EXTENT_DATA_KEY)
2554 break;
2555 if (key.offset >= new->file_pos + new->len)
2556 break;
2557
2558 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2559
2560 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2561 if (key.offset + num_bytes < new->file_pos)
2562 goto next;
2563
2564 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2565 if (!disk_bytenr)
2566 goto next;
2567
2568 extent_offset = btrfs_file_extent_offset(l, extent);
2569
2570 old = kmalloc(sizeof(*old), GFP_NOFS);
2571 if (!old)
2572 goto out_free_list;
2573
2574 offset = max(new->file_pos, key.offset);
2575 end = min(new->file_pos + new->len, key.offset + num_bytes);
2576
2577 old->bytenr = disk_bytenr;
2578 old->extent_offset = extent_offset;
2579 old->offset = offset - key.offset;
2580 old->len = end - offset;
2581 old->new = new;
2582 old->count = 0;
2583 list_add_tail(&old->list, &new->head);
2584next:
2585 path->slots[0]++;
2586 cond_resched();
2587 }
2588
2589 btrfs_free_path(path);
2590 atomic_inc(&root->fs_info->defrag_running);
2591
2592 return new;
2593
2594out_free_list:
2595 list_for_each_entry_safe(old, tmp, &new->head, list) {
2596 list_del(&old->list);
2597 kfree(old);
2598 }
2599out_free_path:
2600 btrfs_free_path(path);
2601out_kfree:
2602 kfree(new);
2603 return NULL;
2604}
2605
Chris Mason5d13a982009-03-13 11:41:46 -04002606/*
2607 * helper function for btrfs_finish_ordered_io, this
2608 * just reads in some of the csum leaves to prime them into ram
2609 * before we start the transaction. It limits the amount of btree
2610 * reads required while inside the transaction.
2611 */
Chris Masond352ac62008-09-29 15:18:18 -04002612/* as ordered data IO finishes, this gets called so we can finish
2613 * an ordered extent if the range of bytes in the file it covers are
2614 * fully written.
2615 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002616static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002617{
Josef Bacik5fd02042012-05-02 14:00:54 -04002618 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002619 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002620 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002621 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002622 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002623 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002624 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002625 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002626 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002627
Liu Bo83eea1f2012-07-10 05:28:39 -06002628 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002629
Josef Bacik5fd02042012-05-02 14:00:54 -04002630 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2631 ret = -EIO;
2632 goto out;
2633 }
2634
Yan, Zhengc2167752009-11-12 09:34:21 +00002635 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002636 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002637 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2638 if (nolock)
2639 trans = btrfs_join_transaction_nolock(root);
2640 else
2641 trans = btrfs_join_transaction(root);
2642 if (IS_ERR(trans)) {
2643 ret = PTR_ERR(trans);
2644 trans = NULL;
2645 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002646 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002647 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2648 ret = btrfs_update_inode_fallback(trans, root, inode);
2649 if (ret) /* -ENOMEM or corruption */
2650 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002651 goto out;
2652 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002653
Josef Bacik2ac55d42010-02-03 19:33:23 +00002654 lock_extent_bits(io_tree, ordered_extent->file_offset,
2655 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002656 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002657
Liu Bo38c227d2013-01-29 03:18:40 +00002658 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2659 ordered_extent->file_offset + ordered_extent->len - 1,
2660 EXTENT_DEFRAG, 1, cached_state);
2661 if (ret) {
2662 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2663 if (last_snapshot >= BTRFS_I(inode)->generation)
2664 /* the inode is shared */
2665 new = record_old_file_extents(inode, ordered_extent);
2666
2667 clear_extent_bit(io_tree, ordered_extent->file_offset,
2668 ordered_extent->file_offset + ordered_extent->len - 1,
2669 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2670 }
2671
Josef Bacik0cb59c92010-07-02 12:14:14 -04002672 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002673 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002674 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002675 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002676 if (IS_ERR(trans)) {
2677 ret = PTR_ERR(trans);
2678 trans = NULL;
2679 goto out_unlock;
2680 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002681 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002682
Chris Masonc8b97812008-10-29 14:49:59 -04002683 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002684 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002685 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002686 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002687 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002688 ordered_extent->file_offset,
2689 ordered_extent->file_offset +
2690 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002691 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002692 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002693 ret = insert_reserved_file_extent(trans, inode,
2694 ordered_extent->file_offset,
2695 ordered_extent->start,
2696 ordered_extent->disk_len,
2697 ordered_extent->len,
2698 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002699 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002700 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002701 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002702 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2703 ordered_extent->file_offset, ordered_extent->len,
2704 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002705 if (ret < 0) {
2706 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002707 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002708 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002709
Chris Masone6dcd2d2008-07-17 12:53:50 -04002710 add_pending_csums(trans, inode, ordered_extent->file_offset,
2711 &ordered_extent->list);
2712
Josef Bacik6c760c02012-11-09 10:53:21 -05002713 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2714 ret = btrfs_update_inode_fallback(trans, root, inode);
2715 if (ret) { /* -ENOMEM or corruption */
2716 btrfs_abort_transaction(trans, root, ret);
2717 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002718 }
2719 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002720out_unlock:
2721 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2722 ordered_extent->file_offset +
2723 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002724out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002725 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002726 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002727 if (trans)
2728 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002729
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002730 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002731 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2732 ordered_extent->file_offset +
2733 ordered_extent->len - 1, NULL, GFP_NOFS);
2734
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002735 /*
2736 * If the ordered extent had an IOERR or something else went
2737 * wrong we need to return the space for this ordered extent
2738 * back to the allocator.
2739 */
2740 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2741 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2742 btrfs_free_reserved_extent(root, ordered_extent->start,
2743 ordered_extent->disk_len);
2744 }
2745
2746
Josef Bacik5fd02042012-05-02 14:00:54 -04002747 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002748 * This needs to be done to make sure anybody waiting knows we are done
2749 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002750 */
2751 btrfs_remove_ordered_extent(inode, ordered_extent);
2752
Liu Bo38c227d2013-01-29 03:18:40 +00002753 /* for snapshot-aware defrag */
2754 if (new)
2755 relink_file_extents(new);
2756
Chris Masone6dcd2d2008-07-17 12:53:50 -04002757 /* once for us */
2758 btrfs_put_ordered_extent(ordered_extent);
2759 /* once for the tree */
2760 btrfs_put_ordered_extent(ordered_extent);
2761
Josef Bacik5fd02042012-05-02 14:00:54 -04002762 return ret;
2763}
2764
2765static void finish_ordered_fn(struct btrfs_work *work)
2766{
2767 struct btrfs_ordered_extent *ordered_extent;
2768 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2769 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002770}
2771
Christoph Hellwigb2950862008-12-02 09:54:17 -05002772static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002773 struct extent_state *state, int uptodate)
2774{
Josef Bacik5fd02042012-05-02 14:00:54 -04002775 struct inode *inode = page->mapping->host;
2776 struct btrfs_root *root = BTRFS_I(inode)->root;
2777 struct btrfs_ordered_extent *ordered_extent = NULL;
2778 struct btrfs_workers *workers;
2779
liubo1abe9b82011-03-24 11:18:59 +00002780 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2781
Chris Mason8b62b722009-09-02 16:53:46 -04002782 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002783 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2784 end - start + 1, uptodate))
2785 return 0;
2786
2787 ordered_extent->work.func = finish_ordered_fn;
2788 ordered_extent->work.flags = 0;
2789
Liu Bo83eea1f2012-07-10 05:28:39 -06002790 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002791 workers = &root->fs_info->endio_freespace_worker;
2792 else
2793 workers = &root->fs_info->endio_write_workers;
2794 btrfs_queue_worker(workers, &ordered_extent->work);
2795
2796 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002797}
2798
Chris Masond352ac62008-09-29 15:18:18 -04002799/*
Chris Masond352ac62008-09-29 15:18:18 -04002800 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002801 * if there's a match, we allow the bio to finish. If not, the code in
2802 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002803 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002804static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2805 u64 phy_offset, struct page *page,
2806 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002807{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002808 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002809 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002810 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002811 char *kaddr;
Chris Masonff79f812007-10-15 16:22:25 -04002812 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiefacc8a222013-07-25 19:22:34 +08002813 u32 csum_expected;
Chris Masonff79f812007-10-15 16:22:25 -04002814 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002815 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2816 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002817
Chris Masond20f7042008-12-08 16:58:54 -05002818 if (PageChecked(page)) {
2819 ClearPageChecked(page);
2820 goto good;
2821 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002822
2823 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002824 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002825
Yan Zheng17d217f2008-12-12 10:03:38 -05002826 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002827 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002828 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2829 GFP_NOFS);
2830 return 0;
2831 }
2832
Miao Xiefacc8a222013-07-25 19:22:34 +08002833 phy_offset >>= inode->i_sb->s_blocksize_bits;
2834 csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
Chris Masond3977122009-01-05 21:25:51 -05002835
Miao Xiefacc8a222013-07-25 19:22:34 +08002836 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00002837 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002838 btrfs_csum_final(csum, (char *)&csum);
Miao Xiefacc8a222013-07-25 19:22:34 +08002839 if (csum != csum_expected)
Chris Mason07157aa2007-08-30 08:50:51 -04002840 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002841
Cong Wang7ac687d2011-11-25 23:14:28 +08002842 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002843good:
Chris Mason07157aa2007-08-30 08:50:51 -04002844 return 0;
2845
2846zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002847 if (__ratelimit(&_rs))
Miao Xiefacc8a222013-07-25 19:22:34 +08002848 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002849 (unsigned long long)btrfs_ino(page->mapping->host),
Miao Xiefacc8a222013-07-25 19:22:34 +08002850 (unsigned long long)start, csum, csum_expected);
Chris Masondb945352007-10-15 16:15:53 -04002851 memset(kaddr + offset, 1, end - start + 1);
2852 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002853 kunmap_atomic(kaddr);
Miao Xiefacc8a222013-07-25 19:22:34 +08002854 if (csum_expected == 0)
Chris Mason3b951512008-04-17 11:29:12 -04002855 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002856 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002857}
Chris Masonb888db2b2007-08-27 16:49:44 -04002858
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002859struct delayed_iput {
2860 struct list_head list;
2861 struct inode *inode;
2862};
2863
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002864/* JDM: If this is fs-wide, why can't we add a pointer to
2865 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002866void btrfs_add_delayed_iput(struct inode *inode)
2867{
2868 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2869 struct delayed_iput *delayed;
2870
2871 if (atomic_add_unless(&inode->i_count, -1, 1))
2872 return;
2873
2874 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2875 delayed->inode = inode;
2876
2877 spin_lock(&fs_info->delayed_iput_lock);
2878 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2879 spin_unlock(&fs_info->delayed_iput_lock);
2880}
2881
2882void btrfs_run_delayed_iputs(struct btrfs_root *root)
2883{
2884 LIST_HEAD(list);
2885 struct btrfs_fs_info *fs_info = root->fs_info;
2886 struct delayed_iput *delayed;
2887 int empty;
2888
2889 spin_lock(&fs_info->delayed_iput_lock);
2890 empty = list_empty(&fs_info->delayed_iputs);
2891 spin_unlock(&fs_info->delayed_iput_lock);
2892 if (empty)
2893 return;
2894
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002895 spin_lock(&fs_info->delayed_iput_lock);
2896 list_splice_init(&fs_info->delayed_iputs, &list);
2897 spin_unlock(&fs_info->delayed_iput_lock);
2898
2899 while (!list_empty(&list)) {
2900 delayed = list_entry(list.next, struct delayed_iput, list);
2901 list_del(&delayed->list);
2902 iput(delayed->inode);
2903 kfree(delayed);
2904 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002905}
2906
Yan, Zhengd68fc572010-05-16 10:49:58 -04002907/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002908 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002909 * files in the subvolume, it removes orphan item and frees block_rsv
2910 * structure.
2911 */
2912void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2913 struct btrfs_root *root)
2914{
Josef Bacik90290e12011-12-02 15:44:12 -05002915 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002916 int ret;
2917
Josef Bacik8a35d952012-05-23 14:26:42 -04002918 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002919 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2920 return;
2921
Josef Bacik90290e12011-12-02 15:44:12 -05002922 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002923 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002924 spin_unlock(&root->orphan_lock);
2925 return;
2926 }
2927
2928 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2929 spin_unlock(&root->orphan_lock);
2930 return;
2931 }
2932
2933 block_rsv = root->orphan_block_rsv;
2934 root->orphan_block_rsv = NULL;
2935 spin_unlock(&root->orphan_lock);
2936
Yan, Zhengd68fc572010-05-16 10:49:58 -04002937 if (root->orphan_item_inserted &&
2938 btrfs_root_refs(&root->root_item) > 0) {
2939 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2940 root->root_key.objectid);
2941 BUG_ON(ret);
2942 root->orphan_item_inserted = 0;
2943 }
2944
Josef Bacik90290e12011-12-02 15:44:12 -05002945 if (block_rsv) {
2946 WARN_ON(block_rsv->size > 0);
2947 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002948 }
2949}
2950
2951/*
Josef Bacik7b128762008-07-24 12:17:14 -04002952 * This creates an orphan entry for the given inode in case something goes
2953 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002954 *
2955 * NOTE: caller of this function should reserve 5 units of metadata for
2956 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002957 */
2958int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2959{
2960 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002961 struct btrfs_block_rsv *block_rsv = NULL;
2962 int reserve = 0;
2963 int insert = 0;
2964 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002965
Yan, Zhengd68fc572010-05-16 10:49:58 -04002966 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002967 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002968 if (!block_rsv)
2969 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002970 }
2971
Yan, Zhengd68fc572010-05-16 10:49:58 -04002972 spin_lock(&root->orphan_lock);
2973 if (!root->orphan_block_rsv) {
2974 root->orphan_block_rsv = block_rsv;
2975 } else if (block_rsv) {
2976 btrfs_free_block_rsv(root, block_rsv);
2977 block_rsv = NULL;
2978 }
Josef Bacik7b128762008-07-24 12:17:14 -04002979
Josef Bacik8a35d952012-05-23 14:26:42 -04002980 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2981 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002982#if 0
2983 /*
2984 * For proper ENOSPC handling, we should do orphan
2985 * cleanup when mounting. But this introduces backward
2986 * compatibility issue.
2987 */
2988 if (!xchg(&root->orphan_item_inserted, 1))
2989 insert = 2;
2990 else
2991 insert = 1;
2992#endif
2993 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002994 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002995 }
Josef Bacik7b128762008-07-24 12:17:14 -04002996
Josef Bacik72ac3c02012-05-23 14:13:11 -04002997 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2998 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002999 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003000 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003001
Yan, Zhengd68fc572010-05-16 10:49:58 -04003002 /* grab metadata reservation from transaction handle */
3003 if (reserve) {
3004 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003005 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003006 }
3007
3008 /* insert an orphan item to track this unlinked/truncated file */
3009 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003010 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003011 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003012 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3013 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003014 btrfs_abort_transaction(trans, root, ret);
3015 return ret;
3016 }
3017 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003018 }
3019
3020 /* insert an orphan item to track subvolume contains orphan files */
3021 if (insert >= 2) {
3022 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3023 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003024 if (ret && ret != -EEXIST) {
3025 btrfs_abort_transaction(trans, root, ret);
3026 return ret;
3027 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003028 }
3029 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003030}
3031
3032/*
3033 * We have done the truncate/delete so we can go ahead and remove the orphan
3034 * item for this particular inode.
3035 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003036static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3037 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003038{
3039 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003040 int delete_item = 0;
3041 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003042 int ret = 0;
3043
Yan, Zhengd68fc572010-05-16 10:49:58 -04003044 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003045 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3046 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003047 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003048
Josef Bacik72ac3c02012-05-23 14:13:11 -04003049 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3050 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003051 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003052 spin_unlock(&root->orphan_lock);
3053
3054 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003055 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003056 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003057 }
3058
Josef Bacik8a35d952012-05-23 14:26:42 -04003059 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003060 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003061 atomic_dec(&root->orphan_inodes);
3062 }
Josef Bacik7b128762008-07-24 12:17:14 -04003063
Yan, Zhengd68fc572010-05-16 10:49:58 -04003064 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003065}
3066
3067/*
3068 * this cleans up any orphans that may be left on the list from the last use
3069 * of this root.
3070 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003071int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003072{
3073 struct btrfs_path *path;
3074 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003075 struct btrfs_key key, found_key;
3076 struct btrfs_trans_handle *trans;
3077 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003078 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003079 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3080
Yan, Zhengd68fc572010-05-16 10:49:58 -04003081 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003082 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003083
3084 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003085 if (!path) {
3086 ret = -ENOMEM;
3087 goto out;
3088 }
Josef Bacik7b128762008-07-24 12:17:14 -04003089 path->reada = -1;
3090
3091 key.objectid = BTRFS_ORPHAN_OBJECTID;
3092 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3093 key.offset = (u64)-1;
3094
Josef Bacik7b128762008-07-24 12:17:14 -04003095 while (1) {
3096 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003097 if (ret < 0)
3098 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003099
3100 /*
3101 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003102 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003103 * find the key and see if we have stuff that matches
3104 */
3105 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003106 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003107 if (path->slots[0] == 0)
3108 break;
3109 path->slots[0]--;
3110 }
3111
3112 /* pull out the item */
3113 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003114 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3115
3116 /* make sure the item matches what we want */
3117 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3118 break;
3119 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3120 break;
3121
3122 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003123 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003124
3125 /*
3126 * this is where we are basically btrfs_lookup, without the
3127 * crossing root thing. we store the inode number in the
3128 * offset of the orphan item.
3129 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003130
3131 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003132 btrfs_err(root->fs_info,
3133 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003134 ret = -EINVAL;
3135 goto out;
3136 }
3137
3138 last_objectid = found_key.offset;
3139
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003140 found_key.objectid = found_key.offset;
3141 found_key.type = BTRFS_INODE_ITEM_KEY;
3142 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003143 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003144 ret = PTR_RET(inode);
3145 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003146 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003147
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003148 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3149 struct btrfs_root *dead_root;
3150 struct btrfs_fs_info *fs_info = root->fs_info;
3151 int is_dead_root = 0;
3152
3153 /*
3154 * this is an orphan in the tree root. Currently these
3155 * could come from 2 sources:
3156 * a) a snapshot deletion in progress
3157 * b) a free space cache inode
3158 * We need to distinguish those two, as the snapshot
3159 * orphan must not get deleted.
3160 * find_dead_roots already ran before us, so if this
3161 * is a snapshot deletion, we should find the root
3162 * in the dead_roots list
3163 */
3164 spin_lock(&fs_info->trans_lock);
3165 list_for_each_entry(dead_root, &fs_info->dead_roots,
3166 root_list) {
3167 if (dead_root->root_key.objectid ==
3168 found_key.objectid) {
3169 is_dead_root = 1;
3170 break;
3171 }
3172 }
3173 spin_unlock(&fs_info->trans_lock);
3174 if (is_dead_root) {
3175 /* prevent this orphan from being found again */
3176 key.offset = found_key.objectid - 1;
3177 continue;
3178 }
3179 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003180 /*
3181 * Inode is already gone but the orphan item is still there,
3182 * kill the orphan item.
3183 */
3184 if (ret == -ESTALE) {
3185 trans = btrfs_start_transaction(root, 1);
3186 if (IS_ERR(trans)) {
3187 ret = PTR_ERR(trans);
3188 goto out;
3189 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003190 btrfs_debug(root->fs_info, "auto deleting %Lu",
3191 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003192 ret = btrfs_del_orphan_item(trans, root,
3193 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003194 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003195 btrfs_end_transaction(trans, root);
3196 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003197 }
Josef Bacik7b128762008-07-24 12:17:14 -04003198
Josef Bacik7b128762008-07-24 12:17:14 -04003199 /*
3200 * add this inode to the orphan list so btrfs_orphan_del does
3201 * the proper thing when we hit it
3202 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003203 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3204 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003205 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003206
Josef Bacik7b128762008-07-24 12:17:14 -04003207 /* if we have links, this was a truncate, lets do that */
3208 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003209 if (!S_ISREG(inode->i_mode)) {
3210 WARN_ON(1);
3211 iput(inode);
3212 continue;
3213 }
Josef Bacik7b128762008-07-24 12:17:14 -04003214 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003215
3216 /* 1 for the orphan item deletion. */
3217 trans = btrfs_start_transaction(root, 1);
3218 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003219 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003220 ret = PTR_ERR(trans);
3221 goto out;
3222 }
3223 ret = btrfs_orphan_add(trans, inode);
3224 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003225 if (ret) {
3226 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003227 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003228 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003229
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003230 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003231 if (ret)
3232 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003233 } else {
3234 nr_unlink++;
3235 }
3236
3237 /* this will do delete_inode and everything for us */
3238 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003239 if (ret)
3240 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003241 }
Miao Xie3254c872011-11-10 20:45:05 -05003242 /* release the path since we're done with it */
3243 btrfs_release_path(path);
3244
Yan, Zhengd68fc572010-05-16 10:49:58 -04003245 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3246
3247 if (root->orphan_block_rsv)
3248 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3249 (u64)-1);
3250
3251 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003252 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003253 if (!IS_ERR(trans))
3254 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003255 }
Josef Bacik7b128762008-07-24 12:17:14 -04003256
3257 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003258 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003259 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003260 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003261
3262out:
3263 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003264 btrfs_crit(root->fs_info,
3265 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003266 btrfs_free_path(path);
3267 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003268}
3269
Chris Masond352ac62008-09-29 15:18:18 -04003270/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003271 * very simple check to peek ahead in the leaf looking for xattrs. If we
3272 * don't find any xattrs, we know there can't be any acls.
3273 *
3274 * slot is the slot the inode is in, objectid is the objectid of the inode
3275 */
3276static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3277 int slot, u64 objectid)
3278{
3279 u32 nritems = btrfs_header_nritems(leaf);
3280 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003281 static u64 xattr_access = 0;
3282 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003283 int scanned = 0;
3284
Josef Bacikf23b5a52013-06-19 10:16:26 -04003285 if (!xattr_access) {
3286 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3287 strlen(POSIX_ACL_XATTR_ACCESS));
3288 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3289 strlen(POSIX_ACL_XATTR_DEFAULT));
3290 }
3291
Chris Mason46a53cc2009-04-27 11:47:50 -04003292 slot++;
3293 while (slot < nritems) {
3294 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3295
3296 /* we found a different objectid, there must not be acls */
3297 if (found_key.objectid != objectid)
3298 return 0;
3299
3300 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003301 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3302 if (found_key.offset == xattr_access ||
3303 found_key.offset == xattr_default)
3304 return 1;
3305 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003306
3307 /*
3308 * we found a key greater than an xattr key, there can't
3309 * be any acls later on
3310 */
3311 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3312 return 0;
3313
3314 slot++;
3315 scanned++;
3316
3317 /*
3318 * it goes inode, inode backrefs, xattrs, extents,
3319 * so if there are a ton of hard links to an inode there can
3320 * be a lot of backrefs. Don't waste time searching too hard,
3321 * this is just an optimization
3322 */
3323 if (scanned >= 8)
3324 break;
3325 }
3326 /* we hit the end of the leaf before we found an xattr or
3327 * something larger than an xattr. We have to assume the inode
3328 * has acls
3329 */
3330 return 1;
3331}
3332
3333/*
Chris Masond352ac62008-09-29 15:18:18 -04003334 * read an inode from the btree into the in-memory inode
3335 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003336static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003337{
3338 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003339 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003340 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003341 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003342 struct btrfs_root *root = BTRFS_I(inode)->root;
3343 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003344 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003345 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003346 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003347 bool filled = false;
3348
3349 ret = btrfs_fill_inode(inode, &rdev);
3350 if (!ret)
3351 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003352
3353 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003354 if (!path)
3355 goto make_bad;
3356
Josef Bacikd90c7322011-05-17 09:50:54 -04003357 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003358 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003359
Chris Mason39279cc2007-06-12 06:35:45 -04003360 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003361 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003362 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003363
Chris Mason5f39d392007-10-15 16:14:19 -04003364 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003365
3366 if (filled)
3367 goto cache_acl;
3368
Chris Mason5f39d392007-10-15 16:14:19 -04003369 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3370 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003371 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003372 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003373 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3374 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003375 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003376
3377 tspec = btrfs_inode_atime(inode_item);
3378 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3379 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3380
3381 tspec = btrfs_inode_mtime(inode_item);
3382 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3383 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3384
3385 tspec = btrfs_inode_ctime(inode_item);
3386 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3387 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3388
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003389 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003390 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003391 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3392
3393 /*
3394 * If we were modified in the current generation and evicted from memory
3395 * and then re-read we need to do a full sync since we don't have any
3396 * idea about which extents were modified before we were evicted from
3397 * cache.
3398 */
3399 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3400 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3401 &BTRFS_I(inode)->runtime_flags);
3402
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003403 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003404 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003405 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003406 rdev = btrfs_inode_rdev(leaf, inode_item);
3407
Josef Bacikaec74772008-07-24 12:12:38 -04003408 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003409 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003410cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003411 /*
3412 * try to precache a NULL acl entry for files that don't have
3413 * any xattrs or acls
3414 */
Li Zefan33345d012011-04-20 10:31:50 +08003415 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3416 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003417 if (!maybe_acls)
3418 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003419
Chris Mason39279cc2007-06-12 06:35:45 -04003420 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003421
Chris Mason39279cc2007-06-12 06:35:45 -04003422 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003423 case S_IFREG:
3424 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003425 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003426 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003427 inode->i_fop = &btrfs_file_operations;
3428 inode->i_op = &btrfs_file_inode_operations;
3429 break;
3430 case S_IFDIR:
3431 inode->i_fop = &btrfs_dir_file_operations;
3432 if (root == root->fs_info->tree_root)
3433 inode->i_op = &btrfs_dir_ro_inode_operations;
3434 else
3435 inode->i_op = &btrfs_dir_inode_operations;
3436 break;
3437 case S_IFLNK:
3438 inode->i_op = &btrfs_symlink_inode_operations;
3439 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003440 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003441 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003442 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003443 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003444 init_special_inode(inode, inode->i_mode, rdev);
3445 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003446 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003447
3448 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003449 return;
3450
3451make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003452 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003453 make_bad_inode(inode);
3454}
3455
Chris Masond352ac62008-09-29 15:18:18 -04003456/*
3457 * given a leaf and an inode, copy the inode fields into the leaf
3458 */
Chris Masone02119d2008-09-05 16:13:11 -04003459static void fill_inode_item(struct btrfs_trans_handle *trans,
3460 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003461 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003462 struct inode *inode)
3463{
Liu Bo51fab692012-12-27 09:01:21 +00003464 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003465
Liu Bo51fab692012-12-27 09:01:21 +00003466 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003467
Liu Bo51fab692012-12-27 09:01:21 +00003468 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3469 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3470 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3471 &token);
3472 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3473 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003474
Liu Bo51fab692012-12-27 09:01:21 +00003475 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3476 inode->i_atime.tv_sec, &token);
3477 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3478 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003479
Liu Bo51fab692012-12-27 09:01:21 +00003480 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3481 inode->i_mtime.tv_sec, &token);
3482 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3483 inode->i_mtime.tv_nsec, &token);
3484
3485 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3486 inode->i_ctime.tv_sec, &token);
3487 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3488 inode->i_ctime.tv_nsec, &token);
3489
3490 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3491 &token);
3492 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3493 &token);
3494 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3495 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3496 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3497 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3498 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003499}
3500
Chris Masond352ac62008-09-29 15:18:18 -04003501/*
3502 * copy everything in the in-memory inode into the btree.
3503 */
Chris Mason21151332011-11-10 20:39:08 -05003504static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003505 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003506{
3507 struct btrfs_inode_item *inode_item;
3508 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003509 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003510 int ret;
3511
3512 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003513 if (!path)
3514 return -ENOMEM;
3515
Chris Masonb9473432009-03-13 11:00:37 -04003516 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003517 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3518 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003519 if (ret) {
3520 if (ret > 0)
3521 ret = -ENOENT;
3522 goto failed;
3523 }
3524
Chris Masonb4ce94d2009-02-04 09:25:08 -05003525 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003526 leaf = path->nodes[0];
3527 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003528 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003529
Chris Masone02119d2008-09-05 16:13:11 -04003530 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003531 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003532 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003533 ret = 0;
3534failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003535 btrfs_free_path(path);
3536 return ret;
3537}
3538
Chris Masond352ac62008-09-29 15:18:18 -04003539/*
Chris Mason21151332011-11-10 20:39:08 -05003540 * copy everything in the in-memory inode into the btree.
3541 */
3542noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3543 struct btrfs_root *root, struct inode *inode)
3544{
3545 int ret;
3546
3547 /*
3548 * If the inode is a free space inode, we can deadlock during commit
3549 * if we put it into the delayed code.
3550 *
3551 * The data relocation inode should also be directly updated
3552 * without delay
3553 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003554 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003555 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003556 btrfs_update_root_times(trans, root);
3557
Chris Mason21151332011-11-10 20:39:08 -05003558 ret = btrfs_delayed_update_inode(trans, root, inode);
3559 if (!ret)
3560 btrfs_set_inode_last_trans(trans, inode);
3561 return ret;
3562 }
3563
3564 return btrfs_update_inode_item(trans, root, inode);
3565}
3566
Josef Bacikbe6aef62012-10-22 15:43:12 -04003567noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3568 struct btrfs_root *root,
3569 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003570{
3571 int ret;
3572
3573 ret = btrfs_update_inode(trans, root, inode);
3574 if (ret == -ENOSPC)
3575 return btrfs_update_inode_item(trans, root, inode);
3576 return ret;
3577}
3578
3579/*
Chris Masond352ac62008-09-29 15:18:18 -04003580 * unlink helper that gets used here in inode.c and in the tree logging
3581 * recovery code. It remove a link in a directory with a given name, and
3582 * also drops the back refs in the inode to the directory
3583 */
Al Viro92986792011-03-04 17:14:37 +00003584static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3585 struct btrfs_root *root,
3586 struct inode *dir, struct inode *inode,
3587 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003588{
3589 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003590 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003591 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003592 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003593 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003594 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003595 u64 ino = btrfs_ino(inode);
3596 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003597
3598 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003599 if (!path) {
3600 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003601 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003602 }
3603
Chris Masonb9473432009-03-13 11:00:37 -04003604 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003605 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003606 name, name_len, -1);
3607 if (IS_ERR(di)) {
3608 ret = PTR_ERR(di);
3609 goto err;
3610 }
3611 if (!di) {
3612 ret = -ENOENT;
3613 goto err;
3614 }
Chris Mason5f39d392007-10-15 16:14:19 -04003615 leaf = path->nodes[0];
3616 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003617 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003618 if (ret)
3619 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003620 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003621
Li Zefan33345d012011-04-20 10:31:50 +08003622 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3623 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003624 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003625 btrfs_info(root->fs_info,
3626 "failed to delete reference to %.*s, inode %llu parent %llu",
3627 name_len, name,
3628 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003629 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003630 goto err;
3631 }
3632
Miao Xie16cdcec2011-04-22 18:12:22 +08003633 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003634 if (ret) {
3635 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003636 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003637 }
Chris Mason39279cc2007-06-12 06:35:45 -04003638
Chris Masone02119d2008-09-05 16:13:11 -04003639 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003640 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003641 if (ret != 0 && ret != -ENOENT) {
3642 btrfs_abort_transaction(trans, root, ret);
3643 goto err;
3644 }
Chris Masone02119d2008-09-05 16:13:11 -04003645
3646 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3647 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003648 if (ret == -ENOENT)
3649 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003650 else if (ret)
3651 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003652err:
3653 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003654 if (ret)
3655 goto out;
3656
3657 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003658 inode_inc_iversion(inode);
3659 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003660 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003661 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003662out:
Chris Mason39279cc2007-06-12 06:35:45 -04003663 return ret;
3664}
3665
Al Viro92986792011-03-04 17:14:37 +00003666int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3667 struct btrfs_root *root,
3668 struct inode *dir, struct inode *inode,
3669 const char *name, int name_len)
3670{
3671 int ret;
3672 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3673 if (!ret) {
3674 btrfs_drop_nlink(inode);
3675 ret = btrfs_update_inode(trans, root, inode);
3676 }
3677 return ret;
3678}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003679
3680/*
3681 * helper to start transaction for unlink and rmdir.
3682 *
Josef Bacikd52be812013-05-29 14:54:47 -04003683 * unlink and rmdir are special in btrfs, they do not always free space, so
3684 * if we cannot make our reservations the normal way try and see if there is
3685 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3686 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003687 */
Josef Bacikd52be812013-05-29 14:54:47 -04003688static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003689{
3690 struct btrfs_trans_handle *trans;
3691 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003692 int ret;
3693
Josef Bacike70bea52011-10-11 14:18:24 -04003694 /*
3695 * 1 for the possible orphan item
3696 * 1 for the dir item
3697 * 1 for the dir index
3698 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003699 * 1 for the inode
3700 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003701 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003702 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3703 return trans;
3704
Josef Bacikd52be812013-05-29 14:54:47 -04003705 if (PTR_ERR(trans) == -ENOSPC) {
3706 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003707
Josef Bacikd52be812013-05-29 14:54:47 -04003708 trans = btrfs_start_transaction(root, 0);
3709 if (IS_ERR(trans))
3710 return trans;
3711 ret = btrfs_cond_migrate_bytes(root->fs_info,
3712 &root->fs_info->trans_block_rsv,
3713 num_bytes, 5);
3714 if (ret) {
3715 btrfs_end_transaction(trans, root);
3716 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003717 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003718 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003719 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003720 }
Josef Bacikd52be812013-05-29 14:54:47 -04003721 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003722}
3723
Chris Mason39279cc2007-06-12 06:35:45 -04003724static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3725{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003726 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003727 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003728 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003729 int ret;
3730
Josef Bacikd52be812013-05-29 14:54:47 -04003731 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003732 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003733 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003734
Chris Mason12fcfd22009-03-24 10:24:20 -04003735 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3736
Chris Masone02119d2008-09-05 16:13:11 -04003737 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3738 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003739 if (ret)
3740 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003741
Yan, Zhenga22285a2010-05-16 10:48:46 -04003742 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003743 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003744 if (ret)
3745 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003746 }
Josef Bacik7b128762008-07-24 12:17:14 -04003747
Tsutomu Itohb5324022011-07-19 07:27:20 +00003748out:
Josef Bacikd52be812013-05-29 14:54:47 -04003749 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003750 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003751 return ret;
3752}
3753
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003754int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3755 struct btrfs_root *root,
3756 struct inode *dir, u64 objectid,
3757 const char *name, int name_len)
3758{
3759 struct btrfs_path *path;
3760 struct extent_buffer *leaf;
3761 struct btrfs_dir_item *di;
3762 struct btrfs_key key;
3763 u64 index;
3764 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003765 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003766
3767 path = btrfs_alloc_path();
3768 if (!path)
3769 return -ENOMEM;
3770
Li Zefan33345d012011-04-20 10:31:50 +08003771 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003772 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003773 if (IS_ERR_OR_NULL(di)) {
3774 if (!di)
3775 ret = -ENOENT;
3776 else
3777 ret = PTR_ERR(di);
3778 goto out;
3779 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003780
3781 leaf = path->nodes[0];
3782 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3783 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3784 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003785 if (ret) {
3786 btrfs_abort_transaction(trans, root, ret);
3787 goto out;
3788 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003789 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003790
3791 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3792 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003793 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003794 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003795 if (ret != -ENOENT) {
3796 btrfs_abort_transaction(trans, root, ret);
3797 goto out;
3798 }
Li Zefan33345d012011-04-20 10:31:50 +08003799 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003800 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003801 if (IS_ERR_OR_NULL(di)) {
3802 if (!di)
3803 ret = -ENOENT;
3804 else
3805 ret = PTR_ERR(di);
3806 btrfs_abort_transaction(trans, root, ret);
3807 goto out;
3808 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003809
3810 leaf = path->nodes[0];
3811 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003812 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003813 index = key.offset;
3814 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003815 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003816
Miao Xie16cdcec2011-04-22 18:12:22 +08003817 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003818 if (ret) {
3819 btrfs_abort_transaction(trans, root, ret);
3820 goto out;
3821 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003822
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003823 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003824 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003825 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003826 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003827 if (ret)
3828 btrfs_abort_transaction(trans, root, ret);
3829out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003830 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003831 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003832}
3833
Chris Mason39279cc2007-06-12 06:35:45 -04003834static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3835{
3836 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003837 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003838 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003839 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003840
David Sterbab3ae2442012-09-13 16:04:34 -06003841 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003842 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003843 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3844 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003845
Josef Bacikd52be812013-05-29 14:54:47 -04003846 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003847 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003848 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003849
Li Zefan33345d012011-04-20 10:31:50 +08003850 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003851 err = btrfs_unlink_subvol(trans, root, dir,
3852 BTRFS_I(inode)->location.objectid,
3853 dentry->d_name.name,
3854 dentry->d_name.len);
3855 goto out;
3856 }
3857
Josef Bacik7b128762008-07-24 12:17:14 -04003858 err = btrfs_orphan_add(trans, inode);
3859 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003860 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003861
Chris Mason39279cc2007-06-12 06:35:45 -04003862 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003863 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3864 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003865 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003866 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003867out:
Josef Bacikd52be812013-05-29 14:54:47 -04003868 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003869 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003870
Chris Mason39279cc2007-06-12 06:35:45 -04003871 return err;
3872}
3873
Chris Mason323ac952008-10-01 19:05:46 -04003874/*
Chris Mason39279cc2007-06-12 06:35:45 -04003875 * this can truncate away extent items, csum items and directory items.
3876 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003877 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003878 *
3879 * csum items that cross the new i_size are truncated to the new size
3880 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003881 *
3882 * min_type is the minimum key type to truncate down to. If set to 0, this
3883 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003884 */
Yan, Zheng80825102009-11-12 09:35:36 +00003885int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3886 struct btrfs_root *root,
3887 struct inode *inode,
3888 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003889{
Chris Mason39279cc2007-06-12 06:35:45 -04003890 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003891 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003892 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003893 struct btrfs_key key;
3894 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003895 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003896 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003897 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003898 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003899 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003900 int found_extent;
3901 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003902 int pending_del_nr = 0;
3903 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003904 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003905 int ret;
3906 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003907 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003908
3909 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003910
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003911 path = btrfs_alloc_path();
3912 if (!path)
3913 return -ENOMEM;
3914 path->reada = -1;
3915
Josef Bacik5dc562c2012-08-17 13:14:17 -04003916 /*
3917 * We want to drop from the next block forward in case this new size is
3918 * not block aligned since we will be keeping the last block of the
3919 * extent just the way it is.
3920 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003921 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00003922 btrfs_drop_extent_cache(inode, ALIGN(new_size,
3923 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003924
Miao Xie16cdcec2011-04-22 18:12:22 +08003925 /*
3926 * This function is also used to drop the items in the log tree before
3927 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3928 * it is used to drop the loged items. So we shouldn't kill the delayed
3929 * items.
3930 */
3931 if (min_type == 0 && root == BTRFS_I(inode)->root)
3932 btrfs_kill_delayed_inode_items(inode);
3933
Li Zefan33345d012011-04-20 10:31:50 +08003934 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003935 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003936 key.type = (u8)-1;
3937
Chris Mason85e21ba2008-01-29 15:11:36 -05003938search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003939 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003940 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003941 if (ret < 0) {
3942 err = ret;
3943 goto out;
3944 }
Chris Masond3977122009-01-05 21:25:51 -05003945
Chris Mason85e21ba2008-01-29 15:11:36 -05003946 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003947 /* there are no items in the tree for us to truncate, we're
3948 * done
3949 */
Yan, Zheng80825102009-11-12 09:35:36 +00003950 if (path->slots[0] == 0)
3951 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003952 path->slots[0]--;
3953 }
3954
Chris Masond3977122009-01-05 21:25:51 -05003955 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003956 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003957 leaf = path->nodes[0];
3958 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3959 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003960
Li Zefan33345d012011-04-20 10:31:50 +08003961 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003962 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003963
Chris Mason85e21ba2008-01-29 15:11:36 -05003964 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003965 break;
3966
Chris Mason5f39d392007-10-15 16:14:19 -04003967 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003968 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003969 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003970 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003971 extent_type = btrfs_file_extent_type(leaf, fi);
3972 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003973 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003974 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003975 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003976 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003977 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003978 }
Yan008630c2007-11-07 13:31:09 -05003979 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003980 }
Yan, Zheng80825102009-11-12 09:35:36 +00003981 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003982 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003983 } else {
3984 if (item_end < new_size)
3985 break;
3986 if (found_key.offset >= new_size)
3987 del_item = 1;
3988 else
3989 del_item = 0;
3990 }
Chris Mason39279cc2007-06-12 06:35:45 -04003991 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003992 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003993 if (found_type != BTRFS_EXTENT_DATA_KEY)
3994 goto delete;
3995
3996 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003997 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003998 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003999 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004000 u64 orig_num_bytes =
4001 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004002 extent_num_bytes = ALIGN(new_size -
4003 found_key.offset,
4004 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004005 btrfs_set_file_extent_num_bytes(leaf, fi,
4006 extent_num_bytes);
4007 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004008 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004009 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004010 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004011 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004012 } else {
Chris Masondb945352007-10-15 16:15:53 -04004013 extent_num_bytes =
4014 btrfs_file_extent_disk_num_bytes(leaf,
4015 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004016 extent_offset = found_key.offset -
4017 btrfs_file_extent_offset(leaf, fi);
4018
Chris Mason39279cc2007-06-12 06:35:45 -04004019 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004020 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004021 if (extent_start != 0) {
4022 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004023 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004024 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004025 }
4026 }
Chris Mason90692182008-02-08 13:49:28 -05004027 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004028 /*
4029 * we can't truncate inline items that have had
4030 * special encodings
4031 */
4032 if (!del_item &&
4033 btrfs_file_extent_compression(leaf, fi) == 0 &&
4034 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4035 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004036 u32 size = new_size - found_key.offset;
4037
4038 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004039 inode_sub_bytes(inode, item_end + 1 -
4040 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004041 }
4042 size =
4043 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004044 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004045 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004046 inode_sub_bytes(inode, item_end + 1 -
4047 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004048 }
Chris Mason39279cc2007-06-12 06:35:45 -04004049 }
Chris Mason179e29e2007-11-01 11:28:41 -04004050delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004051 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004052 if (!pending_del_nr) {
4053 /* no pending yet, add ourselves */
4054 pending_del_slot = path->slots[0];
4055 pending_del_nr = 1;
4056 } else if (pending_del_nr &&
4057 path->slots[0] + 1 == pending_del_slot) {
4058 /* hop on the pending chunk */
4059 pending_del_nr++;
4060 pending_del_slot = path->slots[0];
4061 } else {
Chris Masond3977122009-01-05 21:25:51 -05004062 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004063 }
Chris Mason39279cc2007-06-12 06:35:45 -04004064 } else {
4065 break;
4066 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004067 if (found_extent && (root->ref_cows ||
4068 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004069 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004070 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004071 extent_num_bytes, 0,
4072 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004073 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004074 BUG_ON(ret);
4075 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004076
Yan, Zheng80825102009-11-12 09:35:36 +00004077 if (found_type == BTRFS_INODE_ITEM_KEY)
4078 break;
4079
4080 if (path->slots[0] == 0 ||
4081 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004082 if (pending_del_nr) {
4083 ret = btrfs_del_items(trans, root, path,
4084 pending_del_slot,
4085 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004086 if (ret) {
4087 btrfs_abort_transaction(trans,
4088 root, ret);
4089 goto error;
4090 }
Yan, Zheng80825102009-11-12 09:35:36 +00004091 pending_del_nr = 0;
4092 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004093 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004094 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004095 } else {
4096 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004097 }
Chris Mason39279cc2007-06-12 06:35:45 -04004098 }
Yan, Zheng80825102009-11-12 09:35:36 +00004099out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004100 if (pending_del_nr) {
4101 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4102 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004103 if (ret)
4104 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004105 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004106error:
Chris Mason39279cc2007-06-12 06:35:45 -04004107 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004108 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004109}
4110
Chris Masona52d9a82007-08-27 16:49:44 -04004111/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004112 * btrfs_truncate_page - read, zero a chunk and write a page
4113 * @inode - inode that we're zeroing
4114 * @from - the offset to start zeroing
4115 * @len - the length to zero, 0 to zero the entire range respective to the
4116 * offset
4117 * @front - zero up to the offset instead of from the offset on
4118 *
4119 * This will find the page for the "from" offset and cow the page and zero the
4120 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004121 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004122int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4123 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004124{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004125 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004126 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004127 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4128 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004129 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004130 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004131 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004132 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4133 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4134 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004135 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004136 int ret = 0;
4137 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004138 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004139
Josef Bacik2aaa6652012-08-29 14:27:18 -04004140 if ((offset & (blocksize - 1)) == 0 &&
4141 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004142 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004143 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004144 if (ret)
4145 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004146
Chris Mason211c17f2008-05-15 09:13:45 -04004147again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004148 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004149 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004150 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004151 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004152 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004153 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004154
4155 page_start = page_offset(page);
4156 page_end = page_start + PAGE_CACHE_SIZE - 1;
4157
Chris Masona52d9a82007-08-27 16:49:44 -04004158 if (!PageUptodate(page)) {
4159 ret = btrfs_readpage(NULL, page);
4160 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004161 if (page->mapping != mapping) {
4162 unlock_page(page);
4163 page_cache_release(page);
4164 goto again;
4165 }
Chris Masona52d9a82007-08-27 16:49:44 -04004166 if (!PageUptodate(page)) {
4167 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004168 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004169 }
4170 }
Chris Mason211c17f2008-05-15 09:13:45 -04004171 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004172
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004173 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004174 set_page_extent_mapped(page);
4175
4176 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4177 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004178 unlock_extent_cached(io_tree, page_start, page_end,
4179 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004180 unlock_page(page);
4181 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004182 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004183 btrfs_put_ordered_extent(ordered);
4184 goto again;
4185 }
4186
Josef Bacik2ac55d42010-02-03 19:33:23 +00004187 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004188 EXTENT_DIRTY | EXTENT_DELALLOC |
4189 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004190 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004191
Josef Bacik2ac55d42010-02-03 19:33:23 +00004192 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4193 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004194 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004195 unlock_extent_cached(io_tree, page_start, page_end,
4196 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004197 goto out_unlock;
4198 }
4199
Chris Masone6dcd2d2008-07-17 12:53:50 -04004200 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004201 if (!len)
4202 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004203 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004204 if (front)
4205 memset(kaddr, 0, offset);
4206 else
4207 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004208 flush_dcache_page(page);
4209 kunmap(page);
4210 }
Chris Mason247e7432008-07-17 12:53:51 -04004211 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004212 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004213 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4214 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004215
Chris Mason89642222008-07-24 09:41:53 -04004216out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004217 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004218 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004219 unlock_page(page);
4220 page_cache_release(page);
4221out:
4222 return ret;
4223}
4224
Josef Bacik695a0d02011-03-04 15:46:53 -05004225/*
4226 * This function puts in dummy file extents for the area we're creating a hole
4227 * for. So if we are truncating this file to a larger size we need to insert
4228 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4229 * the range between oldsize and size
4230 */
Josef Bacika41ad392011-01-31 15:30:16 -05004231int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004232{
4233 struct btrfs_trans_handle *trans;
4234 struct btrfs_root *root = BTRFS_I(inode)->root;
4235 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004236 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004237 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004238 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004239 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4240 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004241 u64 last_byte;
4242 u64 cur_offset;
4243 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004244 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004245
Josef Bacika71754f2013-06-17 17:14:39 -04004246 /*
4247 * If our size started in the middle of a page we need to zero out the
4248 * rest of the page before we expand the i_size, otherwise we could
4249 * expose stale data.
4250 */
4251 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4252 if (err)
4253 return err;
4254
Yan Zheng9036c102008-10-30 14:19:41 -04004255 if (size <= hole_start)
4256 return 0;
4257
Yan Zheng9036c102008-10-30 14:19:41 -04004258 while (1) {
4259 struct btrfs_ordered_extent *ordered;
4260 btrfs_wait_ordered_range(inode, hole_start,
4261 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004262 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004263 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004264 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4265 if (!ordered)
4266 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004267 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4268 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004269 btrfs_put_ordered_extent(ordered);
4270 }
4271
Yan Zheng9036c102008-10-30 14:19:41 -04004272 cur_offset = hole_start;
4273 while (1) {
4274 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4275 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004276 if (IS_ERR(em)) {
4277 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004278 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004279 break;
4280 }
Yan Zheng9036c102008-10-30 14:19:41 -04004281 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004282 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004283 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004284 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004285 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004286
Miao Xie36423202011-12-14 20:12:02 -05004287 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004288 if (IS_ERR(trans)) {
4289 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004290 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004291 }
Yan, Zheng80825102009-11-12 09:35:36 +00004292
Josef Bacik5dc562c2012-08-17 13:14:17 -04004293 err = btrfs_drop_extents(trans, root, inode,
4294 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004295 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004296 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004297 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004298 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004299 break;
Miao Xie5b397372011-09-11 10:52:24 -04004300 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004301
Yan Zheng9036c102008-10-30 14:19:41 -04004302 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004303 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004304 0, hole_size, 0, hole_size,
4305 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004306 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004307 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004308 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004309 break;
Miao Xie5b397372011-09-11 10:52:24 -04004310 }
Yan, Zheng80825102009-11-12 09:35:36 +00004311
Josef Bacik5dc562c2012-08-17 13:14:17 -04004312 btrfs_drop_extent_cache(inode, cur_offset,
4313 cur_offset + hole_size - 1, 0);
4314 hole_em = alloc_extent_map();
4315 if (!hole_em) {
4316 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4317 &BTRFS_I(inode)->runtime_flags);
4318 goto next;
4319 }
4320 hole_em->start = cur_offset;
4321 hole_em->len = hole_size;
4322 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004323
Josef Bacik5dc562c2012-08-17 13:14:17 -04004324 hole_em->block_start = EXTENT_MAP_HOLE;
4325 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004326 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004327 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004328 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4329 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4330 hole_em->generation = trans->transid;
4331
4332 while (1) {
4333 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004334 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004335 write_unlock(&em_tree->lock);
4336 if (err != -EEXIST)
4337 break;
4338 btrfs_drop_extent_cache(inode, cur_offset,
4339 cur_offset +
4340 hole_size - 1, 0);
4341 }
4342 free_extent_map(hole_em);
4343next:
Miao Xie36423202011-12-14 20:12:02 -05004344 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004345 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004346 }
4347 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004348 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004349 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004350 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004351 break;
4352 }
4353
Yan, Zhenga22285a2010-05-16 10:48:46 -04004354 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004355 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4356 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004357 return err;
4358}
4359
Eric Sandeen3972f262013-01-12 02:57:22 +00004360static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004361{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004362 struct btrfs_root *root = BTRFS_I(inode)->root;
4363 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004364 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004365 loff_t newsize = attr->ia_size;
4366 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004367 int ret;
4368
Eric Sandeen3972f262013-01-12 02:57:22 +00004369 /*
4370 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4371 * special case where we need to update the times despite not having
4372 * these flags set. For all other operations the VFS set these flags
4373 * explicitly if it wants a timestamp update.
4374 */
4375 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4376 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4377
Josef Bacika41ad392011-01-31 15:30:16 -05004378 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004379 truncate_pagecache(inode, oldsize, newsize);
4380 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004381 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004382 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004383
Miao Xief4a2f4c2011-12-14 20:12:01 -05004384 trans = btrfs_start_transaction(root, 1);
4385 if (IS_ERR(trans))
4386 return PTR_ERR(trans);
4387
4388 i_size_write(inode, newsize);
4389 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4390 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004391 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004392 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004393
Josef Bacika41ad392011-01-31 15:30:16 -05004394 /*
4395 * We're truncating a file that used to have good data down to
4396 * zero. Make sure it gets into the ordered flush list so that
4397 * any new writes get down to disk quickly.
4398 */
4399 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004400 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4401 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004402
Josef Bacikf3fe8202013-01-07 17:03:21 -05004403 /*
4404 * 1 for the orphan item we're going to add
4405 * 1 for the orphan item deletion.
4406 */
4407 trans = btrfs_start_transaction(root, 2);
4408 if (IS_ERR(trans))
4409 return PTR_ERR(trans);
4410
4411 /*
4412 * We need to do this in case we fail at _any_ point during the
4413 * actual truncate. Once we do the truncate_setsize we could
4414 * invalidate pages which forces any outstanding ordered io to
4415 * be instantly completed which will give us extents that need
4416 * to be truncated. If we fail to get an orphan inode down we
4417 * could have left over extents that were never meant to live,
4418 * so we need to garuntee from this point on that everything
4419 * will be consistent.
4420 */
4421 ret = btrfs_orphan_add(trans, inode);
4422 btrfs_end_transaction(trans, root);
4423 if (ret)
4424 return ret;
4425
Josef Bacika41ad392011-01-31 15:30:16 -05004426 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4427 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004428
4429 /* Disable nonlocked read DIO to avoid the end less truncate */
4430 btrfs_inode_block_unlocked_dio(inode);
4431 inode_dio_wait(inode);
4432 btrfs_inode_resume_unlocked_dio(inode);
4433
Josef Bacika41ad392011-01-31 15:30:16 -05004434 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004435 if (ret && inode->i_nlink)
4436 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004437 }
4438
Josef Bacika41ad392011-01-31 15:30:16 -05004439 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004440}
4441
Chris Mason39279cc2007-06-12 06:35:45 -04004442static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4443{
4444 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004445 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004446 int err;
4447
Li Zefanb83cc962010-12-20 16:04:08 +08004448 if (btrfs_root_readonly(root))
4449 return -EROFS;
4450
Chris Mason39279cc2007-06-12 06:35:45 -04004451 err = inode_change_ok(inode, attr);
4452 if (err)
4453 return err;
4454
Chris Mason5a3f23d2009-03-31 13:27:11 -04004455 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004456 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004457 if (err)
4458 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004459 }
Yan Zheng9036c102008-10-30 14:19:41 -04004460
Christoph Hellwig10257742010-06-04 11:30:02 +02004461 if (attr->ia_valid) {
4462 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004463 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004464 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004465
Josef Bacik22c44fe2011-11-30 10:45:38 -05004466 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004467 err = btrfs_acl_chmod(inode);
4468 }
4469
Chris Mason39279cc2007-06-12 06:35:45 -04004470 return err;
4471}
Chris Mason61295eb2008-01-14 16:24:38 -05004472
Al Virobd555972010-06-07 11:35:40 -04004473void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004474{
4475 struct btrfs_trans_handle *trans;
4476 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004477 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004478 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004479 int ret;
4480
liubo1abe9b82011-03-24 11:18:59 +00004481 trace_btrfs_inode_evict(inode);
4482
Chris Mason39279cc2007-06-12 06:35:45 -04004483 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004484 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004485 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004486 goto no_delete;
4487
Chris Mason39279cc2007-06-12 06:35:45 -04004488 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004489 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004490 goto no_delete;
4491 }
Al Virobd555972010-06-07 11:35:40 -04004492 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004493 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004494
Yan, Zhengc71bf092009-11-12 09:34:40 +00004495 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004496 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004497 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004498 goto no_delete;
4499 }
4500
Yan, Zheng76dda932009-09-21 16:00:26 -04004501 if (inode->i_nlink > 0) {
4502 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4503 goto no_delete;
4504 }
4505
Miao Xie0e8c36a2012-12-19 06:59:51 +00004506 ret = btrfs_commit_inode_delayed_inode(inode);
4507 if (ret) {
4508 btrfs_orphan_del(NULL, inode);
4509 goto no_delete;
4510 }
4511
Miao Xie66d8f3d2012-09-06 04:02:28 -06004512 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004513 if (!rsv) {
4514 btrfs_orphan_del(NULL, inode);
4515 goto no_delete;
4516 }
Josef Bacik4a338542011-08-29 11:01:31 -04004517 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004518 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004519 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004520
Chris Masondbe674a2008-07-17 12:54:05 -04004521 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004522
Josef Bacik4289a662011-08-05 13:22:24 -04004523 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004524 * This is a bit simpler than btrfs_truncate since we've already
4525 * reserved our space for our orphan item in the unlink, so we just
4526 * need to reserve some slack space in case we add bytes and update
4527 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004528 */
Yan, Zheng80825102009-11-12 09:35:36 +00004529 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004530 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4531 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004532
Josef Bacik726c35f2011-09-26 15:46:06 -04004533 /*
4534 * Try and steal from the global reserve since we will
4535 * likely not use this space anyway, we want to try as
4536 * hard as possible to get this to work.
4537 */
4538 if (ret)
4539 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4540
Yan, Zhengd68fc572010-05-16 10:49:58 -04004541 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004542 btrfs_warn(root->fs_info,
4543 "Could not get space for a delete, will truncate on mount %d",
4544 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004545 btrfs_orphan_del(NULL, inode);
4546 btrfs_free_block_rsv(root, rsv);
4547 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004548 }
4549
Miao Xie0e8c36a2012-12-19 06:59:51 +00004550 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004551 if (IS_ERR(trans)) {
4552 btrfs_orphan_del(NULL, inode);
4553 btrfs_free_block_rsv(root, rsv);
4554 goto no_delete;
4555 }
4556
4557 trans->block_rsv = rsv;
4558
Yan, Zhengd68fc572010-05-16 10:49:58 -04004559 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004560 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004561 break;
4562
Miao Xie8407aa42012-09-07 01:43:32 -06004563 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004564 btrfs_end_transaction(trans, root);
4565 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004566 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004567 }
4568
Josef Bacik4289a662011-08-05 13:22:24 -04004569 btrfs_free_block_rsv(root, rsv);
4570
Yan, Zheng80825102009-11-12 09:35:36 +00004571 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004572 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004573 ret = btrfs_orphan_del(trans, inode);
4574 BUG_ON(ret);
4575 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004576
Josef Bacik4289a662011-08-05 13:22:24 -04004577 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004578 if (!(root == root->fs_info->tree_root ||
4579 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004580 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004581
Chris Mason54aa1f42007-06-22 14:16:25 -04004582 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004583 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004584no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004585 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004586 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004587 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004588}
4589
4590/*
4591 * this returns the key found in the dir entry in the location pointer.
4592 * If no dir entries were found, location->objectid is 0.
4593 */
4594static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4595 struct btrfs_key *location)
4596{
4597 const char *name = dentry->d_name.name;
4598 int namelen = dentry->d_name.len;
4599 struct btrfs_dir_item *di;
4600 struct btrfs_path *path;
4601 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004602 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004603
4604 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004605 if (!path)
4606 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004607
Li Zefan33345d012011-04-20 10:31:50 +08004608 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004609 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004610 if (IS_ERR(di))
4611 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004612
David Sterbac7040052011-04-19 18:00:01 +02004613 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004614 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004615
Chris Mason5f39d392007-10-15 16:14:19 -04004616 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004617out:
Chris Mason39279cc2007-06-12 06:35:45 -04004618 btrfs_free_path(path);
4619 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004620out_err:
4621 location->objectid = 0;
4622 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004623}
4624
4625/*
4626 * when we hit a tree root in a directory, the btrfs part of the inode
4627 * needs to be changed to reflect the root directory of the tree root. This
4628 * is kind of like crossing a mount point.
4629 */
4630static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004631 struct inode *dir,
4632 struct dentry *dentry,
4633 struct btrfs_key *location,
4634 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004635{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004636 struct btrfs_path *path;
4637 struct btrfs_root *new_root;
4638 struct btrfs_root_ref *ref;
4639 struct extent_buffer *leaf;
4640 int ret;
4641 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004642
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004643 path = btrfs_alloc_path();
4644 if (!path) {
4645 err = -ENOMEM;
4646 goto out;
4647 }
Chris Mason39279cc2007-06-12 06:35:45 -04004648
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004649 err = -ENOENT;
4650 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4651 BTRFS_I(dir)->root->root_key.objectid,
4652 location->objectid);
4653 if (ret) {
4654 if (ret < 0)
4655 err = ret;
4656 goto out;
4657 }
Chris Mason39279cc2007-06-12 06:35:45 -04004658
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004659 leaf = path->nodes[0];
4660 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004661 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004662 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4663 goto out;
4664
4665 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4666 (unsigned long)(ref + 1),
4667 dentry->d_name.len);
4668 if (ret)
4669 goto out;
4670
David Sterbab3b4aa72011-04-21 01:20:15 +02004671 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004672
4673 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4674 if (IS_ERR(new_root)) {
4675 err = PTR_ERR(new_root);
4676 goto out;
4677 }
4678
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004679 *sub_root = new_root;
4680 location->objectid = btrfs_root_dirid(&new_root->root_item);
4681 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004682 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004683 err = 0;
4684out:
4685 btrfs_free_path(path);
4686 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004687}
4688
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004689static void inode_tree_add(struct inode *inode)
4690{
4691 struct btrfs_root *root = BTRFS_I(inode)->root;
4692 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004693 struct rb_node **p;
4694 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004695 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004696
Al Viro1d3382cb2010-10-23 15:19:20 -04004697 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004698 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004699again:
4700 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004701 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004702 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004703 while (*p) {
4704 parent = *p;
4705 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4706
Li Zefan33345d012011-04-20 10:31:50 +08004707 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004708 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004709 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004710 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004711 else {
4712 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004713 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004714 rb_erase(parent, &root->inode_tree);
4715 RB_CLEAR_NODE(parent);
4716 spin_unlock(&root->inode_lock);
4717 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004718 }
4719 }
4720 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4721 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4722 spin_unlock(&root->inode_lock);
4723}
4724
4725static void inode_tree_del(struct inode *inode)
4726{
4727 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004728 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004729
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004730 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004731 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004732 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004733 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004734 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004735 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004736 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004737
Josef Bacik0af3d002010-06-21 14:48:16 -04004738 /*
4739 * Free space cache has inodes in the tree root, but the tree root has a
4740 * root_refs of 0, so this could end up dropping the tree root as a
4741 * snapshot, so we need the extra !root->fs_info->tree_root check to
4742 * make sure we don't drop it.
4743 */
4744 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4745 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004746 synchronize_srcu(&root->fs_info->subvol_srcu);
4747 spin_lock(&root->inode_lock);
4748 empty = RB_EMPTY_ROOT(&root->inode_tree);
4749 spin_unlock(&root->inode_lock);
4750 if (empty)
4751 btrfs_add_dead_root(root);
4752 }
4753}
4754
Jeff Mahoney143bede2012-03-01 14:56:26 +01004755void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004756{
4757 struct rb_node *node;
4758 struct rb_node *prev;
4759 struct btrfs_inode *entry;
4760 struct inode *inode;
4761 u64 objectid = 0;
4762
4763 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4764
4765 spin_lock(&root->inode_lock);
4766again:
4767 node = root->inode_tree.rb_node;
4768 prev = NULL;
4769 while (node) {
4770 prev = node;
4771 entry = rb_entry(node, struct btrfs_inode, rb_node);
4772
Li Zefan33345d012011-04-20 10:31:50 +08004773 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004774 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004775 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004776 node = node->rb_right;
4777 else
4778 break;
4779 }
4780 if (!node) {
4781 while (prev) {
4782 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004783 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004784 node = prev;
4785 break;
4786 }
4787 prev = rb_next(prev);
4788 }
4789 }
4790 while (node) {
4791 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004792 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004793 inode = igrab(&entry->vfs_inode);
4794 if (inode) {
4795 spin_unlock(&root->inode_lock);
4796 if (atomic_read(&inode->i_count) > 1)
4797 d_prune_aliases(inode);
4798 /*
Al Viro45321ac2010-06-07 13:43:19 -04004799 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004800 * the inode cache when its usage count
4801 * hits zero.
4802 */
4803 iput(inode);
4804 cond_resched();
4805 spin_lock(&root->inode_lock);
4806 goto again;
4807 }
4808
4809 if (cond_resched_lock(&root->inode_lock))
4810 goto again;
4811
4812 node = rb_next(node);
4813 }
4814 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004815}
4816
Chris Masone02119d2008-09-05 16:13:11 -04004817static int btrfs_init_locked_inode(struct inode *inode, void *p)
4818{
4819 struct btrfs_iget_args *args = p;
4820 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004821 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004822 return 0;
4823}
4824
4825static int btrfs_find_actor(struct inode *inode, void *opaque)
4826{
4827 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004828 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004829 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004830}
4831
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004832static struct inode *btrfs_iget_locked(struct super_block *s,
4833 u64 objectid,
4834 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004835{
4836 struct inode *inode;
4837 struct btrfs_iget_args args;
4838 args.ino = objectid;
4839 args.root = root;
4840
4841 inode = iget5_locked(s, objectid, btrfs_find_actor,
4842 btrfs_init_locked_inode,
4843 (void *)&args);
4844 return inode;
4845}
4846
Balaji Rao1a54ef82008-07-21 02:01:04 +05304847/* Get an inode object given its location and corresponding root.
4848 * Returns in *is_new if the inode was read from disk
4849 */
4850struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004851 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304852{
4853 struct inode *inode;
4854
4855 inode = btrfs_iget_locked(s, location->objectid, root);
4856 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004857 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304858
4859 if (inode->i_state & I_NEW) {
4860 BTRFS_I(inode)->root = root;
4861 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4862 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004863 if (!is_bad_inode(inode)) {
4864 inode_tree_add(inode);
4865 unlock_new_inode(inode);
4866 if (new)
4867 *new = 1;
4868 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004869 unlock_new_inode(inode);
4870 iput(inode);
4871 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004872 }
4873 }
4874
Balaji Rao1a54ef82008-07-21 02:01:04 +05304875 return inode;
4876}
4877
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004878static struct inode *new_simple_dir(struct super_block *s,
4879 struct btrfs_key *key,
4880 struct btrfs_root *root)
4881{
4882 struct inode *inode = new_inode(s);
4883
4884 if (!inode)
4885 return ERR_PTR(-ENOMEM);
4886
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004887 BTRFS_I(inode)->root = root;
4888 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004889 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004890
4891 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004892 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004893 inode->i_fop = &simple_dir_operations;
4894 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4895 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4896
4897 return inode;
4898}
4899
Chris Mason3de45862008-11-17 21:02:50 -05004900struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004901{
Chris Masond3977122009-01-05 21:25:51 -05004902 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004903 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004904 struct btrfs_root *sub_root = root;
4905 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004906 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004907 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004908
4909 if (dentry->d_name.len > BTRFS_NAME_LEN)
4910 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004911
Jeff Layton39e3c952012-11-28 11:30:53 -05004912 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04004913 if (ret < 0)
4914 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004915
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004916 if (location.objectid == 0)
4917 return NULL;
4918
4919 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004920 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004921 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004922 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004923
4924 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4925
Yan, Zheng76dda932009-09-21 16:00:26 -04004926 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004927 ret = fixup_tree_root_location(root, dir, dentry,
4928 &location, &sub_root);
4929 if (ret < 0) {
4930 if (ret != -ENOENT)
4931 inode = ERR_PTR(ret);
4932 else
4933 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4934 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004935 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004936 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004937 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4938
Julia Lawall34d19ba2011-01-24 19:55:19 +00004939 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004940 down_read(&root->fs_info->cleanup_work_sem);
4941 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004942 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004943 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04004944 if (ret) {
4945 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004946 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04004947 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00004948 }
4949
Chris Mason3de45862008-11-17 21:02:50 -05004950 return inode;
4951}
4952
Nick Pigginfe15ce42011-01-07 17:49:23 +11004953static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004954{
4955 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004956 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004957
Li Zefan848cce02012-02-21 17:04:28 +08004958 if (!inode && !IS_ROOT(dentry))
4959 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004960
Li Zefan848cce02012-02-21 17:04:28 +08004961 if (inode) {
4962 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004963 if (btrfs_root_refs(&root->root_item) == 0)
4964 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004965
4966 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4967 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004968 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004969 return 0;
4970}
4971
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004972static void btrfs_dentry_release(struct dentry *dentry)
4973{
4974 if (dentry->d_fsdata)
4975 kfree(dentry->d_fsdata);
4976}
4977
Chris Mason3de45862008-11-17 21:02:50 -05004978static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004979 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004980{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004981 struct dentry *ret;
4982
4983 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04004984 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004985}
4986
Miao Xie16cdcec2011-04-22 18:12:22 +08004987unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004988 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4989};
4990
Al Viro9cdda8d2013-05-22 16:48:09 -04004991static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04004992{
Al Viro9cdda8d2013-05-22 16:48:09 -04004993 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04004994 struct btrfs_root *root = BTRFS_I(inode)->root;
4995 struct btrfs_item *item;
4996 struct btrfs_dir_item *di;
4997 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004998 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004999 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005000 struct list_head ins_list;
5001 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005002 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005003 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005004 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005005 unsigned char d_type;
5006 int over = 0;
5007 u32 di_cur;
5008 u32 di_total;
5009 u32 di_len;
5010 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005011 char tmp_name[32];
5012 char *name_ptr;
5013 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005014 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005015
5016 /* FIXME, use a real flag for deciding about the key type */
5017 if (root->fs_info->tree_root == root)
5018 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005019
Al Viro9cdda8d2013-05-22 16:48:09 -04005020 if (!dir_emit_dots(file, ctx))
5021 return 0;
5022
David Woodhouse49593bf2008-08-17 17:08:36 +01005023 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005024 if (!path)
5025 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005026
Josef Bacik026fd312011-05-13 10:32:11 -04005027 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005028
Miao Xie16cdcec2011-04-22 18:12:22 +08005029 if (key_type == BTRFS_DIR_INDEX_KEY) {
5030 INIT_LIST_HEAD(&ins_list);
5031 INIT_LIST_HEAD(&del_list);
5032 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5033 }
5034
Chris Mason39279cc2007-06-12 06:35:45 -04005035 btrfs_set_key_type(&key, key_type);
Al Viro9cdda8d2013-05-22 16:48:09 -04005036 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005037 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005038
Chris Mason39279cc2007-06-12 06:35:45 -04005039 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5040 if (ret < 0)
5041 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005042
5043 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005044 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005045 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005046 if (slot >= btrfs_header_nritems(leaf)) {
5047 ret = btrfs_next_leaf(root, path);
5048 if (ret < 0)
5049 goto err;
5050 else if (ret > 0)
5051 break;
5052 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005053 }
Chris Mason3de45862008-11-17 21:02:50 -05005054
Chris Mason5f39d392007-10-15 16:14:19 -04005055 item = btrfs_item_nr(leaf, slot);
5056 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5057
5058 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005059 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005060 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005061 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005062 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005063 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005064 if (key_type == BTRFS_DIR_INDEX_KEY &&
5065 btrfs_should_delete_dir_index(&del_list,
5066 found_key.offset))
5067 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005068
Al Viro9cdda8d2013-05-22 16:48:09 -04005069 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005070 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005071
Chris Mason39279cc2007-06-12 06:35:45 -04005072 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5073 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005074 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005075
5076 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005077 struct btrfs_key location;
5078
Josef Bacik22a94d42011-03-16 16:47:17 -04005079 if (verify_dir_item(root, leaf, di))
5080 break;
5081
Chris Mason5f39d392007-10-15 16:14:19 -04005082 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005083 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005084 name_ptr = tmp_name;
5085 } else {
5086 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005087 if (!name_ptr) {
5088 ret = -ENOMEM;
5089 goto err;
5090 }
Chris Mason5f39d392007-10-15 16:14:19 -04005091 }
5092 read_extent_buffer(leaf, name_ptr,
5093 (unsigned long)(di + 1), name_len);
5094
5095 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5096 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005097
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005098
Chris Mason3de45862008-11-17 21:02:50 -05005099 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005100 * skip it.
5101 *
5102 * In contrast to old kernels, we insert the snapshot's
5103 * dir item and dir index after it has been created, so
5104 * we won't find a reference to our own snapshot. We
5105 * still keep the following code for backward
5106 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005107 */
5108 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5109 location.objectid == root->root_key.objectid) {
5110 over = 0;
5111 goto skip;
5112 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005113 over = !dir_emit(ctx, name_ptr, name_len,
5114 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005115
Chris Mason3de45862008-11-17 21:02:50 -05005116skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005117 if (name_ptr != tmp_name)
5118 kfree(name_ptr);
5119
Chris Mason39279cc2007-06-12 06:35:45 -04005120 if (over)
5121 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005122 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005123 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005124 di_cur += di_len;
5125 di = (struct btrfs_dir_item *)((char *)di + di_len);
5126 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005127next:
5128 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005129 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005130
Miao Xie16cdcec2011-04-22 18:12:22 +08005131 if (key_type == BTRFS_DIR_INDEX_KEY) {
5132 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005133 ctx->pos++;
5134 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005135 if (ret)
5136 goto nopos;
5137 }
5138
David Woodhouse49593bf2008-08-17 17:08:36 +01005139 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005140 ctx->pos++;
5141
5142 /*
5143 * Stop new entries from being returned after we return the last
5144 * entry.
5145 *
5146 * New directory entries are assigned a strictly increasing
5147 * offset. This means that new entries created during readdir
5148 * are *guaranteed* to be seen in the future by that readdir.
5149 * This has broken buggy programs which operate on names as
5150 * they're returned by readdir. Until we re-use freed offsets
5151 * we have this hack to stop new entries from being returned
5152 * under the assumption that they'll never reach this huge
5153 * offset.
5154 *
5155 * This is being careful not to overflow 32bit loff_t unless the
5156 * last entry requires it because doing so has broken 32bit apps
5157 * in the past.
5158 */
5159 if (key_type == BTRFS_DIR_INDEX_KEY) {
5160 if (ctx->pos >= INT_MAX)
5161 ctx->pos = LLONG_MAX;
5162 else
5163 ctx->pos = INT_MAX;
5164 }
Chris Mason39279cc2007-06-12 06:35:45 -04005165nopos:
5166 ret = 0;
5167err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005168 if (key_type == BTRFS_DIR_INDEX_KEY)
5169 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005170 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005171 return ret;
5172}
5173
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005174int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005175{
5176 struct btrfs_root *root = BTRFS_I(inode)->root;
5177 struct btrfs_trans_handle *trans;
5178 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005179 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005180
Josef Bacik72ac3c02012-05-23 14:13:11 -04005181 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005182 return 0;
5183
Liu Bo83eea1f2012-07-10 05:28:39 -06005184 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005185 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005186
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005187 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005188 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005189 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005190 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005191 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005192 if (IS_ERR(trans))
5193 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005194 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005195 }
5196 return ret;
5197}
5198
5199/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005200 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005201 * inode changes. But, it is most likely to find the inode in cache.
5202 * FIXME, needs more benchmarking...there are no reasons other than performance
5203 * to keep or drop this code.
5204 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005205static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005206{
5207 struct btrfs_root *root = BTRFS_I(inode)->root;
5208 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005209 int ret;
5210
Josef Bacik72ac3c02012-05-23 14:13:11 -04005211 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005212 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005213
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005214 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005215 if (IS_ERR(trans))
5216 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005217
5218 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005219 if (ret && ret == -ENOSPC) {
5220 /* whoops, lets try again with the full transaction */
5221 btrfs_end_transaction(trans, root);
5222 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005223 if (IS_ERR(trans))
5224 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005225
Chris Mason94b60442010-05-26 11:02:00 -04005226 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005227 }
Chris Mason39279cc2007-06-12 06:35:45 -04005228 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005229 if (BTRFS_I(inode)->delayed_node)
5230 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005231
5232 return ret;
5233}
5234
5235/*
5236 * This is a copy of file_update_time. We need this so we can return error on
5237 * ENOSPC for updating the inode in the case of file write and mmap writes.
5238 */
Josef Bacike41f9412012-03-26 09:46:47 -04005239static int btrfs_update_time(struct inode *inode, struct timespec *now,
5240 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005241{
Alexander Block2bc5565282012-06-15 09:49:33 +02005242 struct btrfs_root *root = BTRFS_I(inode)->root;
5243
5244 if (btrfs_root_readonly(root))
5245 return -EROFS;
5246
Josef Bacike41f9412012-03-26 09:46:47 -04005247 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005248 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005249 if (flags & S_CTIME)
5250 inode->i_ctime = *now;
5251 if (flags & S_MTIME)
5252 inode->i_mtime = *now;
5253 if (flags & S_ATIME)
5254 inode->i_atime = *now;
5255 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005256}
5257
Chris Masond352ac62008-09-29 15:18:18 -04005258/*
5259 * find the highest existing sequence number in a directory
5260 * and then set the in-memory index_cnt variable to reflect
5261 * free sequence numbers
5262 */
Josef Bacikaec74772008-07-24 12:12:38 -04005263static int btrfs_set_inode_index_count(struct inode *inode)
5264{
5265 struct btrfs_root *root = BTRFS_I(inode)->root;
5266 struct btrfs_key key, found_key;
5267 struct btrfs_path *path;
5268 struct extent_buffer *leaf;
5269 int ret;
5270
Li Zefan33345d012011-04-20 10:31:50 +08005271 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005272 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5273 key.offset = (u64)-1;
5274
5275 path = btrfs_alloc_path();
5276 if (!path)
5277 return -ENOMEM;
5278
5279 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5280 if (ret < 0)
5281 goto out;
5282 /* FIXME: we should be able to handle this */
5283 if (ret == 0)
5284 goto out;
5285 ret = 0;
5286
5287 /*
5288 * MAGIC NUMBER EXPLANATION:
5289 * since we search a directory based on f_pos we have to start at 2
5290 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5291 * else has to start at 2
5292 */
5293 if (path->slots[0] == 0) {
5294 BTRFS_I(inode)->index_cnt = 2;
5295 goto out;
5296 }
5297
5298 path->slots[0]--;
5299
5300 leaf = path->nodes[0];
5301 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5302
Li Zefan33345d012011-04-20 10:31:50 +08005303 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005304 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5305 BTRFS_I(inode)->index_cnt = 2;
5306 goto out;
5307 }
5308
5309 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5310out:
5311 btrfs_free_path(path);
5312 return ret;
5313}
5314
Chris Masond352ac62008-09-29 15:18:18 -04005315/*
5316 * helper to find a free sequence number in a given directory. This current
5317 * code is very simple, later versions will do smarter things in the btree
5318 */
Chris Mason3de45862008-11-17 21:02:50 -05005319int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005320{
5321 int ret = 0;
5322
5323 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005324 ret = btrfs_inode_delayed_dir_index_count(dir);
5325 if (ret) {
5326 ret = btrfs_set_inode_index_count(dir);
5327 if (ret)
5328 return ret;
5329 }
Josef Bacikaec74772008-07-24 12:12:38 -04005330 }
5331
Chris Mason00e4e6b2008-08-05 11:18:09 -04005332 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005333 BTRFS_I(dir)->index_cnt++;
5334
5335 return ret;
5336}
5337
Chris Mason39279cc2007-06-12 06:35:45 -04005338static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5339 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005340 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005341 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005342 u64 ref_objectid, u64 objectid,
5343 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005344{
5345 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005346 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005347 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005348 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005349 struct btrfs_inode_ref *ref;
5350 struct btrfs_key key[2];
5351 u32 sizes[2];
5352 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005353 int ret;
5354 int owner;
5355
Chris Mason5f39d392007-10-15 16:14:19 -04005356 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005357 if (!path)
5358 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005359
Chris Mason39279cc2007-06-12 06:35:45 -04005360 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005361 if (!inode) {
5362 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005363 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005364 }
Chris Mason39279cc2007-06-12 06:35:45 -04005365
Li Zefan581bb052011-04-20 10:06:11 +08005366 /*
5367 * we have to initialize this early, so we can reclaim the inode
5368 * number if we fail afterwards in this function.
5369 */
5370 inode->i_ino = objectid;
5371
Josef Bacikaec74772008-07-24 12:12:38 -04005372 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005373 trace_btrfs_inode_request(dir);
5374
Chris Mason3de45862008-11-17 21:02:50 -05005375 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005376 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005377 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005378 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005379 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005380 }
Josef Bacikaec74772008-07-24 12:12:38 -04005381 }
5382 /*
5383 * index_cnt is ignored for everything but a dir,
5384 * btrfs_get_inode_index_count has an explanation for the magic
5385 * number
5386 */
5387 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005388 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005389 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005390 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04005391
Josef Bacik5dc562c2012-08-17 13:14:17 -04005392 /*
5393 * We could have gotten an inode number from somebody who was fsynced
5394 * and then removed in this same transaction, so let's just set full
5395 * sync since it will be a full sync anyway and this will blow away the
5396 * old info in the log.
5397 */
5398 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5399
Al Viro569254b2011-07-24 17:08:40 -04005400 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005401 owner = 0;
5402 else
5403 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005404
5405 key[0].objectid = objectid;
5406 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5407 key[0].offset = 0;
5408
Mark Fashehf1863732012-08-08 11:32:27 -07005409 /*
5410 * Start new inodes with an inode_ref. This is slightly more
5411 * efficient for small numbers of hard links since they will
5412 * be packed into one item. Extended refs will kick in if we
5413 * add more hard links than can fit in the ref item.
5414 */
Chris Mason9c583092008-01-29 15:15:18 -05005415 key[1].objectid = objectid;
5416 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5417 key[1].offset = ref_objectid;
5418
5419 sizes[0] = sizeof(struct btrfs_inode_item);
5420 sizes[1] = name_len + sizeof(*ref);
5421
Chris Masonb9473432009-03-13 11:00:37 -04005422 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005423 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5424 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005425 goto fail;
5426
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005427 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005428 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005429 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005430 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5431 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005432 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5433 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005434 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005435
5436 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5437 struct btrfs_inode_ref);
5438 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005439 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005440 ptr = (unsigned long)(ref + 1);
5441 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5442
Chris Mason5f39d392007-10-15 16:14:19 -04005443 btrfs_mark_buffer_dirty(path->nodes[0]);
5444 btrfs_free_path(path);
5445
Chris Mason39279cc2007-06-12 06:35:45 -04005446 location = &BTRFS_I(inode)->location;
5447 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005448 location->offset = 0;
5449 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5450
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005451 btrfs_inherit_iflags(inode, dir);
5452
Al Viro569254b2011-07-24 17:08:40 -04005453 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005454 if (btrfs_test_opt(root, NODATASUM))
5455 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005456 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005457 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5458 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005459 }
5460
Chris Mason39279cc2007-06-12 06:35:45 -04005461 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005462 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005463
5464 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005465 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005466
Alexander Block8ea05e32012-07-25 17:35:53 +02005467 btrfs_update_root_times(trans, root);
5468
Chris Mason39279cc2007-06-12 06:35:45 -04005469 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005470fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005471 if (dir)
5472 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005473 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005474 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005475 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005476}
5477
5478static inline u8 btrfs_inode_type(struct inode *inode)
5479{
5480 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5481}
5482
Chris Masond352ac62008-09-29 15:18:18 -04005483/*
5484 * utility function to add 'inode' into 'parent_inode' with
5485 * a give name and a given sequence number.
5486 * if 'add_backref' is true, also insert a backref from the
5487 * inode to the parent directory.
5488 */
Chris Masone02119d2008-09-05 16:13:11 -04005489int btrfs_add_link(struct btrfs_trans_handle *trans,
5490 struct inode *parent_inode, struct inode *inode,
5491 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005492{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005493 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005494 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005495 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005496 u64 ino = btrfs_ino(inode);
5497 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005498
Li Zefan33345d012011-04-20 10:31:50 +08005499 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005500 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5501 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005502 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005503 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5504 key.offset = 0;
5505 }
Chris Mason39279cc2007-06-12 06:35:45 -04005506
Li Zefan33345d012011-04-20 10:31:50 +08005507 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005508 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5509 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005510 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005511 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005512 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5513 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005514 }
5515
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005516 /* Nothing to clean up yet */
5517 if (ret)
5518 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005519
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005520 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5521 parent_inode, &key,
5522 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005523 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005524 goto fail_dir_item;
5525 else if (ret) {
5526 btrfs_abort_transaction(trans, root, ret);
5527 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005528 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005529
5530 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5531 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005532 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005533 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5534 ret = btrfs_update_inode(trans, root, parent_inode);
5535 if (ret)
5536 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005537 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005538
5539fail_dir_item:
5540 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5541 u64 local_index;
5542 int err;
5543 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5544 key.objectid, root->root_key.objectid,
5545 parent_ino, &local_index, name, name_len);
5546
5547 } else if (add_backref) {
5548 u64 local_index;
5549 int err;
5550
5551 err = btrfs_del_inode_ref(trans, root, name, name_len,
5552 ino, parent_ino, &local_index);
5553 }
5554 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005555}
5556
5557static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005558 struct inode *dir, struct dentry *dentry,
5559 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005560{
Josef Bacika1b075d2010-11-19 20:36:11 +00005561 int err = btrfs_add_link(trans, dir, inode,
5562 dentry->d_name.name, dentry->d_name.len,
5563 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005564 if (err > 0)
5565 err = -EEXIST;
5566 return err;
5567}
5568
Josef Bacik618e21d2007-07-11 10:18:17 -04005569static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005570 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005571{
5572 struct btrfs_trans_handle *trans;
5573 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005574 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005575 int err;
5576 int drop_inode = 0;
5577 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005578 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005579
5580 if (!new_valid_dev(rdev))
5581 return -EINVAL;
5582
Josef Bacik9ed74f22009-09-11 16:12:44 -04005583 /*
5584 * 2 for inode item and ref
5585 * 2 for dir items
5586 * 1 for xattr if selinux is on
5587 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005588 trans = btrfs_start_transaction(root, 5);
5589 if (IS_ERR(trans))
5590 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005591
Li Zefan581bb052011-04-20 10:06:11 +08005592 err = btrfs_find_free_ino(root, &objectid);
5593 if (err)
5594 goto out_unlock;
5595
Josef Bacikaec74772008-07-24 12:12:38 -04005596 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005597 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005598 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005599 if (IS_ERR(inode)) {
5600 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005601 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005602 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005603
Eric Paris2a7dba32011-02-01 11:05:39 -05005604 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005605 if (err) {
5606 drop_inode = 1;
5607 goto out_unlock;
5608 }
5609
Casey Schauflerad19db72011-12-15 10:09:07 -05005610 /*
5611 * If the active LSM wants to access the inode during
5612 * d_instantiate it needs these. Smack checks to see
5613 * if the filesystem supports xattrs by looking at the
5614 * ops vector.
5615 */
5616
5617 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005618 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005619 if (err)
5620 drop_inode = 1;
5621 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005622 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005623 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005624 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005625 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005626out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005627 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005628 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005629 if (drop_inode) {
5630 inode_dec_link_count(inode);
5631 iput(inode);
5632 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005633 return err;
5634}
5635
Chris Mason39279cc2007-06-12 06:35:45 -04005636static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005637 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005638{
5639 struct btrfs_trans_handle *trans;
5640 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005641 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005642 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005643 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005644 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005645 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005646
Josef Bacik9ed74f22009-09-11 16:12:44 -04005647 /*
5648 * 2 for inode item and ref
5649 * 2 for dir items
5650 * 1 for xattr if selinux is on
5651 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005652 trans = btrfs_start_transaction(root, 5);
5653 if (IS_ERR(trans))
5654 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005655
Li Zefan581bb052011-04-20 10:06:11 +08005656 err = btrfs_find_free_ino(root, &objectid);
5657 if (err)
5658 goto out_unlock;
5659
Josef Bacikaec74772008-07-24 12:12:38 -04005660 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005661 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005662 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005663 if (IS_ERR(inode)) {
5664 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005665 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005666 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005667 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005668
Eric Paris2a7dba32011-02-01 11:05:39 -05005669 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005670 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005671 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005672
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005673 err = btrfs_update_inode(trans, root, inode);
5674 if (err)
5675 goto out_unlock;
5676
Casey Schauflerad19db72011-12-15 10:09:07 -05005677 /*
5678 * If the active LSM wants to access the inode during
5679 * d_instantiate it needs these. Smack checks to see
5680 * if the filesystem supports xattrs by looking at the
5681 * ops vector.
5682 */
5683 inode->i_fop = &btrfs_file_operations;
5684 inode->i_op = &btrfs_file_inode_operations;
5685
Josef Bacika1b075d2010-11-19 20:36:11 +00005686 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005687 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005688 goto out_unlock;
5689
5690 inode->i_mapping->a_ops = &btrfs_aops;
5691 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5692 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5693 d_instantiate(dentry, inode);
5694
Chris Mason39279cc2007-06-12 06:35:45 -04005695out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005696 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005697 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005698 inode_dec_link_count(inode);
5699 iput(inode);
5700 }
Liu Bob53d3f52012-11-14 14:34:34 +00005701 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005702 return err;
5703}
5704
5705static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5706 struct dentry *dentry)
5707{
5708 struct btrfs_trans_handle *trans;
5709 struct btrfs_root *root = BTRFS_I(dir)->root;
5710 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005711 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005712 int err;
5713 int drop_inode = 0;
5714
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005715 /* do not allow sys_link's with other subvols of the same device */
5716 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005717 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005718
Mark Fashehf1863732012-08-08 11:32:27 -07005719 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005720 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005721
Chris Mason3de45862008-11-17 21:02:50 -05005722 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005723 if (err)
5724 goto fail;
5725
Yan, Zhenga22285a2010-05-16 10:48:46 -04005726 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005727 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005728 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005729 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005730 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005731 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005732 if (IS_ERR(trans)) {
5733 err = PTR_ERR(trans);
5734 goto fail;
5735 }
Chris Mason5f39d392007-10-15 16:14:19 -04005736
Miao Xie31534952011-04-13 13:19:21 +08005737 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005738 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005739 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005740 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005741 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005742
Josef Bacika1b075d2010-11-19 20:36:11 +00005743 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005744
Yan, Zhenga5719522009-09-24 09:17:31 -04005745 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005746 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005747 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005748 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005749 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005750 if (err)
5751 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005752 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005753 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005754 }
Chris Mason39279cc2007-06-12 06:35:45 -04005755
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005756 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005757fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005758 if (drop_inode) {
5759 inode_dec_link_count(inode);
5760 iput(inode);
5761 }
Liu Bob53d3f52012-11-14 14:34:34 +00005762 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005763 return err;
5764}
5765
Al Viro18bb1db2011-07-26 01:41:39 -04005766static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005767{
Chris Masonb9d86662008-05-02 16:13:49 -04005768 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005769 struct btrfs_trans_handle *trans;
5770 struct btrfs_root *root = BTRFS_I(dir)->root;
5771 int err = 0;
5772 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005773 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005774 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005775
Josef Bacik9ed74f22009-09-11 16:12:44 -04005776 /*
5777 * 2 items for inode and ref
5778 * 2 items for dir items
5779 * 1 for xattr if selinux is on
5780 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005781 trans = btrfs_start_transaction(root, 5);
5782 if (IS_ERR(trans))
5783 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005784
Li Zefan581bb052011-04-20 10:06:11 +08005785 err = btrfs_find_free_ino(root, &objectid);
5786 if (err)
5787 goto out_fail;
5788
Josef Bacikaec74772008-07-24 12:12:38 -04005789 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005790 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005791 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005792 if (IS_ERR(inode)) {
5793 err = PTR_ERR(inode);
5794 goto out_fail;
5795 }
Chris Mason5f39d392007-10-15 16:14:19 -04005796
Chris Mason39279cc2007-06-12 06:35:45 -04005797 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005798
Eric Paris2a7dba32011-02-01 11:05:39 -05005799 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005800 if (err)
5801 goto out_fail;
5802
Chris Mason39279cc2007-06-12 06:35:45 -04005803 inode->i_op = &btrfs_dir_inode_operations;
5804 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005805
Chris Masondbe674a2008-07-17 12:54:05 -04005806 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005807 err = btrfs_update_inode(trans, root, inode);
5808 if (err)
5809 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005810
Josef Bacika1b075d2010-11-19 20:36:11 +00005811 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5812 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005813 if (err)
5814 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005815
Chris Mason39279cc2007-06-12 06:35:45 -04005816 d_instantiate(dentry, inode);
5817 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005818
5819out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005820 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005821 if (drop_on_err)
5822 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005823 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005824 return err;
5825}
5826
Chris Masond352ac62008-09-29 15:18:18 -04005827/* helper for btfs_get_extent. Given an existing extent in the tree,
5828 * and an extent that you want to insert, deal with overlap and insert
5829 * the new extent into the tree.
5830 */
Chris Mason3b951512008-04-17 11:29:12 -04005831static int merge_extent_mapping(struct extent_map_tree *em_tree,
5832 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005833 struct extent_map *em,
5834 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005835{
5836 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005837
Chris Masone6dcd2d2008-07-17 12:53:50 -04005838 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5839 start_diff = map_start - em->start;
5840 em->start = map_start;
5841 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005842 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5843 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005844 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005845 em->block_len -= start_diff;
5846 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005847 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005848}
5849
Chris Masonc8b97812008-10-29 14:49:59 -04005850static noinline int uncompress_inline(struct btrfs_path *path,
5851 struct inode *inode, struct page *page,
5852 size_t pg_offset, u64 extent_offset,
5853 struct btrfs_file_extent_item *item)
5854{
5855 int ret;
5856 struct extent_buffer *leaf = path->nodes[0];
5857 char *tmp;
5858 size_t max_size;
5859 unsigned long inline_size;
5860 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005861 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005862
5863 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005864 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005865 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5866 inline_size = btrfs_file_extent_inline_item_len(leaf,
5867 btrfs_item_nr(leaf, path->slots[0]));
5868 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005869 if (!tmp)
5870 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005871 ptr = btrfs_file_extent_inline_start(item);
5872
5873 read_extent_buffer(leaf, tmp, ptr, inline_size);
5874
Chris Mason5b050f02008-11-11 09:34:41 -05005875 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005876 ret = btrfs_decompress(compress_type, tmp, page,
5877 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005878 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005879 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005880 unsigned long copy_size = min_t(u64,
5881 PAGE_CACHE_SIZE - pg_offset,
5882 max_size - extent_offset);
5883 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005884 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005885 }
5886 kfree(tmp);
5887 return 0;
5888}
5889
Chris Masond352ac62008-09-29 15:18:18 -04005890/*
5891 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005892 * the ugly parts come from merging extents from the disk with the in-ram
5893 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005894 * where the in-ram extents might be locked pending data=ordered completion.
5895 *
5896 * This also copies inline extents directly into the page.
5897 */
Chris Masond3977122009-01-05 21:25:51 -05005898
Chris Masona52d9a82007-08-27 16:49:44 -04005899struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005900 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005901 int create)
5902{
5903 int ret;
5904 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005905 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005906 u64 extent_start = 0;
5907 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005908 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005909 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005910 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005911 struct btrfs_root *root = BTRFS_I(inode)->root;
5912 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005913 struct extent_buffer *leaf;
5914 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005915 struct extent_map *em = NULL;
5916 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005917 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005918 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005919 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005920
Chris Masona52d9a82007-08-27 16:49:44 -04005921again:
Chris Mason890871b2009-09-02 16:24:52 -04005922 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005923 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005924 if (em)
5925 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005926 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005927
Chris Masona52d9a82007-08-27 16:49:44 -04005928 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005929 if (em->start > start || em->start + em->len <= start)
5930 free_extent_map(em);
5931 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005932 free_extent_map(em);
5933 else
5934 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005935 }
David Sterba172ddd62011-04-21 00:48:27 +02005936 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005937 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005938 err = -ENOMEM;
5939 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005940 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005941 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005942 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005943 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005944 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005945 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005946
5947 if (!path) {
5948 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005949 if (!path) {
5950 err = -ENOMEM;
5951 goto out;
5952 }
5953 /*
5954 * Chances are we'll be called again, so go ahead and do
5955 * readahead
5956 */
5957 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005958 }
5959
Chris Mason179e29e2007-11-01 11:28:41 -04005960 ret = btrfs_lookup_file_extent(trans, root, path,
5961 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005962 if (ret < 0) {
5963 err = ret;
5964 goto out;
5965 }
5966
5967 if (ret != 0) {
5968 if (path->slots[0] == 0)
5969 goto not_found;
5970 path->slots[0]--;
5971 }
5972
Chris Mason5f39d392007-10-15 16:14:19 -04005973 leaf = path->nodes[0];
5974 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005975 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005976 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005977 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5978 found_type = btrfs_key_type(&found_key);
5979 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005980 found_type != BTRFS_EXTENT_DATA_KEY) {
5981 goto not_found;
5982 }
5983
Chris Mason5f39d392007-10-15 16:14:19 -04005984 found_type = btrfs_file_extent_type(leaf, item);
5985 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005986 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005987 if (found_type == BTRFS_FILE_EXTENT_REG ||
5988 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005989 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005990 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005991 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5992 size_t size;
5993 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00005994 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005995 }
5996
5997 if (start >= extent_end) {
5998 path->slots[0]++;
5999 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6000 ret = btrfs_next_leaf(root, path);
6001 if (ret < 0) {
6002 err = ret;
6003 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006004 }
Yan Zheng9036c102008-10-30 14:19:41 -04006005 if (ret > 0)
6006 goto not_found;
6007 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006008 }
Yan Zheng9036c102008-10-30 14:19:41 -04006009 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6010 if (found_key.objectid != objectid ||
6011 found_key.type != BTRFS_EXTENT_DATA_KEY)
6012 goto not_found;
6013 if (start + len <= found_key.offset)
6014 goto not_found;
6015 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006016 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006017 em->len = found_key.offset - start;
6018 goto not_found_em;
6019 }
6020
Josef Bacikcc95bef2013-04-04 14:31:27 -04006021 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006022 if (found_type == BTRFS_FILE_EXTENT_REG ||
6023 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006024 em->start = extent_start;
6025 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006026 em->orig_start = extent_start -
6027 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006028 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6029 item);
Chris Masondb945352007-10-15 16:15:53 -04006030 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6031 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006032 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006033 goto insert;
6034 }
Li Zefan261507a02010-12-17 14:21:50 +08006035 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006036 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006037 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006038 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006039 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006040 } else {
6041 bytenr += btrfs_file_extent_offset(leaf, item);
6042 em->block_start = bytenr;
6043 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006044 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6045 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006046 }
Chris Masona52d9a82007-08-27 16:49:44 -04006047 goto insert;
6048 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006049 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006050 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006051 size_t size;
6052 size_t extent_offset;
6053 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006054
Chris Masona52d9a82007-08-27 16:49:44 -04006055 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006056 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006057 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006058 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006059 goto out;
6060 }
6061
Yan Zheng9036c102008-10-30 14:19:41 -04006062 size = btrfs_file_extent_inline_len(leaf, item);
6063 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006064 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006065 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006066 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006067 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006068 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006069 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006070 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006071 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006072 em->compress_type = compress_type;
6073 }
Yan689f9342007-10-29 11:41:07 -04006074 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006075 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006076 if (btrfs_file_extent_compression(leaf, item) !=
6077 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006078 ret = uncompress_inline(path, inode, page,
6079 pg_offset,
6080 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006081 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006082 } else {
6083 map = kmap(page);
6084 read_extent_buffer(leaf, map + pg_offset, ptr,
6085 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006086 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6087 memset(map + pg_offset + copy_size, 0,
6088 PAGE_CACHE_SIZE - pg_offset -
6089 copy_size);
6090 }
Chris Masonc8b97812008-10-29 14:49:59 -04006091 kunmap(page);
6092 }
Chris Mason179e29e2007-11-01 11:28:41 -04006093 flush_dcache_page(page);
6094 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006095 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006096 if (!trans) {
6097 kunmap(page);
6098 free_extent_map(em);
6099 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006100
David Sterbab3b4aa72011-04-21 01:20:15 +02006101 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006102 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006103
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006104 if (IS_ERR(trans))
6105 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006106 goto again;
6107 }
Chris Masonc8b97812008-10-29 14:49:59 -04006108 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006109 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006110 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006111 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006112 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006113 }
Chris Masond1310b22008-01-24 16:13:08 -05006114 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006115 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006116 goto insert;
6117 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006118 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006119 }
6120not_found:
6121 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006122 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006123 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006124not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006125 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006126 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006127insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006128 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006129 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006130 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6131 (unsigned long long)em->start,
6132 (unsigned long long)em->len,
6133 (unsigned long long)start,
6134 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006135 err = -EIO;
6136 goto out;
6137 }
Chris Masond1310b22008-01-24 16:13:08 -05006138
6139 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006140 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006141 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006142 /* it is possible that someone inserted the extent into the tree
6143 * while we had the lock dropped. It is also possible that
6144 * an overlapping map exists in the tree
6145 */
Chris Masona52d9a82007-08-27 16:49:44 -04006146 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006147 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006148
6149 ret = 0;
6150
Chris Mason3b951512008-04-17 11:29:12 -04006151 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006152 if (existing && (existing->start > start ||
6153 existing->start + existing->len <= start)) {
6154 free_extent_map(existing);
6155 existing = NULL;
6156 }
Chris Mason3b951512008-04-17 11:29:12 -04006157 if (!existing) {
6158 existing = lookup_extent_mapping(em_tree, em->start,
6159 em->len);
6160 if (existing) {
6161 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006162 em, start,
6163 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006164 free_extent_map(existing);
6165 if (err) {
6166 free_extent_map(em);
6167 em = NULL;
6168 }
6169 } else {
6170 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006171 free_extent_map(em);
6172 em = NULL;
6173 }
6174 } else {
6175 free_extent_map(em);
6176 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006177 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006178 }
Chris Masona52d9a82007-08-27 16:49:44 -04006179 }
Chris Mason890871b2009-09-02 16:24:52 -04006180 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006181out:
liubo1abe9b82011-03-24 11:18:59 +00006182
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006183 if (em)
6184 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006185
Chris Masonf4219502008-07-22 11:18:09 -04006186 if (path)
6187 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006188 if (trans) {
6189 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006190 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006191 err = ret;
6192 }
Chris Masona52d9a82007-08-27 16:49:44 -04006193 if (err) {
6194 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006195 return ERR_PTR(err);
6196 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006197 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006198 return em;
6199}
6200
Chris Masonec29ed52011-02-23 16:23:20 -05006201struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6202 size_t pg_offset, u64 start, u64 len,
6203 int create)
6204{
6205 struct extent_map *em;
6206 struct extent_map *hole_em = NULL;
6207 u64 range_start = start;
6208 u64 end;
6209 u64 found;
6210 u64 found_end;
6211 int err = 0;
6212
6213 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6214 if (IS_ERR(em))
6215 return em;
6216 if (em) {
6217 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006218 * if our em maps to
6219 * - a hole or
6220 * - a pre-alloc extent,
6221 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006222 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006223 if (em->block_start != EXTENT_MAP_HOLE &&
6224 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006225 return em;
6226 else
6227 hole_em = em;
6228 }
6229
6230 /* check to see if we've wrapped (len == -1 or similar) */
6231 end = start + len;
6232 if (end < start)
6233 end = (u64)-1;
6234 else
6235 end -= 1;
6236
6237 em = NULL;
6238
6239 /* ok, we didn't find anything, lets look for delalloc */
6240 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6241 end, len, EXTENT_DELALLOC, 1);
6242 found_end = range_start + found;
6243 if (found_end < range_start)
6244 found_end = (u64)-1;
6245
6246 /*
6247 * we didn't find anything useful, return
6248 * the original results from get_extent()
6249 */
6250 if (range_start > end || found_end <= start) {
6251 em = hole_em;
6252 hole_em = NULL;
6253 goto out;
6254 }
6255
6256 /* adjust the range_start to make sure it doesn't
6257 * go backwards from the start they passed in
6258 */
6259 range_start = max(start,range_start);
6260 found = found_end - range_start;
6261
6262 if (found > 0) {
6263 u64 hole_start = start;
6264 u64 hole_len = len;
6265
David Sterba172ddd62011-04-21 00:48:27 +02006266 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006267 if (!em) {
6268 err = -ENOMEM;
6269 goto out;
6270 }
6271 /*
6272 * when btrfs_get_extent can't find anything it
6273 * returns one huge hole
6274 *
6275 * make sure what it found really fits our range, and
6276 * adjust to make sure it is based on the start from
6277 * the caller
6278 */
6279 if (hole_em) {
6280 u64 calc_end = extent_map_end(hole_em);
6281
6282 if (calc_end <= start || (hole_em->start > end)) {
6283 free_extent_map(hole_em);
6284 hole_em = NULL;
6285 } else {
6286 hole_start = max(hole_em->start, start);
6287 hole_len = calc_end - hole_start;
6288 }
6289 }
6290 em->bdev = NULL;
6291 if (hole_em && range_start > hole_start) {
6292 /* our hole starts before our delalloc, so we
6293 * have to return just the parts of the hole
6294 * that go until the delalloc starts
6295 */
6296 em->len = min(hole_len,
6297 range_start - hole_start);
6298 em->start = hole_start;
6299 em->orig_start = hole_start;
6300 /*
6301 * don't adjust block start at all,
6302 * it is fixed at EXTENT_MAP_HOLE
6303 */
6304 em->block_start = hole_em->block_start;
6305 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006306 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6307 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006308 } else {
6309 em->start = range_start;
6310 em->len = found;
6311 em->orig_start = range_start;
6312 em->block_start = EXTENT_MAP_DELALLOC;
6313 em->block_len = found;
6314 }
6315 } else if (hole_em) {
6316 return hole_em;
6317 }
6318out:
6319
6320 free_extent_map(hole_em);
6321 if (err) {
6322 free_extent_map(em);
6323 return ERR_PTR(err);
6324 }
6325 return em;
6326}
6327
Josef Bacik4b46fce2010-05-23 11:00:55 -04006328static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6329 u64 start, u64 len)
6330{
6331 struct btrfs_root *root = BTRFS_I(inode)->root;
6332 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006333 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006334 struct btrfs_key ins;
6335 u64 alloc_hint;
6336 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006337
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006338 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006339 if (IS_ERR(trans))
6340 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006341
6342 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6343
6344 alloc_hint = get_extent_allocation_hint(inode, start, len);
6345 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006346 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006347 if (ret) {
6348 em = ERR_PTR(ret);
6349 goto out;
6350 }
6351
Josef Bacik70c8a912012-10-11 16:54:30 -04006352 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006353 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik70c8a912012-10-11 16:54:30 -04006354 if (IS_ERR(em))
6355 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006356
6357 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6358 ins.offset, ins.offset, 0);
6359 if (ret) {
6360 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6361 em = ERR_PTR(ret);
6362 }
6363out:
6364 btrfs_end_transaction(trans, root);
6365 return em;
6366}
6367
Chris Mason46bfbb52010-05-26 11:04:10 -04006368/*
6369 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6370 * block must be cow'd
6371 */
Josef Bacik7ee9e442013-06-21 16:37:03 -04006372noinline int can_nocow_extent(struct btrfs_trans_handle *trans,
6373 struct inode *inode, u64 offset, u64 *len,
6374 u64 *orig_start, u64 *orig_block_len,
6375 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006376{
6377 struct btrfs_path *path;
6378 int ret;
6379 struct extent_buffer *leaf;
6380 struct btrfs_root *root = BTRFS_I(inode)->root;
6381 struct btrfs_file_extent_item *fi;
6382 struct btrfs_key key;
6383 u64 disk_bytenr;
6384 u64 backref_offset;
6385 u64 extent_end;
6386 u64 num_bytes;
6387 int slot;
6388 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006389 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Chris Mason46bfbb52010-05-26 11:04:10 -04006390 path = btrfs_alloc_path();
6391 if (!path)
6392 return -ENOMEM;
6393
Li Zefan33345d012011-04-20 10:31:50 +08006394 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006395 offset, 0);
6396 if (ret < 0)
6397 goto out;
6398
6399 slot = path->slots[0];
6400 if (ret == 1) {
6401 if (slot == 0) {
6402 /* can't find the item, must cow */
6403 ret = 0;
6404 goto out;
6405 }
6406 slot--;
6407 }
6408 ret = 0;
6409 leaf = path->nodes[0];
6410 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006411 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006412 key.type != BTRFS_EXTENT_DATA_KEY) {
6413 /* not our file or wrong item type, must cow */
6414 goto out;
6415 }
6416
6417 if (key.offset > offset) {
6418 /* Wrong offset, must cow */
6419 goto out;
6420 }
6421
6422 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6423 found_type = btrfs_file_extent_type(leaf, fi);
6424 if (found_type != BTRFS_FILE_EXTENT_REG &&
6425 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6426 /* not a regular extent, must cow */
6427 goto out;
6428 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006429
6430 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6431 goto out;
6432
Chris Mason46bfbb52010-05-26 11:04:10 -04006433 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006434 if (disk_bytenr == 0)
6435 goto out;
6436
6437 if (btrfs_file_extent_compression(leaf, fi) ||
6438 btrfs_file_extent_encryption(leaf, fi) ||
6439 btrfs_file_extent_other_encoding(leaf, fi))
6440 goto out;
6441
Chris Mason46bfbb52010-05-26 11:04:10 -04006442 backref_offset = btrfs_file_extent_offset(leaf, fi);
6443
Josef Bacik7ee9e442013-06-21 16:37:03 -04006444 if (orig_start) {
6445 *orig_start = key.offset - backref_offset;
6446 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6447 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6448 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006449
Chris Mason46bfbb52010-05-26 11:04:10 -04006450 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason46bfbb52010-05-26 11:04:10 -04006451
6452 if (btrfs_extent_readonly(root, disk_bytenr))
6453 goto out;
6454
6455 /*
6456 * look for other files referencing this extent, if we
6457 * find any we must cow
6458 */
Li Zefan33345d012011-04-20 10:31:50 +08006459 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006460 key.offset - backref_offset, disk_bytenr))
6461 goto out;
6462
6463 /*
6464 * adjust disk_bytenr and num_bytes to cover just the bytes
6465 * in this extent we are about to write. If there
6466 * are any csums in that range we have to cow in order
6467 * to keep the csums correct
6468 */
6469 disk_bytenr += backref_offset;
6470 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006471 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006472 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6473 goto out;
6474 /*
6475 * all of the above have passed, it is safe to overwrite this extent
6476 * without cow
6477 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006478 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006479 ret = 1;
6480out:
6481 btrfs_free_path(path);
6482 return ret;
6483}
6484
Josef Bacikeb838e72012-07-31 16:28:48 -04006485static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6486 struct extent_state **cached_state, int writing)
6487{
6488 struct btrfs_ordered_extent *ordered;
6489 int ret = 0;
6490
6491 while (1) {
6492 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6493 0, cached_state);
6494 /*
6495 * We're concerned with the entire range that we're going to be
6496 * doing DIO to, so we need to make sure theres no ordered
6497 * extents in this range.
6498 */
6499 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6500 lockend - lockstart + 1);
6501
6502 /*
6503 * We need to make sure there are no buffered pages in this
6504 * range either, we could have raced between the invalidate in
6505 * generic_file_direct_write and locking the extent. The
6506 * invalidate needs to happen so that reads after a write do not
6507 * get stale data.
6508 */
6509 if (!ordered && (!writing ||
6510 !test_range_bit(&BTRFS_I(inode)->io_tree,
6511 lockstart, lockend, EXTENT_UPTODATE, 0,
6512 *cached_state)))
6513 break;
6514
6515 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6516 cached_state, GFP_NOFS);
6517
6518 if (ordered) {
6519 btrfs_start_ordered_extent(inode, ordered, 1);
6520 btrfs_put_ordered_extent(ordered);
6521 } else {
6522 /* Screw you mmap */
6523 ret = filemap_write_and_wait_range(inode->i_mapping,
6524 lockstart,
6525 lockend);
6526 if (ret)
6527 break;
6528
6529 /*
6530 * If we found a page that couldn't be invalidated just
6531 * fall back to buffered.
6532 */
6533 ret = invalidate_inode_pages2_range(inode->i_mapping,
6534 lockstart >> PAGE_CACHE_SHIFT,
6535 lockend >> PAGE_CACHE_SHIFT);
6536 if (ret)
6537 break;
6538 }
6539
6540 cond_resched();
6541 }
6542
6543 return ret;
6544}
6545
Josef Bacik69ffb542012-09-11 15:40:07 -04006546static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6547 u64 len, u64 orig_start,
6548 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006549 u64 orig_block_len, u64 ram_bytes,
6550 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006551{
6552 struct extent_map_tree *em_tree;
6553 struct extent_map *em;
6554 struct btrfs_root *root = BTRFS_I(inode)->root;
6555 int ret;
6556
6557 em_tree = &BTRFS_I(inode)->extent_tree;
6558 em = alloc_extent_map();
6559 if (!em)
6560 return ERR_PTR(-ENOMEM);
6561
6562 em->start = start;
6563 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006564 em->mod_start = start;
6565 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006566 em->len = len;
6567 em->block_len = block_len;
6568 em->block_start = block_start;
6569 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006570 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006571 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006572 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006573 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6574 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006575 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006576
6577 do {
6578 btrfs_drop_extent_cache(inode, em->start,
6579 em->start + em->len - 1, 0);
6580 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006581 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006582 write_unlock(&em_tree->lock);
6583 } while (ret == -EEXIST);
6584
6585 if (ret) {
6586 free_extent_map(em);
6587 return ERR_PTR(ret);
6588 }
6589
6590 return em;
6591}
6592
6593
Josef Bacik4b46fce2010-05-23 11:00:55 -04006594static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6595 struct buffer_head *bh_result, int create)
6596{
6597 struct extent_map *em;
6598 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006599 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006600 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006601 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006602 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006603 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006604 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006605 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006606
Miao Xie172a5042013-02-21 02:48:22 -07006607 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006608 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006609 else
Josef Bacikc3298612012-08-03 16:49:19 -04006610 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006611
Josef Bacikc3298612012-08-03 16:49:19 -04006612 lockstart = start;
6613 lockend = start + len - 1;
6614
Josef Bacikeb838e72012-07-31 16:28:48 -04006615 /*
6616 * If this errors out it's because we couldn't invalidate pagecache for
6617 * this range and we need to fallback to buffered.
6618 */
6619 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6620 return -ENOTBLK;
6621
Josef Bacik4b46fce2010-05-23 11:00:55 -04006622 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006623 if (IS_ERR(em)) {
6624 ret = PTR_ERR(em);
6625 goto unlock_err;
6626 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006627
6628 /*
6629 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6630 * io. INLINE is special, and we could probably kludge it in here, but
6631 * it's still buffered so for safety lets just fall back to the generic
6632 * buffered path.
6633 *
6634 * For COMPRESSED we _have_ to read the entire extent in so we can
6635 * decompress it, so there will be buffering required no matter what we
6636 * do, so go ahead and fallback to buffered.
6637 *
6638 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6639 * to buffered IO. Don't blame me, this is the price we pay for using
6640 * the generic code.
6641 */
6642 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6643 em->block_start == EXTENT_MAP_INLINE) {
6644 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006645 ret = -ENOTBLK;
6646 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006647 }
6648
6649 /* Just a good old fashioned hole, return */
6650 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6651 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6652 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006653 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006654 }
6655
6656 /*
6657 * We don't allocate a new extent in the following cases
6658 *
6659 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6660 * existing extent.
6661 * 2) The extent is marked as PREALLOC. We're good to go here and can
6662 * just use the extent.
6663 *
6664 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006665 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006666 len = min(len, em->len - (start - em->start));
6667 lockstart = start + len;
6668 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006669 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006670
6671 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6672 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6673 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006674 int type;
6675 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006676 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006677
6678 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6679 type = BTRFS_ORDERED_PREALLOC;
6680 else
6681 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006682 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006683 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006684
6685 /*
6686 * we're not going to log anything, but we do need
6687 * to make sure the current transaction stays open
6688 * while we look for nocow cross refs
6689 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006690 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006691 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006692 goto must_cow;
6693
Josef Bacik7ee9e442013-06-21 16:37:03 -04006694 if (can_nocow_extent(trans, inode, start, &len, &orig_start,
6695 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006696 if (type == BTRFS_ORDERED_PREALLOC) {
6697 free_extent_map(em);
6698 em = create_pinned_em(inode, start, len,
6699 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006700 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006701 orig_block_len,
6702 ram_bytes, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006703 if (IS_ERR(em)) {
6704 btrfs_end_transaction(trans, root);
6705 goto unlock_err;
6706 }
6707 }
6708
Chris Mason46bfbb52010-05-26 11:04:10 -04006709 ret = btrfs_add_ordered_extent_dio(inode, start,
6710 block_start, len, len, type);
6711 btrfs_end_transaction(trans, root);
6712 if (ret) {
6713 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006714 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006715 }
6716 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006717 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006718 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006719 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006720must_cow:
6721 /*
6722 * this will cow the extent, reset the len in case we changed
6723 * it above
6724 */
6725 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006726 free_extent_map(em);
6727 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006728 if (IS_ERR(em)) {
6729 ret = PTR_ERR(em);
6730 goto unlock_err;
6731 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006732 len = min(len, em->len - (start - em->start));
6733unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006734 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6735 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006736 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006737 bh_result->b_bdev = em->bdev;
6738 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006739 if (create) {
6740 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6741 set_buffer_new(bh_result);
6742
6743 /*
6744 * Need to update the i_size under the extent lock so buffered
6745 * readers will get the updated i_size when we unlock.
6746 */
6747 if (start + len > i_size_read(inode))
6748 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006749
Miao Xie172a5042013-02-21 02:48:22 -07006750 spin_lock(&BTRFS_I(inode)->lock);
6751 BTRFS_I(inode)->outstanding_extents++;
6752 spin_unlock(&BTRFS_I(inode)->lock);
6753
Miao Xie09348562013-02-07 10:12:07 +00006754 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6755 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6756 &cached_state, GFP_NOFS);
6757 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006758 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006759
Josef Bacikeb838e72012-07-31 16:28:48 -04006760 /*
6761 * In the case of write we need to clear and unlock the entire range,
6762 * in the case of read we need to unlock only the end area that we
6763 * aren't using if there is any left over space.
6764 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006765 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006766 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6767 lockend, unlock_bits, 1, 0,
6768 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006769 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006770 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006771 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006772
Josef Bacik4b46fce2010-05-23 11:00:55 -04006773 free_extent_map(em);
6774
6775 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006776
6777unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006778 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6779 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6780 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006781}
6782
Josef Bacik4b46fce2010-05-23 11:00:55 -04006783static void btrfs_endio_direct_read(struct bio *bio, int err)
6784{
Miao Xiee65e1532010-11-22 03:04:43 +00006785 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006786 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6787 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006788 struct inode *inode = dip->inode;
6789 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006790 struct bio *dio_bio;
Miao Xiefacc8a222013-07-25 19:22:34 +08006791 u32 *csums = (u32 *)dip->csum;
6792 int index = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006793 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006794
6795 start = dip->logical_offset;
6796 do {
6797 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6798 struct page *page = bvec->bv_page;
6799 char *kaddr;
6800 u32 csum = ~(u32)0;
6801 unsigned long flags;
6802
6803 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006804 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006805 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006806 csum, bvec->bv_len);
6807 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006808 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006809 local_irq_restore(flags);
6810
6811 flush_dcache_page(bvec->bv_page);
Miao Xiefacc8a222013-07-25 19:22:34 +08006812 if (csum != csums[index]) {
6813 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
6814 (unsigned long long)btrfs_ino(inode),
6815 (unsigned long long)start,
6816 csum, csums[index]);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006817 err = -EIO;
6818 }
6819 }
6820
6821 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006822 bvec++;
Miao Xiefacc8a222013-07-25 19:22:34 +08006823 index++;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006824 } while (bvec <= bvec_end);
6825
6826 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006827 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04006828 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006829
Josef Bacik4b46fce2010-05-23 11:00:55 -04006830 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006831
6832 /* If we had a csum failure make sure to clear the uptodate flag */
6833 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006834 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6835 dio_end_io(dio_bio, err);
6836 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006837}
6838
6839static void btrfs_endio_direct_write(struct bio *bio, int err)
6840{
6841 struct btrfs_dio_private *dip = bio->bi_private;
6842 struct inode *inode = dip->inode;
6843 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006844 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006845 u64 ordered_offset = dip->logical_offset;
6846 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04006847 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006848 int ret;
6849
6850 if (err)
6851 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006852again:
6853 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6854 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006855 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006856 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006857 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006858
Josef Bacik5fd02042012-05-02 14:00:54 -04006859 ordered->work.func = finish_ordered_fn;
6860 ordered->work.flags = 0;
6861 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6862 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006863out_test:
6864 /*
6865 * our bio might span multiple ordered extents. If we haven't
6866 * completed the accounting for the whole dio, go back and try again
6867 */
6868 if (ordered_offset < dip->logical_offset + dip->bytes) {
6869 ordered_bytes = dip->logical_offset + dip->bytes -
6870 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006871 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006872 goto again;
6873 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006874out_done:
Chris Mason9be33952013-05-17 18:30:14 -04006875 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006876
Josef Bacik4b46fce2010-05-23 11:00:55 -04006877 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006878
6879 /* If we had an error make sure to clear the uptodate flag */
6880 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006881 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6882 dio_end_io(dio_bio, err);
6883 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006884}
6885
Chris Masoneaf25d92010-05-25 09:48:28 -04006886static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6887 struct bio *bio, int mirror_num,
6888 unsigned long bio_flags, u64 offset)
6889{
6890 int ret;
6891 struct btrfs_root *root = BTRFS_I(inode)->root;
6892 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006893 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006894 return 0;
6895}
6896
Miao Xiee65e1532010-11-22 03:04:43 +00006897static void btrfs_end_dio_bio(struct bio *bio, int err)
6898{
6899 struct btrfs_dio_private *dip = bio->bi_private;
6900
6901 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006902 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006903 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006904 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006905 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006906 dip->errors = 1;
6907
6908 /*
6909 * before atomic variable goto zero, we must make sure
6910 * dip->errors is perceived to be set.
6911 */
6912 smp_mb__before_atomic_dec();
6913 }
6914
6915 /* if there are more bios still pending for this dio, just exit */
6916 if (!atomic_dec_and_test(&dip->pending_bios))
6917 goto out;
6918
Chris Mason9be33952013-05-17 18:30:14 -04006919 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00006920 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04006921 } else {
6922 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00006923 bio_endio(dip->orig_bio, 0);
6924 }
6925out:
6926 bio_put(bio);
6927}
6928
6929static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6930 u64 first_sector, gfp_t gfp_flags)
6931{
6932 int nr_vecs = bio_get_nr_vecs(bdev);
6933 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6934}
6935
6936static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6937 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006938 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006939{
Miao Xiefacc8a222013-07-25 19:22:34 +08006940 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00006941 int write = rw & REQ_WRITE;
6942 struct btrfs_root *root = BTRFS_I(inode)->root;
6943 int ret;
6944
Josef Bacikb812ce22012-11-16 13:56:32 -05006945 if (async_submit)
6946 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6947
Miao Xiee65e1532010-11-22 03:04:43 +00006948 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006949
6950 if (!write) {
6951 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6952 if (ret)
6953 goto err;
6954 }
Miao Xiee65e1532010-11-22 03:04:43 +00006955
Josef Bacik1ae39932011-04-06 14:41:34 -04006956 if (skip_sum)
6957 goto map;
6958
6959 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006960 ret = btrfs_wq_submit_bio(root->fs_info,
6961 inode, rw, bio, 0, 0,
6962 file_offset,
6963 __btrfs_submit_bio_start_direct_io,
6964 __btrfs_submit_bio_done);
6965 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006966 } else if (write) {
6967 /*
6968 * If we aren't doing async submit, calculate the csum of the
6969 * bio now.
6970 */
6971 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6972 if (ret)
6973 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006974 } else if (!skip_sum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08006975 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
6976 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006977 if (ret)
6978 goto err;
6979 }
Miao Xiee65e1532010-11-22 03:04:43 +00006980
Josef Bacik1ae39932011-04-06 14:41:34 -04006981map:
6982 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006983err:
6984 bio_put(bio);
6985 return ret;
6986}
6987
6988static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6989 int skip_sum)
6990{
6991 struct inode *inode = dip->inode;
6992 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00006993 struct bio *bio;
6994 struct bio *orig_bio = dip->orig_bio;
6995 struct bio_vec *bvec = orig_bio->bi_io_vec;
6996 u64 start_sector = orig_bio->bi_sector;
6997 u64 file_offset = dip->logical_offset;
6998 u64 submit_len = 0;
6999 u64 map_length;
7000 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007001 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007002 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007003
Miao Xiee65e1532010-11-22 03:04:43 +00007004 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007005 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007006 &map_length, NULL, 0);
7007 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007008 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007009 return -EIO;
7010 }
Miao Xiefacc8a222013-07-25 19:22:34 +08007011
Josef Bacik02f57c72011-04-06 14:25:44 -04007012 if (map_length >= orig_bio->bi_size) {
7013 bio = orig_bio;
7014 goto submit;
7015 }
7016
David Woodhouse53b381b2013-01-29 18:40:14 -05007017 /* async crcs make it difficult to collect full stripe writes. */
7018 if (btrfs_get_alloc_profile(root, 1) &
7019 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7020 async_submit = 0;
7021 else
7022 async_submit = 1;
7023
Josef Bacik02f57c72011-04-06 14:25:44 -04007024 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7025 if (!bio)
7026 return -ENOMEM;
7027 bio->bi_private = dip;
7028 bio->bi_end_io = btrfs_end_dio_bio;
7029 atomic_inc(&dip->pending_bios);
7030
Miao Xiee65e1532010-11-22 03:04:43 +00007031 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7032 if (unlikely(map_length < submit_len + bvec->bv_len ||
7033 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7034 bvec->bv_offset) < bvec->bv_len)) {
7035 /*
7036 * inc the count before we submit the bio so
7037 * we know the end IO handler won't happen before
7038 * we inc the count. Otherwise, the dip might get freed
7039 * before we're done setting it up
7040 */
7041 atomic_inc(&dip->pending_bios);
7042 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7043 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007044 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007045 if (ret) {
7046 bio_put(bio);
7047 atomic_dec(&dip->pending_bios);
7048 goto out_err;
7049 }
7050
Miao Xiee65e1532010-11-22 03:04:43 +00007051 start_sector += submit_len >> 9;
7052 file_offset += submit_len;
7053
7054 submit_len = 0;
7055 nr_pages = 0;
7056
7057 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7058 start_sector, GFP_NOFS);
7059 if (!bio)
7060 goto out_err;
7061 bio->bi_private = dip;
7062 bio->bi_end_io = btrfs_end_dio_bio;
7063
7064 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007065 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007066 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007067 &map_length, NULL, 0);
7068 if (ret) {
7069 bio_put(bio);
7070 goto out_err;
7071 }
7072 } else {
7073 submit_len += bvec->bv_len;
7074 nr_pages ++;
7075 bvec++;
7076 }
7077 }
7078
Josef Bacik02f57c72011-04-06 14:25:44 -04007079submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007080 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007081 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007082 if (!ret)
7083 return 0;
7084
7085 bio_put(bio);
7086out_err:
7087 dip->errors = 1;
7088 /*
7089 * before atomic variable goto zero, we must
7090 * make sure dip->errors is perceived to be set.
7091 */
7092 smp_mb__before_atomic_dec();
7093 if (atomic_dec_and_test(&dip->pending_bios))
7094 bio_io_error(dip->orig_bio);
7095
7096 /* bio_end_io() will handle error, so we needn't return it */
7097 return 0;
7098}
7099
Chris Mason9be33952013-05-17 18:30:14 -04007100static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7101 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007102{
7103 struct btrfs_root *root = BTRFS_I(inode)->root;
7104 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007105 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007106 int skip_sum;
Miao Xiefacc8a222013-07-25 19:22:34 +08007107 int sum_len;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007108 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007109 int ret = 0;
Miao Xiefacc8a222013-07-25 19:22:34 +08007110 u16 csum_size;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007111
7112 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7113
Chris Mason9be33952013-05-17 18:30:14 -04007114 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007115 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007116 ret = -ENOMEM;
7117 goto free_ordered;
7118 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007119
Miao Xiefacc8a222013-07-25 19:22:34 +08007120 if (!skip_sum && !write) {
7121 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7122 sum_len = dio_bio->bi_size >> inode->i_sb->s_blocksize_bits;
7123 sum_len *= csum_size;
7124 } else {
7125 sum_len = 0;
7126 }
7127
7128 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007129 if (!dip) {
7130 ret = -ENOMEM;
7131 goto free_io_bio;
7132 }
7133
7134 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007135 dip->inode = inode;
7136 dip->logical_offset = file_offset;
Miao Xiee6da5d2e2013-06-19 18:19:17 +08007137 dip->bytes = dio_bio->bi_size;
Chris Mason9be33952013-05-17 18:30:14 -04007138 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7139 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007140 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007141 dip->orig_bio = io_bio;
7142 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007143 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007144
7145 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007146 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007147 else
Chris Mason9be33952013-05-17 18:30:14 -04007148 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007149
Miao Xiee65e1532010-11-22 03:04:43 +00007150 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7151 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007152 return;
Chris Mason9be33952013-05-17 18:30:14 -04007153
7154free_io_bio:
7155 bio_put(io_bio);
7156
Josef Bacik4b46fce2010-05-23 11:00:55 -04007157free_ordered:
7158 /*
7159 * If this is a write, we need to clean up the reserved space and kill
7160 * the ordered extent.
7161 */
7162 if (write) {
7163 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007164 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007165 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7166 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7167 btrfs_free_reserved_extent(root, ordered->start,
7168 ordered->disk_len);
7169 btrfs_put_ordered_extent(ordered);
7170 btrfs_put_ordered_extent(ordered);
7171 }
Chris Mason9be33952013-05-17 18:30:14 -04007172 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007173}
7174
Chris Mason5a5f79b2010-05-26 21:33:37 -04007175static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7176 const struct iovec *iov, loff_t offset,
7177 unsigned long nr_segs)
7178{
7179 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007180 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007181 size_t size;
7182 unsigned long addr;
7183 unsigned blocksize_mask = root->sectorsize - 1;
7184 ssize_t retval = -EINVAL;
7185 loff_t end = offset;
7186
7187 if (offset & blocksize_mask)
7188 goto out;
7189
7190 /* Check the memory alignment. Blocks cannot straddle pages */
7191 for (seg = 0; seg < nr_segs; seg++) {
7192 addr = (unsigned long)iov[seg].iov_base;
7193 size = iov[seg].iov_len;
7194 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007195 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007196 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007197
7198 /* If this is a write we don't need to check anymore */
7199 if (rw & WRITE)
7200 continue;
7201
7202 /*
7203 * Check to make sure we don't have duplicate iov_base's in this
7204 * iovec, if so return EINVAL, otherwise we'll get csum errors
7205 * when reading back.
7206 */
7207 for (i = seg + 1; i < nr_segs; i++) {
7208 if (iov[seg].iov_base == iov[i].iov_base)
7209 goto out;
7210 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007211 }
7212 retval = 0;
7213out:
7214 return retval;
7215}
Josef Bacikeb838e72012-07-31 16:28:48 -04007216
Chris Mason16432982008-04-10 10:23:21 -04007217static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7218 const struct iovec *iov, loff_t offset,
7219 unsigned long nr_segs)
7220{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007221 struct file *file = iocb->ki_filp;
7222 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007223 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007224 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007225 bool wakeup = true;
7226 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007227 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007228
Chris Mason5a5f79b2010-05-26 21:33:37 -04007229 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007230 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007231 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007232
Miao Xie38851cc2013-02-08 07:04:11 +00007233 atomic_inc(&inode->i_dio_count);
7234 smp_mb__after_atomic_inc();
7235
Josef Bacik0e267c42013-07-02 10:38:02 -04007236 /*
7237 * The generic stuff only does filemap_write_and_wait_range, which isn't
7238 * enough if we've written compressed pages to this area, so we need to
7239 * call btrfs_wait_ordered_range to make absolutely sure that any
7240 * outstanding dirty pages are on disk.
7241 */
7242 count = iov_length(iov, nr_segs);
7243 btrfs_wait_ordered_range(inode, offset, count);
7244
Miao Xie09348562013-02-07 10:12:07 +00007245 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007246 /*
7247 * If the write DIO is beyond the EOF, we need update
7248 * the isize, but it is protected by i_mutex. So we can
7249 * not unlock the i_mutex at this case.
7250 */
7251 if (offset + count <= inode->i_size) {
7252 mutex_unlock(&inode->i_mutex);
7253 relock = true;
7254 }
Miao Xie09348562013-02-07 10:12:07 +00007255 ret = btrfs_delalloc_reserve_space(inode, count);
7256 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007257 goto out;
7258 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7259 &BTRFS_I(inode)->runtime_flags))) {
7260 inode_dio_done(inode);
7261 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7262 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007263 }
7264
7265 ret = __blockdev_direct_IO(rw, iocb, inode,
7266 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7267 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007268 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007269 if (rw & WRITE) {
7270 if (ret < 0 && ret != -EIOCBQUEUED)
7271 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007272 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007273 btrfs_delalloc_release_space(inode,
7274 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007275 else
7276 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007277 }
Miao Xie38851cc2013-02-08 07:04:11 +00007278out:
Miao Xie2e60a512013-02-08 07:01:08 +00007279 if (wakeup)
7280 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007281 if (relock)
7282 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007283
7284 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007285}
7286
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007287#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7288
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007289static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7290 __u64 start, __u64 len)
7291{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007292 int ret;
7293
7294 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7295 if (ret)
7296 return ret;
7297
Chris Masonec29ed52011-02-23 16:23:20 -05007298 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007299}
7300
Chris Mason9ebefb182007-06-15 13:50:00 -04007301int btrfs_readpage(struct file *file, struct page *page)
7302{
Chris Masond1310b22008-01-24 16:13:08 -05007303 struct extent_io_tree *tree;
7304 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007305 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007306}
Chris Mason1832a6d2007-12-21 16:27:21 -05007307
Chris Mason39279cc2007-06-12 06:35:45 -04007308static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7309{
Chris Masond1310b22008-01-24 16:13:08 -05007310 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04007311
7312
7313 if (current->flags & PF_MEMALLOC) {
7314 redirty_page_for_writepage(wbc, page);
7315 unlock_page(page);
7316 return 0;
7317 }
Chris Masond1310b22008-01-24 16:13:08 -05007318 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007319 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7320}
Chris Mason39279cc2007-06-12 06:35:45 -04007321
Eric Sandeen48a3b632013-04-25 20:41:01 +00007322static int btrfs_writepages(struct address_space *mapping,
7323 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04007324{
Chris Masond1310b22008-01-24 16:13:08 -05007325 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007326
Chris Masond1310b22008-01-24 16:13:08 -05007327 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04007328 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7329}
7330
Chris Mason3ab2fb52007-11-08 10:59:22 -05007331static int
7332btrfs_readpages(struct file *file, struct address_space *mapping,
7333 struct list_head *pages, unsigned nr_pages)
7334{
Chris Masond1310b22008-01-24 16:13:08 -05007335 struct extent_io_tree *tree;
7336 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007337 return extent_readpages(tree, mapping, pages, nr_pages,
7338 btrfs_get_extent);
7339}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007340static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007341{
Chris Masond1310b22008-01-24 16:13:08 -05007342 struct extent_io_tree *tree;
7343 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007344 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007345
Chris Masond1310b22008-01-24 16:13:08 -05007346 tree = &BTRFS_I(page->mapping->host)->io_tree;
7347 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007348 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007349 if (ret == 1) {
7350 ClearPagePrivate(page);
7351 set_page_private(page, 0);
7352 page_cache_release(page);
7353 }
7354 return ret;
7355}
Chris Mason39279cc2007-06-12 06:35:45 -04007356
Chris Masone6dcd2d2008-07-17 12:53:50 -04007357static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7358{
Chris Mason98509cf2008-09-11 15:51:43 -04007359 if (PageWriteback(page) || PageDirty(page))
7360 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007361 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007362}
7363
Lukas Czernerd47992f2013-05-21 23:17:23 -04007364static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7365 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007366{
Josef Bacik5fd02042012-05-02 14:00:54 -04007367 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007368 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007369 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007370 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007371 u64 page_start = page_offset(page);
7372 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007373
Chris Mason8b62b722009-09-02 16:53:46 -04007374 /*
7375 * we have the page locked, so new writeback can't start,
7376 * and the dirty bit won't be cleared while we are here.
7377 *
7378 * Wait for IO on this page so that we can safely clear
7379 * the PagePrivate2 bit and do ordered accounting
7380 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007381 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007382
Josef Bacik5fd02042012-05-02 14:00:54 -04007383 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007384 if (offset) {
7385 btrfs_releasepage(page, GFP_NOFS);
7386 return;
7387 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007388 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007389 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007390 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007391 /*
7392 * IO on this page will never be started, so we need
7393 * to account for any ordered extents now
7394 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007395 clear_extent_bit(tree, page_start, page_end,
7396 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007397 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7398 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007399 /*
7400 * whoever cleared the private bit is responsible
7401 * for the finish_ordered_io
7402 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007403 if (TestClearPagePrivate2(page) &&
7404 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7405 PAGE_CACHE_SIZE, 1)) {
7406 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007407 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007408 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007409 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007410 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007411 }
7412 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007413 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007414 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7415 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007416 __btrfs_releasepage(page, GFP_NOFS);
7417
Chris Mason4a096752008-07-21 10:29:44 -04007418 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007419 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007420 ClearPagePrivate(page);
7421 set_page_private(page, 0);
7422 page_cache_release(page);
7423 }
Chris Mason39279cc2007-06-12 06:35:45 -04007424}
7425
Chris Mason9ebefb182007-06-15 13:50:00 -04007426/*
7427 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7428 * called from a page fault handler when a page is first dirtied. Hence we must
7429 * be careful to check for EOF conditions here. We set the page up correctly
7430 * for a written page which means we get ENOSPC checking when writing into
7431 * holes and correct delalloc and unwritten extent mapping on filesystems that
7432 * support these features.
7433 *
7434 * We are not allowed to take the i_mutex here so we have to play games to
7435 * protect against truncate races as the page could now be beyond EOF. Because
7436 * vmtruncate() writes the inode size before removing pages, once we have the
7437 * page lock we can determine safely if the page is beyond EOF. If it is not
7438 * beyond EOF, then the page is guaranteed safe against truncation until we
7439 * unlock the page.
7440 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007441int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007442{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007443 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007444 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007445 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007446 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7447 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007448 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007449 char *kaddr;
7450 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007451 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007452 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007453 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007454 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007455 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007456
Jan Karab2b5ef52012-06-12 16:20:45 +02007457 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007458 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007459 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007460 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007461 reserved = 1;
7462 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007463 if (ret) {
7464 if (ret == -ENOMEM)
7465 ret = VM_FAULT_OOM;
7466 else /* -ENOSPC, -EIO, etc */
7467 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007468 if (reserved)
7469 goto out;
7470 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007471 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007472
Nick Piggin56a76f82009-03-31 15:23:23 -07007473 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007474again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007475 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007476 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007477 page_start = page_offset(page);
7478 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007479
Chris Mason9ebefb182007-06-15 13:50:00 -04007480 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007481 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007482 /* page got truncated out from underneath us */
7483 goto out_unlock;
7484 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007485 wait_on_page_writeback(page);
7486
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007487 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007488 set_page_extent_mapped(page);
7489
Chris Masoneb84ae02008-07-17 13:53:27 -04007490 /*
7491 * we can't set the delalloc bits if there are pending ordered
7492 * extents. Drop our locks and wait for them to finish
7493 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007494 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7495 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007496 unlock_extent_cached(io_tree, page_start, page_end,
7497 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007498 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007499 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007500 btrfs_put_ordered_extent(ordered);
7501 goto again;
7502 }
7503
Josef Bacikfbf19082009-10-01 17:10:23 -04007504 /*
7505 * XXX - page_mkwrite gets called every time the page is dirtied, even
7506 * if it was already dirty, so for space accounting reasons we need to
7507 * clear any delalloc bits for the range we are fixing to save. There
7508 * is probably a better way to do this, but for now keep consistent with
7509 * prepare_pages in the normal write path.
7510 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007511 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007512 EXTENT_DIRTY | EXTENT_DELALLOC |
7513 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007514 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007515
Josef Bacik2ac55d42010-02-03 19:33:23 +00007516 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7517 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007518 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007519 unlock_extent_cached(io_tree, page_start, page_end,
7520 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007521 ret = VM_FAULT_SIGBUS;
7522 goto out_unlock;
7523 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007524 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007525
7526 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007527 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007528 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007529 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007530 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007531
Chris Masone6dcd2d2008-07-17 12:53:50 -04007532 if (zero_start != PAGE_CACHE_SIZE) {
7533 kaddr = kmap(page);
7534 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7535 flush_dcache_page(page);
7536 kunmap(page);
7537 }
Chris Mason247e7432008-07-17 12:53:51 -04007538 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007539 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007540 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007541
Chris Mason257c62e2009-10-13 13:21:08 -04007542 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7543 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007544 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007545
Josef Bacik2ac55d42010-02-03 19:33:23 +00007546 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007547
7548out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007549 if (!ret) {
7550 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007551 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007552 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007553 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007554out:
Josef Bacikec39e182012-01-12 19:10:12 -05007555 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007556out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007557 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007558 return ret;
7559}
7560
Josef Bacika41ad392011-01-31 15:30:16 -05007561static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007562{
7563 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007564 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007565 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007566 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007567 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007568 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007569 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007570
Chris Mason4a096752008-07-21 10:29:44 -04007571 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007572 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
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 Bacikd82a6f1d2011-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",
7836 (unsigned long long)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",
7846 (unsigned long long)ordered->file_offset,
7847 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007848 btrfs_remove_ordered_extent(inode, ordered);
7849 btrfs_put_ordered_extent(ordered);
7850 btrfs_put_ordered_extent(ordered);
7851 }
7852 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007853 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007854 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007855free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007856 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007857}
7858
Al Viro45321ac2010-06-07 13:43:19 -04007859int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007860{
7861 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007862
Naohiro Aota6379ef92013-06-06 09:56:34 +00007863 if (root == NULL)
7864 return 1;
7865
Liu Bofa6ac872013-02-20 14:10:23 +00007866 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007867 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007868 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007869 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007870 else
Al Viro45321ac2010-06-07 13:43:19 -04007871 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007872}
7873
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007874static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007875{
7876 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7877
7878 inode_init_once(&ei->vfs_inode);
7879}
7880
7881void btrfs_destroy_cachep(void)
7882{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007883 /*
7884 * Make sure all delayed rcu free inodes are flushed before we
7885 * destroy cache.
7886 */
7887 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007888 if (btrfs_inode_cachep)
7889 kmem_cache_destroy(btrfs_inode_cachep);
7890 if (btrfs_trans_handle_cachep)
7891 kmem_cache_destroy(btrfs_trans_handle_cachep);
7892 if (btrfs_transaction_cachep)
7893 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007894 if (btrfs_path_cachep)
7895 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007896 if (btrfs_free_space_cachep)
7897 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007898 if (btrfs_delalloc_work_cachep)
7899 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007900}
7901
7902int btrfs_init_cachep(void)
7903{
David Sterba837e1972012-09-07 03:00:48 -06007904 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007905 sizeof(struct btrfs_inode), 0,
7906 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007907 if (!btrfs_inode_cachep)
7908 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007909
David Sterba837e1972012-09-07 03:00:48 -06007910 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007911 sizeof(struct btrfs_trans_handle), 0,
7912 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007913 if (!btrfs_trans_handle_cachep)
7914 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007915
David Sterba837e1972012-09-07 03:00:48 -06007916 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007917 sizeof(struct btrfs_transaction), 0,
7918 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007919 if (!btrfs_transaction_cachep)
7920 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007921
David Sterba837e1972012-09-07 03:00:48 -06007922 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007923 sizeof(struct btrfs_path), 0,
7924 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007925 if (!btrfs_path_cachep)
7926 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007927
David Sterba837e1972012-09-07 03:00:48 -06007928 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007929 sizeof(struct btrfs_free_space), 0,
7930 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7931 if (!btrfs_free_space_cachep)
7932 goto fail;
7933
Miao Xie8ccf6f192012-10-25 09:28:04 +00007934 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7935 sizeof(struct btrfs_delalloc_work), 0,
7936 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7937 NULL);
7938 if (!btrfs_delalloc_work_cachep)
7939 goto fail;
7940
Chris Mason39279cc2007-06-12 06:35:45 -04007941 return 0;
7942fail:
7943 btrfs_destroy_cachep();
7944 return -ENOMEM;
7945}
7946
7947static int btrfs_getattr(struct vfsmount *mnt,
7948 struct dentry *dentry, struct kstat *stat)
7949{
Miao Xiedf0af1a2013-01-29 10:11:59 +00007950 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04007951 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007952 u32 blocksize = inode->i_sb->s_blocksize;
7953
Chris Mason39279cc2007-06-12 06:35:45 -04007954 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007955 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007956 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00007957
7958 spin_lock(&BTRFS_I(inode)->lock);
7959 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
7960 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05007961 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00007962 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007963 return 0;
7964}
7965
Chris Masond3977122009-01-05 21:25:51 -05007966static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7967 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007968{
7969 struct btrfs_trans_handle *trans;
7970 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007971 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007972 struct inode *new_inode = new_dentry->d_inode;
7973 struct inode *old_inode = old_dentry->d_inode;
7974 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007975 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007976 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007977 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007978 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007979
Li Zefan33345d012011-04-20 10:31:50 +08007980 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007981 return -EPERM;
7982
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007983 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007984 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007985 return -EXDEV;
7986
Li Zefan33345d012011-04-20 10:31:50 +08007987 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7988 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007989 return -ENOTEMPTY;
7990
Chris Mason39279cc2007-06-12 06:35:45 -04007991 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007992 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007993 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05007994
7995
7996 /* check for collisions, even if the name isn't there */
7997 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
7998 new_dentry->d_name.name,
7999 new_dentry->d_name.len);
8000
8001 if (ret) {
8002 if (ret == -EEXIST) {
8003 /* we shouldn't get
8004 * eexist without a new_inode */
8005 if (!new_inode) {
8006 WARN_ON(1);
8007 return ret;
8008 }
8009 } else {
8010 /* maybe -EOVERFLOW */
8011 return ret;
8012 }
8013 }
8014 ret = 0;
8015
Chris Mason5a3f23d2009-03-31 13:27:11 -04008016 /*
8017 * we're using rename to replace one file with another.
8018 * and the replacement file is large. Start IO on it now so
8019 * we don't add too much work to the end of the transaction
8020 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008021 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008022 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8023 filemap_flush(old_inode->i_mapping);
8024
Yan, Zheng76dda932009-09-21 16:00:26 -04008025 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008026 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008027 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008028 /*
8029 * We want to reserve the absolute worst case amount of items. So if
8030 * both inodes are subvols and we need to unlink them then that would
8031 * require 4 item modifications, but if they are both normal inodes it
8032 * would require 5 item modifications, so we'll assume their normal
8033 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8034 * should cover the worst case number of items we'll modify.
8035 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008036 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008037 if (IS_ERR(trans)) {
8038 ret = PTR_ERR(trans);
8039 goto out_notrans;
8040 }
Chris Mason5f39d392007-10-15 16:14:19 -04008041
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008042 if (dest != root)
8043 btrfs_record_root_in_trans(trans, dest);
8044
Yan, Zhenga5719522009-09-24 09:17:31 -04008045 ret = btrfs_set_inode_index(new_dir, &index);
8046 if (ret)
8047 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008048
Li Zefan33345d012011-04-20 10:31:50 +08008049 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008050 /* force full log commit if subvolume involved. */
8051 root->fs_info->last_trans_log_full_commit = trans->transid;
8052 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008053 ret = btrfs_insert_inode_ref(trans, dest,
8054 new_dentry->d_name.name,
8055 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008056 old_ino,
8057 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008058 if (ret)
8059 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008060 /*
8061 * this is an ugly little race, but the rename is required
8062 * to make sure that if we crash, the inode is either at the
8063 * old name or the new one. pinning the log transaction lets
8064 * us make sure we don't allow a log commit to come in after
8065 * we unlink the name but before we add the new name back in.
8066 */
8067 btrfs_pin_log_trans(root);
8068 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008069 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008070 * make sure the inode gets flushed if it is replacing
8071 * something.
8072 */
Li Zefan33345d012011-04-20 10:31:50 +08008073 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008074 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008075
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008076 inode_inc_iversion(old_dir);
8077 inode_inc_iversion(new_dir);
8078 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008079 old_dir->i_ctime = old_dir->i_mtime = ctime;
8080 new_dir->i_ctime = new_dir->i_mtime = ctime;
8081 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008082
Chris Mason12fcfd22009-03-24 10:24:20 -04008083 if (old_dentry->d_parent != new_dentry->d_parent)
8084 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8085
Li Zefan33345d012011-04-20 10:31:50 +08008086 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008087 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8088 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8089 old_dentry->d_name.name,
8090 old_dentry->d_name.len);
8091 } else {
Al Viro92986792011-03-04 17:14:37 +00008092 ret = __btrfs_unlink_inode(trans, root, old_dir,
8093 old_dentry->d_inode,
8094 old_dentry->d_name.name,
8095 old_dentry->d_name.len);
8096 if (!ret)
8097 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008098 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008099 if (ret) {
8100 btrfs_abort_transaction(trans, root, ret);
8101 goto out_fail;
8102 }
Chris Mason39279cc2007-06-12 06:35:45 -04008103
8104 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008105 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008106 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008107 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008108 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8109 root_objectid = BTRFS_I(new_inode)->location.objectid;
8110 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8111 root_objectid,
8112 new_dentry->d_name.name,
8113 new_dentry->d_name.len);
8114 BUG_ON(new_inode->i_nlink == 0);
8115 } else {
8116 ret = btrfs_unlink_inode(trans, dest, new_dir,
8117 new_dentry->d_inode,
8118 new_dentry->d_name.name,
8119 new_dentry->d_name.len);
8120 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008121 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008122 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008123 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008124 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008125 if (ret) {
8126 btrfs_abort_transaction(trans, root, ret);
8127 goto out_fail;
8128 }
Chris Mason39279cc2007-06-12 06:35:45 -04008129 }
Josef Bacikaec74772008-07-24 12:12:38 -04008130
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008131 ret = btrfs_add_link(trans, new_dir, old_inode,
8132 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008133 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008134 if (ret) {
8135 btrfs_abort_transaction(trans, root, ret);
8136 goto out_fail;
8137 }
Chris Mason39279cc2007-06-12 06:35:45 -04008138
Li Zefan33345d012011-04-20 10:31:50 +08008139 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008140 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008141 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008142 btrfs_end_log_trans(root);
8143 }
Chris Mason39279cc2007-06-12 06:35:45 -04008144out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008145 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008146out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008147 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008148 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008149
Chris Mason39279cc2007-06-12 06:35:45 -04008150 return ret;
8151}
8152
Miao Xie8ccf6f192012-10-25 09:28:04 +00008153static void btrfs_run_delalloc_work(struct btrfs_work *work)
8154{
8155 struct btrfs_delalloc_work *delalloc_work;
8156
8157 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8158 work);
8159 if (delalloc_work->wait)
8160 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8161 else
8162 filemap_flush(delalloc_work->inode->i_mapping);
8163
8164 if (delalloc_work->delay_iput)
8165 btrfs_add_delayed_iput(delalloc_work->inode);
8166 else
8167 iput(delalloc_work->inode);
8168 complete(&delalloc_work->completion);
8169}
8170
8171struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8172 int wait, int delay_iput)
8173{
8174 struct btrfs_delalloc_work *work;
8175
8176 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8177 if (!work)
8178 return NULL;
8179
8180 init_completion(&work->completion);
8181 INIT_LIST_HEAD(&work->list);
8182 work->inode = inode;
8183 work->wait = wait;
8184 work->delay_iput = delay_iput;
8185 work->work.func = btrfs_run_delalloc_work;
8186
8187 return work;
8188}
8189
8190void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8191{
8192 wait_for_completion(&work->completion);
8193 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8194}
8195
Chris Masond352ac62008-09-29 15:18:18 -04008196/*
8197 * some fairly slow code that needs optimization. This walks the list
8198 * of all the inodes with pending delalloc and forces them to disk.
8199 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00008200static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008201{
Chris Masonea8c2812008-08-04 23:17:27 -04008202 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008203 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008204 struct btrfs_delalloc_work *work, *next;
8205 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008206 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008207 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008208
Miao Xie8ccf6f192012-10-25 09:28:04 +00008209 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008210 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008211
Miao Xieeb73c1b2013-05-15 07:48:22 +00008212 spin_lock(&root->delalloc_lock);
8213 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008214 while (!list_empty(&splice)) {
8215 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008216 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008217
Miao Xieeb73c1b2013-05-15 07:48:22 +00008218 list_move_tail(&binode->delalloc_inodes,
8219 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008220 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008221 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008222 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008223 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008224 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008225 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008226
8227 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8228 if (unlikely(!work)) {
8229 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
8352 name_len = strlen(symname) + 1;
8353 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 Bacikd82a6f1d2011-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);
Chris Masondbe674a2008-07-17 12:54:05 -04008440 btrfs_i_size_write(inode, name_len - 1);
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);
8485 ret = btrfs_reserve_extent(trans, root, cur_bytes,
Zach Brown24542bf2012-11-16 00:04:43 +00008486 min_size, 0, *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,
Chris Mason39279cc2007-06-12 06:35:45 -04008626};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008627static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008628 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008629 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008630 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008631};
Yan, Zheng76dda932009-09-21 16:00:26 -04008632
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008633static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008634 .llseek = generic_file_llseek,
8635 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04008636 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008637 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008638#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008639 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008640#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008641 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008642 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008643};
8644
Chris Masond1310b22008-01-24 16:13:08 -05008645static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008646 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008647 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008648 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008649 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008650 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008651 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008652 .set_bit_hook = btrfs_set_bit_hook,
8653 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008654 .merge_extent_hook = btrfs_merge_extent_hook,
8655 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008656};
8657
Chris Mason35054392009-01-21 13:11:13 -05008658/*
8659 * btrfs doesn't support the bmap operation because swapfiles
8660 * use bmap to make a mapping of extents in the file. They assume
8661 * these extents won't change over the life of the file and they
8662 * use the bmap result to do IO directly to the drive.
8663 *
8664 * the btrfs bmap call would return logical addresses that aren't
8665 * suitable for IO and they also will change frequently as COW
8666 * operations happen. So, swapfile + btrfs == corruption.
8667 *
8668 * For now we're avoiding this by dropping bmap.
8669 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008670static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008671 .readpage = btrfs_readpage,
8672 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04008673 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008674 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008675 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008676 .invalidatepage = btrfs_invalidatepage,
8677 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008678 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008679 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008680};
8681
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008682static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008683 .readpage = btrfs_readpage,
8684 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008685 .invalidatepage = btrfs_invalidatepage,
8686 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008687};
8688
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008689static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008690 .getattr = btrfs_getattr,
8691 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008692 .setxattr = btrfs_setxattr,
8693 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008694 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008695 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008696 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008697 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008698 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008699 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008700};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008701static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008702 .getattr = btrfs_getattr,
8703 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008704 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008705 .setxattr = btrfs_setxattr,
8706 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008707 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008708 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008709 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008710 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008711};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008712static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008713 .readlink = generic_readlink,
8714 .follow_link = page_follow_link_light,
8715 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008716 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008717 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008718 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008719 .setxattr = btrfs_setxattr,
8720 .getxattr = btrfs_getxattr,
8721 .listxattr = btrfs_listxattr,
8722 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008723 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008724 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008725};
Yan, Zheng76dda932009-09-21 16:00:26 -04008726
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008727const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008728 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008729 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008730};