blob: d3280b2b759221a3dd82a21408455ebb3b74779c [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070035#include <linux/aio.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050037#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040038#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040039#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020041#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050042#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000043#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050044#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040045#include <linux/posix_acl_xattr.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050046#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040047#include "ctree.h"
48#include "disk-io.h"
49#include "transaction.h"
50#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040051#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040052#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040053#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040054#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020055#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040056#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050057#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050058#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080059#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000060#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040061#include "hash.h"
Chris Mason39279cc2007-06-12 06:35:45 -040062
63struct btrfs_iget_args {
64 u64 ino;
65 struct btrfs_root *root;
66};
67
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070068static const struct inode_operations btrfs_dir_inode_operations;
69static const struct inode_operations btrfs_symlink_inode_operations;
70static const struct inode_operations btrfs_dir_ro_inode_operations;
71static const struct inode_operations btrfs_special_inode_operations;
72static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070073static const struct address_space_operations btrfs_aops;
74static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070075static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050076static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040077
78static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000079static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040080struct kmem_cache *btrfs_trans_handle_cachep;
81struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040082struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050083struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040084
85#define S_SHIFT 12
86static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
87 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
88 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
89 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
90 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
91 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
92 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
93 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
94};
95
Eric Sandeen3972f262013-01-12 02:57:22 +000096static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050097static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040098static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050099static noinline int cow_file_range(struct inode *inode,
100 struct page *locked_page,
101 u64 start, u64 end, int *page_started,
102 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400103static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
104 u64 len, u64 orig_start,
105 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400106 u64 orig_block_len, u64 ram_bytes,
107 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400108
Eric Sandeen48a3b632013-04-25 20:41:01 +0000109static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400110
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000111static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500112 struct inode *inode, struct inode *dir,
113 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500114{
115 int err;
116
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000117 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500118 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500119 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500120 return err;
121}
122
Chris Masond352ac62008-09-29 15:18:18 -0400123/*
Chris Masonc8b97812008-10-29 14:49:59 -0400124 * this does all the hard work for inserting an inline extent into
125 * the btree. The caller should have done a btrfs_drop_extents so that
126 * no overlapping inline items exist in the btree
127 */
Chris Masond3977122009-01-05 21:25:51 -0500128static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400129 struct btrfs_root *root, struct inode *inode,
130 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000131 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400132 struct page **compressed_pages)
133{
134 struct btrfs_key key;
135 struct btrfs_path *path;
136 struct extent_buffer *leaf;
137 struct page *page = NULL;
138 char *kaddr;
139 unsigned long ptr;
140 struct btrfs_file_extent_item *ei;
141 int err = 0;
142 int ret;
143 size_t cur_size = size;
144 size_t datasize;
145 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400146
Li Zefanfe3f5662011-03-28 08:30:38 +0000147 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400148 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400149
Chris Masond3977122009-01-05 21:25:51 -0500150 path = btrfs_alloc_path();
151 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400152 return -ENOMEM;
153
Chris Masonb9473432009-03-13 11:00:37 -0400154 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400155
Li Zefan33345d012011-04-20 10:31:50 +0800156 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400157 key.offset = start;
158 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400159 datasize = btrfs_file_extent_calc_inline_size(cur_size);
160
161 inode_add_bytes(inode, size);
162 ret = btrfs_insert_empty_item(trans, root, path, &key,
163 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400164 if (ret) {
165 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400166 goto fail;
167 }
168 leaf = path->nodes[0];
169 ei = btrfs_item_ptr(leaf, path->slots[0],
170 struct btrfs_file_extent_item);
171 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
172 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
173 btrfs_set_file_extent_encryption(leaf, ei, 0);
174 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
175 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
176 ptr = btrfs_file_extent_inline_start(ei);
177
Li Zefan261507a02010-12-17 14:21:50 +0800178 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400179 struct page *cpage;
180 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500181 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400182 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500183 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400184 PAGE_CACHE_SIZE);
185
Cong Wang7ac687d2011-11-25 23:14:28 +0800186 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400187 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800188 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400189
190 i++;
191 ptr += cur_size;
192 compressed_size -= cur_size;
193 }
194 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800195 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400196 } else {
197 page = find_get_page(inode->i_mapping,
198 start >> PAGE_CACHE_SHIFT);
199 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800200 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400201 offset = start & (PAGE_CACHE_SIZE - 1);
202 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800203 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400204 page_cache_release(page);
205 }
206 btrfs_mark_buffer_dirty(leaf);
207 btrfs_free_path(path);
208
Yan, Zhengc21677542009-11-12 09:34:21 +0000209 /*
210 * we're an inline extent, so nobody can
211 * extend the file past i_size without locking
212 * a page we already have locked.
213 *
214 * We must do any isize and inode updates
215 * before we unlock the pages. Otherwise we
216 * could end up racing with unlink.
217 */
Chris Masonc8b97812008-10-29 14:49:59 -0400218 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100219 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc21677542009-11-12 09:34:21 +0000220
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100221 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400222fail:
223 btrfs_free_path(path);
224 return err;
225}
226
227
228/*
229 * conditionally insert an inline extent into the file. This
230 * does the checks required to make sure the data is small enough
231 * to fit as an inline extent.
232 */
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 Masonb888db22007-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 Masonb888db22007-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 Masonf03d9302009-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 Bacik7a7eaa402011-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) {
Chris Mason771ed682008-11-06 22:02:51 -0500487 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100488 * inline extent creation worked or returned error,
489 * we don't need to create any more async work items.
490 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500491 */
Chris Masonc8b97812008-10-29 14:49:59 -0400492 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400493 &BTRFS_I(inode)->io_tree,
494 start, end, NULL,
495 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400496 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400497 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc21677542009-11-12 09:34:21 +0000498
499 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400500 goto free_pages_out;
501 }
Yan, Zhengc21677542009-11-12 09:34:21 +0000502 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400503 }
504
505 if (will_compress) {
506 /*
507 * we aren't doing an inline extent round the compressed size
508 * up to a block size boundary so the allocator does sane
509 * things
510 */
Qu Wenruofda28322013-02-26 08:10:22 +0000511 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400512
513 /*
514 * one last check to make sure the compression is really a
515 * win, compare the page count read with the blocks on disk
516 */
Qu Wenruofda28322013-02-26 08:10:22 +0000517 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400518 if (total_compressed >= total_in) {
519 will_compress = 0;
520 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400521 num_bytes = total_in;
522 }
523 }
524 if (!will_compress && pages) {
525 /*
526 * the compression code ran but failed to make things smaller,
527 * free any pages it allocated and our page pointer array
528 */
529 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400530 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400531 page_cache_release(pages[i]);
532 }
533 kfree(pages);
534 pages = NULL;
535 total_compressed = 0;
536 nr_pages_ret = 0;
537
538 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500539 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
540 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500541 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500542 }
Chris Masonc8b97812008-10-29 14:49:59 -0400543 }
Chris Mason771ed682008-11-06 22:02:51 -0500544 if (will_compress) {
545 *num_added += 1;
546
547 /* the async work queues will take care of doing actual
548 * allocation on disk for these compressed pages,
549 * and will submit them to the elevator.
550 */
551 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800552 total_compressed, pages, nr_pages_ret,
553 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500554
Yan, Zheng24ae6362010-12-06 07:02:36 +0000555 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500556 start += num_bytes;
557 pages = NULL;
558 cond_resched();
559 goto again;
560 }
561 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500562cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500563 /*
564 * No compression, but we still need to write the pages in
565 * the file we've been given so far. redirty the locked
566 * page if it corresponds to our extent and set things up
567 * for the async work queue to run cow_file_range to do
568 * the normal delalloc dance
569 */
570 if (page_offset(locked_page) >= start &&
571 page_offset(locked_page) <= end) {
572 __set_page_dirty_nobuffers(locked_page);
573 /* unlocked later on in the async handlers */
574 }
Chris Mason4adaa612013-03-26 13:07:00 -0400575 if (redirty)
576 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800577 add_async_extent(async_cow, start, end - start + 1,
578 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500579 *num_added += 1;
580 }
581
582out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100583 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500584
585free_pages_out:
586 for (i = 0; i < nr_pages_ret; i++) {
587 WARN_ON(pages[i]->mapping);
588 page_cache_release(pages[i]);
589 }
Chris Masond3977122009-01-05 21:25:51 -0500590 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500591
592 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100593
594cleanup_and_out:
595 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
596 start, end, NULL,
597 EXTENT_CLEAR_UNLOCK_PAGE |
598 EXTENT_CLEAR_DIRTY |
599 EXTENT_CLEAR_DELALLOC |
600 EXTENT_SET_WRITEBACK |
601 EXTENT_END_WRITEBACK);
602 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 Bacik7a7eaa402011-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, Zhengc21677542009-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, Zhengc21677542009-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 */
773 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400774 &BTRFS_I(inode)->io_tree,
775 async_extent->start,
776 async_extent->start +
777 async_extent->ram_size - 1,
778 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
779 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400780 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400781 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500782
783 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500784 async_extent->start,
785 async_extent->ram_size,
786 ins.objectid,
787 ins.offset, async_extent->pages,
788 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500789 alloc_hint = ins.objectid + ins.offset;
790 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500791 if (ret)
792 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500793 cond_resched();
794 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100795 ret = 0;
796out:
797 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500798out_free_reserve:
799 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100800out_free:
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500801 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
802 async_extent->start,
803 async_extent->start +
804 async_extent->ram_size - 1,
805 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
806 EXTENT_CLEAR_UNLOCK |
807 EXTENT_CLEAR_DELALLOC |
808 EXTENT_CLEAR_DIRTY |
809 EXTENT_SET_WRITEBACK |
810 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100811 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500812 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500813}
814
Josef Bacik4b46fce2010-05-23 11:00:55 -0400815static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
816 u64 num_bytes)
817{
818 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
819 struct extent_map *em;
820 u64 alloc_hint = 0;
821
822 read_lock(&em_tree->lock);
823 em = search_extent_mapping(em_tree, start, num_bytes);
824 if (em) {
825 /*
826 * if block start isn't an actual block number then find the
827 * first block in this inode and use that as a hint. If that
828 * block is also bogus then just don't worry about it.
829 */
830 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
831 free_extent_map(em);
832 em = search_extent_mapping(em_tree, 0, 0);
833 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
834 alloc_hint = em->block_start;
835 if (em)
836 free_extent_map(em);
837 } else {
838 alloc_hint = em->block_start;
839 free_extent_map(em);
840 }
841 }
842 read_unlock(&em_tree->lock);
843
844 return alloc_hint;
845}
846
Chris Mason771ed682008-11-06 22:02:51 -0500847/*
848 * when extent_io.c finds a delayed allocation range in the file,
849 * the call backs end up in this code. The basic idea is to
850 * allocate extents on disk for the range, and create ordered data structs
851 * in ram to track those extents.
852 *
853 * locked_page is the page that writepage had locked already. We use
854 * it to make sure we don't do extra locks or unlocks.
855 *
856 * *page_started is set to one if we unlock locked_page and do everything
857 * required to start IO on it. It may be clean and already done with
858 * IO when we return.
859 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000860static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
861 struct inode *inode,
862 struct btrfs_root *root,
863 struct page *locked_page,
864 u64 start, u64 end, int *page_started,
865 unsigned long *nr_written,
866 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500867{
Chris Mason771ed682008-11-06 22:02:51 -0500868 u64 alloc_hint = 0;
869 u64 num_bytes;
870 unsigned long ram_size;
871 u64 disk_num_bytes;
872 u64 cur_alloc_size;
873 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500874 struct btrfs_key ins;
875 struct extent_map *em;
876 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
877 int ret = 0;
878
Liu Bo83eea1f2012-07-10 05:28:39 -0600879 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500880
Qu Wenruofda28322013-02-26 08:10:22 +0000881 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500882 num_bytes = max(blocksize, num_bytes);
883 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500884
Chris Mason4cb53002011-05-24 15:35:30 -0400885 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400886 if (num_bytes < 64 * 1024 &&
887 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400888 btrfs_add_inode_defrag(trans, inode);
889
Chris Mason771ed682008-11-06 22:02:51 -0500890 if (start == 0) {
891 /* lets try to make an inline extent */
892 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000893 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500894 if (ret == 0) {
895 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400896 &BTRFS_I(inode)->io_tree,
897 start, end, NULL,
898 EXTENT_CLEAR_UNLOCK_PAGE |
899 EXTENT_CLEAR_UNLOCK |
900 EXTENT_CLEAR_DELALLOC |
901 EXTENT_CLEAR_DIRTY |
902 EXTENT_SET_WRITEBACK |
903 EXTENT_END_WRITEBACK);
Yan, Zhengc21677542009-11-12 09:34:21 +0000904
Chris Mason771ed682008-11-06 22:02:51 -0500905 *nr_written = *nr_written +
906 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
907 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500908 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100909 } else if (ret < 0) {
910 btrfs_abort_transaction(trans, root, ret);
911 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500912 }
913 }
Chris Masonc8b97812008-10-29 14:49:59 -0400914
915 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200916 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400917
Josef Bacik4b46fce2010-05-23 11:00:55 -0400918 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400919 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400920
Chris Masond3977122009-01-05 21:25:51 -0500921 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400922 unsigned long op;
923
Josef Bacik287a0ab2010-03-19 18:07:23 +0000924 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400925 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500926 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500927 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100928 if (ret < 0) {
929 btrfs_abort_transaction(trans, root, ret);
930 goto out_unlock;
931 }
Chris Masond3977122009-01-05 21:25:51 -0500932
David Sterba172ddd62011-04-21 00:48:27 +0200933 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000934 if (!em) {
935 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000936 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000937 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400938 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500939 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500940 ram_size = ins.offset;
941 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400942 em->mod_start = em->start;
943 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400944
Chris Masone6dcd2d2008-07-17 12:53:50 -0400945 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400946 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500947 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400948 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400949 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400950 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400951 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400952
Chris Masond3977122009-01-05 21:25:51 -0500953 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400954 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400955 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400956 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400957 if (ret != -EEXIST) {
958 free_extent_map(em);
959 break;
960 }
961 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400962 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400963 }
Liu Boace68ba2013-04-22 10:53:47 +0000964 if (ret)
965 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400966
Chris Mason98d20f62008-04-14 09:46:10 -0400967 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400968 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500969 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000970 if (ret)
971 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400972
Yan Zheng17d217f2008-12-12 10:03:38 -0500973 if (root->root_key.objectid ==
974 BTRFS_DATA_RELOC_TREE_OBJECTID) {
975 ret = btrfs_reloc_clone_csums(inode, start,
976 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100977 if (ret) {
978 btrfs_abort_transaction(trans, root, ret);
Liu Boace68ba2013-04-22 10:53:47 +0000979 goto out_reserve;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100980 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500981 }
982
Chris Masond3977122009-01-05 21:25:51 -0500983 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400984 break;
Chris Masond3977122009-01-05 21:25:51 -0500985
Chris Masonc8b97812008-10-29 14:49:59 -0400986 /* we're not doing compressed IO, don't unlock the first
987 * page (which the caller expects to stay locked), don't
988 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400989 *
990 * Do set the Private2 bit so we know this page was properly
991 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400992 */
Chris Masona791e352009-10-08 11:27:10 -0400993 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
994 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
995 EXTENT_SET_PRIVATE2;
996
Chris Masonc8b97812008-10-29 14:49:59 -0400997 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
998 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400999 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -04001000 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001001 num_bytes -= cur_alloc_size;
1002 alloc_hint = ins.objectid + ins.offset;
1003 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001004 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001005out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001006 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001007
Liu Boace68ba2013-04-22 10:53:47 +00001008out_reserve:
1009 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001010out_unlock:
1011 extent_clear_unlock_delalloc(inode,
1012 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -04001013 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001014 EXTENT_CLEAR_UNLOCK_PAGE |
1015 EXTENT_CLEAR_UNLOCK |
1016 EXTENT_CLEAR_DELALLOC |
1017 EXTENT_CLEAR_DIRTY |
1018 EXTENT_SET_WRITEBACK |
1019 EXTENT_END_WRITEBACK);
1020
1021 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001022}
Chris Masonc8b97812008-10-29 14:49:59 -04001023
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001024static noinline int cow_file_range(struct inode *inode,
1025 struct page *locked_page,
1026 u64 start, u64 end, int *page_started,
1027 unsigned long *nr_written,
1028 int unlock)
1029{
1030 struct btrfs_trans_handle *trans;
1031 struct btrfs_root *root = BTRFS_I(inode)->root;
1032 int ret;
1033
1034 trans = btrfs_join_transaction(root);
1035 if (IS_ERR(trans)) {
1036 extent_clear_unlock_delalloc(inode,
1037 &BTRFS_I(inode)->io_tree,
1038 start, end, locked_page,
1039 EXTENT_CLEAR_UNLOCK_PAGE |
1040 EXTENT_CLEAR_UNLOCK |
1041 EXTENT_CLEAR_DELALLOC |
1042 EXTENT_CLEAR_DIRTY |
1043 EXTENT_SET_WRITEBACK |
1044 EXTENT_END_WRITEBACK);
1045 return PTR_ERR(trans);
1046 }
1047 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1048
1049 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1050 page_started, nr_written, unlock);
1051
1052 btrfs_end_transaction(trans, root);
1053
1054 return ret;
1055}
1056
Chris Mason771ed682008-11-06 22:02:51 -05001057/*
1058 * work queue call back to started compression on a file and pages
1059 */
1060static noinline void async_cow_start(struct btrfs_work *work)
1061{
1062 struct async_cow *async_cow;
1063 int num_added = 0;
1064 async_cow = container_of(work, struct async_cow, work);
1065
1066 compress_file_range(async_cow->inode, async_cow->locked_page,
1067 async_cow->start, async_cow->end, async_cow,
1068 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001069 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001070 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001071 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001072 }
Chris Mason771ed682008-11-06 22:02:51 -05001073}
1074
1075/*
1076 * work queue call back to submit previously compressed pages
1077 */
1078static noinline void async_cow_submit(struct btrfs_work *work)
1079{
1080 struct async_cow *async_cow;
1081 struct btrfs_root *root;
1082 unsigned long nr_pages;
1083
1084 async_cow = container_of(work, struct async_cow, work);
1085
1086 root = async_cow->root;
1087 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1088 PAGE_CACHE_SHIFT;
1089
Josef Bacik66657b32012-08-01 15:36:24 -04001090 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001091 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001092 waitqueue_active(&root->fs_info->async_submit_wait))
1093 wake_up(&root->fs_info->async_submit_wait);
1094
Chris Masond3977122009-01-05 21:25:51 -05001095 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001096 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001097}
Chris Masonc8b97812008-10-29 14:49:59 -04001098
Chris Mason771ed682008-11-06 22:02:51 -05001099static noinline void async_cow_free(struct btrfs_work *work)
1100{
1101 struct async_cow *async_cow;
1102 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001103 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001104 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001105 kfree(async_cow);
1106}
1107
1108static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1109 u64 start, u64 end, int *page_started,
1110 unsigned long *nr_written)
1111{
1112 struct async_cow *async_cow;
1113 struct btrfs_root *root = BTRFS_I(inode)->root;
1114 unsigned long nr_pages;
1115 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001116 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001117
Chris Masona3429ab2009-10-08 12:30:20 -04001118 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1119 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001120 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001121 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001122 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001123 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001124 async_cow->root = root;
1125 async_cow->locked_page = locked_page;
1126 async_cow->start = start;
1127
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001128 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001129 cur_end = end;
1130 else
1131 cur_end = min(end, start + 512 * 1024 - 1);
1132
1133 async_cow->end = cur_end;
1134 INIT_LIST_HEAD(&async_cow->extents);
1135
1136 async_cow->work.func = async_cow_start;
1137 async_cow->work.ordered_func = async_cow_submit;
1138 async_cow->work.ordered_free = async_cow_free;
1139 async_cow->work.flags = 0;
1140
Chris Mason771ed682008-11-06 22:02:51 -05001141 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1142 PAGE_CACHE_SHIFT;
1143 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1144
1145 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1146 &async_cow->work);
1147
1148 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1149 wait_event(root->fs_info->async_submit_wait,
1150 (atomic_read(&root->fs_info->async_delalloc_pages) <
1151 limit));
1152 }
1153
Chris Masond3977122009-01-05 21:25:51 -05001154 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001155 atomic_read(&root->fs_info->async_delalloc_pages)) {
1156 wait_event(root->fs_info->async_submit_wait,
1157 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1158 0));
1159 }
1160
1161 *nr_written += nr_pages;
1162 start = cur_end + 1;
1163 }
1164 *page_started = 1;
1165 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001166}
1167
Chris Masond3977122009-01-05 21:25:51 -05001168static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001169 u64 bytenr, u64 num_bytes)
1170{
1171 int ret;
1172 struct btrfs_ordered_sum *sums;
1173 LIST_HEAD(list);
1174
Yan Zheng07d400a2009-01-06 11:42:00 -05001175 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001176 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001177 if (ret == 0 && list_empty(&list))
1178 return 0;
1179
1180 while (!list_empty(&list)) {
1181 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1182 list_del(&sums->list);
1183 kfree(sums);
1184 }
1185 return 1;
1186}
1187
Chris Masond352ac62008-09-29 15:18:18 -04001188/*
1189 * when nowcow writeback call back. This checks for snapshots or COW copies
1190 * of the extents that exist in the file, and COWs the file as required.
1191 *
1192 * If no cow copies or snapshots exist, we write directly to the existing
1193 * blocks on disk
1194 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001195static noinline int run_delalloc_nocow(struct inode *inode,
1196 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001197 u64 start, u64 end, int *page_started, int force,
1198 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001199{
Chris Masonbe20aa92007-12-17 20:14:01 -05001200 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001201 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001202 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001203 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001204 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001205 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001206 u64 cow_start;
1207 u64 cur_offset;
1208 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001209 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001210 u64 disk_bytenr;
1211 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001212 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001213 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001214 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001215 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001216 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001217 int nocow;
1218 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001219 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001220 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001221
1222 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001223 if (!path) {
1224 extent_clear_unlock_delalloc(inode,
1225 &BTRFS_I(inode)->io_tree,
1226 start, end, locked_page,
1227 EXTENT_CLEAR_UNLOCK_PAGE |
1228 EXTENT_CLEAR_UNLOCK |
1229 EXTENT_CLEAR_DELALLOC |
1230 EXTENT_CLEAR_DIRTY |
1231 EXTENT_SET_WRITEBACK |
1232 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001233 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001234 }
Li Zefan82d59022011-04-20 10:33:24 +08001235
Liu Bo83eea1f2012-07-10 05:28:39 -06001236 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001237
1238 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001239 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001240 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001241 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001242
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001243 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001244 extent_clear_unlock_delalloc(inode,
1245 &BTRFS_I(inode)->io_tree,
1246 start, end, locked_page,
1247 EXTENT_CLEAR_UNLOCK_PAGE |
1248 EXTENT_CLEAR_UNLOCK |
1249 EXTENT_CLEAR_DELALLOC |
1250 EXTENT_CLEAR_DIRTY |
1251 EXTENT_SET_WRITEBACK |
1252 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001253 btrfs_free_path(path);
1254 return PTR_ERR(trans);
1255 }
1256
Josef Bacik74b21072011-04-13 12:02:53 -04001257 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001258
Yan Zheng80ff3852008-10-30 14:20:02 -04001259 cow_start = (u64)-1;
1260 cur_offset = start;
1261 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001262 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001263 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001264 if (ret < 0) {
1265 btrfs_abort_transaction(trans, root, ret);
1266 goto error;
1267 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001268 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1269 leaf = path->nodes[0];
1270 btrfs_item_key_to_cpu(leaf, &found_key,
1271 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001272 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001273 found_key.type == BTRFS_EXTENT_DATA_KEY)
1274 path->slots[0]--;
1275 }
1276 check_prev = 0;
1277next_slot:
1278 leaf = path->nodes[0];
1279 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1280 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001281 if (ret < 0) {
1282 btrfs_abort_transaction(trans, root, ret);
1283 goto error;
1284 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001285 if (ret > 0)
1286 break;
1287 leaf = path->nodes[0];
1288 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001289
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 nocow = 0;
1291 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001292 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001294
Li Zefan33345d012011-04-20 10:31:50 +08001295 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001296 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1297 found_key.offset > end)
1298 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001299
Yan Zheng80ff3852008-10-30 14:20:02 -04001300 if (found_key.offset > cur_offset) {
1301 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001302 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001303 goto out_check;
1304 }
Chris Masonc31f8832008-01-08 15:46:31 -05001305
Yan Zheng80ff3852008-10-30 14:20:02 -04001306 fi = btrfs_item_ptr(leaf, path->slots[0],
1307 struct btrfs_file_extent_item);
1308 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001309
Josef Bacikcc95bef2013-04-04 14:31:27 -04001310 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001311 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1312 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001313 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001314 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001315 extent_end = found_key.offset +
1316 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001317 disk_num_bytes =
1318 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001319 if (extent_end <= start) {
1320 path->slots[0]++;
1321 goto next_slot;
1322 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001323 if (disk_bytenr == 0)
1324 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 if (btrfs_file_extent_compression(leaf, fi) ||
1326 btrfs_file_extent_encryption(leaf, fi) ||
1327 btrfs_file_extent_other_encoding(leaf, fi))
1328 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001329 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1330 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001331 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001332 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001333 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001334 found_key.offset -
1335 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001336 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001337 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001338 disk_bytenr += cur_offset - found_key.offset;
1339 num_bytes = min(end + 1, extent_end) - cur_offset;
1340 /*
1341 * force cow if csum exists in the range.
1342 * this ensure that csum for a given extent are
1343 * either valid or do not exist.
1344 */
1345 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1346 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 nocow = 1;
1348 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1349 extent_end = found_key.offset +
1350 btrfs_file_extent_inline_len(leaf, fi);
1351 extent_end = ALIGN(extent_end, root->sectorsize);
1352 } else {
1353 BUG_ON(1);
1354 }
1355out_check:
1356 if (extent_end <= start) {
1357 path->slots[0]++;
1358 goto next_slot;
1359 }
1360 if (!nocow) {
1361 if (cow_start == (u64)-1)
1362 cow_start = cur_offset;
1363 cur_offset = extent_end;
1364 if (cur_offset > end)
1365 break;
1366 path->slots[0]++;
1367 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001368 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001369
David Sterbab3b4aa72011-04-21 01:20:15 +02001370 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001371 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001372 ret = __cow_file_range(trans, inode, root, locked_page,
1373 cow_start, found_key.offset - 1,
1374 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001375 if (ret) {
1376 btrfs_abort_transaction(trans, root, ret);
1377 goto error;
1378 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001379 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001380 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001381
Yan Zhengd899e052008-10-30 14:25:28 -04001382 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1383 struct extent_map *em;
1384 struct extent_map_tree *em_tree;
1385 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001386 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001387 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001388 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001389 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001390 em->len = num_bytes;
1391 em->block_len = num_bytes;
1392 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001393 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001394 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001395 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001396 em->mod_start = em->start;
1397 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001398 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001399 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001400 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001401 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001402 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001403 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001404 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001405 if (ret != -EEXIST) {
1406 free_extent_map(em);
1407 break;
1408 }
1409 btrfs_drop_extent_cache(inode, em->start,
1410 em->start + em->len - 1, 0);
1411 }
1412 type = BTRFS_ORDERED_PREALLOC;
1413 } else {
1414 type = BTRFS_ORDERED_NOCOW;
1415 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001416
1417 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001418 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001419 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001420
Yan, Zhengefa56462010-05-16 10:49:59 -04001421 if (root->root_key.objectid ==
1422 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1423 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1424 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001425 if (ret) {
1426 btrfs_abort_transaction(trans, root, ret);
1427 goto error;
1428 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001429 }
1430
Yan Zhengd899e052008-10-30 14:25:28 -04001431 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001432 cur_offset, cur_offset + num_bytes - 1,
1433 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1434 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1435 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001436 cur_offset = extent_end;
1437 if (cur_offset > end)
1438 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001439 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001440 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001441
Josef Bacik17ca04a2012-05-31 15:58:55 -04001442 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001443 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001444 cur_offset = end;
1445 }
1446
Yan Zheng80ff3852008-10-30 14:20:02 -04001447 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001448 ret = __cow_file_range(trans, inode, root, locked_page,
1449 cow_start, end,
1450 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001451 if (ret) {
1452 btrfs_abort_transaction(trans, root, ret);
1453 goto error;
1454 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001455 }
1456
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001457error:
Miao Xiea698d0752012-09-20 01:51:59 -06001458 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001459 if (!ret)
1460 ret = err;
1461
Josef Bacik17ca04a2012-05-31 15:58:55 -04001462 if (ret && cur_offset < end)
1463 extent_clear_unlock_delalloc(inode,
1464 &BTRFS_I(inode)->io_tree,
1465 cur_offset, end, locked_page,
1466 EXTENT_CLEAR_UNLOCK_PAGE |
1467 EXTENT_CLEAR_UNLOCK |
1468 EXTENT_CLEAR_DELALLOC |
1469 EXTENT_CLEAR_DIRTY |
1470 EXTENT_SET_WRITEBACK |
1471 EXTENT_END_WRITEBACK);
1472
Yan Zheng7ea394f2008-08-05 13:05:02 -04001473 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001474 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001475}
1476
Chris Masond352ac62008-09-29 15:18:18 -04001477/*
1478 * extent_io.c call back to do delayed allocation processing
1479 */
Chris Masonc8b97812008-10-29 14:49:59 -04001480static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001481 u64 start, u64 end, int *page_started,
1482 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001483{
Chris Masonbe20aa92007-12-17 20:14:01 -05001484 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001485 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001486
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001487 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001488 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001489 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001490 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001491 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001492 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001493 } else if (!btrfs_test_opt(root, COMPRESS) &&
1494 !(BTRFS_I(inode)->force_compress) &&
1495 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001496 ret = cow_file_range(inode, locked_page, start, end,
1497 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001498 } else {
1499 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1500 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001501 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001502 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001503 }
Chris Masonb888db22007-08-27 16:49:44 -04001504 return ret;
1505}
1506
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001507static void btrfs_split_extent_hook(struct inode *inode,
1508 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001509{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001510 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001511 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001512 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001513
Josef Bacik9e0baf62011-07-15 15:16:44 +00001514 spin_lock(&BTRFS_I(inode)->lock);
1515 BTRFS_I(inode)->outstanding_extents++;
1516 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001517}
1518
1519/*
1520 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1521 * extents so we can keep track of new extents that are just merged onto old
1522 * extents, such as when we are doing sequential writes, so we can properly
1523 * account for the metadata space we'll need.
1524 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001525static void btrfs_merge_extent_hook(struct inode *inode,
1526 struct extent_state *new,
1527 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001528{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001529 /* not delalloc, ignore it */
1530 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001531 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001532
Josef Bacik9e0baf62011-07-15 15:16:44 +00001533 spin_lock(&BTRFS_I(inode)->lock);
1534 BTRFS_I(inode)->outstanding_extents--;
1535 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001536}
1537
Miao Xieeb73c1b2013-05-15 07:48:22 +00001538static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1539 struct inode *inode)
1540{
1541 spin_lock(&root->delalloc_lock);
1542 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1543 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1544 &root->delalloc_inodes);
1545 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1546 &BTRFS_I(inode)->runtime_flags);
1547 root->nr_delalloc_inodes++;
1548 if (root->nr_delalloc_inodes == 1) {
1549 spin_lock(&root->fs_info->delalloc_root_lock);
1550 BUG_ON(!list_empty(&root->delalloc_root));
1551 list_add_tail(&root->delalloc_root,
1552 &root->fs_info->delalloc_roots);
1553 spin_unlock(&root->fs_info->delalloc_root_lock);
1554 }
1555 }
1556 spin_unlock(&root->delalloc_lock);
1557}
1558
1559static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1560 struct inode *inode)
1561{
1562 spin_lock(&root->delalloc_lock);
1563 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1564 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1565 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1566 &BTRFS_I(inode)->runtime_flags);
1567 root->nr_delalloc_inodes--;
1568 if (!root->nr_delalloc_inodes) {
1569 spin_lock(&root->fs_info->delalloc_root_lock);
1570 BUG_ON(list_empty(&root->delalloc_root));
1571 list_del_init(&root->delalloc_root);
1572 spin_unlock(&root->fs_info->delalloc_root_lock);
1573 }
1574 }
1575 spin_unlock(&root->delalloc_lock);
1576}
1577
Chris Masond352ac62008-09-29 15:18:18 -04001578/*
1579 * extent_io.c set_bit_hook, used to track delayed allocation
1580 * bytes in this file, and to maintain the list of inodes that
1581 * have pending delalloc work to be done.
1582 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001583static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001584 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001585{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001586
Chris Mason75eff682008-12-15 15:54:40 -05001587 /*
1588 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001589 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001590 * bit, which is only set or cleared with irqs on
1591 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001592 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001593 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001594 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001595 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001596
Josef Bacik9e0baf62011-07-15 15:16:44 +00001597 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001598 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001599 } else {
1600 spin_lock(&BTRFS_I(inode)->lock);
1601 BTRFS_I(inode)->outstanding_extents++;
1602 spin_unlock(&BTRFS_I(inode)->lock);
1603 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001604
Miao Xie963d6782013-01-29 10:10:51 +00001605 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1606 root->fs_info->delalloc_batch);
Miao Xiedf0af1a52013-01-29 10:11:59 +00001607 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001608 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a52013-01-29 10:11:59 +00001609 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001610 &BTRFS_I(inode)->runtime_flags))
1611 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a52013-01-29 10:11:59 +00001612 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001613 }
Chris Mason291d6732008-01-29 15:55:23 -05001614}
1615
Chris Masond352ac62008-09-29 15:18:18 -04001616/*
1617 * extent_io.c clear_bit_hook, see set_bit_hook for why
1618 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001619static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001620 struct extent_state *state,
1621 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001622{
Chris Mason75eff682008-12-15 15:54:40 -05001623 /*
1624 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001625 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001626 * bit, which is only set or cleared with irqs on
1627 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001628 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001629 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001630 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001631 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001632
Josef Bacik9e0baf62011-07-15 15:16:44 +00001633 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001634 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001635 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1636 spin_lock(&BTRFS_I(inode)->lock);
1637 BTRFS_I(inode)->outstanding_extents--;
1638 spin_unlock(&BTRFS_I(inode)->lock);
1639 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001640
1641 if (*bits & EXTENT_DO_ACCOUNTING)
1642 btrfs_delalloc_release_metadata(inode, len);
1643
Josef Bacik0cb59c92010-07-02 12:14:14 -04001644 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001645 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001646 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001647
Miao Xie963d6782013-01-29 10:10:51 +00001648 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1649 root->fs_info->delalloc_batch);
Miao Xiedf0af1a52013-01-29 10:11:59 +00001650 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001651 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001652 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a52013-01-29 10:11:59 +00001653 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001654 &BTRFS_I(inode)->runtime_flags))
1655 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a52013-01-29 10:11:59 +00001656 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001657 }
Chris Mason291d6732008-01-29 15:55:23 -05001658}
1659
Chris Masond352ac62008-09-29 15:18:18 -04001660/*
1661 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1662 * we don't create bios that span stripes or chunks
1663 */
David Woodhouse64a16702009-07-15 23:29:37 +01001664int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001665 size_t size, struct bio *bio,
1666 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001667{
1668 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001669 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001670 u64 length = 0;
1671 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001672 int ret;
1673
Chris Mason771ed682008-11-06 22:02:51 -05001674 if (bio_flags & EXTENT_BIO_COMPRESSED)
1675 return 0;
1676
Chris Masonf2d8d742008-04-21 10:03:05 -04001677 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001678 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001679 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001680 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001681 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001682 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001683 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001684 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001685 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001686}
1687
Chris Masond352ac62008-09-29 15:18:18 -04001688/*
1689 * in order to insert checksums into the metadata in large chunks,
1690 * we wait until bio submission time. All the pages in the bio are
1691 * checksummed and sums are attached onto the ordered extent record.
1692 *
1693 * At IO completion time the cums attached on the ordered extent record
1694 * are inserted into the btree
1695 */
Chris Masond3977122009-01-05 21:25:51 -05001696static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1697 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001698 unsigned long bio_flags,
1699 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001700{
Chris Mason065631f2008-02-20 12:07:25 -05001701 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001702 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001703
Chris Masond20f7042008-12-08 16:58:54 -05001704 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001705 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001706 return 0;
1707}
Chris Masone0156402008-04-16 11:15:20 -04001708
Chris Mason4a69a412008-11-06 22:03:00 -05001709/*
1710 * in order to insert checksums into the metadata in large chunks,
1711 * we wait until bio submission time. All the pages in the bio are
1712 * checksummed and sums are attached onto the ordered extent record.
1713 *
1714 * At IO completion time the cums attached on the ordered extent record
1715 * are inserted into the btree
1716 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001717static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001718 int mirror_num, unsigned long bio_flags,
1719 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001720{
1721 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001722 int ret;
1723
1724 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1725 if (ret)
1726 bio_endio(bio, ret);
1727 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001728}
1729
Chris Masond352ac62008-09-29 15:18:18 -04001730/*
Chris Masoncad321a2008-12-17 14:51:42 -05001731 * extent_io.c submission hook. This does the right thing for csum calculation
1732 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001733 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001734static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001735 int mirror_num, unsigned long bio_flags,
1736 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001737{
1738 struct btrfs_root *root = BTRFS_I(inode)->root;
1739 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001740 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001741 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001742 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001743
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001744 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001745
Liu Bo83eea1f2012-07-10 05:28:39 -06001746 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001747 metadata = 2;
1748
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001749 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001750 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1751 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001752 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001753
Chris Masond20f7042008-12-08 16:58:54 -05001754 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001755 ret = btrfs_submit_compressed_read(inode, bio,
1756 mirror_num,
1757 bio_flags);
1758 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001759 } else if (!skip_sum) {
1760 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1761 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001762 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001763 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001764 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001765 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001766 /* csum items have already been cloned */
1767 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1768 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001769 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001770 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001771 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001772 bio_flags, bio_offset,
1773 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001774 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001775 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001776 } else if (!skip_sum) {
1777 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1778 if (ret)
1779 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001780 }
1781
Chris Mason0b86a832008-03-24 15:01:56 -04001782mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001783 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1784
1785out:
1786 if (ret < 0)
1787 bio_endio(bio, ret);
1788 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001789}
Chris Mason6885f302008-02-20 16:11:05 -05001790
Chris Masond352ac62008-09-29 15:18:18 -04001791/*
1792 * given a list of ordered sums record them in the inode. This happens
1793 * at IO completion time based on sums calculated at bio submission time.
1794 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001795static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001796 struct inode *inode, u64 file_offset,
1797 struct list_head *list)
1798{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001799 struct btrfs_ordered_sum *sum;
1800
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001801 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001802 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001803 btrfs_csum_file_blocks(trans,
1804 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001805 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001806 }
1807 return 0;
1808}
1809
Josef Bacik2ac55d42010-02-03 19:33:23 +00001810int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1811 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001812{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001813 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001814 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001815 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001816}
1817
Chris Masond352ac62008-09-29 15:18:18 -04001818/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001819struct btrfs_writepage_fixup {
1820 struct page *page;
1821 struct btrfs_work work;
1822};
1823
Christoph Hellwigb2950862008-12-02 09:54:17 -05001824static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001825{
1826 struct btrfs_writepage_fixup *fixup;
1827 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001828 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001829 struct page *page;
1830 struct inode *inode;
1831 u64 page_start;
1832 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001833 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001834
1835 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1836 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001837again:
Chris Mason247e7432008-07-17 12:53:51 -04001838 lock_page(page);
1839 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1840 ClearPageChecked(page);
1841 goto out_page;
1842 }
1843
1844 inode = page->mapping->host;
1845 page_start = page_offset(page);
1846 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1847
Josef Bacik2ac55d42010-02-03 19:33:23 +00001848 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001849 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001850
1851 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001852 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001853 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001854
1855 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1856 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001857 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1858 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001859 unlock_page(page);
1860 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001861 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001862 goto again;
1863 }
Chris Mason247e7432008-07-17 12:53:51 -04001864
Jeff Mahoney87826df2012-02-15 16:23:57 +01001865 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1866 if (ret) {
1867 mapping_set_error(page->mapping, ret);
1868 end_extent_writepage(page, ret, page_start, page_end);
1869 ClearPageChecked(page);
1870 goto out;
1871 }
1872
Josef Bacik2ac55d42010-02-03 19:33:23 +00001873 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001874 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001875 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001876out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001877 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1878 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001879out_page:
1880 unlock_page(page);
1881 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001882 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001883}
1884
1885/*
1886 * There are a few paths in the higher layers of the kernel that directly
1887 * set the page dirty bit without asking the filesystem if it is a
1888 * good idea. This causes problems because we want to make sure COW
1889 * properly happens and the data=ordered rules are followed.
1890 *
Chris Masonc8b97812008-10-29 14:49:59 -04001891 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001892 * hasn't been properly setup for IO. We kick off an async process
1893 * to fix it up. The async helper will wait for ordered extents, set
1894 * the delalloc bit and make it safe to write the page.
1895 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001896static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001897{
1898 struct inode *inode = page->mapping->host;
1899 struct btrfs_writepage_fixup *fixup;
1900 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001901
Chris Mason8b62b722009-09-02 16:53:46 -04001902 /* this page is properly in the ordered list */
1903 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001904 return 0;
1905
1906 if (PageChecked(page))
1907 return -EAGAIN;
1908
1909 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1910 if (!fixup)
1911 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001912
Chris Mason247e7432008-07-17 12:53:51 -04001913 SetPageChecked(page);
1914 page_cache_get(page);
1915 fixup->work.func = btrfs_writepage_fixup_worker;
1916 fixup->page = page;
1917 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001918 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001919}
1920
Yan Zhengd899e052008-10-30 14:25:28 -04001921static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1922 struct inode *inode, u64 file_pos,
1923 u64 disk_bytenr, u64 disk_num_bytes,
1924 u64 num_bytes, u64 ram_bytes,
1925 u8 compression, u8 encryption,
1926 u16 other_encoding, int extent_type)
1927{
1928 struct btrfs_root *root = BTRFS_I(inode)->root;
1929 struct btrfs_file_extent_item *fi;
1930 struct btrfs_path *path;
1931 struct extent_buffer *leaf;
1932 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001933 int ret;
1934
1935 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001936 if (!path)
1937 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001938
Chris Masonb9473432009-03-13 11:00:37 -04001939 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001940
1941 /*
1942 * we may be replacing one extent in the tree with another.
1943 * The new extent is pinned in the extent map, and we don't want
1944 * to drop it from the cache until it is completely in the btree.
1945 *
1946 * So, tell btrfs_drop_extents to leave this extent in the cache.
1947 * the caller is expected to unpin it and allow it to be merged
1948 * with the others.
1949 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001950 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001951 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001952 if (ret)
1953 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001954
Li Zefan33345d012011-04-20 10:31:50 +08001955 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001956 ins.offset = file_pos;
1957 ins.type = BTRFS_EXTENT_DATA_KEY;
1958 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001959 if (ret)
1960 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001961 leaf = path->nodes[0];
1962 fi = btrfs_item_ptr(leaf, path->slots[0],
1963 struct btrfs_file_extent_item);
1964 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1965 btrfs_set_file_extent_type(leaf, fi, extent_type);
1966 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1967 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1968 btrfs_set_file_extent_offset(leaf, fi, 0);
1969 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1970 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1971 btrfs_set_file_extent_compression(leaf, fi, compression);
1972 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1973 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001974
Yan Zhengd899e052008-10-30 14:25:28 -04001975 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001976 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001977
1978 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001979
1980 ins.objectid = disk_bytenr;
1981 ins.offset = disk_num_bytes;
1982 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001983 ret = btrfs_alloc_reserved_file_extent(trans, root,
1984 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001985 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001986out:
Yan Zhengd899e052008-10-30 14:25:28 -04001987 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001988
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001989 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001990}
1991
Liu Bo38c227d2013-01-29 03:18:40 +00001992/* snapshot-aware defrag */
1993struct sa_defrag_extent_backref {
1994 struct rb_node node;
1995 struct old_sa_defrag_extent *old;
1996 u64 root_id;
1997 u64 inum;
1998 u64 file_pos;
1999 u64 extent_offset;
2000 u64 num_bytes;
2001 u64 generation;
2002};
2003
2004struct old_sa_defrag_extent {
2005 struct list_head list;
2006 struct new_sa_defrag_extent *new;
2007
2008 u64 extent_offset;
2009 u64 bytenr;
2010 u64 offset;
2011 u64 len;
2012 int count;
2013};
2014
2015struct new_sa_defrag_extent {
2016 struct rb_root root;
2017 struct list_head head;
2018 struct btrfs_path *path;
2019 struct inode *inode;
2020 u64 file_pos;
2021 u64 len;
2022 u64 bytenr;
2023 u64 disk_len;
2024 u8 compress_type;
2025};
2026
2027static int backref_comp(struct sa_defrag_extent_backref *b1,
2028 struct sa_defrag_extent_backref *b2)
2029{
2030 if (b1->root_id < b2->root_id)
2031 return -1;
2032 else if (b1->root_id > b2->root_id)
2033 return 1;
2034
2035 if (b1->inum < b2->inum)
2036 return -1;
2037 else if (b1->inum > b2->inum)
2038 return 1;
2039
2040 if (b1->file_pos < b2->file_pos)
2041 return -1;
2042 else if (b1->file_pos > b2->file_pos)
2043 return 1;
2044
2045 /*
2046 * [------------------------------] ===> (a range of space)
2047 * |<--->| |<---->| =============> (fs/file tree A)
2048 * |<---------------------------->| ===> (fs/file tree B)
2049 *
2050 * A range of space can refer to two file extents in one tree while
2051 * refer to only one file extent in another tree.
2052 *
2053 * So we may process a disk offset more than one time(two extents in A)
2054 * and locate at the same extent(one extent in B), then insert two same
2055 * backrefs(both refer to the extent in B).
2056 */
2057 return 0;
2058}
2059
2060static void backref_insert(struct rb_root *root,
2061 struct sa_defrag_extent_backref *backref)
2062{
2063 struct rb_node **p = &root->rb_node;
2064 struct rb_node *parent = NULL;
2065 struct sa_defrag_extent_backref *entry;
2066 int ret;
2067
2068 while (*p) {
2069 parent = *p;
2070 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2071
2072 ret = backref_comp(backref, entry);
2073 if (ret < 0)
2074 p = &(*p)->rb_left;
2075 else
2076 p = &(*p)->rb_right;
2077 }
2078
2079 rb_link_node(&backref->node, parent, p);
2080 rb_insert_color(&backref->node, root);
2081}
2082
2083/*
2084 * Note the backref might has changed, and in this case we just return 0.
2085 */
2086static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2087 void *ctx)
2088{
2089 struct btrfs_file_extent_item *extent;
2090 struct btrfs_fs_info *fs_info;
2091 struct old_sa_defrag_extent *old = ctx;
2092 struct new_sa_defrag_extent *new = old->new;
2093 struct btrfs_path *path = new->path;
2094 struct btrfs_key key;
2095 struct btrfs_root *root;
2096 struct sa_defrag_extent_backref *backref;
2097 struct extent_buffer *leaf;
2098 struct inode *inode = new->inode;
2099 int slot;
2100 int ret;
2101 u64 extent_offset;
2102 u64 num_bytes;
2103
2104 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2105 inum == btrfs_ino(inode))
2106 return 0;
2107
2108 key.objectid = root_id;
2109 key.type = BTRFS_ROOT_ITEM_KEY;
2110 key.offset = (u64)-1;
2111
2112 fs_info = BTRFS_I(inode)->root->fs_info;
2113 root = btrfs_read_fs_root_no_name(fs_info, &key);
2114 if (IS_ERR(root)) {
2115 if (PTR_ERR(root) == -ENOENT)
2116 return 0;
2117 WARN_ON(1);
2118 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2119 inum, offset, root_id);
2120 return PTR_ERR(root);
2121 }
2122
2123 key.objectid = inum;
2124 key.type = BTRFS_EXTENT_DATA_KEY;
2125 if (offset > (u64)-1 << 32)
2126 key.offset = 0;
2127 else
2128 key.offset = offset;
2129
2130 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2131 if (ret < 0) {
2132 WARN_ON(1);
2133 return ret;
2134 }
Josef Bacik50f13192013-07-22 12:50:37 -04002135 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002136
2137 while (1) {
2138 cond_resched();
2139
2140 leaf = path->nodes[0];
2141 slot = path->slots[0];
2142
2143 if (slot >= btrfs_header_nritems(leaf)) {
2144 ret = btrfs_next_leaf(root, path);
2145 if (ret < 0) {
2146 goto out;
2147 } else if (ret > 0) {
2148 ret = 0;
2149 goto out;
2150 }
2151 continue;
2152 }
2153
2154 path->slots[0]++;
2155
2156 btrfs_item_key_to_cpu(leaf, &key, slot);
2157
2158 if (key.objectid > inum)
2159 goto out;
2160
2161 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2162 continue;
2163
2164 extent = btrfs_item_ptr(leaf, slot,
2165 struct btrfs_file_extent_item);
2166
2167 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2168 continue;
2169
Liu Boe68afa42013-07-01 22:13:26 +08002170 /*
2171 * 'offset' refers to the exact key.offset,
2172 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2173 * (key.offset - extent_offset).
2174 */
2175 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002176 continue;
2177
Liu Boe68afa42013-07-01 22:13:26 +08002178 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002179 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002180
Liu Bo38c227d2013-01-29 03:18:40 +00002181 if (extent_offset >= old->extent_offset + old->offset +
2182 old->len || extent_offset + num_bytes <=
2183 old->extent_offset + old->offset)
2184 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002185 break;
2186 }
2187
2188 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2189 if (!backref) {
2190 ret = -ENOENT;
2191 goto out;
2192 }
2193
2194 backref->root_id = root_id;
2195 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002196 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002197 backref->num_bytes = num_bytes;
2198 backref->extent_offset = extent_offset;
2199 backref->generation = btrfs_file_extent_generation(leaf, extent);
2200 backref->old = old;
2201 backref_insert(&new->root, backref);
2202 old->count++;
2203out:
2204 btrfs_release_path(path);
2205 WARN_ON(ret);
2206 return ret;
2207}
2208
2209static noinline bool record_extent_backrefs(struct btrfs_path *path,
2210 struct new_sa_defrag_extent *new)
2211{
2212 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2213 struct old_sa_defrag_extent *old, *tmp;
2214 int ret;
2215
2216 new->path = path;
2217
2218 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002219 ret = iterate_inodes_from_logical(old->bytenr +
2220 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002221 path, record_one_backref,
2222 old);
2223 BUG_ON(ret < 0 && ret != -ENOENT);
2224
2225 /* no backref to be processed for this extent */
2226 if (!old->count) {
2227 list_del(&old->list);
2228 kfree(old);
2229 }
2230 }
2231
2232 if (list_empty(&new->head))
2233 return false;
2234
2235 return true;
2236}
2237
2238static int relink_is_mergable(struct extent_buffer *leaf,
2239 struct btrfs_file_extent_item *fi,
2240 u64 disk_bytenr)
2241{
2242 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2243 return 0;
2244
2245 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2246 return 0;
2247
2248 if (btrfs_file_extent_compression(leaf, fi) ||
2249 btrfs_file_extent_encryption(leaf, fi) ||
2250 btrfs_file_extent_other_encoding(leaf, fi))
2251 return 0;
2252
2253 return 1;
2254}
2255
2256/*
2257 * Note the backref might has changed, and in this case we just return 0.
2258 */
2259static noinline int relink_extent_backref(struct btrfs_path *path,
2260 struct sa_defrag_extent_backref *prev,
2261 struct sa_defrag_extent_backref *backref)
2262{
2263 struct btrfs_file_extent_item *extent;
2264 struct btrfs_file_extent_item *item;
2265 struct btrfs_ordered_extent *ordered;
2266 struct btrfs_trans_handle *trans;
2267 struct btrfs_fs_info *fs_info;
2268 struct btrfs_root *root;
2269 struct btrfs_key key;
2270 struct extent_buffer *leaf;
2271 struct old_sa_defrag_extent *old = backref->old;
2272 struct new_sa_defrag_extent *new = old->new;
2273 struct inode *src_inode = new->inode;
2274 struct inode *inode;
2275 struct extent_state *cached = NULL;
2276 int ret = 0;
2277 u64 start;
2278 u64 len;
2279 u64 lock_start;
2280 u64 lock_end;
2281 bool merge = false;
2282 int index;
2283
2284 if (prev && prev->root_id == backref->root_id &&
2285 prev->inum == backref->inum &&
2286 prev->file_pos + prev->num_bytes == backref->file_pos)
2287 merge = true;
2288
2289 /* step 1: get root */
2290 key.objectid = backref->root_id;
2291 key.type = BTRFS_ROOT_ITEM_KEY;
2292 key.offset = (u64)-1;
2293
2294 fs_info = BTRFS_I(src_inode)->root->fs_info;
2295 index = srcu_read_lock(&fs_info->subvol_srcu);
2296
2297 root = btrfs_read_fs_root_no_name(fs_info, &key);
2298 if (IS_ERR(root)) {
2299 srcu_read_unlock(&fs_info->subvol_srcu, index);
2300 if (PTR_ERR(root) == -ENOENT)
2301 return 0;
2302 return PTR_ERR(root);
2303 }
Liu Bo38c227d2013-01-29 03:18:40 +00002304
2305 /* step 2: get inode */
2306 key.objectid = backref->inum;
2307 key.type = BTRFS_INODE_ITEM_KEY;
2308 key.offset = 0;
2309
2310 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2311 if (IS_ERR(inode)) {
2312 srcu_read_unlock(&fs_info->subvol_srcu, index);
2313 return 0;
2314 }
2315
2316 srcu_read_unlock(&fs_info->subvol_srcu, index);
2317
2318 /* step 3: relink backref */
2319 lock_start = backref->file_pos;
2320 lock_end = backref->file_pos + backref->num_bytes - 1;
2321 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2322 0, &cached);
2323
2324 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2325 if (ordered) {
2326 btrfs_put_ordered_extent(ordered);
2327 goto out_unlock;
2328 }
2329
2330 trans = btrfs_join_transaction(root);
2331 if (IS_ERR(trans)) {
2332 ret = PTR_ERR(trans);
2333 goto out_unlock;
2334 }
2335
2336 key.objectid = backref->inum;
2337 key.type = BTRFS_EXTENT_DATA_KEY;
2338 key.offset = backref->file_pos;
2339
2340 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2341 if (ret < 0) {
2342 goto out_free_path;
2343 } else if (ret > 0) {
2344 ret = 0;
2345 goto out_free_path;
2346 }
2347
2348 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2349 struct btrfs_file_extent_item);
2350
2351 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2352 backref->generation)
2353 goto out_free_path;
2354
2355 btrfs_release_path(path);
2356
2357 start = backref->file_pos;
2358 if (backref->extent_offset < old->extent_offset + old->offset)
2359 start += old->extent_offset + old->offset -
2360 backref->extent_offset;
2361
2362 len = min(backref->extent_offset + backref->num_bytes,
2363 old->extent_offset + old->offset + old->len);
2364 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2365
2366 ret = btrfs_drop_extents(trans, root, inode, start,
2367 start + len, 1);
2368 if (ret)
2369 goto out_free_path;
2370again:
2371 key.objectid = btrfs_ino(inode);
2372 key.type = BTRFS_EXTENT_DATA_KEY;
2373 key.offset = start;
2374
Liu Boa09a0a72013-03-11 09:20:58 +00002375 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002376 if (merge) {
2377 struct btrfs_file_extent_item *fi;
2378 u64 extent_len;
2379 struct btrfs_key found_key;
2380
2381 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2382 if (ret < 0)
2383 goto out_free_path;
2384
2385 path->slots[0]--;
2386 leaf = path->nodes[0];
2387 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2388
2389 fi = btrfs_item_ptr(leaf, path->slots[0],
2390 struct btrfs_file_extent_item);
2391 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2392
2393 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2394 extent_len + found_key.offset == start) {
2395 btrfs_set_file_extent_num_bytes(leaf, fi,
2396 extent_len + len);
2397 btrfs_mark_buffer_dirty(leaf);
2398 inode_add_bytes(inode, len);
2399
2400 ret = 1;
2401 goto out_free_path;
2402 } else {
2403 merge = false;
2404 btrfs_release_path(path);
2405 goto again;
2406 }
2407 }
2408
2409 ret = btrfs_insert_empty_item(trans, root, path, &key,
2410 sizeof(*extent));
2411 if (ret) {
2412 btrfs_abort_transaction(trans, root, ret);
2413 goto out_free_path;
2414 }
2415
2416 leaf = path->nodes[0];
2417 item = btrfs_item_ptr(leaf, path->slots[0],
2418 struct btrfs_file_extent_item);
2419 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2420 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2421 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2422 btrfs_set_file_extent_num_bytes(leaf, item, len);
2423 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2424 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2425 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2426 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2427 btrfs_set_file_extent_encryption(leaf, item, 0);
2428 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2429
2430 btrfs_mark_buffer_dirty(leaf);
2431 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002432 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002433
2434 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2435 new->disk_len, 0,
2436 backref->root_id, backref->inum,
2437 new->file_pos, 0); /* start - extent_offset */
2438 if (ret) {
2439 btrfs_abort_transaction(trans, root, ret);
2440 goto out_free_path;
2441 }
2442
2443 ret = 1;
2444out_free_path:
2445 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002446 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002447 btrfs_end_transaction(trans, root);
2448out_unlock:
2449 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2450 &cached, GFP_NOFS);
2451 iput(inode);
2452 return ret;
2453}
2454
2455static void relink_file_extents(struct new_sa_defrag_extent *new)
2456{
2457 struct btrfs_path *path;
2458 struct old_sa_defrag_extent *old, *tmp;
2459 struct sa_defrag_extent_backref *backref;
2460 struct sa_defrag_extent_backref *prev = NULL;
2461 struct inode *inode;
2462 struct btrfs_root *root;
2463 struct rb_node *node;
2464 int ret;
2465
2466 inode = new->inode;
2467 root = BTRFS_I(inode)->root;
2468
2469 path = btrfs_alloc_path();
2470 if (!path)
2471 return;
2472
2473 if (!record_extent_backrefs(path, new)) {
2474 btrfs_free_path(path);
2475 goto out;
2476 }
2477 btrfs_release_path(path);
2478
2479 while (1) {
2480 node = rb_first(&new->root);
2481 if (!node)
2482 break;
2483 rb_erase(node, &new->root);
2484
2485 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2486
2487 ret = relink_extent_backref(path, prev, backref);
2488 WARN_ON(ret < 0);
2489
2490 kfree(prev);
2491
2492 if (ret == 1)
2493 prev = backref;
2494 else
2495 prev = NULL;
2496 cond_resched();
2497 }
2498 kfree(prev);
2499
2500 btrfs_free_path(path);
2501
2502 list_for_each_entry_safe(old, tmp, &new->head, list) {
2503 list_del(&old->list);
2504 kfree(old);
2505 }
2506out:
2507 atomic_dec(&root->fs_info->defrag_running);
2508 wake_up(&root->fs_info->transaction_wait);
2509
2510 kfree(new);
2511}
2512
2513static struct new_sa_defrag_extent *
2514record_old_file_extents(struct inode *inode,
2515 struct btrfs_ordered_extent *ordered)
2516{
2517 struct btrfs_root *root = BTRFS_I(inode)->root;
2518 struct btrfs_path *path;
2519 struct btrfs_key key;
2520 struct old_sa_defrag_extent *old, *tmp;
2521 struct new_sa_defrag_extent *new;
2522 int ret;
2523
2524 new = kmalloc(sizeof(*new), GFP_NOFS);
2525 if (!new)
2526 return NULL;
2527
2528 new->inode = inode;
2529 new->file_pos = ordered->file_offset;
2530 new->len = ordered->len;
2531 new->bytenr = ordered->start;
2532 new->disk_len = ordered->disk_len;
2533 new->compress_type = ordered->compress_type;
2534 new->root = RB_ROOT;
2535 INIT_LIST_HEAD(&new->head);
2536
2537 path = btrfs_alloc_path();
2538 if (!path)
2539 goto out_kfree;
2540
2541 key.objectid = btrfs_ino(inode);
2542 key.type = BTRFS_EXTENT_DATA_KEY;
2543 key.offset = new->file_pos;
2544
2545 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2546 if (ret < 0)
2547 goto out_free_path;
2548 if (ret > 0 && path->slots[0] > 0)
2549 path->slots[0]--;
2550
2551 /* find out all the old extents for the file range */
2552 while (1) {
2553 struct btrfs_file_extent_item *extent;
2554 struct extent_buffer *l;
2555 int slot;
2556 u64 num_bytes;
2557 u64 offset;
2558 u64 end;
2559 u64 disk_bytenr;
2560 u64 extent_offset;
2561
2562 l = path->nodes[0];
2563 slot = path->slots[0];
2564
2565 if (slot >= btrfs_header_nritems(l)) {
2566 ret = btrfs_next_leaf(root, path);
2567 if (ret < 0)
2568 goto out_free_list;
2569 else if (ret > 0)
2570 break;
2571 continue;
2572 }
2573
2574 btrfs_item_key_to_cpu(l, &key, slot);
2575
2576 if (key.objectid != btrfs_ino(inode))
2577 break;
2578 if (key.type != BTRFS_EXTENT_DATA_KEY)
2579 break;
2580 if (key.offset >= new->file_pos + new->len)
2581 break;
2582
2583 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2584
2585 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2586 if (key.offset + num_bytes < new->file_pos)
2587 goto next;
2588
2589 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2590 if (!disk_bytenr)
2591 goto next;
2592
2593 extent_offset = btrfs_file_extent_offset(l, extent);
2594
2595 old = kmalloc(sizeof(*old), GFP_NOFS);
2596 if (!old)
2597 goto out_free_list;
2598
2599 offset = max(new->file_pos, key.offset);
2600 end = min(new->file_pos + new->len, key.offset + num_bytes);
2601
2602 old->bytenr = disk_bytenr;
2603 old->extent_offset = extent_offset;
2604 old->offset = offset - key.offset;
2605 old->len = end - offset;
2606 old->new = new;
2607 old->count = 0;
2608 list_add_tail(&old->list, &new->head);
2609next:
2610 path->slots[0]++;
2611 cond_resched();
2612 }
2613
2614 btrfs_free_path(path);
2615 atomic_inc(&root->fs_info->defrag_running);
2616
2617 return new;
2618
2619out_free_list:
2620 list_for_each_entry_safe(old, tmp, &new->head, list) {
2621 list_del(&old->list);
2622 kfree(old);
2623 }
2624out_free_path:
2625 btrfs_free_path(path);
2626out_kfree:
2627 kfree(new);
2628 return NULL;
2629}
2630
Chris Mason5d13a982009-03-13 11:41:46 -04002631/*
2632 * helper function for btrfs_finish_ordered_io, this
2633 * just reads in some of the csum leaves to prime them into ram
2634 * before we start the transaction. It limits the amount of btree
2635 * reads required while inside the transaction.
2636 */
Chris Masond352ac62008-09-29 15:18:18 -04002637/* as ordered data IO finishes, this gets called so we can finish
2638 * an ordered extent if the range of bytes in the file it covers are
2639 * fully written.
2640 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002641static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002642{
Josef Bacik5fd02042012-05-02 14:00:54 -04002643 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002644 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002645 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002646 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002647 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002648 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002649 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002650 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002651 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002652
Liu Bo83eea1f2012-07-10 05:28:39 -06002653 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002654
Josef Bacik5fd02042012-05-02 14:00:54 -04002655 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2656 ret = -EIO;
2657 goto out;
2658 }
2659
Yan, Zhengc21677542009-11-12 09:34:21 +00002660 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002661 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002662 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2663 if (nolock)
2664 trans = btrfs_join_transaction_nolock(root);
2665 else
2666 trans = btrfs_join_transaction(root);
2667 if (IS_ERR(trans)) {
2668 ret = PTR_ERR(trans);
2669 trans = NULL;
2670 goto out;
Yan, Zhengc21677542009-11-12 09:34:21 +00002671 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002672 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2673 ret = btrfs_update_inode_fallback(trans, root, inode);
2674 if (ret) /* -ENOMEM or corruption */
2675 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc21677542009-11-12 09:34:21 +00002676 goto out;
2677 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002678
Josef Bacik2ac55d42010-02-03 19:33:23 +00002679 lock_extent_bits(io_tree, ordered_extent->file_offset,
2680 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002681 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002682
Liu Bo38c227d2013-01-29 03:18:40 +00002683 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2684 ordered_extent->file_offset + ordered_extent->len - 1,
2685 EXTENT_DEFRAG, 1, cached_state);
2686 if (ret) {
2687 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2688 if (last_snapshot >= BTRFS_I(inode)->generation)
2689 /* the inode is shared */
2690 new = record_old_file_extents(inode, ordered_extent);
2691
2692 clear_extent_bit(io_tree, ordered_extent->file_offset,
2693 ordered_extent->file_offset + ordered_extent->len - 1,
2694 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2695 }
2696
Josef Bacik0cb59c92010-07-02 12:14:14 -04002697 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04002698 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002699 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04002700 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002701 if (IS_ERR(trans)) {
2702 ret = PTR_ERR(trans);
2703 trans = NULL;
2704 goto out_unlock;
2705 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002706 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc21677542009-11-12 09:34:21 +00002707
Chris Masonc8b97812008-10-29 14:49:59 -04002708 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002709 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002710 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002711 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002712 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002713 ordered_extent->file_offset,
2714 ordered_extent->file_offset +
2715 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002716 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002717 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002718 ret = insert_reserved_file_extent(trans, inode,
2719 ordered_extent->file_offset,
2720 ordered_extent->start,
2721 ordered_extent->disk_len,
2722 ordered_extent->len,
2723 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002724 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002725 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002726 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002727 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2728 ordered_extent->file_offset, ordered_extent->len,
2729 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002730 if (ret < 0) {
2731 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002732 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002733 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002734
Chris Masone6dcd2d2008-07-17 12:53:50 -04002735 add_pending_csums(trans, inode, ordered_extent->file_offset,
2736 &ordered_extent->list);
2737
Josef Bacik6c760c02012-11-09 10:53:21 -05002738 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2739 ret = btrfs_update_inode_fallback(trans, root, inode);
2740 if (ret) { /* -ENOMEM or corruption */
2741 btrfs_abort_transaction(trans, root, ret);
2742 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002743 }
2744 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002745out_unlock:
2746 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2747 ordered_extent->file_offset +
2748 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc21677542009-11-12 09:34:21 +00002749out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002750 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002751 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002752 if (trans)
2753 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002754
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002755 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002756 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2757 ordered_extent->file_offset +
2758 ordered_extent->len - 1, NULL, GFP_NOFS);
2759
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002760 /*
2761 * If the ordered extent had an IOERR or something else went
2762 * wrong we need to return the space for this ordered extent
2763 * back to the allocator.
2764 */
2765 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2766 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2767 btrfs_free_reserved_extent(root, ordered_extent->start,
2768 ordered_extent->disk_len);
2769 }
2770
2771
Josef Bacik5fd02042012-05-02 14:00:54 -04002772 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002773 * This needs to be done to make sure anybody waiting knows we are done
2774 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002775 */
2776 btrfs_remove_ordered_extent(inode, ordered_extent);
2777
Liu Bo38c227d2013-01-29 03:18:40 +00002778 /* for snapshot-aware defrag */
2779 if (new)
2780 relink_file_extents(new);
2781
Chris Masone6dcd2d2008-07-17 12:53:50 -04002782 /* once for us */
2783 btrfs_put_ordered_extent(ordered_extent);
2784 /* once for the tree */
2785 btrfs_put_ordered_extent(ordered_extent);
2786
Josef Bacik5fd02042012-05-02 14:00:54 -04002787 return ret;
2788}
2789
2790static void finish_ordered_fn(struct btrfs_work *work)
2791{
2792 struct btrfs_ordered_extent *ordered_extent;
2793 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2794 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002795}
2796
Christoph Hellwigb2950862008-12-02 09:54:17 -05002797static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002798 struct extent_state *state, int uptodate)
2799{
Josef Bacik5fd02042012-05-02 14:00:54 -04002800 struct inode *inode = page->mapping->host;
2801 struct btrfs_root *root = BTRFS_I(inode)->root;
2802 struct btrfs_ordered_extent *ordered_extent = NULL;
2803 struct btrfs_workers *workers;
2804
liubo1abe9b82011-03-24 11:18:59 +00002805 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2806
Chris Mason8b62b722009-09-02 16:53:46 -04002807 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002808 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2809 end - start + 1, uptodate))
2810 return 0;
2811
2812 ordered_extent->work.func = finish_ordered_fn;
2813 ordered_extent->work.flags = 0;
2814
Liu Bo83eea1f2012-07-10 05:28:39 -06002815 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002816 workers = &root->fs_info->endio_freespace_worker;
2817 else
2818 workers = &root->fs_info->endio_write_workers;
2819 btrfs_queue_worker(workers, &ordered_extent->work);
2820
2821 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002822}
2823
Chris Masond352ac62008-09-29 15:18:18 -04002824/*
Chris Masond352ac62008-09-29 15:18:18 -04002825 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002826 * if there's a match, we allow the bio to finish. If not, the code in
2827 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002828 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002829static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002830 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002831{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002832 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002833 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002834 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002835 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002836 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002837 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002838 struct btrfs_root *root = BTRFS_I(inode)->root;
2839 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002840 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2841 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002842
Chris Masond20f7042008-12-08 16:58:54 -05002843 if (PageChecked(page)) {
2844 ClearPageChecked(page);
2845 goto good;
2846 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002847
2848 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002849 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002850
Yan Zheng17d217f2008-12-12 10:03:38 -05002851 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002852 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002853 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2854 GFP_NOFS);
2855 return 0;
2856 }
2857
Yanc2e639f2008-02-04 08:57:25 -05002858 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002859 private = state->private;
2860 ret = 0;
2861 } else {
2862 ret = get_state_private(io_tree, start, &private);
2863 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002864 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002865 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002866 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002867
Liu Bob0496682013-03-14 14:57:45 +00002868 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002869 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002870 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002871 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002872
Cong Wang7ac687d2011-11-25 23:14:28 +08002873 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002874good:
Chris Mason07157aa2007-08-30 08:50:51 -04002875 return 0;
2876
2877zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002878 if (__ratelimit(&_rs))
2879 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u private %llu",
2880 (unsigned long long)btrfs_ino(page->mapping->host),
2881 (unsigned long long)start, csum,
2882 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002883 memset(kaddr + offset, 1, end - start + 1);
2884 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002885 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002886 if (private == 0)
2887 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002888 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002889}
Chris Masonb888db22007-08-27 16:49:44 -04002890
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002891struct delayed_iput {
2892 struct list_head list;
2893 struct inode *inode;
2894};
2895
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002896/* JDM: If this is fs-wide, why can't we add a pointer to
2897 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002898void btrfs_add_delayed_iput(struct inode *inode)
2899{
2900 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2901 struct delayed_iput *delayed;
2902
2903 if (atomic_add_unless(&inode->i_count, -1, 1))
2904 return;
2905
2906 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2907 delayed->inode = inode;
2908
2909 spin_lock(&fs_info->delayed_iput_lock);
2910 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2911 spin_unlock(&fs_info->delayed_iput_lock);
2912}
2913
2914void btrfs_run_delayed_iputs(struct btrfs_root *root)
2915{
2916 LIST_HEAD(list);
2917 struct btrfs_fs_info *fs_info = root->fs_info;
2918 struct delayed_iput *delayed;
2919 int empty;
2920
2921 spin_lock(&fs_info->delayed_iput_lock);
2922 empty = list_empty(&fs_info->delayed_iputs);
2923 spin_unlock(&fs_info->delayed_iput_lock);
2924 if (empty)
2925 return;
2926
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002927 spin_lock(&fs_info->delayed_iput_lock);
2928 list_splice_init(&fs_info->delayed_iputs, &list);
2929 spin_unlock(&fs_info->delayed_iput_lock);
2930
2931 while (!list_empty(&list)) {
2932 delayed = list_entry(list.next, struct delayed_iput, list);
2933 list_del(&delayed->list);
2934 iput(delayed->inode);
2935 kfree(delayed);
2936 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002937}
2938
Yan, Zhengd68fc572010-05-16 10:49:58 -04002939/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002940 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002941 * files in the subvolume, it removes orphan item and frees block_rsv
2942 * structure.
2943 */
2944void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2945 struct btrfs_root *root)
2946{
Josef Bacik90290e12011-12-02 15:44:12 -05002947 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002948 int ret;
2949
Josef Bacik8a35d952012-05-23 14:26:42 -04002950 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002951 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2952 return;
2953
Josef Bacik90290e12011-12-02 15:44:12 -05002954 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002955 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002956 spin_unlock(&root->orphan_lock);
2957 return;
2958 }
2959
2960 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2961 spin_unlock(&root->orphan_lock);
2962 return;
2963 }
2964
2965 block_rsv = root->orphan_block_rsv;
2966 root->orphan_block_rsv = NULL;
2967 spin_unlock(&root->orphan_lock);
2968
Yan, Zhengd68fc572010-05-16 10:49:58 -04002969 if (root->orphan_item_inserted &&
2970 btrfs_root_refs(&root->root_item) > 0) {
2971 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2972 root->root_key.objectid);
2973 BUG_ON(ret);
2974 root->orphan_item_inserted = 0;
2975 }
2976
Josef Bacik90290e12011-12-02 15:44:12 -05002977 if (block_rsv) {
2978 WARN_ON(block_rsv->size > 0);
2979 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002980 }
2981}
2982
2983/*
Josef Bacik7b128762008-07-24 12:17:14 -04002984 * This creates an orphan entry for the given inode in case something goes
2985 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002986 *
2987 * NOTE: caller of this function should reserve 5 units of metadata for
2988 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002989 */
2990int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2991{
2992 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002993 struct btrfs_block_rsv *block_rsv = NULL;
2994 int reserve = 0;
2995 int insert = 0;
2996 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002997
Yan, Zhengd68fc572010-05-16 10:49:58 -04002998 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002999 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003000 if (!block_rsv)
3001 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003002 }
3003
Yan, Zhengd68fc572010-05-16 10:49:58 -04003004 spin_lock(&root->orphan_lock);
3005 if (!root->orphan_block_rsv) {
3006 root->orphan_block_rsv = block_rsv;
3007 } else if (block_rsv) {
3008 btrfs_free_block_rsv(root, block_rsv);
3009 block_rsv = NULL;
3010 }
Josef Bacik7b128762008-07-24 12:17:14 -04003011
Josef Bacik8a35d952012-05-23 14:26:42 -04003012 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3013 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003014#if 0
3015 /*
3016 * For proper ENOSPC handling, we should do orphan
3017 * cleanup when mounting. But this introduces backward
3018 * compatibility issue.
3019 */
3020 if (!xchg(&root->orphan_item_inserted, 1))
3021 insert = 2;
3022 else
3023 insert = 1;
3024#endif
3025 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003026 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003027 }
Josef Bacik7b128762008-07-24 12:17:14 -04003028
Josef Bacik72ac3c02012-05-23 14:13:11 -04003029 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3030 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003031 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003032 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003033
Yan, Zhengd68fc572010-05-16 10:49:58 -04003034 /* grab metadata reservation from transaction handle */
3035 if (reserve) {
3036 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003037 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003038 }
3039
3040 /* insert an orphan item to track this unlinked/truncated file */
3041 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003042 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003043 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003044 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3045 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003046 btrfs_abort_transaction(trans, root, ret);
3047 return ret;
3048 }
3049 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003050 }
3051
3052 /* insert an orphan item to track subvolume contains orphan files */
3053 if (insert >= 2) {
3054 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3055 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003056 if (ret && ret != -EEXIST) {
3057 btrfs_abort_transaction(trans, root, ret);
3058 return ret;
3059 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003060 }
3061 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003062}
3063
3064/*
3065 * We have done the truncate/delete so we can go ahead and remove the orphan
3066 * item for this particular inode.
3067 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003068static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3069 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003070{
3071 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003072 int delete_item = 0;
3073 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003074 int ret = 0;
3075
Yan, Zhengd68fc572010-05-16 10:49:58 -04003076 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003077 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3078 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003079 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003080
Josef Bacik72ac3c02012-05-23 14:13:11 -04003081 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3082 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003083 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003084 spin_unlock(&root->orphan_lock);
3085
3086 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003087 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003088 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003089 }
3090
Josef Bacik8a35d952012-05-23 14:26:42 -04003091 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003092 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003093 atomic_dec(&root->orphan_inodes);
3094 }
Josef Bacik7b128762008-07-24 12:17:14 -04003095
Yan, Zhengd68fc572010-05-16 10:49:58 -04003096 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003097}
3098
3099/*
3100 * this cleans up any orphans that may be left on the list from the last use
3101 * of this root.
3102 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003103int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003104{
3105 struct btrfs_path *path;
3106 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003107 struct btrfs_key key, found_key;
3108 struct btrfs_trans_handle *trans;
3109 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003110 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003111 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3112
Yan, Zhengd68fc572010-05-16 10:49:58 -04003113 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003114 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003115
3116 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003117 if (!path) {
3118 ret = -ENOMEM;
3119 goto out;
3120 }
Josef Bacik7b128762008-07-24 12:17:14 -04003121 path->reada = -1;
3122
3123 key.objectid = BTRFS_ORPHAN_OBJECTID;
3124 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3125 key.offset = (u64)-1;
3126
Josef Bacik7b128762008-07-24 12:17:14 -04003127 while (1) {
3128 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003129 if (ret < 0)
3130 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003131
3132 /*
3133 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003134 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003135 * find the key and see if we have stuff that matches
3136 */
3137 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003138 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003139 if (path->slots[0] == 0)
3140 break;
3141 path->slots[0]--;
3142 }
3143
3144 /* pull out the item */
3145 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003146 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3147
3148 /* make sure the item matches what we want */
3149 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3150 break;
3151 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3152 break;
3153
3154 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003155 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003156
3157 /*
3158 * this is where we are basically btrfs_lookup, without the
3159 * crossing root thing. we store the inode number in the
3160 * offset of the orphan item.
3161 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003162
3163 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003164 btrfs_err(root->fs_info,
3165 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003166 ret = -EINVAL;
3167 goto out;
3168 }
3169
3170 last_objectid = found_key.offset;
3171
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003172 found_key.objectid = found_key.offset;
3173 found_key.type = BTRFS_INODE_ITEM_KEY;
3174 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003175 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003176 ret = PTR_RET(inode);
3177 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003178 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003179
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003180 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3181 struct btrfs_root *dead_root;
3182 struct btrfs_fs_info *fs_info = root->fs_info;
3183 int is_dead_root = 0;
3184
3185 /*
3186 * this is an orphan in the tree root. Currently these
3187 * could come from 2 sources:
3188 * a) a snapshot deletion in progress
3189 * b) a free space cache inode
3190 * We need to distinguish those two, as the snapshot
3191 * orphan must not get deleted.
3192 * find_dead_roots already ran before us, so if this
3193 * is a snapshot deletion, we should find the root
3194 * in the dead_roots list
3195 */
3196 spin_lock(&fs_info->trans_lock);
3197 list_for_each_entry(dead_root, &fs_info->dead_roots,
3198 root_list) {
3199 if (dead_root->root_key.objectid ==
3200 found_key.objectid) {
3201 is_dead_root = 1;
3202 break;
3203 }
3204 }
3205 spin_unlock(&fs_info->trans_lock);
3206 if (is_dead_root) {
3207 /* prevent this orphan from being found again */
3208 key.offset = found_key.objectid - 1;
3209 continue;
3210 }
3211 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003212 /*
3213 * Inode is already gone but the orphan item is still there,
3214 * kill the orphan item.
3215 */
3216 if (ret == -ESTALE) {
3217 trans = btrfs_start_transaction(root, 1);
3218 if (IS_ERR(trans)) {
3219 ret = PTR_ERR(trans);
3220 goto out;
3221 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003222 btrfs_debug(root->fs_info, "auto deleting %Lu",
3223 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003224 ret = btrfs_del_orphan_item(trans, root,
3225 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003226 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003227 btrfs_end_transaction(trans, root);
3228 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003229 }
Josef Bacik7b128762008-07-24 12:17:14 -04003230
Josef Bacik7b128762008-07-24 12:17:14 -04003231 /*
3232 * add this inode to the orphan list so btrfs_orphan_del does
3233 * the proper thing when we hit it
3234 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003235 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3236 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003237 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003238
Josef Bacik7b128762008-07-24 12:17:14 -04003239 /* if we have links, this was a truncate, lets do that */
3240 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003241 if (!S_ISREG(inode->i_mode)) {
3242 WARN_ON(1);
3243 iput(inode);
3244 continue;
3245 }
Josef Bacik7b128762008-07-24 12:17:14 -04003246 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003247
3248 /* 1 for the orphan item deletion. */
3249 trans = btrfs_start_transaction(root, 1);
3250 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003251 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003252 ret = PTR_ERR(trans);
3253 goto out;
3254 }
3255 ret = btrfs_orphan_add(trans, inode);
3256 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003257 if (ret) {
3258 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003259 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003260 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003261
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003262 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003263 if (ret)
3264 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003265 } else {
3266 nr_unlink++;
3267 }
3268
3269 /* this will do delete_inode and everything for us */
3270 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003271 if (ret)
3272 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003273 }
Miao Xie3254c872011-11-10 20:45:05 -05003274 /* release the path since we're done with it */
3275 btrfs_release_path(path);
3276
Yan, Zhengd68fc572010-05-16 10:49:58 -04003277 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3278
3279 if (root->orphan_block_rsv)
3280 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3281 (u64)-1);
3282
3283 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003284 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003285 if (!IS_ERR(trans))
3286 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003287 }
Josef Bacik7b128762008-07-24 12:17:14 -04003288
3289 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003290 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003291 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003292 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003293
3294out:
3295 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003296 btrfs_crit(root->fs_info,
3297 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003298 btrfs_free_path(path);
3299 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003300}
3301
Chris Masond352ac62008-09-29 15:18:18 -04003302/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003303 * very simple check to peek ahead in the leaf looking for xattrs. If we
3304 * don't find any xattrs, we know there can't be any acls.
3305 *
3306 * slot is the slot the inode is in, objectid is the objectid of the inode
3307 */
3308static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3309 int slot, u64 objectid)
3310{
3311 u32 nritems = btrfs_header_nritems(leaf);
3312 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003313 static u64 xattr_access = 0;
3314 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003315 int scanned = 0;
3316
Josef Bacikf23b5a52013-06-19 10:16:26 -04003317 if (!xattr_access) {
3318 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3319 strlen(POSIX_ACL_XATTR_ACCESS));
3320 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3321 strlen(POSIX_ACL_XATTR_DEFAULT));
3322 }
3323
Chris Mason46a53cc2009-04-27 11:47:50 -04003324 slot++;
3325 while (slot < nritems) {
3326 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3327
3328 /* we found a different objectid, there must not be acls */
3329 if (found_key.objectid != objectid)
3330 return 0;
3331
3332 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003333 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3334 if (found_key.offset == xattr_access ||
3335 found_key.offset == xattr_default)
3336 return 1;
3337 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003338
3339 /*
3340 * we found a key greater than an xattr key, there can't
3341 * be any acls later on
3342 */
3343 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3344 return 0;
3345
3346 slot++;
3347 scanned++;
3348
3349 /*
3350 * it goes inode, inode backrefs, xattrs, extents,
3351 * so if there are a ton of hard links to an inode there can
3352 * be a lot of backrefs. Don't waste time searching too hard,
3353 * this is just an optimization
3354 */
3355 if (scanned >= 8)
3356 break;
3357 }
3358 /* we hit the end of the leaf before we found an xattr or
3359 * something larger than an xattr. We have to assume the inode
3360 * has acls
3361 */
3362 return 1;
3363}
3364
3365/*
Chris Masond352ac62008-09-29 15:18:18 -04003366 * read an inode from the btree into the in-memory inode
3367 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003368static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003369{
3370 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003371 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003372 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003373 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003374 struct btrfs_root *root = BTRFS_I(inode)->root;
3375 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003376 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003377 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003378 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003379 bool filled = false;
3380
3381 ret = btrfs_fill_inode(inode, &rdev);
3382 if (!ret)
3383 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003384
3385 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003386 if (!path)
3387 goto make_bad;
3388
Josef Bacikd90c7322011-05-17 09:50:54 -04003389 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003390 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003391
Chris Mason39279cc2007-06-12 06:35:45 -04003392 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003393 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003394 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003395
Chris Mason5f39d392007-10-15 16:14:19 -04003396 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003397
3398 if (filled)
3399 goto cache_acl;
3400
Chris Mason5f39d392007-10-15 16:14:19 -04003401 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3402 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003403 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003404 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003405 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3406 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003407 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003408
3409 tspec = btrfs_inode_atime(inode_item);
3410 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3411 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3412
3413 tspec = btrfs_inode_mtime(inode_item);
3414 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3415 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3416
3417 tspec = btrfs_inode_ctime(inode_item);
3418 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3419 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3420
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003421 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003422 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003423 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3424
3425 /*
3426 * If we were modified in the current generation and evicted from memory
3427 * and then re-read we need to do a full sync since we don't have any
3428 * idea about which extents were modified before we were evicted from
3429 * cache.
3430 */
3431 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3432 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3433 &BTRFS_I(inode)->runtime_flags);
3434
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003435 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003436 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003437 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003438 rdev = btrfs_inode_rdev(leaf, inode_item);
3439
Josef Bacikaec74772008-07-24 12:12:38 -04003440 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003441 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003442cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003443 /*
3444 * try to precache a NULL acl entry for files that don't have
3445 * any xattrs or acls
3446 */
Li Zefan33345d012011-04-20 10:31:50 +08003447 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3448 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003449 if (!maybe_acls)
3450 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003451
Chris Mason39279cc2007-06-12 06:35:45 -04003452 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003453
Chris Mason39279cc2007-06-12 06:35:45 -04003454 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003455 case S_IFREG:
3456 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003457 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003458 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003459 inode->i_fop = &btrfs_file_operations;
3460 inode->i_op = &btrfs_file_inode_operations;
3461 break;
3462 case S_IFDIR:
3463 inode->i_fop = &btrfs_dir_file_operations;
3464 if (root == root->fs_info->tree_root)
3465 inode->i_op = &btrfs_dir_ro_inode_operations;
3466 else
3467 inode->i_op = &btrfs_dir_inode_operations;
3468 break;
3469 case S_IFLNK:
3470 inode->i_op = &btrfs_symlink_inode_operations;
3471 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003472 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003473 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003474 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003475 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003476 init_special_inode(inode, inode->i_mode, rdev);
3477 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003478 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003479
3480 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003481 return;
3482
3483make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003484 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003485 make_bad_inode(inode);
3486}
3487
Chris Masond352ac62008-09-29 15:18:18 -04003488/*
3489 * given a leaf and an inode, copy the inode fields into the leaf
3490 */
Chris Masone02119d2008-09-05 16:13:11 -04003491static void fill_inode_item(struct btrfs_trans_handle *trans,
3492 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003493 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003494 struct inode *inode)
3495{
Liu Bo51fab692012-12-27 09:01:21 +00003496 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003497
Liu Bo51fab692012-12-27 09:01:21 +00003498 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003499
Liu Bo51fab692012-12-27 09:01:21 +00003500 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3501 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3502 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3503 &token);
3504 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3505 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003506
Liu Bo51fab692012-12-27 09:01:21 +00003507 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3508 inode->i_atime.tv_sec, &token);
3509 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3510 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003511
Liu Bo51fab692012-12-27 09:01:21 +00003512 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3513 inode->i_mtime.tv_sec, &token);
3514 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3515 inode->i_mtime.tv_nsec, &token);
3516
3517 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3518 inode->i_ctime.tv_sec, &token);
3519 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3520 inode->i_ctime.tv_nsec, &token);
3521
3522 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3523 &token);
3524 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3525 &token);
3526 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3527 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3528 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3529 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3530 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003531}
3532
Chris Masond352ac62008-09-29 15:18:18 -04003533/*
3534 * copy everything in the in-memory inode into the btree.
3535 */
Chris Mason21151332011-11-10 20:39:08 -05003536static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003537 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003538{
3539 struct btrfs_inode_item *inode_item;
3540 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003541 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003542 int ret;
3543
3544 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003545 if (!path)
3546 return -ENOMEM;
3547
Chris Masonb9473432009-03-13 11:00:37 -04003548 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003549 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3550 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003551 if (ret) {
3552 if (ret > 0)
3553 ret = -ENOENT;
3554 goto failed;
3555 }
3556
Chris Masonb4ce94d2009-02-04 09:25:08 -05003557 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003558 leaf = path->nodes[0];
3559 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003560 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003561
Chris Masone02119d2008-09-05 16:13:11 -04003562 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003563 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003564 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003565 ret = 0;
3566failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003567 btrfs_free_path(path);
3568 return ret;
3569}
3570
Chris Masond352ac62008-09-29 15:18:18 -04003571/*
Chris Mason21151332011-11-10 20:39:08 -05003572 * copy everything in the in-memory inode into the btree.
3573 */
3574noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3575 struct btrfs_root *root, struct inode *inode)
3576{
3577 int ret;
3578
3579 /*
3580 * If the inode is a free space inode, we can deadlock during commit
3581 * if we put it into the delayed code.
3582 *
3583 * The data relocation inode should also be directly updated
3584 * without delay
3585 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003586 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003587 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003588 btrfs_update_root_times(trans, root);
3589
Chris Mason21151332011-11-10 20:39:08 -05003590 ret = btrfs_delayed_update_inode(trans, root, inode);
3591 if (!ret)
3592 btrfs_set_inode_last_trans(trans, inode);
3593 return ret;
3594 }
3595
3596 return btrfs_update_inode_item(trans, root, inode);
3597}
3598
Josef Bacikbe6aef62012-10-22 15:43:12 -04003599noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3600 struct btrfs_root *root,
3601 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003602{
3603 int ret;
3604
3605 ret = btrfs_update_inode(trans, root, inode);
3606 if (ret == -ENOSPC)
3607 return btrfs_update_inode_item(trans, root, inode);
3608 return ret;
3609}
3610
3611/*
Chris Masond352ac62008-09-29 15:18:18 -04003612 * unlink helper that gets used here in inode.c and in the tree logging
3613 * recovery code. It remove a link in a directory with a given name, and
3614 * also drops the back refs in the inode to the directory
3615 */
Al Viro92986792011-03-04 17:14:37 +00003616static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3617 struct btrfs_root *root,
3618 struct inode *dir, struct inode *inode,
3619 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003620{
3621 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003622 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003623 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003624 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003625 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003626 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003627 u64 ino = btrfs_ino(inode);
3628 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003629
3630 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003631 if (!path) {
3632 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003633 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003634 }
3635
Chris Masonb9473432009-03-13 11:00:37 -04003636 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003637 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003638 name, name_len, -1);
3639 if (IS_ERR(di)) {
3640 ret = PTR_ERR(di);
3641 goto err;
3642 }
3643 if (!di) {
3644 ret = -ENOENT;
3645 goto err;
3646 }
Chris Mason5f39d392007-10-15 16:14:19 -04003647 leaf = path->nodes[0];
3648 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003649 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003650 if (ret)
3651 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003652 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003653
Li Zefan33345d012011-04-20 10:31:50 +08003654 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3655 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003656 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003657 btrfs_info(root->fs_info,
3658 "failed to delete reference to %.*s, inode %llu parent %llu",
3659 name_len, name,
3660 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003661 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003662 goto err;
3663 }
3664
Miao Xie16cdcec2011-04-22 18:12:22 +08003665 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003666 if (ret) {
3667 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003668 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003669 }
Chris Mason39279cc2007-06-12 06:35:45 -04003670
Chris Masone02119d2008-09-05 16:13:11 -04003671 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003672 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003673 if (ret != 0 && ret != -ENOENT) {
3674 btrfs_abort_transaction(trans, root, ret);
3675 goto err;
3676 }
Chris Masone02119d2008-09-05 16:13:11 -04003677
3678 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3679 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003680 if (ret == -ENOENT)
3681 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003682 else if (ret)
3683 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003684err:
3685 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003686 if (ret)
3687 goto out;
3688
3689 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003690 inode_inc_iversion(inode);
3691 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003692 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003693 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003694out:
Chris Mason39279cc2007-06-12 06:35:45 -04003695 return ret;
3696}
3697
Al Viro92986792011-03-04 17:14:37 +00003698int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3699 struct btrfs_root *root,
3700 struct inode *dir, struct inode *inode,
3701 const char *name, int name_len)
3702{
3703 int ret;
3704 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3705 if (!ret) {
3706 btrfs_drop_nlink(inode);
3707 ret = btrfs_update_inode(trans, root, inode);
3708 }
3709 return ret;
3710}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003711
3712/*
3713 * helper to start transaction for unlink and rmdir.
3714 *
Josef Bacikd52be812013-05-29 14:54:47 -04003715 * unlink and rmdir are special in btrfs, they do not always free space, so
3716 * if we cannot make our reservations the normal way try and see if there is
3717 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3718 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003719 */
Josef Bacikd52be812013-05-29 14:54:47 -04003720static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003721{
3722 struct btrfs_trans_handle *trans;
3723 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003724 int ret;
3725
Josef Bacike70bea52011-10-11 14:18:24 -04003726 /*
3727 * 1 for the possible orphan item
3728 * 1 for the dir item
3729 * 1 for the dir index
3730 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003731 * 1 for the inode
3732 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003733 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003734 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3735 return trans;
3736
Josef Bacikd52be812013-05-29 14:54:47 -04003737 if (PTR_ERR(trans) == -ENOSPC) {
3738 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003739
Josef Bacikd52be812013-05-29 14:54:47 -04003740 trans = btrfs_start_transaction(root, 0);
3741 if (IS_ERR(trans))
3742 return trans;
3743 ret = btrfs_cond_migrate_bytes(root->fs_info,
3744 &root->fs_info->trans_block_rsv,
3745 num_bytes, 5);
3746 if (ret) {
3747 btrfs_end_transaction(trans, root);
3748 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003749 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003750 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003751 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003752 }
Josef Bacikd52be812013-05-29 14:54:47 -04003753 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003754}
3755
Chris Mason39279cc2007-06-12 06:35:45 -04003756static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3757{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003758 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003759 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003760 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003761 int ret;
3762
Josef Bacikd52be812013-05-29 14:54:47 -04003763 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003764 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003765 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003766
Chris Mason12fcfd22009-03-24 10:24:20 -04003767 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3768
Chris Masone02119d2008-09-05 16:13:11 -04003769 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3770 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003771 if (ret)
3772 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003773
Yan, Zhenga22285a2010-05-16 10:48:46 -04003774 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003775 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003776 if (ret)
3777 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003778 }
Josef Bacik7b128762008-07-24 12:17:14 -04003779
Tsutomu Itohb5324022011-07-19 07:27:20 +00003780out:
Josef Bacikd52be812013-05-29 14:54:47 -04003781 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003782 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003783 return ret;
3784}
3785
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003786int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3787 struct btrfs_root *root,
3788 struct inode *dir, u64 objectid,
3789 const char *name, int name_len)
3790{
3791 struct btrfs_path *path;
3792 struct extent_buffer *leaf;
3793 struct btrfs_dir_item *di;
3794 struct btrfs_key key;
3795 u64 index;
3796 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003797 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003798
3799 path = btrfs_alloc_path();
3800 if (!path)
3801 return -ENOMEM;
3802
Li Zefan33345d012011-04-20 10:31:50 +08003803 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003804 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003805 if (IS_ERR_OR_NULL(di)) {
3806 if (!di)
3807 ret = -ENOENT;
3808 else
3809 ret = PTR_ERR(di);
3810 goto out;
3811 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003812
3813 leaf = path->nodes[0];
3814 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3815 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3816 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003817 if (ret) {
3818 btrfs_abort_transaction(trans, root, ret);
3819 goto out;
3820 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003821 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003822
3823 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3824 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003825 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003826 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003827 if (ret != -ENOENT) {
3828 btrfs_abort_transaction(trans, root, ret);
3829 goto out;
3830 }
Li Zefan33345d012011-04-20 10:31:50 +08003831 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003832 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003833 if (IS_ERR_OR_NULL(di)) {
3834 if (!di)
3835 ret = -ENOENT;
3836 else
3837 ret = PTR_ERR(di);
3838 btrfs_abort_transaction(trans, root, ret);
3839 goto out;
3840 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003841
3842 leaf = path->nodes[0];
3843 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003844 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003845 index = key.offset;
3846 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003847 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003848
Miao Xie16cdcec2011-04-22 18:12:22 +08003849 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003850 if (ret) {
3851 btrfs_abort_transaction(trans, root, ret);
3852 goto out;
3853 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003854
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003855 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003856 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003857 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003858 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003859 if (ret)
3860 btrfs_abort_transaction(trans, root, ret);
3861out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003862 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003863 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003864}
3865
Chris Mason39279cc2007-06-12 06:35:45 -04003866static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3867{
3868 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003869 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003870 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003871 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003872
David Sterbab3ae2442012-09-13 16:04:34 -06003873 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003874 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003875 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3876 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003877
Josef Bacikd52be812013-05-29 14:54:47 -04003878 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003879 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003880 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003881
Li Zefan33345d012011-04-20 10:31:50 +08003882 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003883 err = btrfs_unlink_subvol(trans, root, dir,
3884 BTRFS_I(inode)->location.objectid,
3885 dentry->d_name.name,
3886 dentry->d_name.len);
3887 goto out;
3888 }
3889
Josef Bacik7b128762008-07-24 12:17:14 -04003890 err = btrfs_orphan_add(trans, inode);
3891 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003892 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003893
Chris Mason39279cc2007-06-12 06:35:45 -04003894 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003895 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3896 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003897 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003898 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003899out:
Josef Bacikd52be812013-05-29 14:54:47 -04003900 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003901 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003902
Chris Mason39279cc2007-06-12 06:35:45 -04003903 return err;
3904}
3905
Chris Mason323ac952008-10-01 19:05:46 -04003906/*
Chris Mason39279cc2007-06-12 06:35:45 -04003907 * this can truncate away extent items, csum items and directory items.
3908 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003909 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003910 *
3911 * csum items that cross the new i_size are truncated to the new size
3912 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003913 *
3914 * min_type is the minimum key type to truncate down to. If set to 0, this
3915 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003916 */
Yan, Zheng80825102009-11-12 09:35:36 +00003917int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3918 struct btrfs_root *root,
3919 struct inode *inode,
3920 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003921{
Chris Mason39279cc2007-06-12 06:35:45 -04003922 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003923 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003924 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003925 struct btrfs_key key;
3926 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003927 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003928 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003929 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003930 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003931 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003932 int found_extent;
3933 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003934 int pending_del_nr = 0;
3935 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003936 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003937 int ret;
3938 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003939 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003940
3941 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003942
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003943 path = btrfs_alloc_path();
3944 if (!path)
3945 return -ENOMEM;
3946 path->reada = -1;
3947
Josef Bacik5dc562c2012-08-17 13:14:17 -04003948 /*
3949 * We want to drop from the next block forward in case this new size is
3950 * not block aligned since we will be keeping the last block of the
3951 * extent just the way it is.
3952 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003953 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00003954 btrfs_drop_extent_cache(inode, ALIGN(new_size,
3955 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003956
Miao Xie16cdcec2011-04-22 18:12:22 +08003957 /*
3958 * This function is also used to drop the items in the log tree before
3959 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3960 * it is used to drop the loged items. So we shouldn't kill the delayed
3961 * items.
3962 */
3963 if (min_type == 0 && root == BTRFS_I(inode)->root)
3964 btrfs_kill_delayed_inode_items(inode);
3965
Li Zefan33345d012011-04-20 10:31:50 +08003966 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003967 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003968 key.type = (u8)-1;
3969
Chris Mason85e21ba2008-01-29 15:11:36 -05003970search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003971 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003972 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003973 if (ret < 0) {
3974 err = ret;
3975 goto out;
3976 }
Chris Masond3977122009-01-05 21:25:51 -05003977
Chris Mason85e21ba2008-01-29 15:11:36 -05003978 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003979 /* there are no items in the tree for us to truncate, we're
3980 * done
3981 */
Yan, Zheng80825102009-11-12 09:35:36 +00003982 if (path->slots[0] == 0)
3983 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003984 path->slots[0]--;
3985 }
3986
Chris Masond3977122009-01-05 21:25:51 -05003987 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003988 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003989 leaf = path->nodes[0];
3990 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3991 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003992
Li Zefan33345d012011-04-20 10:31:50 +08003993 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003994 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003995
Chris Mason85e21ba2008-01-29 15:11:36 -05003996 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003997 break;
3998
Chris Mason5f39d392007-10-15 16:14:19 -04003999 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004000 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004001 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004002 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004003 extent_type = btrfs_file_extent_type(leaf, fi);
4004 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004005 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004006 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004007 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004008 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04004009 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004010 }
Yan008630c2007-11-07 13:31:09 -05004011 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004012 }
Yan, Zheng80825102009-11-12 09:35:36 +00004013 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004014 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004015 } else {
4016 if (item_end < new_size)
4017 break;
4018 if (found_key.offset >= new_size)
4019 del_item = 1;
4020 else
4021 del_item = 0;
4022 }
Chris Mason39279cc2007-06-12 06:35:45 -04004023 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004024 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004025 if (found_type != BTRFS_EXTENT_DATA_KEY)
4026 goto delete;
4027
4028 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004029 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004030 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004031 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004032 u64 orig_num_bytes =
4033 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004034 extent_num_bytes = ALIGN(new_size -
4035 found_key.offset,
4036 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004037 btrfs_set_file_extent_num_bytes(leaf, fi,
4038 extent_num_bytes);
4039 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004040 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004041 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004042 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004043 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004044 } else {
Chris Masondb945352007-10-15 16:15:53 -04004045 extent_num_bytes =
4046 btrfs_file_extent_disk_num_bytes(leaf,
4047 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004048 extent_offset = found_key.offset -
4049 btrfs_file_extent_offset(leaf, fi);
4050
Chris Mason39279cc2007-06-12 06:35:45 -04004051 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004052 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004053 if (extent_start != 0) {
4054 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004055 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004056 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004057 }
4058 }
Chris Mason90692182008-02-08 13:49:28 -05004059 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004060 /*
4061 * we can't truncate inline items that have had
4062 * special encodings
4063 */
4064 if (!del_item &&
4065 btrfs_file_extent_compression(leaf, fi) == 0 &&
4066 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4067 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004068 u32 size = new_size - found_key.offset;
4069
4070 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004071 inode_sub_bytes(inode, item_end + 1 -
4072 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004073 }
4074 size =
4075 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004076 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004077 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004078 inode_sub_bytes(inode, item_end + 1 -
4079 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004080 }
Chris Mason39279cc2007-06-12 06:35:45 -04004081 }
Chris Mason179e29e2007-11-01 11:28:41 -04004082delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004083 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004084 if (!pending_del_nr) {
4085 /* no pending yet, add ourselves */
4086 pending_del_slot = path->slots[0];
4087 pending_del_nr = 1;
4088 } else if (pending_del_nr &&
4089 path->slots[0] + 1 == pending_del_slot) {
4090 /* hop on the pending chunk */
4091 pending_del_nr++;
4092 pending_del_slot = path->slots[0];
4093 } else {
Chris Masond3977122009-01-05 21:25:51 -05004094 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004095 }
Chris Mason39279cc2007-06-12 06:35:45 -04004096 } else {
4097 break;
4098 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004099 if (found_extent && (root->ref_cows ||
4100 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004101 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004102 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004103 extent_num_bytes, 0,
4104 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004105 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004106 BUG_ON(ret);
4107 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004108
Yan, Zheng80825102009-11-12 09:35:36 +00004109 if (found_type == BTRFS_INODE_ITEM_KEY)
4110 break;
4111
4112 if (path->slots[0] == 0 ||
4113 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004114 if (pending_del_nr) {
4115 ret = btrfs_del_items(trans, root, path,
4116 pending_del_slot,
4117 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004118 if (ret) {
4119 btrfs_abort_transaction(trans,
4120 root, ret);
4121 goto error;
4122 }
Yan, Zheng80825102009-11-12 09:35:36 +00004123 pending_del_nr = 0;
4124 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004125 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004126 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004127 } else {
4128 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004129 }
Chris Mason39279cc2007-06-12 06:35:45 -04004130 }
Yan, Zheng80825102009-11-12 09:35:36 +00004131out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004132 if (pending_del_nr) {
4133 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4134 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004135 if (ret)
4136 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004137 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004138error:
Chris Mason39279cc2007-06-12 06:35:45 -04004139 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004140 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004141}
4142
Chris Masona52d9a82007-08-27 16:49:44 -04004143/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004144 * btrfs_truncate_page - read, zero a chunk and write a page
4145 * @inode - inode that we're zeroing
4146 * @from - the offset to start zeroing
4147 * @len - the length to zero, 0 to zero the entire range respective to the
4148 * offset
4149 * @front - zero up to the offset instead of from the offset on
4150 *
4151 * This will find the page for the "from" offset and cow the page and zero the
4152 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004153 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004154int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4155 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004156{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004157 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004158 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004159 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4160 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004161 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004162 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004163 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004164 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4165 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4166 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004167 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004168 int ret = 0;
4169 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004170 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004171
Josef Bacik2aaa6652012-08-29 14:27:18 -04004172 if ((offset & (blocksize - 1)) == 0 &&
4173 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004174 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004175 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004176 if (ret)
4177 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004178
Chris Mason211c17f2008-05-15 09:13:45 -04004179again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004180 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004181 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004182 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004183 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004184 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004185 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004186
4187 page_start = page_offset(page);
4188 page_end = page_start + PAGE_CACHE_SIZE - 1;
4189
Chris Masona52d9a82007-08-27 16:49:44 -04004190 if (!PageUptodate(page)) {
4191 ret = btrfs_readpage(NULL, page);
4192 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004193 if (page->mapping != mapping) {
4194 unlock_page(page);
4195 page_cache_release(page);
4196 goto again;
4197 }
Chris Masona52d9a82007-08-27 16:49:44 -04004198 if (!PageUptodate(page)) {
4199 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004200 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004201 }
4202 }
Chris Mason211c17f2008-05-15 09:13:45 -04004203 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004204
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004205 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004206 set_page_extent_mapped(page);
4207
4208 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4209 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004210 unlock_extent_cached(io_tree, page_start, page_end,
4211 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004212 unlock_page(page);
4213 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004214 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004215 btrfs_put_ordered_extent(ordered);
4216 goto again;
4217 }
4218
Josef Bacik2ac55d42010-02-03 19:33:23 +00004219 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004220 EXTENT_DIRTY | EXTENT_DELALLOC |
4221 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004222 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004223
Josef Bacik2ac55d42010-02-03 19:33:23 +00004224 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4225 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004226 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004227 unlock_extent_cached(io_tree, page_start, page_end,
4228 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004229 goto out_unlock;
4230 }
4231
Chris Masone6dcd2d2008-07-17 12:53:50 -04004232 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004233 if (!len)
4234 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004235 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004236 if (front)
4237 memset(kaddr, 0, offset);
4238 else
4239 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004240 flush_dcache_page(page);
4241 kunmap(page);
4242 }
Chris Mason247e7432008-07-17 12:53:51 -04004243 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004244 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004245 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4246 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004247
Chris Mason89642222008-07-24 09:41:53 -04004248out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004249 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004250 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004251 unlock_page(page);
4252 page_cache_release(page);
4253out:
4254 return ret;
4255}
4256
Josef Bacik695a0d02011-03-04 15:46:53 -05004257/*
4258 * This function puts in dummy file extents for the area we're creating a hole
4259 * for. So if we are truncating this file to a larger size we need to insert
4260 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4261 * the range between oldsize and size
4262 */
Josef Bacika41ad392011-01-31 15:30:16 -05004263int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004264{
4265 struct btrfs_trans_handle *trans;
4266 struct btrfs_root *root = BTRFS_I(inode)->root;
4267 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004268 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004269 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004270 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004271 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4272 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004273 u64 last_byte;
4274 u64 cur_offset;
4275 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004276 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004277
Josef Bacika71754f2013-06-17 17:14:39 -04004278 /*
4279 * If our size started in the middle of a page we need to zero out the
4280 * rest of the page before we expand the i_size, otherwise we could
4281 * expose stale data.
4282 */
4283 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4284 if (err)
4285 return err;
4286
Yan Zheng9036c102008-10-30 14:19:41 -04004287 if (size <= hole_start)
4288 return 0;
4289
Yan Zheng9036c102008-10-30 14:19:41 -04004290 while (1) {
4291 struct btrfs_ordered_extent *ordered;
4292 btrfs_wait_ordered_range(inode, hole_start,
4293 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004294 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004295 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004296 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4297 if (!ordered)
4298 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004299 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4300 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004301 btrfs_put_ordered_extent(ordered);
4302 }
4303
Yan Zheng9036c102008-10-30 14:19:41 -04004304 cur_offset = hole_start;
4305 while (1) {
4306 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4307 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004308 if (IS_ERR(em)) {
4309 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004310 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004311 break;
4312 }
Yan Zheng9036c102008-10-30 14:19:41 -04004313 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004314 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004315 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004316 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004317 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004318
Miao Xie36423202011-12-14 20:12:02 -05004319 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004320 if (IS_ERR(trans)) {
4321 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004322 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004323 }
Yan, Zheng80825102009-11-12 09:35:36 +00004324
Josef Bacik5dc562c2012-08-17 13:14:17 -04004325 err = btrfs_drop_extents(trans, root, inode,
4326 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004327 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004328 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004329 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004330 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004331 break;
Miao Xie5b397372011-09-11 10:52:24 -04004332 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004333
Yan Zheng9036c102008-10-30 14:19:41 -04004334 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004335 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004336 0, hole_size, 0, hole_size,
4337 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004338 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004339 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004340 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004341 break;
Miao Xie5b397372011-09-11 10:52:24 -04004342 }
Yan, Zheng80825102009-11-12 09:35:36 +00004343
Josef Bacik5dc562c2012-08-17 13:14:17 -04004344 btrfs_drop_extent_cache(inode, cur_offset,
4345 cur_offset + hole_size - 1, 0);
4346 hole_em = alloc_extent_map();
4347 if (!hole_em) {
4348 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4349 &BTRFS_I(inode)->runtime_flags);
4350 goto next;
4351 }
4352 hole_em->start = cur_offset;
4353 hole_em->len = hole_size;
4354 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004355
Josef Bacik5dc562c2012-08-17 13:14:17 -04004356 hole_em->block_start = EXTENT_MAP_HOLE;
4357 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004358 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004359 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004360 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4361 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4362 hole_em->generation = trans->transid;
4363
4364 while (1) {
4365 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004366 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004367 write_unlock(&em_tree->lock);
4368 if (err != -EEXIST)
4369 break;
4370 btrfs_drop_extent_cache(inode, cur_offset,
4371 cur_offset +
4372 hole_size - 1, 0);
4373 }
4374 free_extent_map(hole_em);
4375next:
Miao Xie36423202011-12-14 20:12:02 -05004376 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004377 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004378 }
4379 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004380 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004381 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004382 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004383 break;
4384 }
4385
Yan, Zhenga22285a2010-05-16 10:48:46 -04004386 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004387 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4388 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004389 return err;
4390}
4391
Eric Sandeen3972f262013-01-12 02:57:22 +00004392static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004393{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004394 struct btrfs_root *root = BTRFS_I(inode)->root;
4395 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004396 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004397 loff_t newsize = attr->ia_size;
4398 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004399 int ret;
4400
Eric Sandeen3972f262013-01-12 02:57:22 +00004401 /*
4402 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4403 * special case where we need to update the times despite not having
4404 * these flags set. For all other operations the VFS set these flags
4405 * explicitly if it wants a timestamp update.
4406 */
4407 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4408 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4409
Josef Bacika41ad392011-01-31 15:30:16 -05004410 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004411 truncate_pagecache(inode, oldsize, newsize);
4412 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004413 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004414 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004415
Miao Xief4a2f4c2011-12-14 20:12:01 -05004416 trans = btrfs_start_transaction(root, 1);
4417 if (IS_ERR(trans))
4418 return PTR_ERR(trans);
4419
4420 i_size_write(inode, newsize);
4421 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4422 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004423 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004424 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004425
Josef Bacika41ad392011-01-31 15:30:16 -05004426 /*
4427 * We're truncating a file that used to have good data down to
4428 * zero. Make sure it gets into the ordered flush list so that
4429 * any new writes get down to disk quickly.
4430 */
4431 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004432 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4433 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004434
Josef Bacikf3fe8202013-01-07 17:03:21 -05004435 /*
4436 * 1 for the orphan item we're going to add
4437 * 1 for the orphan item deletion.
4438 */
4439 trans = btrfs_start_transaction(root, 2);
4440 if (IS_ERR(trans))
4441 return PTR_ERR(trans);
4442
4443 /*
4444 * We need to do this in case we fail at _any_ point during the
4445 * actual truncate. Once we do the truncate_setsize we could
4446 * invalidate pages which forces any outstanding ordered io to
4447 * be instantly completed which will give us extents that need
4448 * to be truncated. If we fail to get an orphan inode down we
4449 * could have left over extents that were never meant to live,
4450 * so we need to garuntee from this point on that everything
4451 * will be consistent.
4452 */
4453 ret = btrfs_orphan_add(trans, inode);
4454 btrfs_end_transaction(trans, root);
4455 if (ret)
4456 return ret;
4457
Josef Bacika41ad392011-01-31 15:30:16 -05004458 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4459 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004460
4461 /* Disable nonlocked read DIO to avoid the end less truncate */
4462 btrfs_inode_block_unlocked_dio(inode);
4463 inode_dio_wait(inode);
4464 btrfs_inode_resume_unlocked_dio(inode);
4465
Josef Bacika41ad392011-01-31 15:30:16 -05004466 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004467 if (ret && inode->i_nlink)
4468 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004469 }
4470
Josef Bacika41ad392011-01-31 15:30:16 -05004471 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004472}
4473
Chris Mason39279cc2007-06-12 06:35:45 -04004474static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4475{
4476 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004477 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004478 int err;
4479
Li Zefanb83cc962010-12-20 16:04:08 +08004480 if (btrfs_root_readonly(root))
4481 return -EROFS;
4482
Chris Mason39279cc2007-06-12 06:35:45 -04004483 err = inode_change_ok(inode, attr);
4484 if (err)
4485 return err;
4486
Chris Mason5a3f23d2009-03-31 13:27:11 -04004487 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004488 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004489 if (err)
4490 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004491 }
Yan Zheng9036c102008-10-30 14:19:41 -04004492
Christoph Hellwig10257742010-06-04 11:30:02 +02004493 if (attr->ia_valid) {
4494 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004495 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004496 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004497
Josef Bacik22c44fe2011-11-30 10:45:38 -05004498 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004499 err = btrfs_acl_chmod(inode);
4500 }
4501
Chris Mason39279cc2007-06-12 06:35:45 -04004502 return err;
4503}
Chris Mason61295eb2008-01-14 16:24:38 -05004504
Al Virobd555972010-06-07 11:35:40 -04004505void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004506{
4507 struct btrfs_trans_handle *trans;
4508 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004509 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004510 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004511 int ret;
4512
liubo1abe9b82011-03-24 11:18:59 +00004513 trace_btrfs_inode_evict(inode);
4514
Chris Mason39279cc2007-06-12 06:35:45 -04004515 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004516 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004517 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004518 goto no_delete;
4519
Chris Mason39279cc2007-06-12 06:35:45 -04004520 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004521 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004522 goto no_delete;
4523 }
Al Virobd555972010-06-07 11:35:40 -04004524 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004525 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004526
Yan, Zhengc71bf092009-11-12 09:34:40 +00004527 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004528 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004529 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004530 goto no_delete;
4531 }
4532
Yan, Zheng76dda932009-09-21 16:00:26 -04004533 if (inode->i_nlink > 0) {
4534 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4535 goto no_delete;
4536 }
4537
Miao Xie0e8c36a2012-12-19 06:59:51 +00004538 ret = btrfs_commit_inode_delayed_inode(inode);
4539 if (ret) {
4540 btrfs_orphan_del(NULL, inode);
4541 goto no_delete;
4542 }
4543
Miao Xie66d8f3d2012-09-06 04:02:28 -06004544 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004545 if (!rsv) {
4546 btrfs_orphan_del(NULL, inode);
4547 goto no_delete;
4548 }
Josef Bacik4a338542011-08-29 11:01:31 -04004549 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004550 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004551 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004552
Chris Masondbe674a2008-07-17 12:54:05 -04004553 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004554
Josef Bacik4289a662011-08-05 13:22:24 -04004555 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004556 * This is a bit simpler than btrfs_truncate since we've already
4557 * reserved our space for our orphan item in the unlink, so we just
4558 * need to reserve some slack space in case we add bytes and update
4559 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004560 */
Yan, Zheng80825102009-11-12 09:35:36 +00004561 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004562 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4563 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004564
Josef Bacik726c35f2011-09-26 15:46:06 -04004565 /*
4566 * Try and steal from the global reserve since we will
4567 * likely not use this space anyway, we want to try as
4568 * hard as possible to get this to work.
4569 */
4570 if (ret)
4571 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4572
Yan, Zhengd68fc572010-05-16 10:49:58 -04004573 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004574 btrfs_warn(root->fs_info,
4575 "Could not get space for a delete, will truncate on mount %d",
4576 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004577 btrfs_orphan_del(NULL, inode);
4578 btrfs_free_block_rsv(root, rsv);
4579 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004580 }
4581
Miao Xie0e8c36a2012-12-19 06:59:51 +00004582 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004583 if (IS_ERR(trans)) {
4584 btrfs_orphan_del(NULL, inode);
4585 btrfs_free_block_rsv(root, rsv);
4586 goto no_delete;
4587 }
4588
4589 trans->block_rsv = rsv;
4590
Yan, Zhengd68fc572010-05-16 10:49:58 -04004591 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004592 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004593 break;
4594
Miao Xie8407aa42012-09-07 01:43:32 -06004595 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004596 btrfs_end_transaction(trans, root);
4597 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004598 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004599 }
4600
Josef Bacik4289a662011-08-05 13:22:24 -04004601 btrfs_free_block_rsv(root, rsv);
4602
Yan, Zheng80825102009-11-12 09:35:36 +00004603 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004604 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004605 ret = btrfs_orphan_del(trans, inode);
4606 BUG_ON(ret);
4607 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004608
Josef Bacik4289a662011-08-05 13:22:24 -04004609 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004610 if (!(root == root->fs_info->tree_root ||
4611 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004612 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004613
Chris Mason54aa1f42007-06-22 14:16:25 -04004614 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004615 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004616no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004617 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004618 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004619 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004620}
4621
4622/*
4623 * this returns the key found in the dir entry in the location pointer.
4624 * If no dir entries were found, location->objectid is 0.
4625 */
4626static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4627 struct btrfs_key *location)
4628{
4629 const char *name = dentry->d_name.name;
4630 int namelen = dentry->d_name.len;
4631 struct btrfs_dir_item *di;
4632 struct btrfs_path *path;
4633 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004634 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004635
4636 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004637 if (!path)
4638 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004639
Li Zefan33345d012011-04-20 10:31:50 +08004640 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004641 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004642 if (IS_ERR(di))
4643 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004644
David Sterbac7040052011-04-19 18:00:01 +02004645 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004646 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004647
Chris Mason5f39d392007-10-15 16:14:19 -04004648 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004649out:
Chris Mason39279cc2007-06-12 06:35:45 -04004650 btrfs_free_path(path);
4651 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004652out_err:
4653 location->objectid = 0;
4654 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004655}
4656
4657/*
4658 * when we hit a tree root in a directory, the btrfs part of the inode
4659 * needs to be changed to reflect the root directory of the tree root. This
4660 * is kind of like crossing a mount point.
4661 */
4662static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004663 struct inode *dir,
4664 struct dentry *dentry,
4665 struct btrfs_key *location,
4666 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004667{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004668 struct btrfs_path *path;
4669 struct btrfs_root *new_root;
4670 struct btrfs_root_ref *ref;
4671 struct extent_buffer *leaf;
4672 int ret;
4673 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004674
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004675 path = btrfs_alloc_path();
4676 if (!path) {
4677 err = -ENOMEM;
4678 goto out;
4679 }
Chris Mason39279cc2007-06-12 06:35:45 -04004680
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004681 err = -ENOENT;
4682 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4683 BTRFS_I(dir)->root->root_key.objectid,
4684 location->objectid);
4685 if (ret) {
4686 if (ret < 0)
4687 err = ret;
4688 goto out;
4689 }
Chris Mason39279cc2007-06-12 06:35:45 -04004690
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004691 leaf = path->nodes[0];
4692 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004693 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004694 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4695 goto out;
4696
4697 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4698 (unsigned long)(ref + 1),
4699 dentry->d_name.len);
4700 if (ret)
4701 goto out;
4702
David Sterbab3b4aa72011-04-21 01:20:15 +02004703 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004704
4705 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4706 if (IS_ERR(new_root)) {
4707 err = PTR_ERR(new_root);
4708 goto out;
4709 }
4710
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004711 *sub_root = new_root;
4712 location->objectid = btrfs_root_dirid(&new_root->root_item);
4713 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004714 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004715 err = 0;
4716out:
4717 btrfs_free_path(path);
4718 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004719}
4720
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004721static void inode_tree_add(struct inode *inode)
4722{
4723 struct btrfs_root *root = BTRFS_I(inode)->root;
4724 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004725 struct rb_node **p;
4726 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004727 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004728
Al Viro1d3382c2010-10-23 15:19:20 -04004729 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004730 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004731again:
4732 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004733 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004734 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004735 while (*p) {
4736 parent = *p;
4737 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4738
Li Zefan33345d012011-04-20 10:31:50 +08004739 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004740 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004741 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004742 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004743 else {
4744 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004745 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004746 rb_erase(parent, &root->inode_tree);
4747 RB_CLEAR_NODE(parent);
4748 spin_unlock(&root->inode_lock);
4749 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004750 }
4751 }
4752 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4753 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4754 spin_unlock(&root->inode_lock);
4755}
4756
4757static void inode_tree_del(struct inode *inode)
4758{
4759 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004760 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004761
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004762 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004763 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004764 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004765 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004766 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004767 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004768 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004769
Josef Bacik0af3d002010-06-21 14:48:16 -04004770 /*
4771 * Free space cache has inodes in the tree root, but the tree root has a
4772 * root_refs of 0, so this could end up dropping the tree root as a
4773 * snapshot, so we need the extra !root->fs_info->tree_root check to
4774 * make sure we don't drop it.
4775 */
4776 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4777 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004778 synchronize_srcu(&root->fs_info->subvol_srcu);
4779 spin_lock(&root->inode_lock);
4780 empty = RB_EMPTY_ROOT(&root->inode_tree);
4781 spin_unlock(&root->inode_lock);
4782 if (empty)
4783 btrfs_add_dead_root(root);
4784 }
4785}
4786
Jeff Mahoney143bede2012-03-01 14:56:26 +01004787void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004788{
4789 struct rb_node *node;
4790 struct rb_node *prev;
4791 struct btrfs_inode *entry;
4792 struct inode *inode;
4793 u64 objectid = 0;
4794
4795 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4796
4797 spin_lock(&root->inode_lock);
4798again:
4799 node = root->inode_tree.rb_node;
4800 prev = NULL;
4801 while (node) {
4802 prev = node;
4803 entry = rb_entry(node, struct btrfs_inode, rb_node);
4804
Li Zefan33345d012011-04-20 10:31:50 +08004805 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004806 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004807 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004808 node = node->rb_right;
4809 else
4810 break;
4811 }
4812 if (!node) {
4813 while (prev) {
4814 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004815 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004816 node = prev;
4817 break;
4818 }
4819 prev = rb_next(prev);
4820 }
4821 }
4822 while (node) {
4823 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004824 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004825 inode = igrab(&entry->vfs_inode);
4826 if (inode) {
4827 spin_unlock(&root->inode_lock);
4828 if (atomic_read(&inode->i_count) > 1)
4829 d_prune_aliases(inode);
4830 /*
Al Viro45321ac2010-06-07 13:43:19 -04004831 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004832 * the inode cache when its usage count
4833 * hits zero.
4834 */
4835 iput(inode);
4836 cond_resched();
4837 spin_lock(&root->inode_lock);
4838 goto again;
4839 }
4840
4841 if (cond_resched_lock(&root->inode_lock))
4842 goto again;
4843
4844 node = rb_next(node);
4845 }
4846 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004847}
4848
Chris Masone02119d2008-09-05 16:13:11 -04004849static int btrfs_init_locked_inode(struct inode *inode, void *p)
4850{
4851 struct btrfs_iget_args *args = p;
4852 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004853 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004854 return 0;
4855}
4856
4857static int btrfs_find_actor(struct inode *inode, void *opaque)
4858{
4859 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004860 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004861 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004862}
4863
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004864static struct inode *btrfs_iget_locked(struct super_block *s,
4865 u64 objectid,
4866 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004867{
4868 struct inode *inode;
4869 struct btrfs_iget_args args;
4870 args.ino = objectid;
4871 args.root = root;
4872
4873 inode = iget5_locked(s, objectid, btrfs_find_actor,
4874 btrfs_init_locked_inode,
4875 (void *)&args);
4876 return inode;
4877}
4878
Balaji Rao1a54ef82008-07-21 02:01:04 +05304879/* Get an inode object given its location and corresponding root.
4880 * Returns in *is_new if the inode was read from disk
4881 */
4882struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004883 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304884{
4885 struct inode *inode;
4886
4887 inode = btrfs_iget_locked(s, location->objectid, root);
4888 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004889 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304890
4891 if (inode->i_state & I_NEW) {
4892 BTRFS_I(inode)->root = root;
4893 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4894 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004895 if (!is_bad_inode(inode)) {
4896 inode_tree_add(inode);
4897 unlock_new_inode(inode);
4898 if (new)
4899 *new = 1;
4900 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004901 unlock_new_inode(inode);
4902 iput(inode);
4903 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004904 }
4905 }
4906
Balaji Rao1a54ef82008-07-21 02:01:04 +05304907 return inode;
4908}
4909
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004910static struct inode *new_simple_dir(struct super_block *s,
4911 struct btrfs_key *key,
4912 struct btrfs_root *root)
4913{
4914 struct inode *inode = new_inode(s);
4915
4916 if (!inode)
4917 return ERR_PTR(-ENOMEM);
4918
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004919 BTRFS_I(inode)->root = root;
4920 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004921 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004922
4923 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004924 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004925 inode->i_fop = &simple_dir_operations;
4926 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4927 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4928
4929 return inode;
4930}
4931
Chris Mason3de45862008-11-17 21:02:50 -05004932struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004933{
Chris Masond3977122009-01-05 21:25:51 -05004934 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004935 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004936 struct btrfs_root *sub_root = root;
4937 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004938 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004939 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004940
4941 if (dentry->d_name.len > BTRFS_NAME_LEN)
4942 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004943
Jeff Layton39e3c952012-11-28 11:30:53 -05004944 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04004945 if (ret < 0)
4946 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004947
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004948 if (location.objectid == 0)
4949 return NULL;
4950
4951 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004952 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004953 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004954 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004955
4956 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4957
Yan, Zheng76dda932009-09-21 16:00:26 -04004958 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004959 ret = fixup_tree_root_location(root, dir, dentry,
4960 &location, &sub_root);
4961 if (ret < 0) {
4962 if (ret != -ENOENT)
4963 inode = ERR_PTR(ret);
4964 else
4965 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4966 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004967 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004968 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004969 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4970
Julia Lawall34d19ba2011-01-24 19:55:19 +00004971 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004972 down_read(&root->fs_info->cleanup_work_sem);
4973 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004974 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004975 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04004976 if (ret) {
4977 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004978 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04004979 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00004980 }
4981
Chris Mason3de45862008-11-17 21:02:50 -05004982 return inode;
4983}
4984
Nick Pigginfe15ce42011-01-07 17:49:23 +11004985static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004986{
4987 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004988 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004989
Li Zefan848cce02012-02-21 17:04:28 +08004990 if (!inode && !IS_ROOT(dentry))
4991 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004992
Li Zefan848cce02012-02-21 17:04:28 +08004993 if (inode) {
4994 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004995 if (btrfs_root_refs(&root->root_item) == 0)
4996 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004997
4998 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4999 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005000 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005001 return 0;
5002}
5003
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005004static void btrfs_dentry_release(struct dentry *dentry)
5005{
5006 if (dentry->d_fsdata)
5007 kfree(dentry->d_fsdata);
5008}
5009
Chris Mason3de45862008-11-17 21:02:50 -05005010static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005011 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005012{
Josef Bacika66e7cc2011-09-18 10:34:03 -04005013 struct dentry *ret;
5014
5015 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005016 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005017}
5018
Miao Xie16cdcec2011-04-22 18:12:22 +08005019unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005020 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5021};
5022
Al Viro9cdda8d2013-05-22 16:48:09 -04005023static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005024{
Al Viro9cdda8d2013-05-22 16:48:09 -04005025 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005026 struct btrfs_root *root = BTRFS_I(inode)->root;
5027 struct btrfs_item *item;
5028 struct btrfs_dir_item *di;
5029 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005030 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005031 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005032 struct list_head ins_list;
5033 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005034 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005035 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005036 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005037 unsigned char d_type;
5038 int over = 0;
5039 u32 di_cur;
5040 u32 di_total;
5041 u32 di_len;
5042 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005043 char tmp_name[32];
5044 char *name_ptr;
5045 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005046 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005047
5048 /* FIXME, use a real flag for deciding about the key type */
5049 if (root->fs_info->tree_root == root)
5050 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005051
Al Viro9cdda8d2013-05-22 16:48:09 -04005052 if (!dir_emit_dots(file, ctx))
5053 return 0;
5054
David Woodhouse49593bf2008-08-17 17:08:36 +01005055 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005056 if (!path)
5057 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005058
Josef Bacik026fd312011-05-13 10:32:11 -04005059 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005060
Miao Xie16cdcec2011-04-22 18:12:22 +08005061 if (key_type == BTRFS_DIR_INDEX_KEY) {
5062 INIT_LIST_HEAD(&ins_list);
5063 INIT_LIST_HEAD(&del_list);
5064 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5065 }
5066
Chris Mason39279cc2007-06-12 06:35:45 -04005067 btrfs_set_key_type(&key, key_type);
Al Viro9cdda8d2013-05-22 16:48:09 -04005068 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005069 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005070
Chris Mason39279cc2007-06-12 06:35:45 -04005071 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5072 if (ret < 0)
5073 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005074
5075 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005076 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005077 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005078 if (slot >= btrfs_header_nritems(leaf)) {
5079 ret = btrfs_next_leaf(root, path);
5080 if (ret < 0)
5081 goto err;
5082 else if (ret > 0)
5083 break;
5084 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005085 }
Chris Mason3de45862008-11-17 21:02:50 -05005086
Chris Mason5f39d392007-10-15 16:14:19 -04005087 item = btrfs_item_nr(leaf, slot);
5088 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5089
5090 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005091 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005092 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005093 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005094 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005095 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005096 if (key_type == BTRFS_DIR_INDEX_KEY &&
5097 btrfs_should_delete_dir_index(&del_list,
5098 found_key.offset))
5099 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005100
Al Viro9cdda8d2013-05-22 16:48:09 -04005101 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005102 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005103
Chris Mason39279cc2007-06-12 06:35:45 -04005104 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5105 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005106 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005107
5108 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005109 struct btrfs_key location;
5110
Josef Bacik22a94d42011-03-16 16:47:17 -04005111 if (verify_dir_item(root, leaf, di))
5112 break;
5113
Chris Mason5f39d392007-10-15 16:14:19 -04005114 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005115 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005116 name_ptr = tmp_name;
5117 } else {
5118 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005119 if (!name_ptr) {
5120 ret = -ENOMEM;
5121 goto err;
5122 }
Chris Mason5f39d392007-10-15 16:14:19 -04005123 }
5124 read_extent_buffer(leaf, name_ptr,
5125 (unsigned long)(di + 1), name_len);
5126
5127 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5128 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005129
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005130
Chris Mason3de45862008-11-17 21:02:50 -05005131 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005132 * skip it.
5133 *
5134 * In contrast to old kernels, we insert the snapshot's
5135 * dir item and dir index after it has been created, so
5136 * we won't find a reference to our own snapshot. We
5137 * still keep the following code for backward
5138 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005139 */
5140 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5141 location.objectid == root->root_key.objectid) {
5142 over = 0;
5143 goto skip;
5144 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005145 over = !dir_emit(ctx, name_ptr, name_len,
5146 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005147
Chris Mason3de45862008-11-17 21:02:50 -05005148skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005149 if (name_ptr != tmp_name)
5150 kfree(name_ptr);
5151
Chris Mason39279cc2007-06-12 06:35:45 -04005152 if (over)
5153 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005154 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005155 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005156 di_cur += di_len;
5157 di = (struct btrfs_dir_item *)((char *)di + di_len);
5158 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005159next:
5160 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005161 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005162
Miao Xie16cdcec2011-04-22 18:12:22 +08005163 if (key_type == BTRFS_DIR_INDEX_KEY) {
5164 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005165 ctx->pos++;
5166 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005167 if (ret)
5168 goto nopos;
5169 }
5170
David Woodhouse49593bf2008-08-17 17:08:36 +01005171 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005172 ctx->pos++;
5173
5174 /*
5175 * Stop new entries from being returned after we return the last
5176 * entry.
5177 *
5178 * New directory entries are assigned a strictly increasing
5179 * offset. This means that new entries created during readdir
5180 * are *guaranteed* to be seen in the future by that readdir.
5181 * This has broken buggy programs which operate on names as
5182 * they're returned by readdir. Until we re-use freed offsets
5183 * we have this hack to stop new entries from being returned
5184 * under the assumption that they'll never reach this huge
5185 * offset.
5186 *
5187 * This is being careful not to overflow 32bit loff_t unless the
5188 * last entry requires it because doing so has broken 32bit apps
5189 * in the past.
5190 */
5191 if (key_type == BTRFS_DIR_INDEX_KEY) {
5192 if (ctx->pos >= INT_MAX)
5193 ctx->pos = LLONG_MAX;
5194 else
5195 ctx->pos = INT_MAX;
5196 }
Chris Mason39279cc2007-06-12 06:35:45 -04005197nopos:
5198 ret = 0;
5199err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005200 if (key_type == BTRFS_DIR_INDEX_KEY)
5201 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005202 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005203 return ret;
5204}
5205
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005206int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005207{
5208 struct btrfs_root *root = BTRFS_I(inode)->root;
5209 struct btrfs_trans_handle *trans;
5210 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005211 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005212
Josef Bacik72ac3c02012-05-23 14:13:11 -04005213 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005214 return 0;
5215
Liu Bo83eea1f2012-07-10 05:28:39 -06005216 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005217 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005218
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005219 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005220 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005221 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005222 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005223 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005224 if (IS_ERR(trans))
5225 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005226 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005227 }
5228 return ret;
5229}
5230
5231/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005232 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005233 * inode changes. But, it is most likely to find the inode in cache.
5234 * FIXME, needs more benchmarking...there are no reasons other than performance
5235 * to keep or drop this code.
5236 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005237static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005238{
5239 struct btrfs_root *root = BTRFS_I(inode)->root;
5240 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005241 int ret;
5242
Josef Bacik72ac3c02012-05-23 14:13:11 -04005243 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005244 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005245
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005246 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005247 if (IS_ERR(trans))
5248 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005249
5250 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005251 if (ret && ret == -ENOSPC) {
5252 /* whoops, lets try again with the full transaction */
5253 btrfs_end_transaction(trans, root);
5254 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005255 if (IS_ERR(trans))
5256 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005257
Chris Mason94b60442010-05-26 11:02:00 -04005258 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005259 }
Chris Mason39279cc2007-06-12 06:35:45 -04005260 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005261 if (BTRFS_I(inode)->delayed_node)
5262 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005263
5264 return ret;
5265}
5266
5267/*
5268 * This is a copy of file_update_time. We need this so we can return error on
5269 * ENOSPC for updating the inode in the case of file write and mmap writes.
5270 */
Josef Bacike41f9412012-03-26 09:46:47 -04005271static int btrfs_update_time(struct inode *inode, struct timespec *now,
5272 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005273{
Alexander Block2bc5565282012-06-15 09:49:33 +02005274 struct btrfs_root *root = BTRFS_I(inode)->root;
5275
5276 if (btrfs_root_readonly(root))
5277 return -EROFS;
5278
Josef Bacike41f9412012-03-26 09:46:47 -04005279 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005280 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005281 if (flags & S_CTIME)
5282 inode->i_ctime = *now;
5283 if (flags & S_MTIME)
5284 inode->i_mtime = *now;
5285 if (flags & S_ATIME)
5286 inode->i_atime = *now;
5287 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005288}
5289
Chris Masond352ac62008-09-29 15:18:18 -04005290/*
5291 * find the highest existing sequence number in a directory
5292 * and then set the in-memory index_cnt variable to reflect
5293 * free sequence numbers
5294 */
Josef Bacikaec74772008-07-24 12:12:38 -04005295static int btrfs_set_inode_index_count(struct inode *inode)
5296{
5297 struct btrfs_root *root = BTRFS_I(inode)->root;
5298 struct btrfs_key key, found_key;
5299 struct btrfs_path *path;
5300 struct extent_buffer *leaf;
5301 int ret;
5302
Li Zefan33345d012011-04-20 10:31:50 +08005303 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005304 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5305 key.offset = (u64)-1;
5306
5307 path = btrfs_alloc_path();
5308 if (!path)
5309 return -ENOMEM;
5310
5311 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5312 if (ret < 0)
5313 goto out;
5314 /* FIXME: we should be able to handle this */
5315 if (ret == 0)
5316 goto out;
5317 ret = 0;
5318
5319 /*
5320 * MAGIC NUMBER EXPLANATION:
5321 * since we search a directory based on f_pos we have to start at 2
5322 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5323 * else has to start at 2
5324 */
5325 if (path->slots[0] == 0) {
5326 BTRFS_I(inode)->index_cnt = 2;
5327 goto out;
5328 }
5329
5330 path->slots[0]--;
5331
5332 leaf = path->nodes[0];
5333 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5334
Li Zefan33345d012011-04-20 10:31:50 +08005335 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005336 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5337 BTRFS_I(inode)->index_cnt = 2;
5338 goto out;
5339 }
5340
5341 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5342out:
5343 btrfs_free_path(path);
5344 return ret;
5345}
5346
Chris Masond352ac62008-09-29 15:18:18 -04005347/*
5348 * helper to find a free sequence number in a given directory. This current
5349 * code is very simple, later versions will do smarter things in the btree
5350 */
Chris Mason3de45862008-11-17 21:02:50 -05005351int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005352{
5353 int ret = 0;
5354
5355 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005356 ret = btrfs_inode_delayed_dir_index_count(dir);
5357 if (ret) {
5358 ret = btrfs_set_inode_index_count(dir);
5359 if (ret)
5360 return ret;
5361 }
Josef Bacikaec74772008-07-24 12:12:38 -04005362 }
5363
Chris Mason00e4e6b2008-08-05 11:18:09 -04005364 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005365 BTRFS_I(dir)->index_cnt++;
5366
5367 return ret;
5368}
5369
Chris Mason39279cc2007-06-12 06:35:45 -04005370static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5371 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005372 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005373 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005374 u64 ref_objectid, u64 objectid,
5375 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005376{
5377 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005378 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005379 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005380 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005381 struct btrfs_inode_ref *ref;
5382 struct btrfs_key key[2];
5383 u32 sizes[2];
5384 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005385 int ret;
5386 int owner;
5387
Chris Mason5f39d392007-10-15 16:14:19 -04005388 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005389 if (!path)
5390 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005391
Chris Mason39279cc2007-06-12 06:35:45 -04005392 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005393 if (!inode) {
5394 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005395 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005396 }
Chris Mason39279cc2007-06-12 06:35:45 -04005397
Li Zefan581bb052011-04-20 10:06:11 +08005398 /*
5399 * we have to initialize this early, so we can reclaim the inode
5400 * number if we fail afterwards in this function.
5401 */
5402 inode->i_ino = objectid;
5403
Josef Bacikaec74772008-07-24 12:12:38 -04005404 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005405 trace_btrfs_inode_request(dir);
5406
Chris Mason3de45862008-11-17 21:02:50 -05005407 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005408 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005409 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005410 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005411 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005412 }
Josef Bacikaec74772008-07-24 12:12:38 -04005413 }
5414 /*
5415 * index_cnt is ignored for everything but a dir,
5416 * btrfs_get_inode_index_count has an explanation for the magic
5417 * number
5418 */
5419 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005420 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005421 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005422 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005423
Josef Bacik5dc562c2012-08-17 13:14:17 -04005424 /*
5425 * We could have gotten an inode number from somebody who was fsynced
5426 * and then removed in this same transaction, so let's just set full
5427 * sync since it will be a full sync anyway and this will blow away the
5428 * old info in the log.
5429 */
5430 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5431
Al Viro569254b2011-07-24 17:08:40 -04005432 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005433 owner = 0;
5434 else
5435 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005436
5437 key[0].objectid = objectid;
5438 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5439 key[0].offset = 0;
5440
Mark Fashehf1863732012-08-08 11:32:27 -07005441 /*
5442 * Start new inodes with an inode_ref. This is slightly more
5443 * efficient for small numbers of hard links since they will
5444 * be packed into one item. Extended refs will kick in if we
5445 * add more hard links than can fit in the ref item.
5446 */
Chris Mason9c583092008-01-29 15:15:18 -05005447 key[1].objectid = objectid;
5448 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5449 key[1].offset = ref_objectid;
5450
5451 sizes[0] = sizeof(struct btrfs_inode_item);
5452 sizes[1] = name_len + sizeof(*ref);
5453
Chris Masonb9473432009-03-13 11:00:37 -04005454 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005455 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5456 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005457 goto fail;
5458
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005459 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005460 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005461 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005462 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5463 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005464 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5465 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005466 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005467
5468 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5469 struct btrfs_inode_ref);
5470 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005471 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005472 ptr = (unsigned long)(ref + 1);
5473 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5474
Chris Mason5f39d392007-10-15 16:14:19 -04005475 btrfs_mark_buffer_dirty(path->nodes[0]);
5476 btrfs_free_path(path);
5477
Chris Mason39279cc2007-06-12 06:35:45 -04005478 location = &BTRFS_I(inode)->location;
5479 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005480 location->offset = 0;
5481 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5482
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005483 btrfs_inherit_iflags(inode, dir);
5484
Al Viro569254b2011-07-24 17:08:40 -04005485 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005486 if (btrfs_test_opt(root, NODATASUM))
5487 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005488 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005489 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5490 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005491 }
5492
Chris Mason39279cc2007-06-12 06:35:45 -04005493 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005494 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005495
5496 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005497 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005498
Alexander Block8ea05e32012-07-25 17:35:53 +02005499 btrfs_update_root_times(trans, root);
5500
Chris Mason39279cc2007-06-12 06:35:45 -04005501 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005502fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005503 if (dir)
5504 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005505 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005506 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005507 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005508}
5509
5510static inline u8 btrfs_inode_type(struct inode *inode)
5511{
5512 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5513}
5514
Chris Masond352ac62008-09-29 15:18:18 -04005515/*
5516 * utility function to add 'inode' into 'parent_inode' with
5517 * a give name and a given sequence number.
5518 * if 'add_backref' is true, also insert a backref from the
5519 * inode to the parent directory.
5520 */
Chris Masone02119d2008-09-05 16:13:11 -04005521int btrfs_add_link(struct btrfs_trans_handle *trans,
5522 struct inode *parent_inode, struct inode *inode,
5523 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005524{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005525 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005526 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005527 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005528 u64 ino = btrfs_ino(inode);
5529 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005530
Li Zefan33345d012011-04-20 10:31:50 +08005531 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005532 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5533 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005534 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005535 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5536 key.offset = 0;
5537 }
Chris Mason39279cc2007-06-12 06:35:45 -04005538
Li Zefan33345d012011-04-20 10:31:50 +08005539 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005540 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5541 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005542 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005543 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005544 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5545 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005546 }
5547
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005548 /* Nothing to clean up yet */
5549 if (ret)
5550 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005551
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005552 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5553 parent_inode, &key,
5554 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005555 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005556 goto fail_dir_item;
5557 else if (ret) {
5558 btrfs_abort_transaction(trans, root, ret);
5559 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005560 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005561
5562 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5563 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005564 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005565 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5566 ret = btrfs_update_inode(trans, root, parent_inode);
5567 if (ret)
5568 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005569 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005570
5571fail_dir_item:
5572 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5573 u64 local_index;
5574 int err;
5575 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5576 key.objectid, root->root_key.objectid,
5577 parent_ino, &local_index, name, name_len);
5578
5579 } else if (add_backref) {
5580 u64 local_index;
5581 int err;
5582
5583 err = btrfs_del_inode_ref(trans, root, name, name_len,
5584 ino, parent_ino, &local_index);
5585 }
5586 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005587}
5588
5589static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005590 struct inode *dir, struct dentry *dentry,
5591 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005592{
Josef Bacika1b075d2010-11-19 20:36:11 +00005593 int err = btrfs_add_link(trans, dir, inode,
5594 dentry->d_name.name, dentry->d_name.len,
5595 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005596 if (err > 0)
5597 err = -EEXIST;
5598 return err;
5599}
5600
Josef Bacik618e21d2007-07-11 10:18:17 -04005601static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005602 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005603{
5604 struct btrfs_trans_handle *trans;
5605 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005606 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005607 int err;
5608 int drop_inode = 0;
5609 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005610 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005611
5612 if (!new_valid_dev(rdev))
5613 return -EINVAL;
5614
Josef Bacik9ed74f22009-09-11 16:12:44 -04005615 /*
5616 * 2 for inode item and ref
5617 * 2 for dir items
5618 * 1 for xattr if selinux is on
5619 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005620 trans = btrfs_start_transaction(root, 5);
5621 if (IS_ERR(trans))
5622 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005623
Li Zefan581bb052011-04-20 10:06:11 +08005624 err = btrfs_find_free_ino(root, &objectid);
5625 if (err)
5626 goto out_unlock;
5627
Josef Bacikaec74772008-07-24 12:12:38 -04005628 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005629 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005630 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005631 if (IS_ERR(inode)) {
5632 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005633 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005634 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005635
Eric Paris2a7dba32011-02-01 11:05:39 -05005636 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005637 if (err) {
5638 drop_inode = 1;
5639 goto out_unlock;
5640 }
5641
Casey Schauflerad19db72011-12-15 10:09:07 -05005642 /*
5643 * If the active LSM wants to access the inode during
5644 * d_instantiate it needs these. Smack checks to see
5645 * if the filesystem supports xattrs by looking at the
5646 * ops vector.
5647 */
5648
5649 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005650 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005651 if (err)
5652 drop_inode = 1;
5653 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005654 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005655 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005656 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005657 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005658out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005659 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005660 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005661 if (drop_inode) {
5662 inode_dec_link_count(inode);
5663 iput(inode);
5664 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005665 return err;
5666}
5667
Chris Mason39279cc2007-06-12 06:35:45 -04005668static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005669 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005670{
5671 struct btrfs_trans_handle *trans;
5672 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005673 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005674 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005675 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005676 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005677 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005678
Josef Bacik9ed74f22009-09-11 16:12:44 -04005679 /*
5680 * 2 for inode item and ref
5681 * 2 for dir items
5682 * 1 for xattr if selinux is on
5683 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005684 trans = btrfs_start_transaction(root, 5);
5685 if (IS_ERR(trans))
5686 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005687
Li Zefan581bb052011-04-20 10:06:11 +08005688 err = btrfs_find_free_ino(root, &objectid);
5689 if (err)
5690 goto out_unlock;
5691
Josef Bacikaec74772008-07-24 12:12:38 -04005692 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005693 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005694 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005695 if (IS_ERR(inode)) {
5696 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005697 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005698 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005699 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005700
Eric Paris2a7dba32011-02-01 11:05:39 -05005701 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005702 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005703 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005704
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005705 err = btrfs_update_inode(trans, root, inode);
5706 if (err)
5707 goto out_unlock;
5708
Casey Schauflerad19db72011-12-15 10:09:07 -05005709 /*
5710 * If the active LSM wants to access the inode during
5711 * d_instantiate it needs these. Smack checks to see
5712 * if the filesystem supports xattrs by looking at the
5713 * ops vector.
5714 */
5715 inode->i_fop = &btrfs_file_operations;
5716 inode->i_op = &btrfs_file_inode_operations;
5717
Josef Bacika1b075d2010-11-19 20:36:11 +00005718 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005719 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005720 goto out_unlock;
5721
5722 inode->i_mapping->a_ops = &btrfs_aops;
5723 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5724 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5725 d_instantiate(dentry, inode);
5726
Chris Mason39279cc2007-06-12 06:35:45 -04005727out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005728 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005729 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005730 inode_dec_link_count(inode);
5731 iput(inode);
5732 }
Liu Bob53d3f52012-11-14 14:34:34 +00005733 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005734 return err;
5735}
5736
5737static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5738 struct dentry *dentry)
5739{
5740 struct btrfs_trans_handle *trans;
5741 struct btrfs_root *root = BTRFS_I(dir)->root;
5742 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005743 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005744 int err;
5745 int drop_inode = 0;
5746
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005747 /* do not allow sys_link's with other subvols of the same device */
5748 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005749 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005750
Mark Fashehf1863732012-08-08 11:32:27 -07005751 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005752 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005753
Chris Mason3de45862008-11-17 21:02:50 -05005754 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005755 if (err)
5756 goto fail;
5757
Yan, Zhenga22285a2010-05-16 10:48:46 -04005758 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005759 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005760 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005761 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005762 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005763 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005764 if (IS_ERR(trans)) {
5765 err = PTR_ERR(trans);
5766 goto fail;
5767 }
Chris Mason5f39d392007-10-15 16:14:19 -04005768
Miao Xie31534952011-04-13 13:19:21 +08005769 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005770 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005771 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6e2010-10-23 11:11:40 -04005772 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005773 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005774
Josef Bacika1b075d2010-11-19 20:36:11 +00005775 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005776
Yan, Zhenga5719522009-09-24 09:17:31 -04005777 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005778 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005779 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005780 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005781 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005782 if (err)
5783 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005784 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005785 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005786 }
Chris Mason39279cc2007-06-12 06:35:45 -04005787
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005788 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005789fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005790 if (drop_inode) {
5791 inode_dec_link_count(inode);
5792 iput(inode);
5793 }
Liu Bob53d3f52012-11-14 14:34:34 +00005794 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005795 return err;
5796}
5797
Al Viro18bb1db2011-07-26 01:41:39 -04005798static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005799{
Chris Masonb9d86662008-05-02 16:13:49 -04005800 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005801 struct btrfs_trans_handle *trans;
5802 struct btrfs_root *root = BTRFS_I(dir)->root;
5803 int err = 0;
5804 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005805 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005806 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005807
Josef Bacik9ed74f22009-09-11 16:12:44 -04005808 /*
5809 * 2 items for inode and ref
5810 * 2 items for dir items
5811 * 1 for xattr if selinux is on
5812 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005813 trans = btrfs_start_transaction(root, 5);
5814 if (IS_ERR(trans))
5815 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005816
Li Zefan581bb052011-04-20 10:06:11 +08005817 err = btrfs_find_free_ino(root, &objectid);
5818 if (err)
5819 goto out_fail;
5820
Josef Bacikaec74772008-07-24 12:12:38 -04005821 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005822 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005823 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005824 if (IS_ERR(inode)) {
5825 err = PTR_ERR(inode);
5826 goto out_fail;
5827 }
Chris Mason5f39d392007-10-15 16:14:19 -04005828
Chris Mason39279cc2007-06-12 06:35:45 -04005829 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005830
Eric Paris2a7dba32011-02-01 11:05:39 -05005831 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005832 if (err)
5833 goto out_fail;
5834
Chris Mason39279cc2007-06-12 06:35:45 -04005835 inode->i_op = &btrfs_dir_inode_operations;
5836 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005837
Chris Masondbe674a2008-07-17 12:54:05 -04005838 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005839 err = btrfs_update_inode(trans, root, inode);
5840 if (err)
5841 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005842
Josef Bacika1b075d2010-11-19 20:36:11 +00005843 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5844 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005845 if (err)
5846 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005847
Chris Mason39279cc2007-06-12 06:35:45 -04005848 d_instantiate(dentry, inode);
5849 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005850
5851out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005852 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005853 if (drop_on_err)
5854 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005855 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005856 return err;
5857}
5858
Chris Masond352ac62008-09-29 15:18:18 -04005859/* helper for btfs_get_extent. Given an existing extent in the tree,
5860 * and an extent that you want to insert, deal with overlap and insert
5861 * the new extent into the tree.
5862 */
Chris Mason3b951512008-04-17 11:29:12 -04005863static int merge_extent_mapping(struct extent_map_tree *em_tree,
5864 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005865 struct extent_map *em,
5866 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005867{
5868 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005869
Chris Masone6dcd2d2008-07-17 12:53:50 -04005870 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5871 start_diff = map_start - em->start;
5872 em->start = map_start;
5873 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005874 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5875 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005876 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005877 em->block_len -= start_diff;
5878 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005879 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005880}
5881
Chris Masonc8b97812008-10-29 14:49:59 -04005882static noinline int uncompress_inline(struct btrfs_path *path,
5883 struct inode *inode, struct page *page,
5884 size_t pg_offset, u64 extent_offset,
5885 struct btrfs_file_extent_item *item)
5886{
5887 int ret;
5888 struct extent_buffer *leaf = path->nodes[0];
5889 char *tmp;
5890 size_t max_size;
5891 unsigned long inline_size;
5892 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005893 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005894
5895 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005896 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005897 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5898 inline_size = btrfs_file_extent_inline_item_len(leaf,
5899 btrfs_item_nr(leaf, path->slots[0]));
5900 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005901 if (!tmp)
5902 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005903 ptr = btrfs_file_extent_inline_start(item);
5904
5905 read_extent_buffer(leaf, tmp, ptr, inline_size);
5906
Chris Mason5b050f02008-11-11 09:34:41 -05005907 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005908 ret = btrfs_decompress(compress_type, tmp, page,
5909 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005910 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005911 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005912 unsigned long copy_size = min_t(u64,
5913 PAGE_CACHE_SIZE - pg_offset,
5914 max_size - extent_offset);
5915 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005916 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005917 }
5918 kfree(tmp);
5919 return 0;
5920}
5921
Chris Masond352ac62008-09-29 15:18:18 -04005922/*
5923 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005924 * the ugly parts come from merging extents from the disk with the in-ram
5925 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005926 * where the in-ram extents might be locked pending data=ordered completion.
5927 *
5928 * This also copies inline extents directly into the page.
5929 */
Chris Masond3977122009-01-05 21:25:51 -05005930
Chris Masona52d9a82007-08-27 16:49:44 -04005931struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005932 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005933 int create)
5934{
5935 int ret;
5936 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005937 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005938 u64 extent_start = 0;
5939 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005940 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005941 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005942 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005943 struct btrfs_root *root = BTRFS_I(inode)->root;
5944 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005945 struct extent_buffer *leaf;
5946 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005947 struct extent_map *em = NULL;
5948 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005949 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005950 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005951 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005952
Chris Masona52d9a82007-08-27 16:49:44 -04005953again:
Chris Mason890871b2009-09-02 16:24:52 -04005954 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005955 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005956 if (em)
5957 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005958 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005959
Chris Masona52d9a82007-08-27 16:49:44 -04005960 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005961 if (em->start > start || em->start + em->len <= start)
5962 free_extent_map(em);
5963 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005964 free_extent_map(em);
5965 else
5966 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005967 }
David Sterba172ddd62011-04-21 00:48:27 +02005968 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005969 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005970 err = -ENOMEM;
5971 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005972 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005973 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005974 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005975 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005976 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005977 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005978
5979 if (!path) {
5980 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005981 if (!path) {
5982 err = -ENOMEM;
5983 goto out;
5984 }
5985 /*
5986 * Chances are we'll be called again, so go ahead and do
5987 * readahead
5988 */
5989 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005990 }
5991
Chris Mason179e29e2007-11-01 11:28:41 -04005992 ret = btrfs_lookup_file_extent(trans, root, path,
5993 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005994 if (ret < 0) {
5995 err = ret;
5996 goto out;
5997 }
5998
5999 if (ret != 0) {
6000 if (path->slots[0] == 0)
6001 goto not_found;
6002 path->slots[0]--;
6003 }
6004
Chris Mason5f39d392007-10-15 16:14:19 -04006005 leaf = path->nodes[0];
6006 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006007 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006008 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006009 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6010 found_type = btrfs_key_type(&found_key);
6011 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006012 found_type != BTRFS_EXTENT_DATA_KEY) {
6013 goto not_found;
6014 }
6015
Chris Mason5f39d392007-10-15 16:14:19 -04006016 found_type = btrfs_file_extent_type(leaf, item);
6017 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006018 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006019 if (found_type == BTRFS_FILE_EXTENT_REG ||
6020 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006021 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006022 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006023 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6024 size_t size;
6025 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006026 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006027 }
6028
6029 if (start >= extent_end) {
6030 path->slots[0]++;
6031 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6032 ret = btrfs_next_leaf(root, path);
6033 if (ret < 0) {
6034 err = ret;
6035 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006036 }
Yan Zheng9036c102008-10-30 14:19:41 -04006037 if (ret > 0)
6038 goto not_found;
6039 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006040 }
Yan Zheng9036c102008-10-30 14:19:41 -04006041 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6042 if (found_key.objectid != objectid ||
6043 found_key.type != BTRFS_EXTENT_DATA_KEY)
6044 goto not_found;
6045 if (start + len <= found_key.offset)
6046 goto not_found;
6047 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006048 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006049 em->len = found_key.offset - start;
6050 goto not_found_em;
6051 }
6052
Josef Bacikcc95bef2013-04-04 14:31:27 -04006053 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006054 if (found_type == BTRFS_FILE_EXTENT_REG ||
6055 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006056 em->start = extent_start;
6057 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006058 em->orig_start = extent_start -
6059 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006060 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6061 item);
Chris Masondb945352007-10-15 16:15:53 -04006062 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6063 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006064 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006065 goto insert;
6066 }
Li Zefan261507a02010-12-17 14:21:50 +08006067 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006068 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006069 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006070 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006071 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006072 } else {
6073 bytenr += btrfs_file_extent_offset(leaf, item);
6074 em->block_start = bytenr;
6075 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006076 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6077 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006078 }
Chris Masona52d9a82007-08-27 16:49:44 -04006079 goto insert;
6080 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006081 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006082 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006083 size_t size;
6084 size_t extent_offset;
6085 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006086
Chris Masona52d9a82007-08-27 16:49:44 -04006087 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006088 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006089 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006090 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006091 goto out;
6092 }
6093
Yan Zheng9036c102008-10-30 14:19:41 -04006094 size = btrfs_file_extent_inline_len(leaf, item);
6095 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006096 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006097 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006098 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006099 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006100 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006101 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006102 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006103 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006104 em->compress_type = compress_type;
6105 }
Yan689f9342007-10-29 11:41:07 -04006106 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006107 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006108 if (btrfs_file_extent_compression(leaf, item) !=
6109 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006110 ret = uncompress_inline(path, inode, page,
6111 pg_offset,
6112 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006113 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006114 } else {
6115 map = kmap(page);
6116 read_extent_buffer(leaf, map + pg_offset, ptr,
6117 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006118 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6119 memset(map + pg_offset + copy_size, 0,
6120 PAGE_CACHE_SIZE - pg_offset -
6121 copy_size);
6122 }
Chris Masonc8b97812008-10-29 14:49:59 -04006123 kunmap(page);
6124 }
Chris Mason179e29e2007-11-01 11:28:41 -04006125 flush_dcache_page(page);
6126 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006127 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006128 if (!trans) {
6129 kunmap(page);
6130 free_extent_map(em);
6131 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006132
David Sterbab3b4aa72011-04-21 01:20:15 +02006133 btrfs_release_path(path);
Josef Bacik7a7eaa402011-04-13 12:54:33 -04006134 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006135
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006136 if (IS_ERR(trans))
6137 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006138 goto again;
6139 }
Chris Masonc8b97812008-10-29 14:49:59 -04006140 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006141 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006142 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006143 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006144 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006145 }
Chris Masond1310b22008-01-24 16:13:08 -05006146 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006147 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006148 goto insert;
6149 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006150 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006151 }
6152not_found:
6153 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006154 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006155 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006156not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006157 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006158 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006159insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006160 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006161 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006162 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6163 (unsigned long long)em->start,
6164 (unsigned long long)em->len,
6165 (unsigned long long)start,
6166 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006167 err = -EIO;
6168 goto out;
6169 }
Chris Masond1310b22008-01-24 16:13:08 -05006170
6171 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006172 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006173 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006174 /* it is possible that someone inserted the extent into the tree
6175 * while we had the lock dropped. It is also possible that
6176 * an overlapping map exists in the tree
6177 */
Chris Masona52d9a82007-08-27 16:49:44 -04006178 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006179 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006180
6181 ret = 0;
6182
Chris Mason3b951512008-04-17 11:29:12 -04006183 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006184 if (existing && (existing->start > start ||
6185 existing->start + existing->len <= start)) {
6186 free_extent_map(existing);
6187 existing = NULL;
6188 }
Chris Mason3b951512008-04-17 11:29:12 -04006189 if (!existing) {
6190 existing = lookup_extent_mapping(em_tree, em->start,
6191 em->len);
6192 if (existing) {
6193 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006194 em, start,
6195 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006196 free_extent_map(existing);
6197 if (err) {
6198 free_extent_map(em);
6199 em = NULL;
6200 }
6201 } else {
6202 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006203 free_extent_map(em);
6204 em = NULL;
6205 }
6206 } else {
6207 free_extent_map(em);
6208 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006209 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006210 }
Chris Masona52d9a82007-08-27 16:49:44 -04006211 }
Chris Mason890871b2009-09-02 16:24:52 -04006212 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006213out:
liubo1abe9b82011-03-24 11:18:59 +00006214
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006215 if (em)
6216 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006217
Chris Masonf4219502008-07-22 11:18:09 -04006218 if (path)
6219 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006220 if (trans) {
6221 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006222 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006223 err = ret;
6224 }
Chris Masona52d9a82007-08-27 16:49:44 -04006225 if (err) {
6226 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006227 return ERR_PTR(err);
6228 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006229 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006230 return em;
6231}
6232
Chris Masonec29ed52011-02-23 16:23:20 -05006233struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6234 size_t pg_offset, u64 start, u64 len,
6235 int create)
6236{
6237 struct extent_map *em;
6238 struct extent_map *hole_em = NULL;
6239 u64 range_start = start;
6240 u64 end;
6241 u64 found;
6242 u64 found_end;
6243 int err = 0;
6244
6245 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6246 if (IS_ERR(em))
6247 return em;
6248 if (em) {
6249 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006250 * if our em maps to
6251 * - a hole or
6252 * - a pre-alloc extent,
6253 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006254 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006255 if (em->block_start != EXTENT_MAP_HOLE &&
6256 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006257 return em;
6258 else
6259 hole_em = em;
6260 }
6261
6262 /* check to see if we've wrapped (len == -1 or similar) */
6263 end = start + len;
6264 if (end < start)
6265 end = (u64)-1;
6266 else
6267 end -= 1;
6268
6269 em = NULL;
6270
6271 /* ok, we didn't find anything, lets look for delalloc */
6272 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6273 end, len, EXTENT_DELALLOC, 1);
6274 found_end = range_start + found;
6275 if (found_end < range_start)
6276 found_end = (u64)-1;
6277
6278 /*
6279 * we didn't find anything useful, return
6280 * the original results from get_extent()
6281 */
6282 if (range_start > end || found_end <= start) {
6283 em = hole_em;
6284 hole_em = NULL;
6285 goto out;
6286 }
6287
6288 /* adjust the range_start to make sure it doesn't
6289 * go backwards from the start they passed in
6290 */
6291 range_start = max(start,range_start);
6292 found = found_end - range_start;
6293
6294 if (found > 0) {
6295 u64 hole_start = start;
6296 u64 hole_len = len;
6297
David Sterba172ddd62011-04-21 00:48:27 +02006298 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006299 if (!em) {
6300 err = -ENOMEM;
6301 goto out;
6302 }
6303 /*
6304 * when btrfs_get_extent can't find anything it
6305 * returns one huge hole
6306 *
6307 * make sure what it found really fits our range, and
6308 * adjust to make sure it is based on the start from
6309 * the caller
6310 */
6311 if (hole_em) {
6312 u64 calc_end = extent_map_end(hole_em);
6313
6314 if (calc_end <= start || (hole_em->start > end)) {
6315 free_extent_map(hole_em);
6316 hole_em = NULL;
6317 } else {
6318 hole_start = max(hole_em->start, start);
6319 hole_len = calc_end - hole_start;
6320 }
6321 }
6322 em->bdev = NULL;
6323 if (hole_em && range_start > hole_start) {
6324 /* our hole starts before our delalloc, so we
6325 * have to return just the parts of the hole
6326 * that go until the delalloc starts
6327 */
6328 em->len = min(hole_len,
6329 range_start - hole_start);
6330 em->start = hole_start;
6331 em->orig_start = hole_start;
6332 /*
6333 * don't adjust block start at all,
6334 * it is fixed at EXTENT_MAP_HOLE
6335 */
6336 em->block_start = hole_em->block_start;
6337 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006338 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6339 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006340 } else {
6341 em->start = range_start;
6342 em->len = found;
6343 em->orig_start = range_start;
6344 em->block_start = EXTENT_MAP_DELALLOC;
6345 em->block_len = found;
6346 }
6347 } else if (hole_em) {
6348 return hole_em;
6349 }
6350out:
6351
6352 free_extent_map(hole_em);
6353 if (err) {
6354 free_extent_map(em);
6355 return ERR_PTR(err);
6356 }
6357 return em;
6358}
6359
Josef Bacik4b46fce2010-05-23 11:00:55 -04006360static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6361 u64 start, u64 len)
6362{
6363 struct btrfs_root *root = BTRFS_I(inode)->root;
6364 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006365 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006366 struct btrfs_key ins;
6367 u64 alloc_hint;
6368 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006369
Josef Bacik7a7eaa402011-04-13 12:54:33 -04006370 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006371 if (IS_ERR(trans))
6372 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006373
6374 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6375
6376 alloc_hint = get_extent_allocation_hint(inode, start, len);
6377 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006378 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006379 if (ret) {
6380 em = ERR_PTR(ret);
6381 goto out;
6382 }
6383
Josef Bacik70c8a912012-10-11 16:54:30 -04006384 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006385 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik70c8a912012-10-11 16:54:30 -04006386 if (IS_ERR(em))
6387 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006388
6389 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6390 ins.offset, ins.offset, 0);
6391 if (ret) {
6392 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6393 em = ERR_PTR(ret);
6394 }
6395out:
6396 btrfs_end_transaction(trans, root);
6397 return em;
6398}
6399
Chris Mason46bfbb52010-05-26 11:04:10 -04006400/*
6401 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6402 * block must be cow'd
6403 */
Josef Bacik7ee9e442013-06-21 16:37:03 -04006404noinline int can_nocow_extent(struct btrfs_trans_handle *trans,
6405 struct inode *inode, u64 offset, u64 *len,
6406 u64 *orig_start, u64 *orig_block_len,
6407 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006408{
6409 struct btrfs_path *path;
6410 int ret;
6411 struct extent_buffer *leaf;
6412 struct btrfs_root *root = BTRFS_I(inode)->root;
6413 struct btrfs_file_extent_item *fi;
6414 struct btrfs_key key;
6415 u64 disk_bytenr;
6416 u64 backref_offset;
6417 u64 extent_end;
6418 u64 num_bytes;
6419 int slot;
6420 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006421 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Chris Mason46bfbb52010-05-26 11:04:10 -04006422 path = btrfs_alloc_path();
6423 if (!path)
6424 return -ENOMEM;
6425
Li Zefan33345d012011-04-20 10:31:50 +08006426 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006427 offset, 0);
6428 if (ret < 0)
6429 goto out;
6430
6431 slot = path->slots[0];
6432 if (ret == 1) {
6433 if (slot == 0) {
6434 /* can't find the item, must cow */
6435 ret = 0;
6436 goto out;
6437 }
6438 slot--;
6439 }
6440 ret = 0;
6441 leaf = path->nodes[0];
6442 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006443 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006444 key.type != BTRFS_EXTENT_DATA_KEY) {
6445 /* not our file or wrong item type, must cow */
6446 goto out;
6447 }
6448
6449 if (key.offset > offset) {
6450 /* Wrong offset, must cow */
6451 goto out;
6452 }
6453
6454 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6455 found_type = btrfs_file_extent_type(leaf, fi);
6456 if (found_type != BTRFS_FILE_EXTENT_REG &&
6457 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6458 /* not a regular extent, must cow */
6459 goto out;
6460 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006461
6462 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6463 goto out;
6464
Chris Mason46bfbb52010-05-26 11:04:10 -04006465 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006466 if (disk_bytenr == 0)
6467 goto out;
6468
6469 if (btrfs_file_extent_compression(leaf, fi) ||
6470 btrfs_file_extent_encryption(leaf, fi) ||
6471 btrfs_file_extent_other_encoding(leaf, fi))
6472 goto out;
6473
Chris Mason46bfbb52010-05-26 11:04:10 -04006474 backref_offset = btrfs_file_extent_offset(leaf, fi);
6475
Josef Bacik7ee9e442013-06-21 16:37:03 -04006476 if (orig_start) {
6477 *orig_start = key.offset - backref_offset;
6478 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6479 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6480 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006481
Chris Mason46bfbb52010-05-26 11:04:10 -04006482 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason46bfbb52010-05-26 11:04:10 -04006483
6484 if (btrfs_extent_readonly(root, disk_bytenr))
6485 goto out;
6486
6487 /*
6488 * look for other files referencing this extent, if we
6489 * find any we must cow
6490 */
Li Zefan33345d012011-04-20 10:31:50 +08006491 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006492 key.offset - backref_offset, disk_bytenr))
6493 goto out;
6494
6495 /*
6496 * adjust disk_bytenr and num_bytes to cover just the bytes
6497 * in this extent we are about to write. If there
6498 * are any csums in that range we have to cow in order
6499 * to keep the csums correct
6500 */
6501 disk_bytenr += backref_offset;
6502 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006503 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006504 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6505 goto out;
6506 /*
6507 * all of the above have passed, it is safe to overwrite this extent
6508 * without cow
6509 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006510 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006511 ret = 1;
6512out:
6513 btrfs_free_path(path);
6514 return ret;
6515}
6516
Josef Bacikeb838e72012-07-31 16:28:48 -04006517static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6518 struct extent_state **cached_state, int writing)
6519{
6520 struct btrfs_ordered_extent *ordered;
6521 int ret = 0;
6522
6523 while (1) {
6524 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6525 0, cached_state);
6526 /*
6527 * We're concerned with the entire range that we're going to be
6528 * doing DIO to, so we need to make sure theres no ordered
6529 * extents in this range.
6530 */
6531 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6532 lockend - lockstart + 1);
6533
6534 /*
6535 * We need to make sure there are no buffered pages in this
6536 * range either, we could have raced between the invalidate in
6537 * generic_file_direct_write and locking the extent. The
6538 * invalidate needs to happen so that reads after a write do not
6539 * get stale data.
6540 */
6541 if (!ordered && (!writing ||
6542 !test_range_bit(&BTRFS_I(inode)->io_tree,
6543 lockstart, lockend, EXTENT_UPTODATE, 0,
6544 *cached_state)))
6545 break;
6546
6547 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6548 cached_state, GFP_NOFS);
6549
6550 if (ordered) {
6551 btrfs_start_ordered_extent(inode, ordered, 1);
6552 btrfs_put_ordered_extent(ordered);
6553 } else {
6554 /* Screw you mmap */
6555 ret = filemap_write_and_wait_range(inode->i_mapping,
6556 lockstart,
6557 lockend);
6558 if (ret)
6559 break;
6560
6561 /*
6562 * If we found a page that couldn't be invalidated just
6563 * fall back to buffered.
6564 */
6565 ret = invalidate_inode_pages2_range(inode->i_mapping,
6566 lockstart >> PAGE_CACHE_SHIFT,
6567 lockend >> PAGE_CACHE_SHIFT);
6568 if (ret)
6569 break;
6570 }
6571
6572 cond_resched();
6573 }
6574
6575 return ret;
6576}
6577
Josef Bacik69ffb542012-09-11 15:40:07 -04006578static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6579 u64 len, u64 orig_start,
6580 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006581 u64 orig_block_len, u64 ram_bytes,
6582 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006583{
6584 struct extent_map_tree *em_tree;
6585 struct extent_map *em;
6586 struct btrfs_root *root = BTRFS_I(inode)->root;
6587 int ret;
6588
6589 em_tree = &BTRFS_I(inode)->extent_tree;
6590 em = alloc_extent_map();
6591 if (!em)
6592 return ERR_PTR(-ENOMEM);
6593
6594 em->start = start;
6595 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006596 em->mod_start = start;
6597 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006598 em->len = len;
6599 em->block_len = block_len;
6600 em->block_start = block_start;
6601 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006602 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006603 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006604 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006605 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6606 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006607 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006608
6609 do {
6610 btrfs_drop_extent_cache(inode, em->start,
6611 em->start + em->len - 1, 0);
6612 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006613 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006614 write_unlock(&em_tree->lock);
6615 } while (ret == -EEXIST);
6616
6617 if (ret) {
6618 free_extent_map(em);
6619 return ERR_PTR(ret);
6620 }
6621
6622 return em;
6623}
6624
6625
Josef Bacik4b46fce2010-05-23 11:00:55 -04006626static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6627 struct buffer_head *bh_result, int create)
6628{
6629 struct extent_map *em;
6630 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006631 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006632 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006633 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006634 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006635 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006636 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006637 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006638
Miao Xie172a5042013-02-21 02:48:22 -07006639 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006640 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006641 else
Josef Bacikc3298612012-08-03 16:49:19 -04006642 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006643
Josef Bacikc3298612012-08-03 16:49:19 -04006644 lockstart = start;
6645 lockend = start + len - 1;
6646
Josef Bacikeb838e72012-07-31 16:28:48 -04006647 /*
6648 * If this errors out it's because we couldn't invalidate pagecache for
6649 * this range and we need to fallback to buffered.
6650 */
6651 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6652 return -ENOTBLK;
6653
Josef Bacik4b46fce2010-05-23 11:00:55 -04006654 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006655 if (IS_ERR(em)) {
6656 ret = PTR_ERR(em);
6657 goto unlock_err;
6658 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006659
6660 /*
6661 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6662 * io. INLINE is special, and we could probably kludge it in here, but
6663 * it's still buffered so for safety lets just fall back to the generic
6664 * buffered path.
6665 *
6666 * For COMPRESSED we _have_ to read the entire extent in so we can
6667 * decompress it, so there will be buffering required no matter what we
6668 * do, so go ahead and fallback to buffered.
6669 *
6670 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6671 * to buffered IO. Don't blame me, this is the price we pay for using
6672 * the generic code.
6673 */
6674 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6675 em->block_start == EXTENT_MAP_INLINE) {
6676 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006677 ret = -ENOTBLK;
6678 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006679 }
6680
6681 /* Just a good old fashioned hole, return */
6682 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6683 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6684 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006685 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006686 }
6687
6688 /*
6689 * We don't allocate a new extent in the following cases
6690 *
6691 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6692 * existing extent.
6693 * 2) The extent is marked as PREALLOC. We're good to go here and can
6694 * just use the extent.
6695 *
6696 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006697 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006698 len = min(len, em->len - (start - em->start));
6699 lockstart = start + len;
6700 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006701 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006702
6703 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6704 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6705 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006706 int type;
6707 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006708 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006709
6710 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6711 type = BTRFS_ORDERED_PREALLOC;
6712 else
6713 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006714 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006715 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006716
6717 /*
6718 * we're not going to log anything, but we do need
6719 * to make sure the current transaction stays open
6720 * while we look for nocow cross refs
6721 */
Josef Bacik7a7eaa402011-04-13 12:54:33 -04006722 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006723 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006724 goto must_cow;
6725
Josef Bacik7ee9e442013-06-21 16:37:03 -04006726 if (can_nocow_extent(trans, inode, start, &len, &orig_start,
6727 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006728 if (type == BTRFS_ORDERED_PREALLOC) {
6729 free_extent_map(em);
6730 em = create_pinned_em(inode, start, len,
6731 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006732 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006733 orig_block_len,
6734 ram_bytes, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006735 if (IS_ERR(em)) {
6736 btrfs_end_transaction(trans, root);
6737 goto unlock_err;
6738 }
6739 }
6740
Chris Mason46bfbb52010-05-26 11:04:10 -04006741 ret = btrfs_add_ordered_extent_dio(inode, start,
6742 block_start, len, len, type);
6743 btrfs_end_transaction(trans, root);
6744 if (ret) {
6745 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006746 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006747 }
6748 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006749 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006750 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006751 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006752must_cow:
6753 /*
6754 * this will cow the extent, reset the len in case we changed
6755 * it above
6756 */
6757 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006758 free_extent_map(em);
6759 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006760 if (IS_ERR(em)) {
6761 ret = PTR_ERR(em);
6762 goto unlock_err;
6763 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006764 len = min(len, em->len - (start - em->start));
6765unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006766 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6767 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006768 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006769 bh_result->b_bdev = em->bdev;
6770 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006771 if (create) {
6772 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6773 set_buffer_new(bh_result);
6774
6775 /*
6776 * Need to update the i_size under the extent lock so buffered
6777 * readers will get the updated i_size when we unlock.
6778 */
6779 if (start + len > i_size_read(inode))
6780 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006781
Miao Xie172a5042013-02-21 02:48:22 -07006782 spin_lock(&BTRFS_I(inode)->lock);
6783 BTRFS_I(inode)->outstanding_extents++;
6784 spin_unlock(&BTRFS_I(inode)->lock);
6785
Miao Xie09348562013-02-07 10:12:07 +00006786 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6787 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6788 &cached_state, GFP_NOFS);
6789 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006790 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006791
Josef Bacikeb838e72012-07-31 16:28:48 -04006792 /*
6793 * In the case of write we need to clear and unlock the entire range,
6794 * in the case of read we need to unlock only the end area that we
6795 * aren't using if there is any left over space.
6796 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006797 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006798 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6799 lockend, unlock_bits, 1, 0,
6800 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006801 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006802 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006803 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006804
Josef Bacik4b46fce2010-05-23 11:00:55 -04006805 free_extent_map(em);
6806
6807 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006808
6809unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006810 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6811 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6812 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006813}
6814
6815struct btrfs_dio_private {
6816 struct inode *inode;
6817 u64 logical_offset;
6818 u64 disk_bytenr;
6819 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006820 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006821
6822 /* number of bios pending for this dio */
6823 atomic_t pending_bios;
6824
6825 /* IO errors */
6826 int errors;
6827
Chris Mason9be33952013-05-17 18:30:14 -04006828 /* orig_bio is our btrfs_io_bio */
Miao Xiee65e1532010-11-22 03:04:43 +00006829 struct bio *orig_bio;
Chris Mason9be33952013-05-17 18:30:14 -04006830
6831 /* dio_bio came from fs/direct-io.c */
6832 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006833};
6834
6835static void btrfs_endio_direct_read(struct bio *bio, int err)
6836{
Miao Xiee65e1532010-11-22 03:04:43 +00006837 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006838 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6839 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006840 struct inode *inode = dip->inode;
6841 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006842 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006843 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006844
6845 start = dip->logical_offset;
6846 do {
6847 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6848 struct page *page = bvec->bv_page;
6849 char *kaddr;
6850 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006851 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006852 unsigned long flags;
6853
Josef Bacikc3298612012-08-03 16:49:19 -04006854 if (get_state_private(&BTRFS_I(inode)->io_tree,
6855 start, &private))
6856 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006857 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006858 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006859 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006860 csum, bvec->bv_len);
6861 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006862 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006863 local_irq_restore(flags);
6864
6865 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006866 if (csum != private) {
6867failed:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006868 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u private %u",
6869 (unsigned long long)btrfs_ino(inode),
6870 (unsigned long long)start,
6871 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006872 err = -EIO;
6873 }
6874 }
6875
6876 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006877 bvec++;
6878 } while (bvec <= bvec_end);
6879
6880 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006881 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04006882 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006883
Josef Bacik4b46fce2010-05-23 11:00:55 -04006884 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006885
6886 /* If we had a csum failure make sure to clear the uptodate flag */
6887 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006888 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6889 dio_end_io(dio_bio, err);
6890 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006891}
6892
6893static void btrfs_endio_direct_write(struct bio *bio, int err)
6894{
6895 struct btrfs_dio_private *dip = bio->bi_private;
6896 struct inode *inode = dip->inode;
6897 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006898 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006899 u64 ordered_offset = dip->logical_offset;
6900 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04006901 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006902 int ret;
6903
6904 if (err)
6905 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006906again:
6907 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6908 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006909 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006910 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006911 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006912
Josef Bacik5fd02042012-05-02 14:00:54 -04006913 ordered->work.func = finish_ordered_fn;
6914 ordered->work.flags = 0;
6915 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6916 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006917out_test:
6918 /*
6919 * our bio might span multiple ordered extents. If we haven't
6920 * completed the accounting for the whole dio, go back and try again
6921 */
6922 if (ordered_offset < dip->logical_offset + dip->bytes) {
6923 ordered_bytes = dip->logical_offset + dip->bytes -
6924 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006925 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006926 goto again;
6927 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006928out_done:
Chris Mason9be33952013-05-17 18:30:14 -04006929 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006930
Josef Bacik4b46fce2010-05-23 11:00:55 -04006931 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006932
6933 /* If we had an error make sure to clear the uptodate flag */
6934 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006935 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6936 dio_end_io(dio_bio, err);
6937 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006938}
6939
Chris Masoneaf25d92010-05-25 09:48:28 -04006940static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6941 struct bio *bio, int mirror_num,
6942 unsigned long bio_flags, u64 offset)
6943{
6944 int ret;
6945 struct btrfs_root *root = BTRFS_I(inode)->root;
6946 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006947 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006948 return 0;
6949}
6950
Miao Xiee65e1532010-11-22 03:04:43 +00006951static void btrfs_end_dio_bio(struct bio *bio, int err)
6952{
6953 struct btrfs_dio_private *dip = bio->bi_private;
6954
6955 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006956 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006957 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006958 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006959 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006960 dip->errors = 1;
6961
6962 /*
6963 * before atomic variable goto zero, we must make sure
6964 * dip->errors is perceived to be set.
6965 */
6966 smp_mb__before_atomic_dec();
6967 }
6968
6969 /* if there are more bios still pending for this dio, just exit */
6970 if (!atomic_dec_and_test(&dip->pending_bios))
6971 goto out;
6972
Chris Mason9be33952013-05-17 18:30:14 -04006973 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00006974 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04006975 } else {
6976 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00006977 bio_endio(dip->orig_bio, 0);
6978 }
6979out:
6980 bio_put(bio);
6981}
6982
6983static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6984 u64 first_sector, gfp_t gfp_flags)
6985{
6986 int nr_vecs = bio_get_nr_vecs(bdev);
6987 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6988}
6989
6990static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6991 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006992 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006993{
6994 int write = rw & REQ_WRITE;
6995 struct btrfs_root *root = BTRFS_I(inode)->root;
6996 int ret;
6997
Josef Bacikb812ce22012-11-16 13:56:32 -05006998 if (async_submit)
6999 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7000
Miao Xiee65e1532010-11-22 03:04:43 +00007001 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04007002
7003 if (!write) {
7004 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7005 if (ret)
7006 goto err;
7007 }
Miao Xiee65e1532010-11-22 03:04:43 +00007008
Josef Bacik1ae39932011-04-06 14:41:34 -04007009 if (skip_sum)
7010 goto map;
7011
7012 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007013 ret = btrfs_wq_submit_bio(root->fs_info,
7014 inode, rw, bio, 0, 0,
7015 file_offset,
7016 __btrfs_submit_bio_start_direct_io,
7017 __btrfs_submit_bio_done);
7018 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007019 } else if (write) {
7020 /*
7021 * If we aren't doing async submit, calculate the csum of the
7022 * bio now.
7023 */
7024 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7025 if (ret)
7026 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007027 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04007028 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007029 if (ret)
7030 goto err;
7031 }
Miao Xiee65e1532010-11-22 03:04:43 +00007032
Josef Bacik1ae39932011-04-06 14:41:34 -04007033map:
7034 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007035err:
7036 bio_put(bio);
7037 return ret;
7038}
7039
7040static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7041 int skip_sum)
7042{
7043 struct inode *inode = dip->inode;
7044 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007045 struct bio *bio;
7046 struct bio *orig_bio = dip->orig_bio;
7047 struct bio_vec *bvec = orig_bio->bi_io_vec;
7048 u64 start_sector = orig_bio->bi_sector;
7049 u64 file_offset = dip->logical_offset;
7050 u64 submit_len = 0;
7051 u64 map_length;
7052 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007053 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007054 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007055
Miao Xiee65e1532010-11-22 03:04:43 +00007056 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007057 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007058 &map_length, NULL, 0);
7059 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007060 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007061 return -EIO;
7062 }
Josef Bacik02f57c72011-04-06 14:25:44 -04007063 if (map_length >= orig_bio->bi_size) {
7064 bio = orig_bio;
7065 goto submit;
7066 }
7067
David Woodhouse53b381b2013-01-29 18:40:14 -05007068 /* async crcs make it difficult to collect full stripe writes. */
7069 if (btrfs_get_alloc_profile(root, 1) &
7070 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7071 async_submit = 0;
7072 else
7073 async_submit = 1;
7074
Josef Bacik02f57c72011-04-06 14:25:44 -04007075 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7076 if (!bio)
7077 return -ENOMEM;
7078 bio->bi_private = dip;
7079 bio->bi_end_io = btrfs_end_dio_bio;
7080 atomic_inc(&dip->pending_bios);
7081
Miao Xiee65e1532010-11-22 03:04:43 +00007082 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7083 if (unlikely(map_length < submit_len + bvec->bv_len ||
7084 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7085 bvec->bv_offset) < bvec->bv_len)) {
7086 /*
7087 * inc the count before we submit the bio so
7088 * we know the end IO handler won't happen before
7089 * we inc the count. Otherwise, the dip might get freed
7090 * before we're done setting it up
7091 */
7092 atomic_inc(&dip->pending_bios);
7093 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7094 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007095 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007096 if (ret) {
7097 bio_put(bio);
7098 atomic_dec(&dip->pending_bios);
7099 goto out_err;
7100 }
7101
Miao Xiee65e1532010-11-22 03:04:43 +00007102 start_sector += submit_len >> 9;
7103 file_offset += submit_len;
7104
7105 submit_len = 0;
7106 nr_pages = 0;
7107
7108 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7109 start_sector, GFP_NOFS);
7110 if (!bio)
7111 goto out_err;
7112 bio->bi_private = dip;
7113 bio->bi_end_io = btrfs_end_dio_bio;
7114
7115 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007116 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007117 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007118 &map_length, NULL, 0);
7119 if (ret) {
7120 bio_put(bio);
7121 goto out_err;
7122 }
7123 } else {
7124 submit_len += bvec->bv_len;
7125 nr_pages ++;
7126 bvec++;
7127 }
7128 }
7129
Josef Bacik02f57c72011-04-06 14:25:44 -04007130submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007131 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007132 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007133 if (!ret)
7134 return 0;
7135
7136 bio_put(bio);
7137out_err:
7138 dip->errors = 1;
7139 /*
7140 * before atomic variable goto zero, we must
7141 * make sure dip->errors is perceived to be set.
7142 */
7143 smp_mb__before_atomic_dec();
7144 if (atomic_dec_and_test(&dip->pending_bios))
7145 bio_io_error(dip->orig_bio);
7146
7147 /* bio_end_io() will handle error, so we needn't return it */
7148 return 0;
7149}
7150
Chris Mason9be33952013-05-17 18:30:14 -04007151static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7152 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007153{
7154 struct btrfs_root *root = BTRFS_I(inode)->root;
7155 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007156 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007157 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007158 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007159 int ret = 0;
7160
7161 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7162
Chris Mason9be33952013-05-17 18:30:14 -04007163 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
7164
7165 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007166 ret = -ENOMEM;
7167 goto free_ordered;
7168 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007169
Chris Mason9be33952013-05-17 18:30:14 -04007170 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7171 if (!dip) {
7172 ret = -ENOMEM;
7173 goto free_io_bio;
7174 }
7175
7176 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007177 dip->inode = inode;
7178 dip->logical_offset = file_offset;
Miao Xiee6da5d22013-06-19 18:19:17 +08007179 dip->bytes = dio_bio->bi_size;
Chris Mason9be33952013-05-17 18:30:14 -04007180 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7181 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007182 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007183 dip->orig_bio = io_bio;
7184 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007185 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007186
7187 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007188 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007189 else
Chris Mason9be33952013-05-17 18:30:14 -04007190 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007191
Miao Xiee65e1532010-11-22 03:04:43 +00007192 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7193 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007194 return;
Chris Mason9be33952013-05-17 18:30:14 -04007195
7196free_io_bio:
7197 bio_put(io_bio);
7198
Josef Bacik4b46fce2010-05-23 11:00:55 -04007199free_ordered:
7200 /*
7201 * If this is a write, we need to clean up the reserved space and kill
7202 * the ordered extent.
7203 */
7204 if (write) {
7205 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007206 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007207 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7208 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7209 btrfs_free_reserved_extent(root, ordered->start,
7210 ordered->disk_len);
7211 btrfs_put_ordered_extent(ordered);
7212 btrfs_put_ordered_extent(ordered);
7213 }
Chris Mason9be33952013-05-17 18:30:14 -04007214 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007215}
7216
Chris Mason5a5f79b2010-05-26 21:33:37 -04007217static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7218 const struct iovec *iov, loff_t offset,
7219 unsigned long nr_segs)
7220{
7221 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007222 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007223 size_t size;
7224 unsigned long addr;
7225 unsigned blocksize_mask = root->sectorsize - 1;
7226 ssize_t retval = -EINVAL;
7227 loff_t end = offset;
7228
7229 if (offset & blocksize_mask)
7230 goto out;
7231
7232 /* Check the memory alignment. Blocks cannot straddle pages */
7233 for (seg = 0; seg < nr_segs; seg++) {
7234 addr = (unsigned long)iov[seg].iov_base;
7235 size = iov[seg].iov_len;
7236 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007237 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007238 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007239
7240 /* If this is a write we don't need to check anymore */
7241 if (rw & WRITE)
7242 continue;
7243
7244 /*
7245 * Check to make sure we don't have duplicate iov_base's in this
7246 * iovec, if so return EINVAL, otherwise we'll get csum errors
7247 * when reading back.
7248 */
7249 for (i = seg + 1; i < nr_segs; i++) {
7250 if (iov[seg].iov_base == iov[i].iov_base)
7251 goto out;
7252 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007253 }
7254 retval = 0;
7255out:
7256 return retval;
7257}
Josef Bacikeb838e72012-07-31 16:28:48 -04007258
Chris Mason16432982008-04-10 10:23:21 -04007259static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7260 const struct iovec *iov, loff_t offset,
7261 unsigned long nr_segs)
7262{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007263 struct file *file = iocb->ki_filp;
7264 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007265 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007266 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007267 bool wakeup = true;
7268 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007269 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007270
Chris Mason5a5f79b2010-05-26 21:33:37 -04007271 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007272 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007273 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007274
Miao Xie38851cc2013-02-08 07:04:11 +00007275 atomic_inc(&inode->i_dio_count);
7276 smp_mb__after_atomic_inc();
7277
Josef Bacik0e267c42013-07-02 10:38:02 -04007278 /*
7279 * The generic stuff only does filemap_write_and_wait_range, which isn't
7280 * enough if we've written compressed pages to this area, so we need to
7281 * call btrfs_wait_ordered_range to make absolutely sure that any
7282 * outstanding dirty pages are on disk.
7283 */
7284 count = iov_length(iov, nr_segs);
7285 btrfs_wait_ordered_range(inode, offset, count);
7286
Miao Xie09348562013-02-07 10:12:07 +00007287 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007288 /*
7289 * If the write DIO is beyond the EOF, we need update
7290 * the isize, but it is protected by i_mutex. So we can
7291 * not unlock the i_mutex at this case.
7292 */
7293 if (offset + count <= inode->i_size) {
7294 mutex_unlock(&inode->i_mutex);
7295 relock = true;
7296 }
Miao Xie09348562013-02-07 10:12:07 +00007297 ret = btrfs_delalloc_reserve_space(inode, count);
7298 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007299 goto out;
7300 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7301 &BTRFS_I(inode)->runtime_flags))) {
7302 inode_dio_done(inode);
7303 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7304 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007305 }
7306
7307 ret = __blockdev_direct_IO(rw, iocb, inode,
7308 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7309 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007310 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007311 if (rw & WRITE) {
7312 if (ret < 0 && ret != -EIOCBQUEUED)
7313 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007314 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007315 btrfs_delalloc_release_space(inode,
7316 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007317 else
7318 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007319 }
Miao Xie38851cc2013-02-08 07:04:11 +00007320out:
Miao Xie2e60a512013-02-08 07:01:08 +00007321 if (wakeup)
7322 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007323 if (relock)
7324 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007325
7326 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007327}
7328
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007329#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7330
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007331static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7332 __u64 start, __u64 len)
7333{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007334 int ret;
7335
7336 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7337 if (ret)
7338 return ret;
7339
Chris Masonec29ed52011-02-23 16:23:20 -05007340 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007341}
7342
Chris Mason9ebefb182007-06-15 13:50:00 -04007343int btrfs_readpage(struct file *file, struct page *page)
7344{
Chris Masond1310b22008-01-24 16:13:08 -05007345 struct extent_io_tree *tree;
7346 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007347 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007348}
Chris Mason1832a6d2007-12-21 16:27:21 -05007349
Chris Mason39279cc2007-06-12 06:35:45 -04007350static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7351{
Chris Masond1310b22008-01-24 16:13:08 -05007352 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007353
7354
7355 if (current->flags & PF_MEMALLOC) {
7356 redirty_page_for_writepage(wbc, page);
7357 unlock_page(page);
7358 return 0;
7359 }
Chris Masond1310b22008-01-24 16:13:08 -05007360 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007361 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7362}
Chris Mason39279cc2007-06-12 06:35:45 -04007363
Eric Sandeen48a3b632013-04-25 20:41:01 +00007364static int btrfs_writepages(struct address_space *mapping,
7365 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007366{
Chris Masond1310b22008-01-24 16:13:08 -05007367 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007368
Chris Masond1310b22008-01-24 16:13:08 -05007369 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007370 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7371}
7372
Chris Mason3ab2fb52007-11-08 10:59:22 -05007373static int
7374btrfs_readpages(struct file *file, struct address_space *mapping,
7375 struct list_head *pages, unsigned nr_pages)
7376{
Chris Masond1310b22008-01-24 16:13:08 -05007377 struct extent_io_tree *tree;
7378 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007379 return extent_readpages(tree, mapping, pages, nr_pages,
7380 btrfs_get_extent);
7381}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007382static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007383{
Chris Masond1310b22008-01-24 16:13:08 -05007384 struct extent_io_tree *tree;
7385 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007386 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007387
Chris Masond1310b22008-01-24 16:13:08 -05007388 tree = &BTRFS_I(page->mapping->host)->io_tree;
7389 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007390 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007391 if (ret == 1) {
7392 ClearPagePrivate(page);
7393 set_page_private(page, 0);
7394 page_cache_release(page);
7395 }
7396 return ret;
7397}
Chris Mason39279cc2007-06-12 06:35:45 -04007398
Chris Masone6dcd2d2008-07-17 12:53:50 -04007399static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7400{
Chris Mason98509cf2008-09-11 15:51:43 -04007401 if (PageWriteback(page) || PageDirty(page))
7402 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007403 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007404}
7405
Lukas Czernerd47992f2013-05-21 23:17:23 -04007406static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7407 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007408{
Josef Bacik5fd02042012-05-02 14:00:54 -04007409 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007410 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007411 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007412 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007413 u64 page_start = page_offset(page);
7414 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007415
Chris Mason8b62b722009-09-02 16:53:46 -04007416 /*
7417 * we have the page locked, so new writeback can't start,
7418 * and the dirty bit won't be cleared while we are here.
7419 *
7420 * Wait for IO on this page so that we can safely clear
7421 * the PagePrivate2 bit and do ordered accounting
7422 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007423 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007424
Josef Bacik5fd02042012-05-02 14:00:54 -04007425 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007426 if (offset) {
7427 btrfs_releasepage(page, GFP_NOFS);
7428 return;
7429 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007430 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007431 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007432 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007433 /*
7434 * IO on this page will never be started, so we need
7435 * to account for any ordered extents now
7436 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007437 clear_extent_bit(tree, page_start, page_end,
7438 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007439 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7440 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007441 /*
7442 * whoever cleared the private bit is responsible
7443 * for the finish_ordered_io
7444 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007445 if (TestClearPagePrivate2(page) &&
7446 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7447 PAGE_CACHE_SIZE, 1)) {
7448 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007449 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007450 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007451 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007452 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007453 }
7454 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007455 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007456 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7457 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007458 __btrfs_releasepage(page, GFP_NOFS);
7459
Chris Mason4a096752008-07-21 10:29:44 -04007460 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007461 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007462 ClearPagePrivate(page);
7463 set_page_private(page, 0);
7464 page_cache_release(page);
7465 }
Chris Mason39279cc2007-06-12 06:35:45 -04007466}
7467
Chris Mason9ebefb182007-06-15 13:50:00 -04007468/*
7469 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7470 * called from a page fault handler when a page is first dirtied. Hence we must
7471 * be careful to check for EOF conditions here. We set the page up correctly
7472 * for a written page which means we get ENOSPC checking when writing into
7473 * holes and correct delalloc and unwritten extent mapping on filesystems that
7474 * support these features.
7475 *
7476 * We are not allowed to take the i_mutex here so we have to play games to
7477 * protect against truncate races as the page could now be beyond EOF. Because
7478 * vmtruncate() writes the inode size before removing pages, once we have the
7479 * page lock we can determine safely if the page is beyond EOF. If it is not
7480 * beyond EOF, then the page is guaranteed safe against truncation until we
7481 * unlock the page.
7482 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007483int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007484{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007485 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007486 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007487 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007488 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7489 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007490 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007491 char *kaddr;
7492 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007493 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007494 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007495 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007496 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007497 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007498
Jan Karab2b5ef52012-06-12 16:20:45 +02007499 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007500 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007501 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007502 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007503 reserved = 1;
7504 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007505 if (ret) {
7506 if (ret == -ENOMEM)
7507 ret = VM_FAULT_OOM;
7508 else /* -ENOSPC, -EIO, etc */
7509 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007510 if (reserved)
7511 goto out;
7512 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007513 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007514
Nick Piggin56a76f82009-03-31 15:23:23 -07007515 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007516again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007517 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007518 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007519 page_start = page_offset(page);
7520 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007521
Chris Mason9ebefb182007-06-15 13:50:00 -04007522 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007523 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007524 /* page got truncated out from underneath us */
7525 goto out_unlock;
7526 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007527 wait_on_page_writeback(page);
7528
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007529 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007530 set_page_extent_mapped(page);
7531
Chris Masoneb84ae02008-07-17 13:53:27 -04007532 /*
7533 * we can't set the delalloc bits if there are pending ordered
7534 * extents. Drop our locks and wait for them to finish
7535 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007536 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7537 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007538 unlock_extent_cached(io_tree, page_start, page_end,
7539 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007540 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007541 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007542 btrfs_put_ordered_extent(ordered);
7543 goto again;
7544 }
7545
Josef Bacikfbf19082009-10-01 17:10:23 -04007546 /*
7547 * XXX - page_mkwrite gets called every time the page is dirtied, even
7548 * if it was already dirty, so for space accounting reasons we need to
7549 * clear any delalloc bits for the range we are fixing to save. There
7550 * is probably a better way to do this, but for now keep consistent with
7551 * prepare_pages in the normal write path.
7552 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007553 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007554 EXTENT_DIRTY | EXTENT_DELALLOC |
7555 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007556 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007557
Josef Bacik2ac55d42010-02-03 19:33:23 +00007558 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7559 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007560 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007561 unlock_extent_cached(io_tree, page_start, page_end,
7562 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007563 ret = VM_FAULT_SIGBUS;
7564 goto out_unlock;
7565 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007566 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007567
7568 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007569 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007570 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007571 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007572 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007573
Chris Masone6dcd2d2008-07-17 12:53:50 -04007574 if (zero_start != PAGE_CACHE_SIZE) {
7575 kaddr = kmap(page);
7576 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7577 flush_dcache_page(page);
7578 kunmap(page);
7579 }
Chris Mason247e7432008-07-17 12:53:51 -04007580 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007581 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007582 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007583
Chris Mason257c62e2009-10-13 13:21:08 -04007584 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7585 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007586 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007587
Josef Bacik2ac55d42010-02-03 19:33:23 +00007588 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007589
7590out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007591 if (!ret) {
7592 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007593 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007594 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007595 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007596out:
Josef Bacikec39e182012-01-12 19:10:12 -05007597 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007598out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007599 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007600 return ret;
7601}
7602
Josef Bacika41ad392011-01-31 15:30:16 -05007603static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007604{
7605 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007606 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007607 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007608 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007609 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007610 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007611 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007612
Chris Mason4a096752008-07-21 10:29:44 -04007613 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007614 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007615
Josef Bacikfcb80c22011-05-03 10:40:22 -04007616 /*
7617 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7618 * 3 things going on here
7619 *
7620 * 1) We need to reserve space for our orphan item and the space to
7621 * delete our orphan item. Lord knows we don't want to have a dangling
7622 * orphan item because we didn't reserve space to remove it.
7623 *
7624 * 2) We need to reserve space to update our inode.
7625 *
7626 * 3) We need to have something to cache all the space that is going to
7627 * be free'd up by the truncate operation, but also have some slack
7628 * space reserved in case it uses space during the truncate (thank you
7629 * very much snapshotting).
7630 *
7631 * And we need these to all be seperate. The fact is we can use alot of
7632 * space doing the truncate, and we have no earthly idea how much space
7633 * we will use, so we need the truncate reservation to be seperate so it
7634 * doesn't end up using space reserved for updating the inode or
7635 * removing the orphan item. We also need to be able to stop the
7636 * transaction and start a new one, which means we need to be able to
7637 * update the inode several times, and we have no idea of knowing how
7638 * many times that will be, so we can't just reserve 1 item for the
7639 * entirety of the opration, so that has to be done seperately as well.
7640 * Then there is the orphan item, which does indeed need to be held on
7641 * to for the whole operation, and we need nobody to touch this reserved
7642 * space except the orphan code.
7643 *
7644 * So that leaves us with
7645 *
7646 * 1) root->orphan_block_rsv - for the orphan deletion.
7647 * 2) rsv - for the truncate reservation, which we will steal from the
7648 * transaction reservation.
7649 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7650 * updating the inode.
7651 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007652 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007653 if (!rsv)
7654 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007655 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007656 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007657
Josef Bacik907cbce2011-08-08 13:46:15 -04007658 /*
Josef Bacik07127182011-08-19 10:29:59 -04007659 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007660 * 1 for updating the inode.
7661 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007662 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007663 if (IS_ERR(trans)) {
7664 err = PTR_ERR(trans);
7665 goto out;
7666 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007667
Josef Bacik907cbce2011-08-08 13:46:15 -04007668 /* Migrate the slack space for the truncate to our reserve */
7669 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7670 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007671 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007672
Chris Mason5a3f23d2009-03-31 13:27:11 -04007673 /*
7674 * setattr is responsible for setting the ordered_data_close flag,
7675 * but that is only tested during the last file release. That
7676 * could happen well after the next commit, leaving a great big
7677 * window where new writes may get lost if someone chooses to write
7678 * to this file after truncating to zero
7679 *
7680 * The inode doesn't have any dirty data here, and so if we commit
7681 * this is a noop. If someone immediately starts writing to the inode
7682 * it is very likely we'll catch some of their writes in this
7683 * transaction, and the commit will find this file on the ordered
7684 * data list with good things to send down.
7685 *
7686 * This is a best effort solution, there is still a window where
7687 * using truncate to replace the contents of the file will
7688 * end up with a zero length file after a crash.
7689 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007690 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7691 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007692 btrfs_add_ordered_operation(trans, root, inode);
7693
Josef Bacik5dc562c2012-08-17 13:14:17 -04007694 /*
7695 * So if we truncate and then write and fsync we normally would just
7696 * write the extents that changed, which is a problem if we need to
7697 * first truncate that entire inode. So set this flag so we write out
7698 * all of the extents in the inode to the sync log so we're completely
7699 * safe.
7700 */
7701 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007702 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007703
Yan, Zheng80825102009-11-12 09:35:36 +00007704 while (1) {
7705 ret = btrfs_truncate_inode_items(trans, root, inode,
7706 inode->i_size,
7707 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007708 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007709 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007710 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007711 }
Chris Mason39279cc2007-06-12 06:35:45 -04007712
Josef Bacikfcb80c22011-05-03 10:40:22 -04007713 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007714 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007715 if (ret) {
7716 err = ret;
7717 break;
7718 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007719
Yan, Zheng80825102009-11-12 09:35:36 +00007720 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007721 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007722
7723 trans = btrfs_start_transaction(root, 2);
7724 if (IS_ERR(trans)) {
7725 ret = err = PTR_ERR(trans);
7726 trans = NULL;
7727 break;
7728 }
7729
7730 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7731 rsv, min_size);
7732 BUG_ON(ret); /* shouldn't happen */
7733 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007734 }
7735
7736 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007737 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007738 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007739 if (ret)
7740 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007741 }
7742
Chris Mason917c16b2011-11-08 14:49:59 -05007743 if (trans) {
7744 trans->block_rsv = &root->fs_info->trans_block_rsv;
7745 ret = btrfs_update_inode(trans, root, inode);
7746 if (ret && !err)
7747 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007748
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007749 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007750 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007751 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007752
7753out:
7754 btrfs_free_block_rsv(root, rsv);
7755
Josef Bacik3893e332011-01-31 16:03:11 -05007756 if (ret && !err)
7757 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007758
Josef Bacik3893e332011-01-31 16:03:11 -05007759 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007760}
7761
Sven Wegener3b963622008-06-09 21:57:42 -04007762/*
Chris Masond352ac62008-09-29 15:18:18 -04007763 * create a new subvolume directory/inode (helper for the ioctl).
7764 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007765int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007766 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007767{
Chris Mason39279cc2007-06-12 06:35:45 -04007768 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007769 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007770 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007771
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007772 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7773 new_dirid, new_dirid,
7774 S_IFDIR | (~current_umask() & S_IRWXUGO),
7775 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007776 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007777 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007778 inode->i_op = &btrfs_dir_inode_operations;
7779 inode->i_fop = &btrfs_dir_file_operations;
7780
Miklos Szeredibfe86842011-10-28 14:13:29 +02007781 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007782 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007783
Yan, Zheng76dda932009-09-21 16:00:26 -04007784 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007785
Yan, Zheng76dda932009-09-21 16:00:26 -04007786 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007787 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007788}
7789
Chris Mason39279cc2007-06-12 06:35:45 -04007790struct inode *btrfs_alloc_inode(struct super_block *sb)
7791{
7792 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007793 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007794
7795 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7796 if (!ei)
7797 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007798
7799 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007800 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007801 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007802 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007803 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007804 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007805 ei->disk_i_size = 0;
7806 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007807 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007808 ei->index_cnt = (u64)-1;
7809 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007810 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007811
Josef Bacik9e0baf62011-07-15 15:16:44 +00007812 spin_lock_init(&ei->lock);
7813 ei->outstanding_extents = 0;
7814 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007815
Josef Bacik72ac3c02012-05-23 14:13:11 -04007816 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007817 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007818
Miao Xie16cdcec2011-04-22 18:12:22 +08007819 ei->delayed_node = NULL;
7820
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007821 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007822 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007823 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7824 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007825 ei->io_tree.track_uptodate = 1;
7826 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007827 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007828 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007829 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007830 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007831 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007832 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007833 RB_CLEAR_NODE(&ei->rb_node);
7834
7835 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007836}
7837
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007838static void btrfs_i_callback(struct rcu_head *head)
7839{
7840 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007841 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7842}
7843
Chris Mason39279cc2007-06-12 06:35:45 -04007844void btrfs_destroy_inode(struct inode *inode)
7845{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007846 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007847 struct btrfs_root *root = BTRFS_I(inode)->root;
7848
Al Virob3d9b7a2012-06-09 13:51:19 -04007849 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007850 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007851 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7852 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007853 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7854 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007855
Chris Mason5a3f23d2009-03-31 13:27:11 -04007856 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007857 * This can happen where we create an inode, but somebody else also
7858 * created the same inode and we need to destroy the one we already
7859 * created.
7860 */
7861 if (!root)
7862 goto free;
7863
7864 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007865 * Make sure we're properly removed from the ordered operation
7866 * lists.
7867 */
7868 smp_mb();
7869 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
Miao Xie199c2a92013-05-15 07:48:23 +00007870 spin_lock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007871 list_del_init(&BTRFS_I(inode)->ordered_operations);
Miao Xie199c2a92013-05-15 07:48:23 +00007872 spin_unlock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007873 }
7874
Josef Bacik8a35d952012-05-23 14:26:42 -04007875 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7876 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007877 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
7878 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007879 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007880 }
Josef Bacik7b128762008-07-24 12:17:14 -04007881
Chris Masond3977122009-01-05 21:25:51 -05007882 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007883 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7884 if (!ordered)
7885 break;
7886 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007887 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
7888 (unsigned long long)ordered->file_offset,
7889 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007890 btrfs_remove_ordered_extent(inode, ordered);
7891 btrfs_put_ordered_extent(ordered);
7892 btrfs_put_ordered_extent(ordered);
7893 }
7894 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007895 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007896 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007897free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007898 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007899}
7900
Al Viro45321ac2010-06-07 13:43:19 -04007901int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007902{
7903 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007904
Naohiro Aota6379ef92013-06-06 09:56:34 +00007905 if (root == NULL)
7906 return 1;
7907
Liu Bofa6ac872013-02-20 14:10:23 +00007908 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007909 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007910 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007911 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007912 else
Al Viro45321ac2010-06-07 13:43:19 -04007913 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007914}
7915
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007916static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007917{
7918 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7919
7920 inode_init_once(&ei->vfs_inode);
7921}
7922
7923void btrfs_destroy_cachep(void)
7924{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007925 /*
7926 * Make sure all delayed rcu free inodes are flushed before we
7927 * destroy cache.
7928 */
7929 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007930 if (btrfs_inode_cachep)
7931 kmem_cache_destroy(btrfs_inode_cachep);
7932 if (btrfs_trans_handle_cachep)
7933 kmem_cache_destroy(btrfs_trans_handle_cachep);
7934 if (btrfs_transaction_cachep)
7935 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007936 if (btrfs_path_cachep)
7937 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007938 if (btrfs_free_space_cachep)
7939 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007940 if (btrfs_delalloc_work_cachep)
7941 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007942}
7943
7944int btrfs_init_cachep(void)
7945{
David Sterba837e1972012-09-07 03:00:48 -06007946 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007947 sizeof(struct btrfs_inode), 0,
7948 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007949 if (!btrfs_inode_cachep)
7950 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007951
David Sterba837e1972012-09-07 03:00:48 -06007952 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007953 sizeof(struct btrfs_trans_handle), 0,
7954 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007955 if (!btrfs_trans_handle_cachep)
7956 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007957
David Sterba837e1972012-09-07 03:00:48 -06007958 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007959 sizeof(struct btrfs_transaction), 0,
7960 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007961 if (!btrfs_transaction_cachep)
7962 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007963
David Sterba837e1972012-09-07 03:00:48 -06007964 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007965 sizeof(struct btrfs_path), 0,
7966 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007967 if (!btrfs_path_cachep)
7968 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007969
David Sterba837e1972012-09-07 03:00:48 -06007970 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007971 sizeof(struct btrfs_free_space), 0,
7972 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7973 if (!btrfs_free_space_cachep)
7974 goto fail;
7975
Miao Xie8ccf6f192012-10-25 09:28:04 +00007976 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7977 sizeof(struct btrfs_delalloc_work), 0,
7978 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7979 NULL);
7980 if (!btrfs_delalloc_work_cachep)
7981 goto fail;
7982
Chris Mason39279cc2007-06-12 06:35:45 -04007983 return 0;
7984fail:
7985 btrfs_destroy_cachep();
7986 return -ENOMEM;
7987}
7988
7989static int btrfs_getattr(struct vfsmount *mnt,
7990 struct dentry *dentry, struct kstat *stat)
7991{
Miao Xiedf0af1a52013-01-29 10:11:59 +00007992 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04007993 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007994 u32 blocksize = inode->i_sb->s_blocksize;
7995
Chris Mason39279cc2007-06-12 06:35:45 -04007996 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007997 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007998 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a52013-01-29 10:11:59 +00007999
8000 spin_lock(&BTRFS_I(inode)->lock);
8001 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8002 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008003 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a52013-01-29 10:11:59 +00008004 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008005 return 0;
8006}
8007
Chris Masond3977122009-01-05 21:25:51 -05008008static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8009 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008010{
8011 struct btrfs_trans_handle *trans;
8012 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008013 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008014 struct inode *new_inode = new_dentry->d_inode;
8015 struct inode *old_inode = old_dentry->d_inode;
8016 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008017 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008018 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008019 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008020 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008021
Li Zefan33345d012011-04-20 10:31:50 +08008022 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008023 return -EPERM;
8024
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008025 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008026 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008027 return -EXDEV;
8028
Li Zefan33345d012011-04-20 10:31:50 +08008029 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8030 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008031 return -ENOTEMPTY;
8032
Chris Mason39279cc2007-06-12 06:35:45 -04008033 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008034 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008035 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008036
8037
8038 /* check for collisions, even if the name isn't there */
8039 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8040 new_dentry->d_name.name,
8041 new_dentry->d_name.len);
8042
8043 if (ret) {
8044 if (ret == -EEXIST) {
8045 /* we shouldn't get
8046 * eexist without a new_inode */
8047 if (!new_inode) {
8048 WARN_ON(1);
8049 return ret;
8050 }
8051 } else {
8052 /* maybe -EOVERFLOW */
8053 return ret;
8054 }
8055 }
8056 ret = 0;
8057
Chris Mason5a3f23d2009-03-31 13:27:11 -04008058 /*
8059 * we're using rename to replace one file with another.
8060 * and the replacement file is large. Start IO on it now so
8061 * we don't add too much work to the end of the transaction
8062 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008063 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008064 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8065 filemap_flush(old_inode->i_mapping);
8066
Yan, Zheng76dda932009-09-21 16:00:26 -04008067 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008068 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008069 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008070 /*
8071 * We want to reserve the absolute worst case amount of items. So if
8072 * both inodes are subvols and we need to unlink them then that would
8073 * require 4 item modifications, but if they are both normal inodes it
8074 * would require 5 item modifications, so we'll assume their normal
8075 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8076 * should cover the worst case number of items we'll modify.
8077 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008078 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008079 if (IS_ERR(trans)) {
8080 ret = PTR_ERR(trans);
8081 goto out_notrans;
8082 }
Chris Mason5f39d392007-10-15 16:14:19 -04008083
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008084 if (dest != root)
8085 btrfs_record_root_in_trans(trans, dest);
8086
Yan, Zhenga5719522009-09-24 09:17:31 -04008087 ret = btrfs_set_inode_index(new_dir, &index);
8088 if (ret)
8089 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008090
Li Zefan33345d012011-04-20 10:31:50 +08008091 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008092 /* force full log commit if subvolume involved. */
8093 root->fs_info->last_trans_log_full_commit = trans->transid;
8094 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008095 ret = btrfs_insert_inode_ref(trans, dest,
8096 new_dentry->d_name.name,
8097 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008098 old_ino,
8099 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008100 if (ret)
8101 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008102 /*
8103 * this is an ugly little race, but the rename is required
8104 * to make sure that if we crash, the inode is either at the
8105 * old name or the new one. pinning the log transaction lets
8106 * us make sure we don't allow a log commit to come in after
8107 * we unlink the name but before we add the new name back in.
8108 */
8109 btrfs_pin_log_trans(root);
8110 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008111 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008112 * make sure the inode gets flushed if it is replacing
8113 * something.
8114 */
Li Zefan33345d012011-04-20 10:31:50 +08008115 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008116 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008117
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008118 inode_inc_iversion(old_dir);
8119 inode_inc_iversion(new_dir);
8120 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008121 old_dir->i_ctime = old_dir->i_mtime = ctime;
8122 new_dir->i_ctime = new_dir->i_mtime = ctime;
8123 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008124
Chris Mason12fcfd22009-03-24 10:24:20 -04008125 if (old_dentry->d_parent != new_dentry->d_parent)
8126 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8127
Li Zefan33345d012011-04-20 10:31:50 +08008128 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008129 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8130 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8131 old_dentry->d_name.name,
8132 old_dentry->d_name.len);
8133 } else {
Al Viro92986792011-03-04 17:14:37 +00008134 ret = __btrfs_unlink_inode(trans, root, old_dir,
8135 old_dentry->d_inode,
8136 old_dentry->d_name.name,
8137 old_dentry->d_name.len);
8138 if (!ret)
8139 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008140 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008141 if (ret) {
8142 btrfs_abort_transaction(trans, root, ret);
8143 goto out_fail;
8144 }
Chris Mason39279cc2007-06-12 06:35:45 -04008145
8146 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008147 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008148 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008149 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008150 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8151 root_objectid = BTRFS_I(new_inode)->location.objectid;
8152 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8153 root_objectid,
8154 new_dentry->d_name.name,
8155 new_dentry->d_name.len);
8156 BUG_ON(new_inode->i_nlink == 0);
8157 } else {
8158 ret = btrfs_unlink_inode(trans, dest, new_dir,
8159 new_dentry->d_inode,
8160 new_dentry->d_name.name,
8161 new_dentry->d_name.len);
8162 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008163 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008164 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008165 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008166 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008167 if (ret) {
8168 btrfs_abort_transaction(trans, root, ret);
8169 goto out_fail;
8170 }
Chris Mason39279cc2007-06-12 06:35:45 -04008171 }
Josef Bacikaec74772008-07-24 12:12:38 -04008172
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008173 ret = btrfs_add_link(trans, new_dir, old_inode,
8174 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008175 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008176 if (ret) {
8177 btrfs_abort_transaction(trans, root, ret);
8178 goto out_fail;
8179 }
Chris Mason39279cc2007-06-12 06:35:45 -04008180
Li Zefan33345d012011-04-20 10:31:50 +08008181 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008182 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008183 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008184 btrfs_end_log_trans(root);
8185 }
Chris Mason39279cc2007-06-12 06:35:45 -04008186out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008187 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008188out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008189 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008190 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008191
Chris Mason39279cc2007-06-12 06:35:45 -04008192 return ret;
8193}
8194
Miao Xie8ccf6f192012-10-25 09:28:04 +00008195static void btrfs_run_delalloc_work(struct btrfs_work *work)
8196{
8197 struct btrfs_delalloc_work *delalloc_work;
8198
8199 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8200 work);
8201 if (delalloc_work->wait)
8202 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8203 else
8204 filemap_flush(delalloc_work->inode->i_mapping);
8205
8206 if (delalloc_work->delay_iput)
8207 btrfs_add_delayed_iput(delalloc_work->inode);
8208 else
8209 iput(delalloc_work->inode);
8210 complete(&delalloc_work->completion);
8211}
8212
8213struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8214 int wait, int delay_iput)
8215{
8216 struct btrfs_delalloc_work *work;
8217
8218 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8219 if (!work)
8220 return NULL;
8221
8222 init_completion(&work->completion);
8223 INIT_LIST_HEAD(&work->list);
8224 work->inode = inode;
8225 work->wait = wait;
8226 work->delay_iput = delay_iput;
8227 work->work.func = btrfs_run_delalloc_work;
8228
8229 return work;
8230}
8231
8232void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8233{
8234 wait_for_completion(&work->completion);
8235 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8236}
8237
Chris Masond352ac62008-09-29 15:18:18 -04008238/*
8239 * some fairly slow code that needs optimization. This walks the list
8240 * of all the inodes with pending delalloc and forces them to disk.
8241 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00008242static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008243{
Chris Masonea8c2812008-08-04 23:17:27 -04008244 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008245 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008246 struct btrfs_delalloc_work *work, *next;
8247 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008248 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008249 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008250
Miao Xie8ccf6f192012-10-25 09:28:04 +00008251 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008252 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008253
Miao Xieeb73c1b2013-05-15 07:48:22 +00008254 spin_lock(&root->delalloc_lock);
8255 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008256 while (!list_empty(&splice)) {
8257 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008258 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008259
Miao Xieeb73c1b2013-05-15 07:48:22 +00008260 list_move_tail(&binode->delalloc_inodes,
8261 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008262 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a52013-01-29 10:11:59 +00008263 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008264 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008265 continue;
Miao Xiedf0af1a52013-01-29 10:11:59 +00008266 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008267 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008268
8269 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8270 if (unlikely(!work)) {
8271 ret = -ENOMEM;
8272 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008273 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008274 list_add_tail(&work->list, &works);
8275 btrfs_queue_worker(&root->fs_info->flush_workers,
8276 &work->work);
8277
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008278 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008279 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008280 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008281 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008282
Miao Xie1eafa6c2013-01-22 10:49:00 +00008283 list_for_each_entry_safe(work, next, &works, list) {
8284 list_del_init(&work->list);
8285 btrfs_wait_and_free_delalloc_work(work);
8286 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008287 return 0;
8288out:
8289 list_for_each_entry_safe(work, next, &works, list) {
8290 list_del_init(&work->list);
8291 btrfs_wait_and_free_delalloc_work(work);
8292 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008293
Miao Xieeb73c1b2013-05-15 07:48:22 +00008294 if (!list_empty_careful(&splice)) {
8295 spin_lock(&root->delalloc_lock);
8296 list_splice_tail(&splice, &root->delalloc_inodes);
8297 spin_unlock(&root->delalloc_lock);
8298 }
8299 return ret;
8300}
8301
8302int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8303{
8304 int ret;
8305
8306 if (root->fs_info->sb->s_flags & MS_RDONLY)
8307 return -EROFS;
8308
8309 ret = __start_delalloc_inodes(root, delay_iput);
8310 /*
8311 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008312 * we have to make sure the IO is actually started and that
8313 * ordered extents get created before we return
8314 */
8315 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008316 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008317 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008318 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008319 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8320 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008321 }
8322 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008323 return ret;
8324}
8325
8326int btrfs_start_all_delalloc_inodes(struct btrfs_fs_info *fs_info,
8327 int delay_iput)
8328{
8329 struct btrfs_root *root;
8330 struct list_head splice;
8331 int ret;
8332
8333 if (fs_info->sb->s_flags & MS_RDONLY)
8334 return -EROFS;
8335
8336 INIT_LIST_HEAD(&splice);
8337
8338 spin_lock(&fs_info->delalloc_root_lock);
8339 list_splice_init(&fs_info->delalloc_roots, &splice);
8340 while (!list_empty(&splice)) {
8341 root = list_first_entry(&splice, struct btrfs_root,
8342 delalloc_root);
8343 root = btrfs_grab_fs_root(root);
8344 BUG_ON(!root);
8345 list_move_tail(&root->delalloc_root,
8346 &fs_info->delalloc_roots);
8347 spin_unlock(&fs_info->delalloc_root_lock);
8348
8349 ret = __start_delalloc_inodes(root, delay_iput);
8350 btrfs_put_fs_root(root);
8351 if (ret)
8352 goto out;
8353
8354 spin_lock(&fs_info->delalloc_root_lock);
8355 }
8356 spin_unlock(&fs_info->delalloc_root_lock);
8357
8358 atomic_inc(&fs_info->async_submit_draining);
8359 while (atomic_read(&fs_info->nr_async_submits) ||
8360 atomic_read(&fs_info->async_delalloc_pages)) {
8361 wait_event(fs_info->async_submit_wait,
8362 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8363 atomic_read(&fs_info->async_delalloc_pages) == 0));
8364 }
8365 atomic_dec(&fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008366 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008367out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008368 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008369 spin_lock(&fs_info->delalloc_root_lock);
8370 list_splice_tail(&splice, &fs_info->delalloc_roots);
8371 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008372 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008373 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008374}
8375
Chris Mason39279cc2007-06-12 06:35:45 -04008376static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8377 const char *symname)
8378{
8379 struct btrfs_trans_handle *trans;
8380 struct btrfs_root *root = BTRFS_I(dir)->root;
8381 struct btrfs_path *path;
8382 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008383 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008384 int err;
8385 int drop_inode = 0;
8386 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008387 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008388 int name_len;
8389 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008390 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008391 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008392 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008393
8394 name_len = strlen(symname) + 1;
8395 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8396 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008397
Josef Bacik9ed74f22009-09-11 16:12:44 -04008398 /*
8399 * 2 items for inode item and ref
8400 * 2 items for dir items
8401 * 1 item for xattr if selinux is on
8402 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008403 trans = btrfs_start_transaction(root, 5);
8404 if (IS_ERR(trans))
8405 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008406
Li Zefan581bb052011-04-20 10:06:11 +08008407 err = btrfs_find_free_ino(root, &objectid);
8408 if (err)
8409 goto out_unlock;
8410
Josef Bacikaec74772008-07-24 12:12:38 -04008411 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008412 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008413 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008414 if (IS_ERR(inode)) {
8415 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008416 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008417 }
Chris Mason39279cc2007-06-12 06:35:45 -04008418
Eric Paris2a7dba32011-02-01 11:05:39 -05008419 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008420 if (err) {
8421 drop_inode = 1;
8422 goto out_unlock;
8423 }
8424
Casey Schauflerad19db72011-12-15 10:09:07 -05008425 /*
8426 * If the active LSM wants to access the inode during
8427 * d_instantiate it needs these. Smack checks to see
8428 * if the filesystem supports xattrs by looking at the
8429 * ops vector.
8430 */
8431 inode->i_fop = &btrfs_file_operations;
8432 inode->i_op = &btrfs_file_inode_operations;
8433
Josef Bacika1b075d2010-11-19 20:36:11 +00008434 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008435 if (err)
8436 drop_inode = 1;
8437 else {
8438 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008439 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008440 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008441 }
Chris Mason39279cc2007-06-12 06:35:45 -04008442 if (drop_inode)
8443 goto out_unlock;
8444
8445 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008446 if (!path) {
8447 err = -ENOMEM;
8448 drop_inode = 1;
8449 goto out_unlock;
8450 }
Li Zefan33345d012011-04-20 10:31:50 +08008451 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008452 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008453 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8454 datasize = btrfs_file_extent_calc_inline_size(name_len);
8455 err = btrfs_insert_empty_item(trans, root, path, &key,
8456 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008457 if (err) {
8458 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008459 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008460 goto out_unlock;
8461 }
Chris Mason5f39d392007-10-15 16:14:19 -04008462 leaf = path->nodes[0];
8463 ei = btrfs_item_ptr(leaf, path->slots[0],
8464 struct btrfs_file_extent_item);
8465 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8466 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008467 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008468 btrfs_set_file_extent_encryption(leaf, ei, 0);
8469 btrfs_set_file_extent_compression(leaf, ei, 0);
8470 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8471 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8472
Chris Mason39279cc2007-06-12 06:35:45 -04008473 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008474 write_extent_buffer(leaf, symname, ptr, name_len);
8475 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008476 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008477
Chris Mason39279cc2007-06-12 06:35:45 -04008478 inode->i_op = &btrfs_symlink_inode_operations;
8479 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008480 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008481 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008482 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008483 err = btrfs_update_inode(trans, root, inode);
8484 if (err)
8485 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008486
8487out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008488 if (!err)
8489 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008490 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008491 if (drop_inode) {
8492 inode_dec_link_count(inode);
8493 iput(inode);
8494 }
Liu Bob53d3f52012-11-14 14:34:34 +00008495 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008496 return err;
8497}
Chris Mason16432982008-04-10 10:23:21 -04008498
Josef Bacik0af3d002010-06-21 14:48:16 -04008499static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8500 u64 start, u64 num_bytes, u64 min_size,
8501 loff_t actual_len, u64 *alloc_hint,
8502 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008503{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008504 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8505 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008506 struct btrfs_root *root = BTRFS_I(inode)->root;
8507 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008508 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008509 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008510 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008511 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008512 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008513
Josef Bacik0af3d002010-06-21 14:48:16 -04008514 if (trans)
8515 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008516 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008517 if (own_trans) {
8518 trans = btrfs_start_transaction(root, 3);
8519 if (IS_ERR(trans)) {
8520 ret = PTR_ERR(trans);
8521 break;
8522 }
Yan Zhengd899e052008-10-30 14:25:28 -04008523 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008524
Chris Mason154ea282013-03-05 11:11:26 -05008525 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8526 cur_bytes = max(cur_bytes, min_size);
8527 ret = btrfs_reserve_extent(trans, root, cur_bytes,
Zach Brown24542bf2012-11-16 00:04:43 +00008528 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008529 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008530 if (own_trans)
8531 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008532 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008533 }
8534
Yan Zhengd899e052008-10-30 14:25:28 -04008535 ret = insert_reserved_file_extent(trans, inode,
8536 cur_offset, ins.objectid,
8537 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008538 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008539 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008540 if (ret) {
8541 btrfs_abort_transaction(trans, root, ret);
8542 if (own_trans)
8543 btrfs_end_transaction(trans, root);
8544 break;
8545 }
Chris Masona1ed8352009-09-11 12:27:37 -04008546 btrfs_drop_extent_cache(inode, cur_offset,
8547 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008548
Josef Bacik5dc562c2012-08-17 13:14:17 -04008549 em = alloc_extent_map();
8550 if (!em) {
8551 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8552 &BTRFS_I(inode)->runtime_flags);
8553 goto next;
8554 }
8555
8556 em->start = cur_offset;
8557 em->orig_start = cur_offset;
8558 em->len = ins.offset;
8559 em->block_start = ins.objectid;
8560 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008561 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008562 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008563 em->bdev = root->fs_info->fs_devices->latest_bdev;
8564 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8565 em->generation = trans->transid;
8566
8567 while (1) {
8568 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008569 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008570 write_unlock(&em_tree->lock);
8571 if (ret != -EEXIST)
8572 break;
8573 btrfs_drop_extent_cache(inode, cur_offset,
8574 cur_offset + ins.offset - 1,
8575 0);
8576 }
8577 free_extent_map(em);
8578next:
Yan Zhengd899e052008-10-30 14:25:28 -04008579 num_bytes -= ins.offset;
8580 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008581 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008582
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008583 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008584 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008585 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008586 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008587 (actual_len > inode->i_size) &&
8588 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008589 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008590 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008591 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008592 i_size = cur_offset;
8593 i_size_write(inode, i_size);
8594 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008595 }
8596
Yan Zhengd899e052008-10-30 14:25:28 -04008597 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008598
8599 if (ret) {
8600 btrfs_abort_transaction(trans, root, ret);
8601 if (own_trans)
8602 btrfs_end_transaction(trans, root);
8603 break;
8604 }
Yan Zhengd899e052008-10-30 14:25:28 -04008605
Josef Bacik0af3d002010-06-21 14:48:16 -04008606 if (own_trans)
8607 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008608 }
Yan Zhengd899e052008-10-30 14:25:28 -04008609 return ret;
8610}
8611
Josef Bacik0af3d002010-06-21 14:48:16 -04008612int btrfs_prealloc_file_range(struct inode *inode, int mode,
8613 u64 start, u64 num_bytes, u64 min_size,
8614 loff_t actual_len, u64 *alloc_hint)
8615{
8616 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8617 min_size, actual_len, alloc_hint,
8618 NULL);
8619}
8620
8621int btrfs_prealloc_file_range_trans(struct inode *inode,
8622 struct btrfs_trans_handle *trans, int mode,
8623 u64 start, u64 num_bytes, u64 min_size,
8624 loff_t actual_len, u64 *alloc_hint)
8625{
8626 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8627 min_size, actual_len, alloc_hint, trans);
8628}
8629
Chris Masone6dcd2d2008-07-17 12:53:50 -04008630static int btrfs_set_page_dirty(struct page *page)
8631{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008632 return __set_page_dirty_nobuffers(page);
8633}
8634
Al Viro10556cb2011-06-20 19:28:19 -04008635static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008636{
Li Zefanb83cc962010-12-20 16:04:08 +08008637 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008638 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008639
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008640 if (mask & MAY_WRITE &&
8641 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8642 if (btrfs_root_readonly(root))
8643 return -EROFS;
8644 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8645 return -EACCES;
8646 }
Al Viro2830ba72011-06-20 19:16:29 -04008647 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008648}
Chris Mason39279cc2007-06-12 06:35:45 -04008649
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008650static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008651 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008652 .lookup = btrfs_lookup,
8653 .create = btrfs_create,
8654 .unlink = btrfs_unlink,
8655 .link = btrfs_link,
8656 .mkdir = btrfs_mkdir,
8657 .rmdir = btrfs_rmdir,
8658 .rename = btrfs_rename,
8659 .symlink = btrfs_symlink,
8660 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008661 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008662 .setxattr = btrfs_setxattr,
8663 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008664 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008665 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008666 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008667 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008668};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008669static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008670 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008671 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008672 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008673};
Yan, Zheng76dda932009-09-21 16:00:26 -04008674
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008675static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008676 .llseek = generic_file_llseek,
8677 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04008678 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008679 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008680#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008681 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008682#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008683 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008684 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008685};
8686
Chris Masond1310b22008-01-24 16:13:08 -05008687static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008688 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008689 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008690 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008691 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008692 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008693 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008694 .set_bit_hook = btrfs_set_bit_hook,
8695 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008696 .merge_extent_hook = btrfs_merge_extent_hook,
8697 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008698};
8699
Chris Mason35054392009-01-21 13:11:13 -05008700/*
8701 * btrfs doesn't support the bmap operation because swapfiles
8702 * use bmap to make a mapping of extents in the file. They assume
8703 * these extents won't change over the life of the file and they
8704 * use the bmap result to do IO directly to the drive.
8705 *
8706 * the btrfs bmap call would return logical addresses that aren't
8707 * suitable for IO and they also will change frequently as COW
8708 * operations happen. So, swapfile + btrfs == corruption.
8709 *
8710 * For now we're avoiding this by dropping bmap.
8711 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008712static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008713 .readpage = btrfs_readpage,
8714 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008715 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008716 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008717 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008718 .invalidatepage = btrfs_invalidatepage,
8719 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008720 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008721 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008722};
8723
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008724static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008725 .readpage = btrfs_readpage,
8726 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008727 .invalidatepage = btrfs_invalidatepage,
8728 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008729};
8730
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008731static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008732 .getattr = btrfs_getattr,
8733 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008734 .setxattr = btrfs_setxattr,
8735 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008736 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008737 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008738 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008739 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008740 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008741 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008742};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008743static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008744 .getattr = btrfs_getattr,
8745 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008746 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008747 .setxattr = btrfs_setxattr,
8748 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008749 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008750 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008751 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008752 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008753};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008754static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008755 .readlink = generic_readlink,
8756 .follow_link = page_follow_link_light,
8757 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008758 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008759 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008760 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008761 .setxattr = btrfs_setxattr,
8762 .getxattr = btrfs_getxattr,
8763 .listxattr = btrfs_listxattr,
8764 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008765 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008766 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008767};
Yan, Zheng76dda932009-09-21 16:00:26 -04008768
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008769const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008770 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008771 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008772};