blob: c72033ee6017b09325a752c0e3bb760aa6ffc147 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070035#include <linux/aio.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050037#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040038#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040039#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020041#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050042#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000043#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050044#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040045#include <linux/posix_acl_xattr.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050046#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040047#include "ctree.h"
48#include "disk-io.h"
49#include "transaction.h"
50#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040051#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040052#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040053#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040054#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020055#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040056#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050057#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050058#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080059#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000060#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040061#include "hash.h"
Chris Mason39279cc2007-06-12 06:35:45 -040062
63struct btrfs_iget_args {
64 u64 ino;
65 struct btrfs_root *root;
66};
67
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070068static const struct inode_operations btrfs_dir_inode_operations;
69static const struct inode_operations btrfs_symlink_inode_operations;
70static const struct inode_operations btrfs_dir_ro_inode_operations;
71static const struct inode_operations btrfs_special_inode_operations;
72static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070073static const struct address_space_operations btrfs_aops;
74static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070075static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050076static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040077
78static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000079static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040080struct kmem_cache *btrfs_trans_handle_cachep;
81struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040082struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050083struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040084
85#define S_SHIFT 12
86static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
87 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
88 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
89 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
90 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
91 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
92 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
93 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
94};
95
Eric Sandeen3972f262013-01-12 02:57:22 +000096static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050097static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040098static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050099static noinline int cow_file_range(struct inode *inode,
100 struct page *locked_page,
101 u64 start, u64 end, int *page_started,
102 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400103static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
104 u64 len, u64 orig_start,
105 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400106 u64 orig_block_len, u64 ram_bytes,
107 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400108
Eric Sandeen48a3b632013-04-25 20:41:01 +0000109static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400110
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000111static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500112 struct inode *inode, struct inode *dir,
113 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500114{
115 int err;
116
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000117 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500118 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500119 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500120 return err;
121}
122
Chris Masond352ac62008-09-29 15:18:18 -0400123/*
Chris Masonc8b97812008-10-29 14:49:59 -0400124 * this does all the hard work for inserting an inline extent into
125 * the btree. The caller should have done a btrfs_drop_extents so that
126 * no overlapping inline items exist in the btree
127 */
Chris Masond3977122009-01-05 21:25:51 -0500128static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400129 struct btrfs_root *root, struct inode *inode,
130 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000131 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400132 struct page **compressed_pages)
133{
134 struct btrfs_key key;
135 struct btrfs_path *path;
136 struct extent_buffer *leaf;
137 struct page *page = NULL;
138 char *kaddr;
139 unsigned long ptr;
140 struct btrfs_file_extent_item *ei;
141 int err = 0;
142 int ret;
143 size_t cur_size = size;
144 size_t datasize;
145 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400146
Li Zefanfe3f5662011-03-28 08:30:38 +0000147 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400148 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400149
Chris Masond3977122009-01-05 21:25:51 -0500150 path = btrfs_alloc_path();
151 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400152 return -ENOMEM;
153
Chris Masonb9473432009-03-13 11:00:37 -0400154 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400155
Li Zefan33345d012011-04-20 10:31:50 +0800156 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400157 key.offset = start;
158 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400159 datasize = btrfs_file_extent_calc_inline_size(cur_size);
160
161 inode_add_bytes(inode, size);
162 ret = btrfs_insert_empty_item(trans, root, path, &key,
163 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400164 if (ret) {
165 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400166 goto fail;
167 }
168 leaf = path->nodes[0];
169 ei = btrfs_item_ptr(leaf, path->slots[0],
170 struct btrfs_file_extent_item);
171 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
172 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
173 btrfs_set_file_extent_encryption(leaf, ei, 0);
174 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
175 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
176 ptr = btrfs_file_extent_inline_start(ei);
177
Li Zefan261507a02010-12-17 14:21:50 +0800178 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400179 struct page *cpage;
180 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500181 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400182 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500183 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400184 PAGE_CACHE_SIZE);
185
Cong Wang7ac687d2011-11-25 23:14:28 +0800186 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400187 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800188 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400189
190 i++;
191 ptr += cur_size;
192 compressed_size -= cur_size;
193 }
194 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800195 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400196 } else {
197 page = find_get_page(inode->i_mapping,
198 start >> PAGE_CACHE_SHIFT);
199 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800200 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400201 offset = start & (PAGE_CACHE_SIZE - 1);
202 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800203 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400204 page_cache_release(page);
205 }
206 btrfs_mark_buffer_dirty(leaf);
207 btrfs_free_path(path);
208
Yan, Zhengc2167752009-11-12 09:34:21 +0000209 /*
210 * we're an inline extent, so nobody can
211 * extend the file past i_size without locking
212 * a page we already have locked.
213 *
214 * We must do any isize and inode updates
215 * before we unlock the pages. Otherwise we
216 * could end up racing with unlink.
217 */
Chris Masonc8b97812008-10-29 14:49:59 -0400218 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100219 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000220
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100221 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400222fail:
223 btrfs_free_path(path);
224 return err;
225}
226
227
228/*
229 * conditionally insert an inline extent into the file. This
230 * does the checks required to make sure the data is small enough
231 * to fit as an inline extent.
232 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400233static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400234 struct btrfs_root *root,
235 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000236 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400237 struct page **compressed_pages)
238{
239 u64 isize = i_size_read(inode);
240 u64 actual_end = min(end + 1, isize);
241 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000242 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400243 u64 data_len = inline_len;
244 int ret;
245
246 if (compressed_size)
247 data_len = compressed_size;
248
249 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400250 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400251 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
252 (!compressed_size &&
253 (actual_end & (root->sectorsize - 1)) == 0) ||
254 end + 1 < isize ||
255 data_len > root->fs_info->max_inline) {
256 return 1;
257 }
258
Josef Bacik26714852012-08-29 12:24:27 -0400259 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100260 if (ret)
261 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400262
263 if (isize > actual_end)
264 inline_len = min_t(u64, isize, actual_end);
265 ret = insert_inline_extent(trans, root, inode, start,
266 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000267 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400268 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100269 btrfs_abort_transaction(trans, root, ret);
270 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400271 } else if (ret == -ENOSPC) {
272 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100273 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400274
Josef Bacikbdc20e62013-02-28 13:23:38 -0500275 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400276 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400277 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400278 return 0;
279}
280
Chris Mason771ed682008-11-06 22:02:51 -0500281struct async_extent {
282 u64 start;
283 u64 ram_size;
284 u64 compressed_size;
285 struct page **pages;
286 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800287 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500288 struct list_head list;
289};
290
291struct async_cow {
292 struct inode *inode;
293 struct btrfs_root *root;
294 struct page *locked_page;
295 u64 start;
296 u64 end;
297 struct list_head extents;
298 struct btrfs_work work;
299};
300
301static noinline int add_async_extent(struct async_cow *cow,
302 u64 start, u64 ram_size,
303 u64 compressed_size,
304 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800305 unsigned long nr_pages,
306 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500307{
308 struct async_extent *async_extent;
309
310 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100311 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500312 async_extent->start = start;
313 async_extent->ram_size = ram_size;
314 async_extent->compressed_size = compressed_size;
315 async_extent->pages = pages;
316 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800317 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500318 list_add_tail(&async_extent->list, &cow->extents);
319 return 0;
320}
321
Chris Masonc8b97812008-10-29 14:49:59 -0400322/*
Chris Mason771ed682008-11-06 22:02:51 -0500323 * we create compressed extents in two phases. The first
324 * phase compresses a range of pages that have already been
325 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400326 *
Chris Mason771ed682008-11-06 22:02:51 -0500327 * This is done inside an ordered work queue, and the compression
328 * is spread across many cpus. The actual IO submission is step
329 * two, and the ordered work queue takes care of making sure that
330 * happens in the same order things were put onto the queue by
331 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400332 *
Chris Mason771ed682008-11-06 22:02:51 -0500333 * If this code finds it can't get good compression, it puts an
334 * entry onto the work queue to write the uncompressed bytes. This
335 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300336 * are written in the same order that the flusher thread sent them
337 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400338 */
Chris Mason771ed682008-11-06 22:02:51 -0500339static noinline int compress_file_range(struct inode *inode,
340 struct page *locked_page,
341 u64 start, u64 end,
342 struct async_cow *async_cow,
343 int *num_added)
Chris 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 Bacik7a7eaa42011-04-13 12:54:33 -0400464 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100465 if (IS_ERR(trans)) {
466 ret = PTR_ERR(trans);
467 trans = NULL;
468 goto cleanup_and_out;
469 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400470 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500471
Chris Masonc8b97812008-10-29 14:49:59 -0400472 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500473 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400474 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500475 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400476 */
477 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000478 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400479 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500480 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400481 ret = cow_file_range_inline(trans, root, inode,
482 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000483 total_compressed,
484 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400485 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100486 if (ret <= 0) {
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, Zhengc2167752009-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, Zhengc2167752009-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 Bacik7a7eaa42011-04-13 12:54:33 -0400681 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100682 if (IS_ERR(trans)) {
683 ret = PTR_ERR(trans);
684 } else {
685 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
686 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500687 async_extent->compressed_size,
688 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500689 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600690 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100691 btrfs_abort_transaction(trans, root, ret);
692 btrfs_end_transaction(trans, root);
693 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000694
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500695 if (ret) {
696 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500697
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500698 for (i = 0; i < async_extent->nr_pages; i++) {
699 WARN_ON(async_extent->pages[i]->mapping);
700 page_cache_release(async_extent->pages[i]);
701 }
702 kfree(async_extent->pages);
703 async_extent->nr_pages = 0;
704 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500705
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400706 if (ret == -ENOSPC) {
707 unlock_extent(io_tree, async_extent->start,
708 async_extent->start +
709 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100710 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400711 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500712 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500713 }
714
Yan, Zhengc2167752009-11-12 09:34:21 +0000715 /*
716 * here we're doing allocation and writeback of the
717 * compressed pages
718 */
719 btrfs_drop_extent_cache(inode, async_extent->start,
720 async_extent->start +
721 async_extent->ram_size - 1, 0);
722
David Sterba172ddd62011-04-21 00:48:27 +0200723 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000724 if (!em) {
725 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500726 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000727 }
Chris Mason771ed682008-11-06 22:02:51 -0500728 em->start = async_extent->start;
729 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500730 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400731 em->mod_start = em->start;
732 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500733
734 em->block_start = ins.objectid;
735 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500736 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400737 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500738 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800739 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500740 set_bit(EXTENT_FLAG_PINNED, &em->flags);
741 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400742 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500743
Chris Masond3977122009-01-05 21:25:51 -0500744 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400745 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400746 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400747 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500748 if (ret != -EEXIST) {
749 free_extent_map(em);
750 break;
751 }
752 btrfs_drop_extent_cache(inode, async_extent->start,
753 async_extent->start +
754 async_extent->ram_size - 1, 0);
755 }
756
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500757 if (ret)
758 goto out_free_reserve;
759
Li Zefan261507a02010-12-17 14:21:50 +0800760 ret = btrfs_add_ordered_extent_compress(inode,
761 async_extent->start,
762 ins.objectid,
763 async_extent->ram_size,
764 ins.offset,
765 BTRFS_ORDERED_COMPRESSED,
766 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500767 if (ret)
768 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500769
Chris Mason771ed682008-11-06 22:02:51 -0500770 /*
771 * clear dirty, set writeback and unlock the pages.
772 */
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, Zhengc2167752009-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 Bacik7a7eaa42011-04-13 12:54:33 -04001239 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001240 else
Josef Bacik7a7eaa42011-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 Xiedf0af1a2013-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 Xiedf0af1a2013-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 Xiedf0af1a2013-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 Xiedf0af1a2013-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 Xiedf0af1a2013-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 Xiedf0af1a2013-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 }
2135
2136 while (1) {
2137 cond_resched();
2138
2139 leaf = path->nodes[0];
2140 slot = path->slots[0];
2141
2142 if (slot >= btrfs_header_nritems(leaf)) {
2143 ret = btrfs_next_leaf(root, path);
2144 if (ret < 0) {
2145 goto out;
2146 } else if (ret > 0) {
2147 ret = 0;
2148 goto out;
2149 }
2150 continue;
2151 }
2152
2153 path->slots[0]++;
2154
2155 btrfs_item_key_to_cpu(leaf, &key, slot);
2156
2157 if (key.objectid > inum)
2158 goto out;
2159
2160 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2161 continue;
2162
2163 extent = btrfs_item_ptr(leaf, slot,
2164 struct btrfs_file_extent_item);
2165
2166 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2167 continue;
2168
Liu Boe68afa42013-07-01 22:13:26 +08002169 /*
2170 * 'offset' refers to the exact key.offset,
2171 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2172 * (key.offset - extent_offset).
2173 */
2174 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002175 continue;
2176
Liu Boe68afa42013-07-01 22:13:26 +08002177 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002178 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002179
Liu Bo38c227d2013-01-29 03:18:40 +00002180 if (extent_offset >= old->extent_offset + old->offset +
2181 old->len || extent_offset + num_bytes <=
2182 old->extent_offset + old->offset)
2183 continue;
2184
Liu Boe68afa42013-07-01 22:13:26 +08002185 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002186 break;
2187 }
2188
2189 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2190 if (!backref) {
2191 ret = -ENOENT;
2192 goto out;
2193 }
2194
2195 backref->root_id = root_id;
2196 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002197 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002198 backref->num_bytes = num_bytes;
2199 backref->extent_offset = extent_offset;
2200 backref->generation = btrfs_file_extent_generation(leaf, extent);
2201 backref->old = old;
2202 backref_insert(&new->root, backref);
2203 old->count++;
2204out:
2205 btrfs_release_path(path);
2206 WARN_ON(ret);
2207 return ret;
2208}
2209
2210static noinline bool record_extent_backrefs(struct btrfs_path *path,
2211 struct new_sa_defrag_extent *new)
2212{
2213 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2214 struct old_sa_defrag_extent *old, *tmp;
2215 int ret;
2216
2217 new->path = path;
2218
2219 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002220 ret = iterate_inodes_from_logical(old->bytenr +
2221 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002222 path, record_one_backref,
2223 old);
2224 BUG_ON(ret < 0 && ret != -ENOENT);
2225
2226 /* no backref to be processed for this extent */
2227 if (!old->count) {
2228 list_del(&old->list);
2229 kfree(old);
2230 }
2231 }
2232
2233 if (list_empty(&new->head))
2234 return false;
2235
2236 return true;
2237}
2238
2239static int relink_is_mergable(struct extent_buffer *leaf,
2240 struct btrfs_file_extent_item *fi,
2241 u64 disk_bytenr)
2242{
2243 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2244 return 0;
2245
2246 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2247 return 0;
2248
2249 if (btrfs_file_extent_compression(leaf, fi) ||
2250 btrfs_file_extent_encryption(leaf, fi) ||
2251 btrfs_file_extent_other_encoding(leaf, fi))
2252 return 0;
2253
2254 return 1;
2255}
2256
2257/*
2258 * Note the backref might has changed, and in this case we just return 0.
2259 */
2260static noinline int relink_extent_backref(struct btrfs_path *path,
2261 struct sa_defrag_extent_backref *prev,
2262 struct sa_defrag_extent_backref *backref)
2263{
2264 struct btrfs_file_extent_item *extent;
2265 struct btrfs_file_extent_item *item;
2266 struct btrfs_ordered_extent *ordered;
2267 struct btrfs_trans_handle *trans;
2268 struct btrfs_fs_info *fs_info;
2269 struct btrfs_root *root;
2270 struct btrfs_key key;
2271 struct extent_buffer *leaf;
2272 struct old_sa_defrag_extent *old = backref->old;
2273 struct new_sa_defrag_extent *new = old->new;
2274 struct inode *src_inode = new->inode;
2275 struct inode *inode;
2276 struct extent_state *cached = NULL;
2277 int ret = 0;
2278 u64 start;
2279 u64 len;
2280 u64 lock_start;
2281 u64 lock_end;
2282 bool merge = false;
2283 int index;
2284
2285 if (prev && prev->root_id == backref->root_id &&
2286 prev->inum == backref->inum &&
2287 prev->file_pos + prev->num_bytes == backref->file_pos)
2288 merge = true;
2289
2290 /* step 1: get root */
2291 key.objectid = backref->root_id;
2292 key.type = BTRFS_ROOT_ITEM_KEY;
2293 key.offset = (u64)-1;
2294
2295 fs_info = BTRFS_I(src_inode)->root->fs_info;
2296 index = srcu_read_lock(&fs_info->subvol_srcu);
2297
2298 root = btrfs_read_fs_root_no_name(fs_info, &key);
2299 if (IS_ERR(root)) {
2300 srcu_read_unlock(&fs_info->subvol_srcu, index);
2301 if (PTR_ERR(root) == -ENOENT)
2302 return 0;
2303 return PTR_ERR(root);
2304 }
Liu Bo38c227d2013-01-29 03:18:40 +00002305
2306 /* step 2: get inode */
2307 key.objectid = backref->inum;
2308 key.type = BTRFS_INODE_ITEM_KEY;
2309 key.offset = 0;
2310
2311 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2312 if (IS_ERR(inode)) {
2313 srcu_read_unlock(&fs_info->subvol_srcu, index);
2314 return 0;
2315 }
2316
2317 srcu_read_unlock(&fs_info->subvol_srcu, index);
2318
2319 /* step 3: relink backref */
2320 lock_start = backref->file_pos;
2321 lock_end = backref->file_pos + backref->num_bytes - 1;
2322 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2323 0, &cached);
2324
2325 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2326 if (ordered) {
2327 btrfs_put_ordered_extent(ordered);
2328 goto out_unlock;
2329 }
2330
2331 trans = btrfs_join_transaction(root);
2332 if (IS_ERR(trans)) {
2333 ret = PTR_ERR(trans);
2334 goto out_unlock;
2335 }
2336
2337 key.objectid = backref->inum;
2338 key.type = BTRFS_EXTENT_DATA_KEY;
2339 key.offset = backref->file_pos;
2340
2341 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2342 if (ret < 0) {
2343 goto out_free_path;
2344 } else if (ret > 0) {
2345 ret = 0;
2346 goto out_free_path;
2347 }
2348
2349 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2350 struct btrfs_file_extent_item);
2351
2352 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2353 backref->generation)
2354 goto out_free_path;
2355
2356 btrfs_release_path(path);
2357
2358 start = backref->file_pos;
2359 if (backref->extent_offset < old->extent_offset + old->offset)
2360 start += old->extent_offset + old->offset -
2361 backref->extent_offset;
2362
2363 len = min(backref->extent_offset + backref->num_bytes,
2364 old->extent_offset + old->offset + old->len);
2365 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2366
2367 ret = btrfs_drop_extents(trans, root, inode, start,
2368 start + len, 1);
2369 if (ret)
2370 goto out_free_path;
2371again:
2372 key.objectid = btrfs_ino(inode);
2373 key.type = BTRFS_EXTENT_DATA_KEY;
2374 key.offset = start;
2375
Liu Boa09a0a72013-03-11 09:20:58 +00002376 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002377 if (merge) {
2378 struct btrfs_file_extent_item *fi;
2379 u64 extent_len;
2380 struct btrfs_key found_key;
2381
2382 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2383 if (ret < 0)
2384 goto out_free_path;
2385
2386 path->slots[0]--;
2387 leaf = path->nodes[0];
2388 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2389
2390 fi = btrfs_item_ptr(leaf, path->slots[0],
2391 struct btrfs_file_extent_item);
2392 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2393
2394 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2395 extent_len + found_key.offset == start) {
2396 btrfs_set_file_extent_num_bytes(leaf, fi,
2397 extent_len + len);
2398 btrfs_mark_buffer_dirty(leaf);
2399 inode_add_bytes(inode, len);
2400
2401 ret = 1;
2402 goto out_free_path;
2403 } else {
2404 merge = false;
2405 btrfs_release_path(path);
2406 goto again;
2407 }
2408 }
2409
2410 ret = btrfs_insert_empty_item(trans, root, path, &key,
2411 sizeof(*extent));
2412 if (ret) {
2413 btrfs_abort_transaction(trans, root, ret);
2414 goto out_free_path;
2415 }
2416
2417 leaf = path->nodes[0];
2418 item = btrfs_item_ptr(leaf, path->slots[0],
2419 struct btrfs_file_extent_item);
2420 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2421 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2422 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2423 btrfs_set_file_extent_num_bytes(leaf, item, len);
2424 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2425 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2426 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2427 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2428 btrfs_set_file_extent_encryption(leaf, item, 0);
2429 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2430
2431 btrfs_mark_buffer_dirty(leaf);
2432 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002433 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002434
2435 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2436 new->disk_len, 0,
2437 backref->root_id, backref->inum,
2438 new->file_pos, 0); /* start - extent_offset */
2439 if (ret) {
2440 btrfs_abort_transaction(trans, root, ret);
2441 goto out_free_path;
2442 }
2443
2444 ret = 1;
2445out_free_path:
2446 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002447 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002448 btrfs_end_transaction(trans, root);
2449out_unlock:
2450 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2451 &cached, GFP_NOFS);
2452 iput(inode);
2453 return ret;
2454}
2455
2456static void relink_file_extents(struct new_sa_defrag_extent *new)
2457{
2458 struct btrfs_path *path;
2459 struct old_sa_defrag_extent *old, *tmp;
2460 struct sa_defrag_extent_backref *backref;
2461 struct sa_defrag_extent_backref *prev = NULL;
2462 struct inode *inode;
2463 struct btrfs_root *root;
2464 struct rb_node *node;
2465 int ret;
2466
2467 inode = new->inode;
2468 root = BTRFS_I(inode)->root;
2469
2470 path = btrfs_alloc_path();
2471 if (!path)
2472 return;
2473
2474 if (!record_extent_backrefs(path, new)) {
2475 btrfs_free_path(path);
2476 goto out;
2477 }
2478 btrfs_release_path(path);
2479
2480 while (1) {
2481 node = rb_first(&new->root);
2482 if (!node)
2483 break;
2484 rb_erase(node, &new->root);
2485
2486 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2487
2488 ret = relink_extent_backref(path, prev, backref);
2489 WARN_ON(ret < 0);
2490
2491 kfree(prev);
2492
2493 if (ret == 1)
2494 prev = backref;
2495 else
2496 prev = NULL;
2497 cond_resched();
2498 }
2499 kfree(prev);
2500
2501 btrfs_free_path(path);
2502
2503 list_for_each_entry_safe(old, tmp, &new->head, list) {
2504 list_del(&old->list);
2505 kfree(old);
2506 }
2507out:
2508 atomic_dec(&root->fs_info->defrag_running);
2509 wake_up(&root->fs_info->transaction_wait);
2510
2511 kfree(new);
2512}
2513
2514static struct new_sa_defrag_extent *
2515record_old_file_extents(struct inode *inode,
2516 struct btrfs_ordered_extent *ordered)
2517{
2518 struct btrfs_root *root = BTRFS_I(inode)->root;
2519 struct btrfs_path *path;
2520 struct btrfs_key key;
2521 struct old_sa_defrag_extent *old, *tmp;
2522 struct new_sa_defrag_extent *new;
2523 int ret;
2524
2525 new = kmalloc(sizeof(*new), GFP_NOFS);
2526 if (!new)
2527 return NULL;
2528
2529 new->inode = inode;
2530 new->file_pos = ordered->file_offset;
2531 new->len = ordered->len;
2532 new->bytenr = ordered->start;
2533 new->disk_len = ordered->disk_len;
2534 new->compress_type = ordered->compress_type;
2535 new->root = RB_ROOT;
2536 INIT_LIST_HEAD(&new->head);
2537
2538 path = btrfs_alloc_path();
2539 if (!path)
2540 goto out_kfree;
2541
2542 key.objectid = btrfs_ino(inode);
2543 key.type = BTRFS_EXTENT_DATA_KEY;
2544 key.offset = new->file_pos;
2545
2546 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2547 if (ret < 0)
2548 goto out_free_path;
2549 if (ret > 0 && path->slots[0] > 0)
2550 path->slots[0]--;
2551
2552 /* find out all the old extents for the file range */
2553 while (1) {
2554 struct btrfs_file_extent_item *extent;
2555 struct extent_buffer *l;
2556 int slot;
2557 u64 num_bytes;
2558 u64 offset;
2559 u64 end;
2560 u64 disk_bytenr;
2561 u64 extent_offset;
2562
2563 l = path->nodes[0];
2564 slot = path->slots[0];
2565
2566 if (slot >= btrfs_header_nritems(l)) {
2567 ret = btrfs_next_leaf(root, path);
2568 if (ret < 0)
2569 goto out_free_list;
2570 else if (ret > 0)
2571 break;
2572 continue;
2573 }
2574
2575 btrfs_item_key_to_cpu(l, &key, slot);
2576
2577 if (key.objectid != btrfs_ino(inode))
2578 break;
2579 if (key.type != BTRFS_EXTENT_DATA_KEY)
2580 break;
2581 if (key.offset >= new->file_pos + new->len)
2582 break;
2583
2584 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2585
2586 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2587 if (key.offset + num_bytes < new->file_pos)
2588 goto next;
2589
2590 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2591 if (!disk_bytenr)
2592 goto next;
2593
2594 extent_offset = btrfs_file_extent_offset(l, extent);
2595
2596 old = kmalloc(sizeof(*old), GFP_NOFS);
2597 if (!old)
2598 goto out_free_list;
2599
2600 offset = max(new->file_pos, key.offset);
2601 end = min(new->file_pos + new->len, key.offset + num_bytes);
2602
2603 old->bytenr = disk_bytenr;
2604 old->extent_offset = extent_offset;
2605 old->offset = offset - key.offset;
2606 old->len = end - offset;
2607 old->new = new;
2608 old->count = 0;
2609 list_add_tail(&old->list, &new->head);
2610next:
2611 path->slots[0]++;
2612 cond_resched();
2613 }
2614
2615 btrfs_free_path(path);
2616 atomic_inc(&root->fs_info->defrag_running);
2617
2618 return new;
2619
2620out_free_list:
2621 list_for_each_entry_safe(old, tmp, &new->head, list) {
2622 list_del(&old->list);
2623 kfree(old);
2624 }
2625out_free_path:
2626 btrfs_free_path(path);
2627out_kfree:
2628 kfree(new);
2629 return NULL;
2630}
2631
Chris Mason5d13a982009-03-13 11:41:46 -04002632/*
2633 * helper function for btrfs_finish_ordered_io, this
2634 * just reads in some of the csum leaves to prime them into ram
2635 * before we start the transaction. It limits the amount of btree
2636 * reads required while inside the transaction.
2637 */
Chris Masond352ac62008-09-29 15:18:18 -04002638/* as ordered data IO finishes, this gets called so we can finish
2639 * an ordered extent if the range of bytes in the file it covers are
2640 * fully written.
2641 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002642static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002643{
Josef Bacik5fd02042012-05-02 14:00:54 -04002644 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002645 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002646 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002647 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002648 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002649 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002650 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002651 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002652 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002653
Liu Bo83eea1f2012-07-10 05:28:39 -06002654 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002655
Josef Bacik5fd02042012-05-02 14:00:54 -04002656 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2657 ret = -EIO;
2658 goto out;
2659 }
2660
Yan, Zhengc2167752009-11-12 09:34:21 +00002661 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002662 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002663 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2664 if (nolock)
2665 trans = btrfs_join_transaction_nolock(root);
2666 else
2667 trans = btrfs_join_transaction(root);
2668 if (IS_ERR(trans)) {
2669 ret = PTR_ERR(trans);
2670 trans = NULL;
2671 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002672 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002673 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2674 ret = btrfs_update_inode_fallback(trans, root, inode);
2675 if (ret) /* -ENOMEM or corruption */
2676 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002677 goto out;
2678 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002679
Josef Bacik2ac55d42010-02-03 19:33:23 +00002680 lock_extent_bits(io_tree, ordered_extent->file_offset,
2681 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002682 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002683
Liu Bo38c227d2013-01-29 03:18:40 +00002684 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2685 ordered_extent->file_offset + ordered_extent->len - 1,
2686 EXTENT_DEFRAG, 1, cached_state);
2687 if (ret) {
2688 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2689 if (last_snapshot >= BTRFS_I(inode)->generation)
2690 /* the inode is shared */
2691 new = record_old_file_extents(inode, ordered_extent);
2692
2693 clear_extent_bit(io_tree, ordered_extent->file_offset,
2694 ordered_extent->file_offset + ordered_extent->len - 1,
2695 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2696 }
2697
Josef Bacik0cb59c92010-07-02 12:14:14 -04002698 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002699 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002700 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002701 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002702 if (IS_ERR(trans)) {
2703 ret = PTR_ERR(trans);
2704 trans = NULL;
2705 goto out_unlock;
2706 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002707 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002708
Chris Masonc8b97812008-10-29 14:49:59 -04002709 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002710 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002711 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002712 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002713 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002714 ordered_extent->file_offset,
2715 ordered_extent->file_offset +
2716 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002717 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002718 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002719 ret = insert_reserved_file_extent(trans, inode,
2720 ordered_extent->file_offset,
2721 ordered_extent->start,
2722 ordered_extent->disk_len,
2723 ordered_extent->len,
2724 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002725 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002726 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002727 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002728 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2729 ordered_extent->file_offset, ordered_extent->len,
2730 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002731 if (ret < 0) {
2732 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002733 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002734 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002735
Chris Masone6dcd2d2008-07-17 12:53:50 -04002736 add_pending_csums(trans, inode, ordered_extent->file_offset,
2737 &ordered_extent->list);
2738
Josef Bacik6c760c02012-11-09 10:53:21 -05002739 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2740 ret = btrfs_update_inode_fallback(trans, root, inode);
2741 if (ret) { /* -ENOMEM or corruption */
2742 btrfs_abort_transaction(trans, root, ret);
2743 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002744 }
2745 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002746out_unlock:
2747 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2748 ordered_extent->file_offset +
2749 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002750out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002751 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002752 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002753 if (trans)
2754 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002755
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002756 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002757 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2758 ordered_extent->file_offset +
2759 ordered_extent->len - 1, NULL, GFP_NOFS);
2760
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002761 /*
2762 * If the ordered extent had an IOERR or something else went
2763 * wrong we need to return the space for this ordered extent
2764 * back to the allocator.
2765 */
2766 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2767 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2768 btrfs_free_reserved_extent(root, ordered_extent->start,
2769 ordered_extent->disk_len);
2770 }
2771
2772
Josef Bacik5fd02042012-05-02 14:00:54 -04002773 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002774 * This needs to be done to make sure anybody waiting knows we are done
2775 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002776 */
2777 btrfs_remove_ordered_extent(inode, ordered_extent);
2778
Liu Bo38c227d2013-01-29 03:18:40 +00002779 /* for snapshot-aware defrag */
2780 if (new)
2781 relink_file_extents(new);
2782
Chris Masone6dcd2d2008-07-17 12:53:50 -04002783 /* once for us */
2784 btrfs_put_ordered_extent(ordered_extent);
2785 /* once for the tree */
2786 btrfs_put_ordered_extent(ordered_extent);
2787
Josef Bacik5fd02042012-05-02 14:00:54 -04002788 return ret;
2789}
2790
2791static void finish_ordered_fn(struct btrfs_work *work)
2792{
2793 struct btrfs_ordered_extent *ordered_extent;
2794 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2795 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002796}
2797
Christoph Hellwigb2950862008-12-02 09:54:17 -05002798static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002799 struct extent_state *state, int uptodate)
2800{
Josef Bacik5fd02042012-05-02 14:00:54 -04002801 struct inode *inode = page->mapping->host;
2802 struct btrfs_root *root = BTRFS_I(inode)->root;
2803 struct btrfs_ordered_extent *ordered_extent = NULL;
2804 struct btrfs_workers *workers;
2805
liubo1abe9b82011-03-24 11:18:59 +00002806 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2807
Chris Mason8b62b722009-09-02 16:53:46 -04002808 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002809 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2810 end - start + 1, uptodate))
2811 return 0;
2812
2813 ordered_extent->work.func = finish_ordered_fn;
2814 ordered_extent->work.flags = 0;
2815
Liu Bo83eea1f2012-07-10 05:28:39 -06002816 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002817 workers = &root->fs_info->endio_freespace_worker;
2818 else
2819 workers = &root->fs_info->endio_write_workers;
2820 btrfs_queue_worker(workers, &ordered_extent->work);
2821
2822 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002823}
2824
Chris Masond352ac62008-09-29 15:18:18 -04002825/*
Chris Masond352ac62008-09-29 15:18:18 -04002826 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002827 * if there's a match, we allow the bio to finish. If not, the code in
2828 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002829 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002830static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002831 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002832{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002833 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002834 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002835 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002836 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002837 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002838 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002839 struct btrfs_root *root = BTRFS_I(inode)->root;
2840 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002841 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2842 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002843
Chris Masond20f7042008-12-08 16:58:54 -05002844 if (PageChecked(page)) {
2845 ClearPageChecked(page);
2846 goto good;
2847 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002848
2849 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002850 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002851
Yan Zheng17d217f2008-12-12 10:03:38 -05002852 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002853 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002854 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2855 GFP_NOFS);
2856 return 0;
2857 }
2858
Yanc2e639f2008-02-04 08:57:25 -05002859 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002860 private = state->private;
2861 ret = 0;
2862 } else {
2863 ret = get_state_private(io_tree, start, &private);
2864 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002865 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002866 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002867 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002868
Liu Bob0496682013-03-14 14:57:45 +00002869 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002870 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002871 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002872 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002873
Cong Wang7ac687d2011-11-25 23:14:28 +08002874 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002875good:
Chris Mason07157aa2007-08-30 08:50:51 -04002876 return 0;
2877
2878zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002879 if (__ratelimit(&_rs))
2880 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u private %llu",
2881 (unsigned long long)btrfs_ino(page->mapping->host),
2882 (unsigned long long)start, csum,
2883 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002884 memset(kaddr + offset, 1, end - start + 1);
2885 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002886 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002887 if (private == 0)
2888 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002889 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002890}
Chris Masonb888db22007-08-27 16:49:44 -04002891
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002892struct delayed_iput {
2893 struct list_head list;
2894 struct inode *inode;
2895};
2896
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002897/* JDM: If this is fs-wide, why can't we add a pointer to
2898 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002899void btrfs_add_delayed_iput(struct inode *inode)
2900{
2901 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2902 struct delayed_iput *delayed;
2903
2904 if (atomic_add_unless(&inode->i_count, -1, 1))
2905 return;
2906
2907 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2908 delayed->inode = inode;
2909
2910 spin_lock(&fs_info->delayed_iput_lock);
2911 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2912 spin_unlock(&fs_info->delayed_iput_lock);
2913}
2914
2915void btrfs_run_delayed_iputs(struct btrfs_root *root)
2916{
2917 LIST_HEAD(list);
2918 struct btrfs_fs_info *fs_info = root->fs_info;
2919 struct delayed_iput *delayed;
2920 int empty;
2921
2922 spin_lock(&fs_info->delayed_iput_lock);
2923 empty = list_empty(&fs_info->delayed_iputs);
2924 spin_unlock(&fs_info->delayed_iput_lock);
2925 if (empty)
2926 return;
2927
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002928 spin_lock(&fs_info->delayed_iput_lock);
2929 list_splice_init(&fs_info->delayed_iputs, &list);
2930 spin_unlock(&fs_info->delayed_iput_lock);
2931
2932 while (!list_empty(&list)) {
2933 delayed = list_entry(list.next, struct delayed_iput, list);
2934 list_del(&delayed->list);
2935 iput(delayed->inode);
2936 kfree(delayed);
2937 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002938}
2939
Yan, Zhengd68fc572010-05-16 10:49:58 -04002940/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002941 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002942 * files in the subvolume, it removes orphan item and frees block_rsv
2943 * structure.
2944 */
2945void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2946 struct btrfs_root *root)
2947{
Josef Bacik90290e12011-12-02 15:44:12 -05002948 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002949 int ret;
2950
Josef Bacik8a35d952012-05-23 14:26:42 -04002951 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002952 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2953 return;
2954
Josef Bacik90290e12011-12-02 15:44:12 -05002955 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002956 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002957 spin_unlock(&root->orphan_lock);
2958 return;
2959 }
2960
2961 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2962 spin_unlock(&root->orphan_lock);
2963 return;
2964 }
2965
2966 block_rsv = root->orphan_block_rsv;
2967 root->orphan_block_rsv = NULL;
2968 spin_unlock(&root->orphan_lock);
2969
Yan, Zhengd68fc572010-05-16 10:49:58 -04002970 if (root->orphan_item_inserted &&
2971 btrfs_root_refs(&root->root_item) > 0) {
2972 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2973 root->root_key.objectid);
2974 BUG_ON(ret);
2975 root->orphan_item_inserted = 0;
2976 }
2977
Josef Bacik90290e12011-12-02 15:44:12 -05002978 if (block_rsv) {
2979 WARN_ON(block_rsv->size > 0);
2980 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002981 }
2982}
2983
2984/*
Josef Bacik7b128762008-07-24 12:17:14 -04002985 * This creates an orphan entry for the given inode in case something goes
2986 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002987 *
2988 * NOTE: caller of this function should reserve 5 units of metadata for
2989 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002990 */
2991int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2992{
2993 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002994 struct btrfs_block_rsv *block_rsv = NULL;
2995 int reserve = 0;
2996 int insert = 0;
2997 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002998
Yan, Zhengd68fc572010-05-16 10:49:58 -04002999 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003000 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003001 if (!block_rsv)
3002 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003003 }
3004
Yan, Zhengd68fc572010-05-16 10:49:58 -04003005 spin_lock(&root->orphan_lock);
3006 if (!root->orphan_block_rsv) {
3007 root->orphan_block_rsv = block_rsv;
3008 } else if (block_rsv) {
3009 btrfs_free_block_rsv(root, block_rsv);
3010 block_rsv = NULL;
3011 }
Josef Bacik7b128762008-07-24 12:17:14 -04003012
Josef Bacik8a35d952012-05-23 14:26:42 -04003013 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3014 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003015#if 0
3016 /*
3017 * For proper ENOSPC handling, we should do orphan
3018 * cleanup when mounting. But this introduces backward
3019 * compatibility issue.
3020 */
3021 if (!xchg(&root->orphan_item_inserted, 1))
3022 insert = 2;
3023 else
3024 insert = 1;
3025#endif
3026 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003027 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003028 }
Josef Bacik7b128762008-07-24 12:17:14 -04003029
Josef Bacik72ac3c02012-05-23 14:13:11 -04003030 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3031 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003032 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003033 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003034
Yan, Zhengd68fc572010-05-16 10:49:58 -04003035 /* grab metadata reservation from transaction handle */
3036 if (reserve) {
3037 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003038 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003039 }
3040
3041 /* insert an orphan item to track this unlinked/truncated file */
3042 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003043 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003044 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003045 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3046 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003047 btrfs_abort_transaction(trans, root, ret);
3048 return ret;
3049 }
3050 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003051 }
3052
3053 /* insert an orphan item to track subvolume contains orphan files */
3054 if (insert >= 2) {
3055 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3056 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003057 if (ret && ret != -EEXIST) {
3058 btrfs_abort_transaction(trans, root, ret);
3059 return ret;
3060 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003061 }
3062 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003063}
3064
3065/*
3066 * We have done the truncate/delete so we can go ahead and remove the orphan
3067 * item for this particular inode.
3068 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003069static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3070 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003071{
3072 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003073 int delete_item = 0;
3074 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003075 int ret = 0;
3076
Yan, Zhengd68fc572010-05-16 10:49:58 -04003077 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003078 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3079 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003080 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003081
Josef Bacik72ac3c02012-05-23 14:13:11 -04003082 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3083 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003084 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003085 spin_unlock(&root->orphan_lock);
3086
3087 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003088 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003089 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003090 }
3091
Josef Bacik8a35d952012-05-23 14:26:42 -04003092 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003093 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003094 atomic_dec(&root->orphan_inodes);
3095 }
Josef Bacik7b128762008-07-24 12:17:14 -04003096
Yan, Zhengd68fc572010-05-16 10:49:58 -04003097 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003098}
3099
3100/*
3101 * this cleans up any orphans that may be left on the list from the last use
3102 * of this root.
3103 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003104int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003105{
3106 struct btrfs_path *path;
3107 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003108 struct btrfs_key key, found_key;
3109 struct btrfs_trans_handle *trans;
3110 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003111 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003112 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3113
Yan, Zhengd68fc572010-05-16 10:49:58 -04003114 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003115 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003116
3117 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003118 if (!path) {
3119 ret = -ENOMEM;
3120 goto out;
3121 }
Josef Bacik7b128762008-07-24 12:17:14 -04003122 path->reada = -1;
3123
3124 key.objectid = BTRFS_ORPHAN_OBJECTID;
3125 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3126 key.offset = (u64)-1;
3127
Josef Bacik7b128762008-07-24 12:17:14 -04003128 while (1) {
3129 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003130 if (ret < 0)
3131 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003132
3133 /*
3134 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003135 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003136 * find the key and see if we have stuff that matches
3137 */
3138 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003139 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003140 if (path->slots[0] == 0)
3141 break;
3142 path->slots[0]--;
3143 }
3144
3145 /* pull out the item */
3146 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003147 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3148
3149 /* make sure the item matches what we want */
3150 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3151 break;
3152 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3153 break;
3154
3155 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003156 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003157
3158 /*
3159 * this is where we are basically btrfs_lookup, without the
3160 * crossing root thing. we store the inode number in the
3161 * offset of the orphan item.
3162 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003163
3164 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003165 btrfs_err(root->fs_info,
3166 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003167 ret = -EINVAL;
3168 goto out;
3169 }
3170
3171 last_objectid = found_key.offset;
3172
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003173 found_key.objectid = found_key.offset;
3174 found_key.type = BTRFS_INODE_ITEM_KEY;
3175 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003176 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003177 ret = PTR_RET(inode);
3178 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003179 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003180
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003181 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3182 struct btrfs_root *dead_root;
3183 struct btrfs_fs_info *fs_info = root->fs_info;
3184 int is_dead_root = 0;
3185
3186 /*
3187 * this is an orphan in the tree root. Currently these
3188 * could come from 2 sources:
3189 * a) a snapshot deletion in progress
3190 * b) a free space cache inode
3191 * We need to distinguish those two, as the snapshot
3192 * orphan must not get deleted.
3193 * find_dead_roots already ran before us, so if this
3194 * is a snapshot deletion, we should find the root
3195 * in the dead_roots list
3196 */
3197 spin_lock(&fs_info->trans_lock);
3198 list_for_each_entry(dead_root, &fs_info->dead_roots,
3199 root_list) {
3200 if (dead_root->root_key.objectid ==
3201 found_key.objectid) {
3202 is_dead_root = 1;
3203 break;
3204 }
3205 }
3206 spin_unlock(&fs_info->trans_lock);
3207 if (is_dead_root) {
3208 /* prevent this orphan from being found again */
3209 key.offset = found_key.objectid - 1;
3210 continue;
3211 }
3212 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003213 /*
3214 * Inode is already gone but the orphan item is still there,
3215 * kill the orphan item.
3216 */
3217 if (ret == -ESTALE) {
3218 trans = btrfs_start_transaction(root, 1);
3219 if (IS_ERR(trans)) {
3220 ret = PTR_ERR(trans);
3221 goto out;
3222 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003223 btrfs_debug(root->fs_info, "auto deleting %Lu",
3224 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003225 ret = btrfs_del_orphan_item(trans, root,
3226 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003227 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003228 btrfs_end_transaction(trans, root);
3229 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003230 }
Josef Bacik7b128762008-07-24 12:17:14 -04003231
Josef Bacik7b128762008-07-24 12:17:14 -04003232 /*
3233 * add this inode to the orphan list so btrfs_orphan_del does
3234 * the proper thing when we hit it
3235 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003236 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3237 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003238 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003239
Josef Bacik7b128762008-07-24 12:17:14 -04003240 /* if we have links, this was a truncate, lets do that */
3241 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003242 if (!S_ISREG(inode->i_mode)) {
3243 WARN_ON(1);
3244 iput(inode);
3245 continue;
3246 }
Josef Bacik7b128762008-07-24 12:17:14 -04003247 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003248
3249 /* 1 for the orphan item deletion. */
3250 trans = btrfs_start_transaction(root, 1);
3251 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003252 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003253 ret = PTR_ERR(trans);
3254 goto out;
3255 }
3256 ret = btrfs_orphan_add(trans, inode);
3257 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003258 if (ret) {
3259 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003260 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003261 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003262
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003263 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003264 if (ret)
3265 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003266 } else {
3267 nr_unlink++;
3268 }
3269
3270 /* this will do delete_inode and everything for us */
3271 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003272 if (ret)
3273 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003274 }
Miao Xie3254c872011-11-10 20:45:05 -05003275 /* release the path since we're done with it */
3276 btrfs_release_path(path);
3277
Yan, Zhengd68fc572010-05-16 10:49:58 -04003278 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3279
3280 if (root->orphan_block_rsv)
3281 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3282 (u64)-1);
3283
3284 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003285 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003286 if (!IS_ERR(trans))
3287 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003288 }
Josef Bacik7b128762008-07-24 12:17:14 -04003289
3290 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003291 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003292 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003293 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003294
3295out:
3296 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003297 btrfs_crit(root->fs_info,
3298 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003299 btrfs_free_path(path);
3300 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003301}
3302
Chris Masond352ac62008-09-29 15:18:18 -04003303/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003304 * very simple check to peek ahead in the leaf looking for xattrs. If we
3305 * don't find any xattrs, we know there can't be any acls.
3306 *
3307 * slot is the slot the inode is in, objectid is the objectid of the inode
3308 */
3309static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3310 int slot, u64 objectid)
3311{
3312 u32 nritems = btrfs_header_nritems(leaf);
3313 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003314 static u64 xattr_access = 0;
3315 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003316 int scanned = 0;
3317
Josef Bacikf23b5a52013-06-19 10:16:26 -04003318 if (!xattr_access) {
3319 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3320 strlen(POSIX_ACL_XATTR_ACCESS));
3321 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3322 strlen(POSIX_ACL_XATTR_DEFAULT));
3323 }
3324
Chris Mason46a53cc2009-04-27 11:47:50 -04003325 slot++;
3326 while (slot < nritems) {
3327 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3328
3329 /* we found a different objectid, there must not be acls */
3330 if (found_key.objectid != objectid)
3331 return 0;
3332
3333 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003334 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3335 if (found_key.offset == xattr_access ||
3336 found_key.offset == xattr_default)
3337 return 1;
3338 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003339
3340 /*
3341 * we found a key greater than an xattr key, there can't
3342 * be any acls later on
3343 */
3344 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3345 return 0;
3346
3347 slot++;
3348 scanned++;
3349
3350 /*
3351 * it goes inode, inode backrefs, xattrs, extents,
3352 * so if there are a ton of hard links to an inode there can
3353 * be a lot of backrefs. Don't waste time searching too hard,
3354 * this is just an optimization
3355 */
3356 if (scanned >= 8)
3357 break;
3358 }
3359 /* we hit the end of the leaf before we found an xattr or
3360 * something larger than an xattr. We have to assume the inode
3361 * has acls
3362 */
3363 return 1;
3364}
3365
3366/*
Chris Masond352ac62008-09-29 15:18:18 -04003367 * read an inode from the btree into the in-memory inode
3368 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003369static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003370{
3371 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003372 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003373 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003374 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003375 struct btrfs_root *root = BTRFS_I(inode)->root;
3376 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003377 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003378 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003379 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003380 bool filled = false;
3381
3382 ret = btrfs_fill_inode(inode, &rdev);
3383 if (!ret)
3384 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003385
3386 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003387 if (!path)
3388 goto make_bad;
3389
Josef Bacikd90c7322011-05-17 09:50:54 -04003390 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003391 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003392
Chris Mason39279cc2007-06-12 06:35:45 -04003393 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003394 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003395 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003396
Chris Mason5f39d392007-10-15 16:14:19 -04003397 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003398
3399 if (filled)
3400 goto cache_acl;
3401
Chris Mason5f39d392007-10-15 16:14:19 -04003402 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3403 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003404 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003405 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003406 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3407 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003408 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003409
3410 tspec = btrfs_inode_atime(inode_item);
3411 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3412 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3413
3414 tspec = btrfs_inode_mtime(inode_item);
3415 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3416 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3417
3418 tspec = btrfs_inode_ctime(inode_item);
3419 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3420 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3421
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003422 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003423 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003424 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3425
3426 /*
3427 * If we were modified in the current generation and evicted from memory
3428 * and then re-read we need to do a full sync since we don't have any
3429 * idea about which extents were modified before we were evicted from
3430 * cache.
3431 */
3432 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3433 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3434 &BTRFS_I(inode)->runtime_flags);
3435
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003436 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003437 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003438 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003439 rdev = btrfs_inode_rdev(leaf, inode_item);
3440
Josef Bacikaec74772008-07-24 12:12:38 -04003441 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003442 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003443cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003444 /*
3445 * try to precache a NULL acl entry for files that don't have
3446 * any xattrs or acls
3447 */
Li Zefan33345d012011-04-20 10:31:50 +08003448 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3449 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003450 if (!maybe_acls)
3451 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003452
Chris Mason39279cc2007-06-12 06:35:45 -04003453 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003454
Chris Mason39279cc2007-06-12 06:35:45 -04003455 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003456 case S_IFREG:
3457 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003458 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003459 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003460 inode->i_fop = &btrfs_file_operations;
3461 inode->i_op = &btrfs_file_inode_operations;
3462 break;
3463 case S_IFDIR:
3464 inode->i_fop = &btrfs_dir_file_operations;
3465 if (root == root->fs_info->tree_root)
3466 inode->i_op = &btrfs_dir_ro_inode_operations;
3467 else
3468 inode->i_op = &btrfs_dir_inode_operations;
3469 break;
3470 case S_IFLNK:
3471 inode->i_op = &btrfs_symlink_inode_operations;
3472 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003473 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003474 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003475 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003476 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003477 init_special_inode(inode, inode->i_mode, rdev);
3478 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003479 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003480
3481 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003482 return;
3483
3484make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003485 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003486 make_bad_inode(inode);
3487}
3488
Chris Masond352ac62008-09-29 15:18:18 -04003489/*
3490 * given a leaf and an inode, copy the inode fields into the leaf
3491 */
Chris Masone02119d2008-09-05 16:13:11 -04003492static void fill_inode_item(struct btrfs_trans_handle *trans,
3493 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003494 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003495 struct inode *inode)
3496{
Liu Bo51fab692012-12-27 09:01:21 +00003497 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003498
Liu Bo51fab692012-12-27 09:01:21 +00003499 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003500
Liu Bo51fab692012-12-27 09:01:21 +00003501 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3502 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3503 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3504 &token);
3505 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3506 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003507
Liu Bo51fab692012-12-27 09:01:21 +00003508 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3509 inode->i_atime.tv_sec, &token);
3510 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3511 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003512
Liu Bo51fab692012-12-27 09:01:21 +00003513 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3514 inode->i_mtime.tv_sec, &token);
3515 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3516 inode->i_mtime.tv_nsec, &token);
3517
3518 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3519 inode->i_ctime.tv_sec, &token);
3520 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3521 inode->i_ctime.tv_nsec, &token);
3522
3523 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3524 &token);
3525 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3526 &token);
3527 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3528 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3529 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3530 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3531 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003532}
3533
Chris Masond352ac62008-09-29 15:18:18 -04003534/*
3535 * copy everything in the in-memory inode into the btree.
3536 */
Chris Mason21151332011-11-10 20:39:08 -05003537static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003538 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003539{
3540 struct btrfs_inode_item *inode_item;
3541 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003542 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003543 int ret;
3544
3545 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003546 if (!path)
3547 return -ENOMEM;
3548
Chris Masonb9473432009-03-13 11:00:37 -04003549 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003550 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3551 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003552 if (ret) {
3553 if (ret > 0)
3554 ret = -ENOENT;
3555 goto failed;
3556 }
3557
Chris Masonb4ce94d2009-02-04 09:25:08 -05003558 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003559 leaf = path->nodes[0];
3560 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003561 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003562
Chris Masone02119d2008-09-05 16:13:11 -04003563 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003564 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003565 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003566 ret = 0;
3567failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003568 btrfs_free_path(path);
3569 return ret;
3570}
3571
Chris Masond352ac62008-09-29 15:18:18 -04003572/*
Chris Mason21151332011-11-10 20:39:08 -05003573 * copy everything in the in-memory inode into the btree.
3574 */
3575noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3576 struct btrfs_root *root, struct inode *inode)
3577{
3578 int ret;
3579
3580 /*
3581 * If the inode is a free space inode, we can deadlock during commit
3582 * if we put it into the delayed code.
3583 *
3584 * The data relocation inode should also be directly updated
3585 * without delay
3586 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003587 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003588 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003589 btrfs_update_root_times(trans, root);
3590
Chris Mason21151332011-11-10 20:39:08 -05003591 ret = btrfs_delayed_update_inode(trans, root, inode);
3592 if (!ret)
3593 btrfs_set_inode_last_trans(trans, inode);
3594 return ret;
3595 }
3596
3597 return btrfs_update_inode_item(trans, root, inode);
3598}
3599
Josef Bacikbe6aef62012-10-22 15:43:12 -04003600noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3601 struct btrfs_root *root,
3602 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003603{
3604 int ret;
3605
3606 ret = btrfs_update_inode(trans, root, inode);
3607 if (ret == -ENOSPC)
3608 return btrfs_update_inode_item(trans, root, inode);
3609 return ret;
3610}
3611
3612/*
Chris Masond352ac62008-09-29 15:18:18 -04003613 * unlink helper that gets used here in inode.c and in the tree logging
3614 * recovery code. It remove a link in a directory with a given name, and
3615 * also drops the back refs in the inode to the directory
3616 */
Al Viro92986792011-03-04 17:14:37 +00003617static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3618 struct btrfs_root *root,
3619 struct inode *dir, struct inode *inode,
3620 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003621{
3622 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003623 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003624 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003625 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003626 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003627 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003628 u64 ino = btrfs_ino(inode);
3629 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003630
3631 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003632 if (!path) {
3633 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003634 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003635 }
3636
Chris Masonb9473432009-03-13 11:00:37 -04003637 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003638 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003639 name, name_len, -1);
3640 if (IS_ERR(di)) {
3641 ret = PTR_ERR(di);
3642 goto err;
3643 }
3644 if (!di) {
3645 ret = -ENOENT;
3646 goto err;
3647 }
Chris Mason5f39d392007-10-15 16:14:19 -04003648 leaf = path->nodes[0];
3649 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003650 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003651 if (ret)
3652 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003653 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003654
Li Zefan33345d012011-04-20 10:31:50 +08003655 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3656 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003657 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003658 btrfs_info(root->fs_info,
3659 "failed to delete reference to %.*s, inode %llu parent %llu",
3660 name_len, name,
3661 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003662 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003663 goto err;
3664 }
3665
Miao Xie16cdcec2011-04-22 18:12:22 +08003666 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003667 if (ret) {
3668 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003669 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003670 }
Chris Mason39279cc2007-06-12 06:35:45 -04003671
Chris Masone02119d2008-09-05 16:13:11 -04003672 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003673 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003674 if (ret != 0 && ret != -ENOENT) {
3675 btrfs_abort_transaction(trans, root, ret);
3676 goto err;
3677 }
Chris Masone02119d2008-09-05 16:13:11 -04003678
3679 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3680 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003681 if (ret == -ENOENT)
3682 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003683 else if (ret)
3684 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003685err:
3686 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003687 if (ret)
3688 goto out;
3689
3690 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003691 inode_inc_iversion(inode);
3692 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003693 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003694 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003695out:
Chris Mason39279cc2007-06-12 06:35:45 -04003696 return ret;
3697}
3698
Al Viro92986792011-03-04 17:14:37 +00003699int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3700 struct btrfs_root *root,
3701 struct inode *dir, struct inode *inode,
3702 const char *name, int name_len)
3703{
3704 int ret;
3705 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3706 if (!ret) {
3707 btrfs_drop_nlink(inode);
3708 ret = btrfs_update_inode(trans, root, inode);
3709 }
3710 return ret;
3711}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003712
3713/*
3714 * helper to start transaction for unlink and rmdir.
3715 *
Josef Bacikd52be812013-05-29 14:54:47 -04003716 * unlink and rmdir are special in btrfs, they do not always free space, so
3717 * if we cannot make our reservations the normal way try and see if there is
3718 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3719 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003720 */
Josef Bacikd52be812013-05-29 14:54:47 -04003721static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003722{
3723 struct btrfs_trans_handle *trans;
3724 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003725 int ret;
3726
Josef Bacike70bea52011-10-11 14:18:24 -04003727 /*
3728 * 1 for the possible orphan item
3729 * 1 for the dir item
3730 * 1 for the dir index
3731 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003732 * 1 for the inode
3733 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003734 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003735 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3736 return trans;
3737
Josef Bacikd52be812013-05-29 14:54:47 -04003738 if (PTR_ERR(trans) == -ENOSPC) {
3739 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003740
Josef Bacikd52be812013-05-29 14:54:47 -04003741 trans = btrfs_start_transaction(root, 0);
3742 if (IS_ERR(trans))
3743 return trans;
3744 ret = btrfs_cond_migrate_bytes(root->fs_info,
3745 &root->fs_info->trans_block_rsv,
3746 num_bytes, 5);
3747 if (ret) {
3748 btrfs_end_transaction(trans, root);
3749 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003750 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003751 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003752 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003753 }
Josef Bacikd52be812013-05-29 14:54:47 -04003754 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003755}
3756
Chris Mason39279cc2007-06-12 06:35:45 -04003757static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3758{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003759 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003760 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003761 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003762 int ret;
3763
Josef Bacikd52be812013-05-29 14:54:47 -04003764 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003765 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003766 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003767
Chris Mason12fcfd22009-03-24 10:24:20 -04003768 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3769
Chris Masone02119d2008-09-05 16:13:11 -04003770 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3771 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003772 if (ret)
3773 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003774
Yan, Zhenga22285a2010-05-16 10:48:46 -04003775 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003776 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003777 if (ret)
3778 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003779 }
Josef Bacik7b128762008-07-24 12:17:14 -04003780
Tsutomu Itohb5324022011-07-19 07:27:20 +00003781out:
Josef Bacikd52be812013-05-29 14:54:47 -04003782 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003783 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003784 return ret;
3785}
3786
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003787int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3788 struct btrfs_root *root,
3789 struct inode *dir, u64 objectid,
3790 const char *name, int name_len)
3791{
3792 struct btrfs_path *path;
3793 struct extent_buffer *leaf;
3794 struct btrfs_dir_item *di;
3795 struct btrfs_key key;
3796 u64 index;
3797 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003798 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003799
3800 path = btrfs_alloc_path();
3801 if (!path)
3802 return -ENOMEM;
3803
Li Zefan33345d012011-04-20 10:31:50 +08003804 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003805 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003806 if (IS_ERR_OR_NULL(di)) {
3807 if (!di)
3808 ret = -ENOENT;
3809 else
3810 ret = PTR_ERR(di);
3811 goto out;
3812 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003813
3814 leaf = path->nodes[0];
3815 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3816 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3817 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003818 if (ret) {
3819 btrfs_abort_transaction(trans, root, ret);
3820 goto out;
3821 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003822 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003823
3824 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3825 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003826 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003827 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003828 if (ret != -ENOENT) {
3829 btrfs_abort_transaction(trans, root, ret);
3830 goto out;
3831 }
Li Zefan33345d012011-04-20 10:31:50 +08003832 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003833 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003834 if (IS_ERR_OR_NULL(di)) {
3835 if (!di)
3836 ret = -ENOENT;
3837 else
3838 ret = PTR_ERR(di);
3839 btrfs_abort_transaction(trans, root, ret);
3840 goto out;
3841 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003842
3843 leaf = path->nodes[0];
3844 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003845 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003846 index = key.offset;
3847 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003848 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003849
Miao Xie16cdcec2011-04-22 18:12:22 +08003850 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003851 if (ret) {
3852 btrfs_abort_transaction(trans, root, ret);
3853 goto out;
3854 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003855
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003856 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003857 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003858 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003859 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003860 if (ret)
3861 btrfs_abort_transaction(trans, root, ret);
3862out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003863 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003864 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003865}
3866
Chris Mason39279cc2007-06-12 06:35:45 -04003867static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3868{
3869 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003870 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003871 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003872 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003873
David Sterbab3ae2442012-09-13 16:04:34 -06003874 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003875 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003876 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3877 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003878
Josef Bacikd52be812013-05-29 14:54:47 -04003879 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003880 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003881 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003882
Li Zefan33345d012011-04-20 10:31:50 +08003883 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003884 err = btrfs_unlink_subvol(trans, root, dir,
3885 BTRFS_I(inode)->location.objectid,
3886 dentry->d_name.name,
3887 dentry->d_name.len);
3888 goto out;
3889 }
3890
Josef Bacik7b128762008-07-24 12:17:14 -04003891 err = btrfs_orphan_add(trans, inode);
3892 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003893 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003894
Chris Mason39279cc2007-06-12 06:35:45 -04003895 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003896 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3897 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003898 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003899 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003900out:
Josef Bacikd52be812013-05-29 14:54:47 -04003901 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003902 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003903
Chris Mason39279cc2007-06-12 06:35:45 -04003904 return err;
3905}
3906
Chris Mason323ac952008-10-01 19:05:46 -04003907/*
Chris Mason39279cc2007-06-12 06:35:45 -04003908 * this can truncate away extent items, csum items and directory items.
3909 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003910 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003911 *
3912 * csum items that cross the new i_size are truncated to the new size
3913 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003914 *
3915 * min_type is the minimum key type to truncate down to. If set to 0, this
3916 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003917 */
Yan, Zheng80825102009-11-12 09:35:36 +00003918int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3919 struct btrfs_root *root,
3920 struct inode *inode,
3921 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003922{
Chris Mason39279cc2007-06-12 06:35:45 -04003923 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003924 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003925 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003926 struct btrfs_key key;
3927 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003928 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003929 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003930 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003931 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003932 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003933 int found_extent;
3934 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003935 int pending_del_nr = 0;
3936 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003937 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003938 int ret;
3939 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003940 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003941
3942 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003943
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003944 path = btrfs_alloc_path();
3945 if (!path)
3946 return -ENOMEM;
3947 path->reada = -1;
3948
Josef Bacik5dc562c2012-08-17 13:14:17 -04003949 /*
3950 * We want to drop from the next block forward in case this new size is
3951 * not block aligned since we will be keeping the last block of the
3952 * extent just the way it is.
3953 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003954 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00003955 btrfs_drop_extent_cache(inode, ALIGN(new_size,
3956 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003957
Miao Xie16cdcec2011-04-22 18:12:22 +08003958 /*
3959 * This function is also used to drop the items in the log tree before
3960 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3961 * it is used to drop the loged items. So we shouldn't kill the delayed
3962 * items.
3963 */
3964 if (min_type == 0 && root == BTRFS_I(inode)->root)
3965 btrfs_kill_delayed_inode_items(inode);
3966
Li Zefan33345d012011-04-20 10:31:50 +08003967 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003968 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003969 key.type = (u8)-1;
3970
Chris Mason85e21ba2008-01-29 15:11:36 -05003971search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003972 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003973 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003974 if (ret < 0) {
3975 err = ret;
3976 goto out;
3977 }
Chris Masond3977122009-01-05 21:25:51 -05003978
Chris Mason85e21ba2008-01-29 15:11:36 -05003979 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003980 /* there are no items in the tree for us to truncate, we're
3981 * done
3982 */
Yan, Zheng80825102009-11-12 09:35:36 +00003983 if (path->slots[0] == 0)
3984 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003985 path->slots[0]--;
3986 }
3987
Chris Masond3977122009-01-05 21:25:51 -05003988 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003989 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003990 leaf = path->nodes[0];
3991 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3992 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003993
Li Zefan33345d012011-04-20 10:31:50 +08003994 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003995 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003996
Chris Mason85e21ba2008-01-29 15:11:36 -05003997 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003998 break;
3999
Chris Mason5f39d392007-10-15 16:14:19 -04004000 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004001 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004002 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004003 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004004 extent_type = btrfs_file_extent_type(leaf, fi);
4005 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004006 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004007 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004008 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004009 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04004010 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004011 }
Yan008630c2007-11-07 13:31:09 -05004012 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004013 }
Yan, Zheng80825102009-11-12 09:35:36 +00004014 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004015 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004016 } else {
4017 if (item_end < new_size)
4018 break;
4019 if (found_key.offset >= new_size)
4020 del_item = 1;
4021 else
4022 del_item = 0;
4023 }
Chris Mason39279cc2007-06-12 06:35:45 -04004024 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004025 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004026 if (found_type != BTRFS_EXTENT_DATA_KEY)
4027 goto delete;
4028
4029 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004030 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004031 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004032 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004033 u64 orig_num_bytes =
4034 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004035 extent_num_bytes = ALIGN(new_size -
4036 found_key.offset,
4037 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004038 btrfs_set_file_extent_num_bytes(leaf, fi,
4039 extent_num_bytes);
4040 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004041 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004042 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004043 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004044 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004045 } else {
Chris Masondb945352007-10-15 16:15:53 -04004046 extent_num_bytes =
4047 btrfs_file_extent_disk_num_bytes(leaf,
4048 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004049 extent_offset = found_key.offset -
4050 btrfs_file_extent_offset(leaf, fi);
4051
Chris Mason39279cc2007-06-12 06:35:45 -04004052 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004053 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004054 if (extent_start != 0) {
4055 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004056 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004057 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004058 }
4059 }
Chris Mason90692182008-02-08 13:49:28 -05004060 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004061 /*
4062 * we can't truncate inline items that have had
4063 * special encodings
4064 */
4065 if (!del_item &&
4066 btrfs_file_extent_compression(leaf, fi) == 0 &&
4067 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4068 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004069 u32 size = new_size - found_key.offset;
4070
4071 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004072 inode_sub_bytes(inode, item_end + 1 -
4073 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004074 }
4075 size =
4076 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004077 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004078 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004079 inode_sub_bytes(inode, item_end + 1 -
4080 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004081 }
Chris Mason39279cc2007-06-12 06:35:45 -04004082 }
Chris Mason179e29e2007-11-01 11:28:41 -04004083delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004084 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004085 if (!pending_del_nr) {
4086 /* no pending yet, add ourselves */
4087 pending_del_slot = path->slots[0];
4088 pending_del_nr = 1;
4089 } else if (pending_del_nr &&
4090 path->slots[0] + 1 == pending_del_slot) {
4091 /* hop on the pending chunk */
4092 pending_del_nr++;
4093 pending_del_slot = path->slots[0];
4094 } else {
Chris Masond3977122009-01-05 21:25:51 -05004095 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004096 }
Chris Mason39279cc2007-06-12 06:35:45 -04004097 } else {
4098 break;
4099 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004100 if (found_extent && (root->ref_cows ||
4101 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004102 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004103 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004104 extent_num_bytes, 0,
4105 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004106 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004107 BUG_ON(ret);
4108 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004109
Yan, Zheng80825102009-11-12 09:35:36 +00004110 if (found_type == BTRFS_INODE_ITEM_KEY)
4111 break;
4112
4113 if (path->slots[0] == 0 ||
4114 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004115 if (pending_del_nr) {
4116 ret = btrfs_del_items(trans, root, path,
4117 pending_del_slot,
4118 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004119 if (ret) {
4120 btrfs_abort_transaction(trans,
4121 root, ret);
4122 goto error;
4123 }
Yan, Zheng80825102009-11-12 09:35:36 +00004124 pending_del_nr = 0;
4125 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004126 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004127 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004128 } else {
4129 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004130 }
Chris Mason39279cc2007-06-12 06:35:45 -04004131 }
Yan, Zheng80825102009-11-12 09:35:36 +00004132out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004133 if (pending_del_nr) {
4134 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4135 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004136 if (ret)
4137 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004138 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004139error:
Chris Mason39279cc2007-06-12 06:35:45 -04004140 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004141 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004142}
4143
Chris Masona52d9a82007-08-27 16:49:44 -04004144/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004145 * btrfs_truncate_page - read, zero a chunk and write a page
4146 * @inode - inode that we're zeroing
4147 * @from - the offset to start zeroing
4148 * @len - the length to zero, 0 to zero the entire range respective to the
4149 * offset
4150 * @front - zero up to the offset instead of from the offset on
4151 *
4152 * This will find the page for the "from" offset and cow the page and zero the
4153 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004154 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004155int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4156 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004157{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004158 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004159 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004160 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4161 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004162 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004163 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004164 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004165 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4166 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4167 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004168 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004169 int ret = 0;
4170 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004171 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004172
Josef Bacik2aaa6652012-08-29 14:27:18 -04004173 if ((offset & (blocksize - 1)) == 0 &&
4174 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004175 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004176 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004177 if (ret)
4178 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004179
Chris Mason211c17f2008-05-15 09:13:45 -04004180again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004181 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004182 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004183 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004184 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004185 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004186 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004187
4188 page_start = page_offset(page);
4189 page_end = page_start + PAGE_CACHE_SIZE - 1;
4190
Chris Masona52d9a82007-08-27 16:49:44 -04004191 if (!PageUptodate(page)) {
4192 ret = btrfs_readpage(NULL, page);
4193 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004194 if (page->mapping != mapping) {
4195 unlock_page(page);
4196 page_cache_release(page);
4197 goto again;
4198 }
Chris Masona52d9a82007-08-27 16:49:44 -04004199 if (!PageUptodate(page)) {
4200 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004201 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004202 }
4203 }
Chris Mason211c17f2008-05-15 09:13:45 -04004204 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004205
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004206 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004207 set_page_extent_mapped(page);
4208
4209 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4210 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004211 unlock_extent_cached(io_tree, page_start, page_end,
4212 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004213 unlock_page(page);
4214 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004215 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004216 btrfs_put_ordered_extent(ordered);
4217 goto again;
4218 }
4219
Josef Bacik2ac55d42010-02-03 19:33:23 +00004220 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004221 EXTENT_DIRTY | EXTENT_DELALLOC |
4222 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004223 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004224
Josef Bacik2ac55d42010-02-03 19:33:23 +00004225 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4226 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004227 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004228 unlock_extent_cached(io_tree, page_start, page_end,
4229 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004230 goto out_unlock;
4231 }
4232
Chris Masone6dcd2d2008-07-17 12:53:50 -04004233 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004234 if (!len)
4235 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004236 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004237 if (front)
4238 memset(kaddr, 0, offset);
4239 else
4240 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004241 flush_dcache_page(page);
4242 kunmap(page);
4243 }
Chris Mason247e7432008-07-17 12:53:51 -04004244 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004245 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004246 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4247 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004248
Chris Mason89642222008-07-24 09:41:53 -04004249out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004250 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004251 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004252 unlock_page(page);
4253 page_cache_release(page);
4254out:
4255 return ret;
4256}
4257
Josef Bacik695a0d02011-03-04 15:46:53 -05004258/*
4259 * This function puts in dummy file extents for the area we're creating a hole
4260 * for. So if we are truncating this file to a larger size we need to insert
4261 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4262 * the range between oldsize and size
4263 */
Josef Bacika41ad392011-01-31 15:30:16 -05004264int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004265{
4266 struct btrfs_trans_handle *trans;
4267 struct btrfs_root *root = BTRFS_I(inode)->root;
4268 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004269 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004270 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004271 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004272 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4273 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004274 u64 last_byte;
4275 u64 cur_offset;
4276 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004277 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004278
Josef Bacika71754f2013-06-17 17:14:39 -04004279 /*
4280 * If our size started in the middle of a page we need to zero out the
4281 * rest of the page before we expand the i_size, otherwise we could
4282 * expose stale data.
4283 */
4284 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4285 if (err)
4286 return err;
4287
Yan Zheng9036c102008-10-30 14:19:41 -04004288 if (size <= hole_start)
4289 return 0;
4290
Yan Zheng9036c102008-10-30 14:19:41 -04004291 while (1) {
4292 struct btrfs_ordered_extent *ordered;
4293 btrfs_wait_ordered_range(inode, hole_start,
4294 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004295 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004296 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004297 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4298 if (!ordered)
4299 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004300 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4301 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004302 btrfs_put_ordered_extent(ordered);
4303 }
4304
Yan Zheng9036c102008-10-30 14:19:41 -04004305 cur_offset = hole_start;
4306 while (1) {
4307 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4308 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004309 if (IS_ERR(em)) {
4310 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004311 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004312 break;
4313 }
Yan Zheng9036c102008-10-30 14:19:41 -04004314 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004315 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004316 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004317 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004318 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004319
Miao Xie36423202011-12-14 20:12:02 -05004320 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004321 if (IS_ERR(trans)) {
4322 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004323 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004324 }
Yan, Zheng80825102009-11-12 09:35:36 +00004325
Josef Bacik5dc562c2012-08-17 13:14:17 -04004326 err = btrfs_drop_extents(trans, root, inode,
4327 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004328 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004329 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004330 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004331 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004332 break;
Miao Xie5b397372011-09-11 10:52:24 -04004333 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004334
Yan Zheng9036c102008-10-30 14:19:41 -04004335 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004336 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004337 0, hole_size, 0, hole_size,
4338 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004339 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004340 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004341 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004342 break;
Miao Xie5b397372011-09-11 10:52:24 -04004343 }
Yan, Zheng80825102009-11-12 09:35:36 +00004344
Josef Bacik5dc562c2012-08-17 13:14:17 -04004345 btrfs_drop_extent_cache(inode, cur_offset,
4346 cur_offset + hole_size - 1, 0);
4347 hole_em = alloc_extent_map();
4348 if (!hole_em) {
4349 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4350 &BTRFS_I(inode)->runtime_flags);
4351 goto next;
4352 }
4353 hole_em->start = cur_offset;
4354 hole_em->len = hole_size;
4355 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004356
Josef Bacik5dc562c2012-08-17 13:14:17 -04004357 hole_em->block_start = EXTENT_MAP_HOLE;
4358 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004359 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004360 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004361 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4362 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4363 hole_em->generation = trans->transid;
4364
4365 while (1) {
4366 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004367 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004368 write_unlock(&em_tree->lock);
4369 if (err != -EEXIST)
4370 break;
4371 btrfs_drop_extent_cache(inode, cur_offset,
4372 cur_offset +
4373 hole_size - 1, 0);
4374 }
4375 free_extent_map(hole_em);
4376next:
Miao Xie36423202011-12-14 20:12:02 -05004377 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004378 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004379 }
4380 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004381 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004382 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004383 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004384 break;
4385 }
4386
Yan, Zhenga22285a2010-05-16 10:48:46 -04004387 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004388 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4389 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004390 return err;
4391}
4392
Eric Sandeen3972f262013-01-12 02:57:22 +00004393static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004394{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004395 struct btrfs_root *root = BTRFS_I(inode)->root;
4396 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004397 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004398 loff_t newsize = attr->ia_size;
4399 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004400 int ret;
4401
Eric Sandeen3972f262013-01-12 02:57:22 +00004402 /*
4403 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4404 * special case where we need to update the times despite not having
4405 * these flags set. For all other operations the VFS set these flags
4406 * explicitly if it wants a timestamp update.
4407 */
4408 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4409 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4410
Josef Bacika41ad392011-01-31 15:30:16 -05004411 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004412 truncate_pagecache(inode, oldsize, newsize);
4413 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004414 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004415 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004416
Miao Xief4a2f4c2011-12-14 20:12:01 -05004417 trans = btrfs_start_transaction(root, 1);
4418 if (IS_ERR(trans))
4419 return PTR_ERR(trans);
4420
4421 i_size_write(inode, newsize);
4422 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4423 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004424 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004425 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004426
Josef Bacika41ad392011-01-31 15:30:16 -05004427 /*
4428 * We're truncating a file that used to have good data down to
4429 * zero. Make sure it gets into the ordered flush list so that
4430 * any new writes get down to disk quickly.
4431 */
4432 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004433 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4434 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004435
Josef Bacikf3fe8202013-01-07 17:03:21 -05004436 /*
4437 * 1 for the orphan item we're going to add
4438 * 1 for the orphan item deletion.
4439 */
4440 trans = btrfs_start_transaction(root, 2);
4441 if (IS_ERR(trans))
4442 return PTR_ERR(trans);
4443
4444 /*
4445 * We need to do this in case we fail at _any_ point during the
4446 * actual truncate. Once we do the truncate_setsize we could
4447 * invalidate pages which forces any outstanding ordered io to
4448 * be instantly completed which will give us extents that need
4449 * to be truncated. If we fail to get an orphan inode down we
4450 * could have left over extents that were never meant to live,
4451 * so we need to garuntee from this point on that everything
4452 * will be consistent.
4453 */
4454 ret = btrfs_orphan_add(trans, inode);
4455 btrfs_end_transaction(trans, root);
4456 if (ret)
4457 return ret;
4458
Josef Bacika41ad392011-01-31 15:30:16 -05004459 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4460 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004461
4462 /* Disable nonlocked read DIO to avoid the end less truncate */
4463 btrfs_inode_block_unlocked_dio(inode);
4464 inode_dio_wait(inode);
4465 btrfs_inode_resume_unlocked_dio(inode);
4466
Josef Bacika41ad392011-01-31 15:30:16 -05004467 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004468 if (ret && inode->i_nlink)
4469 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004470 }
4471
Josef Bacika41ad392011-01-31 15:30:16 -05004472 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004473}
4474
Chris Mason39279cc2007-06-12 06:35:45 -04004475static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4476{
4477 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004478 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004479 int err;
4480
Li Zefanb83cc962010-12-20 16:04:08 +08004481 if (btrfs_root_readonly(root))
4482 return -EROFS;
4483
Chris Mason39279cc2007-06-12 06:35:45 -04004484 err = inode_change_ok(inode, attr);
4485 if (err)
4486 return err;
4487
Chris Mason5a3f23d2009-03-31 13:27:11 -04004488 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004489 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004490 if (err)
4491 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004492 }
Yan Zheng9036c102008-10-30 14:19:41 -04004493
Christoph Hellwig10257742010-06-04 11:30:02 +02004494 if (attr->ia_valid) {
4495 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004496 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004497 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004498
Josef Bacik22c44fe2011-11-30 10:45:38 -05004499 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004500 err = btrfs_acl_chmod(inode);
4501 }
4502
Chris Mason39279cc2007-06-12 06:35:45 -04004503 return err;
4504}
Chris Mason61295eb2008-01-14 16:24:38 -05004505
Al Virobd555972010-06-07 11:35:40 -04004506void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004507{
4508 struct btrfs_trans_handle *trans;
4509 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004510 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004511 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004512 int ret;
4513
liubo1abe9b82011-03-24 11:18:59 +00004514 trace_btrfs_inode_evict(inode);
4515
Chris Mason39279cc2007-06-12 06:35:45 -04004516 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004517 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004518 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004519 goto no_delete;
4520
Chris Mason39279cc2007-06-12 06:35:45 -04004521 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004522 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004523 goto no_delete;
4524 }
Al Virobd555972010-06-07 11:35:40 -04004525 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004526 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004527
Yan, Zhengc71bf092009-11-12 09:34:40 +00004528 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004529 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004530 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004531 goto no_delete;
4532 }
4533
Yan, Zheng76dda932009-09-21 16:00:26 -04004534 if (inode->i_nlink > 0) {
4535 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4536 goto no_delete;
4537 }
4538
Miao Xie0e8c36a2012-12-19 06:59:51 +00004539 ret = btrfs_commit_inode_delayed_inode(inode);
4540 if (ret) {
4541 btrfs_orphan_del(NULL, inode);
4542 goto no_delete;
4543 }
4544
Miao Xie66d8f3d2012-09-06 04:02:28 -06004545 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004546 if (!rsv) {
4547 btrfs_orphan_del(NULL, inode);
4548 goto no_delete;
4549 }
Josef Bacik4a338542011-08-29 11:01:31 -04004550 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004551 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004552 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004553
Chris Masondbe674a2008-07-17 12:54:05 -04004554 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004555
Josef Bacik4289a662011-08-05 13:22:24 -04004556 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004557 * This is a bit simpler than btrfs_truncate since we've already
4558 * reserved our space for our orphan item in the unlink, so we just
4559 * need to reserve some slack space in case we add bytes and update
4560 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004561 */
Yan, Zheng80825102009-11-12 09:35:36 +00004562 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004563 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4564 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004565
Josef Bacik726c35f2011-09-26 15:46:06 -04004566 /*
4567 * Try and steal from the global reserve since we will
4568 * likely not use this space anyway, we want to try as
4569 * hard as possible to get this to work.
4570 */
4571 if (ret)
4572 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4573
Yan, Zhengd68fc572010-05-16 10:49:58 -04004574 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004575 btrfs_warn(root->fs_info,
4576 "Could not get space for a delete, will truncate on mount %d",
4577 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004578 btrfs_orphan_del(NULL, inode);
4579 btrfs_free_block_rsv(root, rsv);
4580 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004581 }
4582
Miao Xie0e8c36a2012-12-19 06:59:51 +00004583 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004584 if (IS_ERR(trans)) {
4585 btrfs_orphan_del(NULL, inode);
4586 btrfs_free_block_rsv(root, rsv);
4587 goto no_delete;
4588 }
4589
4590 trans->block_rsv = rsv;
4591
Yan, Zhengd68fc572010-05-16 10:49:58 -04004592 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004593 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004594 break;
4595
Miao Xie8407aa42012-09-07 01:43:32 -06004596 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004597 btrfs_end_transaction(trans, root);
4598 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004599 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004600 }
4601
Josef Bacik4289a662011-08-05 13:22:24 -04004602 btrfs_free_block_rsv(root, rsv);
4603
Yan, Zheng80825102009-11-12 09:35:36 +00004604 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004605 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004606 ret = btrfs_orphan_del(trans, inode);
4607 BUG_ON(ret);
4608 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004609
Josef Bacik4289a662011-08-05 13:22:24 -04004610 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004611 if (!(root == root->fs_info->tree_root ||
4612 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004613 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004614
Chris Mason54aa1f42007-06-22 14:16:25 -04004615 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004616 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004617no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004618 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004619 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004620 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004621}
4622
4623/*
4624 * this returns the key found in the dir entry in the location pointer.
4625 * If no dir entries were found, location->objectid is 0.
4626 */
4627static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4628 struct btrfs_key *location)
4629{
4630 const char *name = dentry->d_name.name;
4631 int namelen = dentry->d_name.len;
4632 struct btrfs_dir_item *di;
4633 struct btrfs_path *path;
4634 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004635 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004636
4637 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004638 if (!path)
4639 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004640
Li Zefan33345d012011-04-20 10:31:50 +08004641 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004642 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004643 if (IS_ERR(di))
4644 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004645
David Sterbac7040052011-04-19 18:00:01 +02004646 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004647 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004648
Chris Mason5f39d392007-10-15 16:14:19 -04004649 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004650out:
Chris Mason39279cc2007-06-12 06:35:45 -04004651 btrfs_free_path(path);
4652 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004653out_err:
4654 location->objectid = 0;
4655 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004656}
4657
4658/*
4659 * when we hit a tree root in a directory, the btrfs part of the inode
4660 * needs to be changed to reflect the root directory of the tree root. This
4661 * is kind of like crossing a mount point.
4662 */
4663static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004664 struct inode *dir,
4665 struct dentry *dentry,
4666 struct btrfs_key *location,
4667 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004668{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004669 struct btrfs_path *path;
4670 struct btrfs_root *new_root;
4671 struct btrfs_root_ref *ref;
4672 struct extent_buffer *leaf;
4673 int ret;
4674 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004675
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004676 path = btrfs_alloc_path();
4677 if (!path) {
4678 err = -ENOMEM;
4679 goto out;
4680 }
Chris Mason39279cc2007-06-12 06:35:45 -04004681
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004682 err = -ENOENT;
4683 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4684 BTRFS_I(dir)->root->root_key.objectid,
4685 location->objectid);
4686 if (ret) {
4687 if (ret < 0)
4688 err = ret;
4689 goto out;
4690 }
Chris Mason39279cc2007-06-12 06:35:45 -04004691
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004692 leaf = path->nodes[0];
4693 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004694 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004695 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4696 goto out;
4697
4698 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4699 (unsigned long)(ref + 1),
4700 dentry->d_name.len);
4701 if (ret)
4702 goto out;
4703
David Sterbab3b4aa72011-04-21 01:20:15 +02004704 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004705
4706 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4707 if (IS_ERR(new_root)) {
4708 err = PTR_ERR(new_root);
4709 goto out;
4710 }
4711
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004712 *sub_root = new_root;
4713 location->objectid = btrfs_root_dirid(&new_root->root_item);
4714 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004715 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004716 err = 0;
4717out:
4718 btrfs_free_path(path);
4719 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004720}
4721
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004722static void inode_tree_add(struct inode *inode)
4723{
4724 struct btrfs_root *root = BTRFS_I(inode)->root;
4725 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004726 struct rb_node **p;
4727 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004728 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004729
Al Viro1d3382cb2010-10-23 15:19:20 -04004730 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004731 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004732again:
4733 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004734 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004735 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004736 while (*p) {
4737 parent = *p;
4738 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4739
Li Zefan33345d012011-04-20 10:31:50 +08004740 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004741 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004742 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004743 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004744 else {
4745 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004746 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004747 rb_erase(parent, &root->inode_tree);
4748 RB_CLEAR_NODE(parent);
4749 spin_unlock(&root->inode_lock);
4750 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004751 }
4752 }
4753 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4754 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4755 spin_unlock(&root->inode_lock);
4756}
4757
4758static void inode_tree_del(struct inode *inode)
4759{
4760 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004761 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004762
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004763 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004764 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004765 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004766 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004767 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004768 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004769 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004770
Josef Bacik0af3d002010-06-21 14:48:16 -04004771 /*
4772 * Free space cache has inodes in the tree root, but the tree root has a
4773 * root_refs of 0, so this could end up dropping the tree root as a
4774 * snapshot, so we need the extra !root->fs_info->tree_root check to
4775 * make sure we don't drop it.
4776 */
4777 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4778 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004779 synchronize_srcu(&root->fs_info->subvol_srcu);
4780 spin_lock(&root->inode_lock);
4781 empty = RB_EMPTY_ROOT(&root->inode_tree);
4782 spin_unlock(&root->inode_lock);
4783 if (empty)
4784 btrfs_add_dead_root(root);
4785 }
4786}
4787
Jeff Mahoney143bede2012-03-01 14:56:26 +01004788void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004789{
4790 struct rb_node *node;
4791 struct rb_node *prev;
4792 struct btrfs_inode *entry;
4793 struct inode *inode;
4794 u64 objectid = 0;
4795
4796 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4797
4798 spin_lock(&root->inode_lock);
4799again:
4800 node = root->inode_tree.rb_node;
4801 prev = NULL;
4802 while (node) {
4803 prev = node;
4804 entry = rb_entry(node, struct btrfs_inode, rb_node);
4805
Li Zefan33345d012011-04-20 10:31:50 +08004806 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004807 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004808 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004809 node = node->rb_right;
4810 else
4811 break;
4812 }
4813 if (!node) {
4814 while (prev) {
4815 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004816 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004817 node = prev;
4818 break;
4819 }
4820 prev = rb_next(prev);
4821 }
4822 }
4823 while (node) {
4824 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004825 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004826 inode = igrab(&entry->vfs_inode);
4827 if (inode) {
4828 spin_unlock(&root->inode_lock);
4829 if (atomic_read(&inode->i_count) > 1)
4830 d_prune_aliases(inode);
4831 /*
Al Viro45321ac2010-06-07 13:43:19 -04004832 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004833 * the inode cache when its usage count
4834 * hits zero.
4835 */
4836 iput(inode);
4837 cond_resched();
4838 spin_lock(&root->inode_lock);
4839 goto again;
4840 }
4841
4842 if (cond_resched_lock(&root->inode_lock))
4843 goto again;
4844
4845 node = rb_next(node);
4846 }
4847 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004848}
4849
Chris Masone02119d2008-09-05 16:13:11 -04004850static int btrfs_init_locked_inode(struct inode *inode, void *p)
4851{
4852 struct btrfs_iget_args *args = p;
4853 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004854 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004855 return 0;
4856}
4857
4858static int btrfs_find_actor(struct inode *inode, void *opaque)
4859{
4860 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004861 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004862 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004863}
4864
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004865static struct inode *btrfs_iget_locked(struct super_block *s,
4866 u64 objectid,
4867 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004868{
4869 struct inode *inode;
4870 struct btrfs_iget_args args;
4871 args.ino = objectid;
4872 args.root = root;
4873
4874 inode = iget5_locked(s, objectid, btrfs_find_actor,
4875 btrfs_init_locked_inode,
4876 (void *)&args);
4877 return inode;
4878}
4879
Balaji Rao1a54ef82008-07-21 02:01:04 +05304880/* Get an inode object given its location and corresponding root.
4881 * Returns in *is_new if the inode was read from disk
4882 */
4883struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004884 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304885{
4886 struct inode *inode;
4887
4888 inode = btrfs_iget_locked(s, location->objectid, root);
4889 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004890 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304891
4892 if (inode->i_state & I_NEW) {
4893 BTRFS_I(inode)->root = root;
4894 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4895 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004896 if (!is_bad_inode(inode)) {
4897 inode_tree_add(inode);
4898 unlock_new_inode(inode);
4899 if (new)
4900 *new = 1;
4901 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004902 unlock_new_inode(inode);
4903 iput(inode);
4904 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004905 }
4906 }
4907
Balaji Rao1a54ef82008-07-21 02:01:04 +05304908 return inode;
4909}
4910
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004911static struct inode *new_simple_dir(struct super_block *s,
4912 struct btrfs_key *key,
4913 struct btrfs_root *root)
4914{
4915 struct inode *inode = new_inode(s);
4916
4917 if (!inode)
4918 return ERR_PTR(-ENOMEM);
4919
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004920 BTRFS_I(inode)->root = root;
4921 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004922 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004923
4924 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004925 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004926 inode->i_fop = &simple_dir_operations;
4927 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4928 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4929
4930 return inode;
4931}
4932
Chris Mason3de45862008-11-17 21:02:50 -05004933struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004934{
Chris Masond3977122009-01-05 21:25:51 -05004935 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004936 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004937 struct btrfs_root *sub_root = root;
4938 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004939 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004940 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004941
4942 if (dentry->d_name.len > BTRFS_NAME_LEN)
4943 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004944
Jeff Layton39e3c952012-11-28 11:30:53 -05004945 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04004946 if (ret < 0)
4947 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004948
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004949 if (location.objectid == 0)
4950 return NULL;
4951
4952 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004953 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004954 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004955 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004956
4957 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4958
Yan, Zheng76dda932009-09-21 16:00:26 -04004959 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004960 ret = fixup_tree_root_location(root, dir, dentry,
4961 &location, &sub_root);
4962 if (ret < 0) {
4963 if (ret != -ENOENT)
4964 inode = ERR_PTR(ret);
4965 else
4966 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4967 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004968 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004969 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004970 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4971
Julia Lawall34d19ba2011-01-24 19:55:19 +00004972 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004973 down_read(&root->fs_info->cleanup_work_sem);
4974 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004975 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004976 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04004977 if (ret) {
4978 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004979 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04004980 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00004981 }
4982
Chris Mason3de45862008-11-17 21:02:50 -05004983 return inode;
4984}
4985
Nick Pigginfe15ce42011-01-07 17:49:23 +11004986static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004987{
4988 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004989 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004990
Li Zefan848cce02012-02-21 17:04:28 +08004991 if (!inode && !IS_ROOT(dentry))
4992 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004993
Li Zefan848cce02012-02-21 17:04:28 +08004994 if (inode) {
4995 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004996 if (btrfs_root_refs(&root->root_item) == 0)
4997 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004998
4999 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5000 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005001 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005002 return 0;
5003}
5004
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005005static void btrfs_dentry_release(struct dentry *dentry)
5006{
5007 if (dentry->d_fsdata)
5008 kfree(dentry->d_fsdata);
5009}
5010
Chris Mason3de45862008-11-17 21:02:50 -05005011static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005012 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005013{
Josef Bacika66e7cc2011-09-18 10:34:03 -04005014 struct dentry *ret;
5015
5016 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005017 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005018}
5019
Miao Xie16cdcec2011-04-22 18:12:22 +08005020unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005021 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5022};
5023
Al Viro9cdda8d2013-05-22 16:48:09 -04005024static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005025{
Al Viro9cdda8d2013-05-22 16:48:09 -04005026 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005027 struct btrfs_root *root = BTRFS_I(inode)->root;
5028 struct btrfs_item *item;
5029 struct btrfs_dir_item *di;
5030 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005031 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005032 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005033 struct list_head ins_list;
5034 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005035 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005036 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005037 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005038 unsigned char d_type;
5039 int over = 0;
5040 u32 di_cur;
5041 u32 di_total;
5042 u32 di_len;
5043 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005044 char tmp_name[32];
5045 char *name_ptr;
5046 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005047 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005048
5049 /* FIXME, use a real flag for deciding about the key type */
5050 if (root->fs_info->tree_root == root)
5051 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005052
Al Viro9cdda8d2013-05-22 16:48:09 -04005053 if (!dir_emit_dots(file, ctx))
5054 return 0;
5055
David Woodhouse49593bf2008-08-17 17:08:36 +01005056 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005057 if (!path)
5058 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005059
Josef Bacik026fd312011-05-13 10:32:11 -04005060 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005061
Miao Xie16cdcec2011-04-22 18:12:22 +08005062 if (key_type == BTRFS_DIR_INDEX_KEY) {
5063 INIT_LIST_HEAD(&ins_list);
5064 INIT_LIST_HEAD(&del_list);
5065 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5066 }
5067
Chris Mason39279cc2007-06-12 06:35:45 -04005068 btrfs_set_key_type(&key, key_type);
Al Viro9cdda8d2013-05-22 16:48:09 -04005069 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005070 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005071
Chris Mason39279cc2007-06-12 06:35:45 -04005072 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5073 if (ret < 0)
5074 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005075
5076 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005077 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005078 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005079 if (slot >= btrfs_header_nritems(leaf)) {
5080 ret = btrfs_next_leaf(root, path);
5081 if (ret < 0)
5082 goto err;
5083 else if (ret > 0)
5084 break;
5085 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005086 }
Chris Mason3de45862008-11-17 21:02:50 -05005087
Chris Mason5f39d392007-10-15 16:14:19 -04005088 item = btrfs_item_nr(leaf, slot);
5089 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5090
5091 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005092 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005093 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005094 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005095 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005096 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005097 if (key_type == BTRFS_DIR_INDEX_KEY &&
5098 btrfs_should_delete_dir_index(&del_list,
5099 found_key.offset))
5100 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005101
Al Viro9cdda8d2013-05-22 16:48:09 -04005102 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005103 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005104
Chris Mason39279cc2007-06-12 06:35:45 -04005105 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5106 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005107 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005108
5109 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005110 struct btrfs_key location;
5111
Josef Bacik22a94d42011-03-16 16:47:17 -04005112 if (verify_dir_item(root, leaf, di))
5113 break;
5114
Chris Mason5f39d392007-10-15 16:14:19 -04005115 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005116 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005117 name_ptr = tmp_name;
5118 } else {
5119 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005120 if (!name_ptr) {
5121 ret = -ENOMEM;
5122 goto err;
5123 }
Chris Mason5f39d392007-10-15 16:14:19 -04005124 }
5125 read_extent_buffer(leaf, name_ptr,
5126 (unsigned long)(di + 1), name_len);
5127
5128 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5129 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005130
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005131
Chris Mason3de45862008-11-17 21:02:50 -05005132 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005133 * skip it.
5134 *
5135 * In contrast to old kernels, we insert the snapshot's
5136 * dir item and dir index after it has been created, so
5137 * we won't find a reference to our own snapshot. We
5138 * still keep the following code for backward
5139 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005140 */
5141 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5142 location.objectid == root->root_key.objectid) {
5143 over = 0;
5144 goto skip;
5145 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005146 over = !dir_emit(ctx, name_ptr, name_len,
5147 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005148
Chris Mason3de45862008-11-17 21:02:50 -05005149skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005150 if (name_ptr != tmp_name)
5151 kfree(name_ptr);
5152
Chris Mason39279cc2007-06-12 06:35:45 -04005153 if (over)
5154 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005155 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005156 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005157 di_cur += di_len;
5158 di = (struct btrfs_dir_item *)((char *)di + di_len);
5159 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005160next:
5161 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005162 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005163
Miao Xie16cdcec2011-04-22 18:12:22 +08005164 if (key_type == BTRFS_DIR_INDEX_KEY) {
5165 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005166 ctx->pos++;
5167 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005168 if (ret)
5169 goto nopos;
5170 }
5171
David Woodhouse49593bf2008-08-17 17:08:36 +01005172 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05005173 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00005174 /*
5175 * 32-bit glibc will use getdents64, but then strtol -
5176 * so the last number we can serve is this.
5177 */
Al Viro9cdda8d2013-05-22 16:48:09 -04005178 ctx->pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05005179 else
Al Viro9cdda8d2013-05-22 16:48:09 -04005180 ctx->pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04005181nopos:
5182 ret = 0;
5183err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005184 if (key_type == BTRFS_DIR_INDEX_KEY)
5185 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005186 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005187 return ret;
5188}
5189
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005190int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005191{
5192 struct btrfs_root *root = BTRFS_I(inode)->root;
5193 struct btrfs_trans_handle *trans;
5194 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005195 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005196
Josef Bacik72ac3c02012-05-23 14:13:11 -04005197 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005198 return 0;
5199
Liu Bo83eea1f2012-07-10 05:28:39 -06005200 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005201 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005202
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005203 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005204 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005205 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005206 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005207 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005208 if (IS_ERR(trans))
5209 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005210 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005211 }
5212 return ret;
5213}
5214
5215/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005216 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005217 * inode changes. But, it is most likely to find the inode in cache.
5218 * FIXME, needs more benchmarking...there are no reasons other than performance
5219 * to keep or drop this code.
5220 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005221static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005222{
5223 struct btrfs_root *root = BTRFS_I(inode)->root;
5224 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005225 int ret;
5226
Josef Bacik72ac3c02012-05-23 14:13:11 -04005227 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005228 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005229
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005230 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005231 if (IS_ERR(trans))
5232 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005233
5234 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005235 if (ret && ret == -ENOSPC) {
5236 /* whoops, lets try again with the full transaction */
5237 btrfs_end_transaction(trans, root);
5238 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005239 if (IS_ERR(trans))
5240 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005241
Chris Mason94b60442010-05-26 11:02:00 -04005242 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005243 }
Chris Mason39279cc2007-06-12 06:35:45 -04005244 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005245 if (BTRFS_I(inode)->delayed_node)
5246 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005247
5248 return ret;
5249}
5250
5251/*
5252 * This is a copy of file_update_time. We need this so we can return error on
5253 * ENOSPC for updating the inode in the case of file write and mmap writes.
5254 */
Josef Bacike41f9412012-03-26 09:46:47 -04005255static int btrfs_update_time(struct inode *inode, struct timespec *now,
5256 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005257{
Alexander Block2bc5565282012-06-15 09:49:33 +02005258 struct btrfs_root *root = BTRFS_I(inode)->root;
5259
5260 if (btrfs_root_readonly(root))
5261 return -EROFS;
5262
Josef Bacike41f9412012-03-26 09:46:47 -04005263 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005264 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005265 if (flags & S_CTIME)
5266 inode->i_ctime = *now;
5267 if (flags & S_MTIME)
5268 inode->i_mtime = *now;
5269 if (flags & S_ATIME)
5270 inode->i_atime = *now;
5271 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005272}
5273
Chris Masond352ac62008-09-29 15:18:18 -04005274/*
5275 * find the highest existing sequence number in a directory
5276 * and then set the in-memory index_cnt variable to reflect
5277 * free sequence numbers
5278 */
Josef Bacikaec74772008-07-24 12:12:38 -04005279static int btrfs_set_inode_index_count(struct inode *inode)
5280{
5281 struct btrfs_root *root = BTRFS_I(inode)->root;
5282 struct btrfs_key key, found_key;
5283 struct btrfs_path *path;
5284 struct extent_buffer *leaf;
5285 int ret;
5286
Li Zefan33345d012011-04-20 10:31:50 +08005287 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005288 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5289 key.offset = (u64)-1;
5290
5291 path = btrfs_alloc_path();
5292 if (!path)
5293 return -ENOMEM;
5294
5295 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5296 if (ret < 0)
5297 goto out;
5298 /* FIXME: we should be able to handle this */
5299 if (ret == 0)
5300 goto out;
5301 ret = 0;
5302
5303 /*
5304 * MAGIC NUMBER EXPLANATION:
5305 * since we search a directory based on f_pos we have to start at 2
5306 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5307 * else has to start at 2
5308 */
5309 if (path->slots[0] == 0) {
5310 BTRFS_I(inode)->index_cnt = 2;
5311 goto out;
5312 }
5313
5314 path->slots[0]--;
5315
5316 leaf = path->nodes[0];
5317 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5318
Li Zefan33345d012011-04-20 10:31:50 +08005319 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005320 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5321 BTRFS_I(inode)->index_cnt = 2;
5322 goto out;
5323 }
5324
5325 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5326out:
5327 btrfs_free_path(path);
5328 return ret;
5329}
5330
Chris Masond352ac62008-09-29 15:18:18 -04005331/*
5332 * helper to find a free sequence number in a given directory. This current
5333 * code is very simple, later versions will do smarter things in the btree
5334 */
Chris Mason3de45862008-11-17 21:02:50 -05005335int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005336{
5337 int ret = 0;
5338
5339 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005340 ret = btrfs_inode_delayed_dir_index_count(dir);
5341 if (ret) {
5342 ret = btrfs_set_inode_index_count(dir);
5343 if (ret)
5344 return ret;
5345 }
Josef Bacikaec74772008-07-24 12:12:38 -04005346 }
5347
Chris Mason00e4e6b2008-08-05 11:18:09 -04005348 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005349 BTRFS_I(dir)->index_cnt++;
5350
5351 return ret;
5352}
5353
Chris Mason39279cc2007-06-12 06:35:45 -04005354static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5355 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005356 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005357 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005358 u64 ref_objectid, u64 objectid,
5359 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005360{
5361 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005362 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005363 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005364 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005365 struct btrfs_inode_ref *ref;
5366 struct btrfs_key key[2];
5367 u32 sizes[2];
5368 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005369 int ret;
5370 int owner;
5371
Chris Mason5f39d392007-10-15 16:14:19 -04005372 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005373 if (!path)
5374 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005375
Chris Mason39279cc2007-06-12 06:35:45 -04005376 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005377 if (!inode) {
5378 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005379 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005380 }
Chris Mason39279cc2007-06-12 06:35:45 -04005381
Li Zefan581bb052011-04-20 10:06:11 +08005382 /*
5383 * we have to initialize this early, so we can reclaim the inode
5384 * number if we fail afterwards in this function.
5385 */
5386 inode->i_ino = objectid;
5387
Josef Bacikaec74772008-07-24 12:12:38 -04005388 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005389 trace_btrfs_inode_request(dir);
5390
Chris Mason3de45862008-11-17 21:02:50 -05005391 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005392 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005393 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005394 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005395 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005396 }
Josef Bacikaec74772008-07-24 12:12:38 -04005397 }
5398 /*
5399 * index_cnt is ignored for everything but a dir,
5400 * btrfs_get_inode_index_count has an explanation for the magic
5401 * number
5402 */
5403 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005404 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005405 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005406 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005407
Josef Bacik5dc562c2012-08-17 13:14:17 -04005408 /*
5409 * We could have gotten an inode number from somebody who was fsynced
5410 * and then removed in this same transaction, so let's just set full
5411 * sync since it will be a full sync anyway and this will blow away the
5412 * old info in the log.
5413 */
5414 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5415
Al Viro569254b2011-07-24 17:08:40 -04005416 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005417 owner = 0;
5418 else
5419 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005420
5421 key[0].objectid = objectid;
5422 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5423 key[0].offset = 0;
5424
Mark Fashehf1863732012-08-08 11:32:27 -07005425 /*
5426 * Start new inodes with an inode_ref. This is slightly more
5427 * efficient for small numbers of hard links since they will
5428 * be packed into one item. Extended refs will kick in if we
5429 * add more hard links than can fit in the ref item.
5430 */
Chris Mason9c583092008-01-29 15:15:18 -05005431 key[1].objectid = objectid;
5432 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5433 key[1].offset = ref_objectid;
5434
5435 sizes[0] = sizeof(struct btrfs_inode_item);
5436 sizes[1] = name_len + sizeof(*ref);
5437
Chris Masonb9473432009-03-13 11:00:37 -04005438 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005439 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5440 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005441 goto fail;
5442
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005443 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005444 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005445 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005446 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5447 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005448 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5449 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005450 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005451
5452 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5453 struct btrfs_inode_ref);
5454 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005455 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005456 ptr = (unsigned long)(ref + 1);
5457 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5458
Chris Mason5f39d392007-10-15 16:14:19 -04005459 btrfs_mark_buffer_dirty(path->nodes[0]);
5460 btrfs_free_path(path);
5461
Chris Mason39279cc2007-06-12 06:35:45 -04005462 location = &BTRFS_I(inode)->location;
5463 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005464 location->offset = 0;
5465 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5466
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005467 btrfs_inherit_iflags(inode, dir);
5468
Al Viro569254b2011-07-24 17:08:40 -04005469 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005470 if (btrfs_test_opt(root, NODATASUM))
5471 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005472 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005473 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5474 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005475 }
5476
Chris Mason39279cc2007-06-12 06:35:45 -04005477 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005478 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005479
5480 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005481 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005482
Alexander Block8ea05e32012-07-25 17:35:53 +02005483 btrfs_update_root_times(trans, root);
5484
Chris Mason39279cc2007-06-12 06:35:45 -04005485 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005486fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005487 if (dir)
5488 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005489 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005490 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005491 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005492}
5493
5494static inline u8 btrfs_inode_type(struct inode *inode)
5495{
5496 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5497}
5498
Chris Masond352ac62008-09-29 15:18:18 -04005499/*
5500 * utility function to add 'inode' into 'parent_inode' with
5501 * a give name and a given sequence number.
5502 * if 'add_backref' is true, also insert a backref from the
5503 * inode to the parent directory.
5504 */
Chris Masone02119d2008-09-05 16:13:11 -04005505int btrfs_add_link(struct btrfs_trans_handle *trans,
5506 struct inode *parent_inode, struct inode *inode,
5507 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005508{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005509 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005510 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005511 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005512 u64 ino = btrfs_ino(inode);
5513 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005514
Li Zefan33345d012011-04-20 10:31:50 +08005515 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005516 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5517 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005518 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005519 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5520 key.offset = 0;
5521 }
Chris Mason39279cc2007-06-12 06:35:45 -04005522
Li Zefan33345d012011-04-20 10:31:50 +08005523 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005524 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5525 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005526 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005527 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005528 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5529 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005530 }
5531
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005532 /* Nothing to clean up yet */
5533 if (ret)
5534 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005535
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005536 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5537 parent_inode, &key,
5538 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005539 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005540 goto fail_dir_item;
5541 else if (ret) {
5542 btrfs_abort_transaction(trans, root, ret);
5543 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005544 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005545
5546 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5547 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005548 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005549 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5550 ret = btrfs_update_inode(trans, root, parent_inode);
5551 if (ret)
5552 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005553 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005554
5555fail_dir_item:
5556 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5557 u64 local_index;
5558 int err;
5559 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5560 key.objectid, root->root_key.objectid,
5561 parent_ino, &local_index, name, name_len);
5562
5563 } else if (add_backref) {
5564 u64 local_index;
5565 int err;
5566
5567 err = btrfs_del_inode_ref(trans, root, name, name_len,
5568 ino, parent_ino, &local_index);
5569 }
5570 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005571}
5572
5573static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005574 struct inode *dir, struct dentry *dentry,
5575 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005576{
Josef Bacika1b075d2010-11-19 20:36:11 +00005577 int err = btrfs_add_link(trans, dir, inode,
5578 dentry->d_name.name, dentry->d_name.len,
5579 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005580 if (err > 0)
5581 err = -EEXIST;
5582 return err;
5583}
5584
Josef Bacik618e21d2007-07-11 10:18:17 -04005585static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005586 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005587{
5588 struct btrfs_trans_handle *trans;
5589 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005590 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005591 int err;
5592 int drop_inode = 0;
5593 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005594 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005595
5596 if (!new_valid_dev(rdev))
5597 return -EINVAL;
5598
Josef Bacik9ed74f22009-09-11 16:12:44 -04005599 /*
5600 * 2 for inode item and ref
5601 * 2 for dir items
5602 * 1 for xattr if selinux is on
5603 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005604 trans = btrfs_start_transaction(root, 5);
5605 if (IS_ERR(trans))
5606 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005607
Li Zefan581bb052011-04-20 10:06:11 +08005608 err = btrfs_find_free_ino(root, &objectid);
5609 if (err)
5610 goto out_unlock;
5611
Josef Bacikaec74772008-07-24 12:12:38 -04005612 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005613 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005614 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005615 if (IS_ERR(inode)) {
5616 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005617 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005618 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005619
Eric Paris2a7dba32011-02-01 11:05:39 -05005620 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005621 if (err) {
5622 drop_inode = 1;
5623 goto out_unlock;
5624 }
5625
Casey Schauflerad19db72011-12-15 10:09:07 -05005626 /*
5627 * If the active LSM wants to access the inode during
5628 * d_instantiate it needs these. Smack checks to see
5629 * if the filesystem supports xattrs by looking at the
5630 * ops vector.
5631 */
5632
5633 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005634 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005635 if (err)
5636 drop_inode = 1;
5637 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005638 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005639 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005640 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005641 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005642out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005643 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005644 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005645 if (drop_inode) {
5646 inode_dec_link_count(inode);
5647 iput(inode);
5648 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005649 return err;
5650}
5651
Chris Mason39279cc2007-06-12 06:35:45 -04005652static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005653 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005654{
5655 struct btrfs_trans_handle *trans;
5656 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005657 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005658 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005659 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005660 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005661 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005662
Josef Bacik9ed74f22009-09-11 16:12:44 -04005663 /*
5664 * 2 for inode item and ref
5665 * 2 for dir items
5666 * 1 for xattr if selinux is on
5667 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005668 trans = btrfs_start_transaction(root, 5);
5669 if (IS_ERR(trans))
5670 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005671
Li Zefan581bb052011-04-20 10:06:11 +08005672 err = btrfs_find_free_ino(root, &objectid);
5673 if (err)
5674 goto out_unlock;
5675
Josef Bacikaec74772008-07-24 12:12:38 -04005676 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005677 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005678 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005679 if (IS_ERR(inode)) {
5680 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005681 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005682 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005683 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005684
Eric Paris2a7dba32011-02-01 11:05:39 -05005685 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005686 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005687 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005688
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005689 err = btrfs_update_inode(trans, root, inode);
5690 if (err)
5691 goto out_unlock;
5692
Casey Schauflerad19db72011-12-15 10:09:07 -05005693 /*
5694 * If the active LSM wants to access the inode during
5695 * d_instantiate it needs these. Smack checks to see
5696 * if the filesystem supports xattrs by looking at the
5697 * ops vector.
5698 */
5699 inode->i_fop = &btrfs_file_operations;
5700 inode->i_op = &btrfs_file_inode_operations;
5701
Josef Bacika1b075d2010-11-19 20:36:11 +00005702 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005703 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005704 goto out_unlock;
5705
5706 inode->i_mapping->a_ops = &btrfs_aops;
5707 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5708 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5709 d_instantiate(dentry, inode);
5710
Chris Mason39279cc2007-06-12 06:35:45 -04005711out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005712 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005713 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005714 inode_dec_link_count(inode);
5715 iput(inode);
5716 }
Liu Bob53d3f52012-11-14 14:34:34 +00005717 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005718 return err;
5719}
5720
5721static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5722 struct dentry *dentry)
5723{
5724 struct btrfs_trans_handle *trans;
5725 struct btrfs_root *root = BTRFS_I(dir)->root;
5726 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005727 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005728 int err;
5729 int drop_inode = 0;
5730
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005731 /* do not allow sys_link's with other subvols of the same device */
5732 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005733 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005734
Mark Fashehf1863732012-08-08 11:32:27 -07005735 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005736 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005737
Chris Mason3de45862008-11-17 21:02:50 -05005738 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005739 if (err)
5740 goto fail;
5741
Yan, Zhenga22285a2010-05-16 10:48:46 -04005742 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005743 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005744 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005745 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005746 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005747 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005748 if (IS_ERR(trans)) {
5749 err = PTR_ERR(trans);
5750 goto fail;
5751 }
Chris Mason5f39d392007-10-15 16:14:19 -04005752
Miao Xie31534952011-04-13 13:19:21 +08005753 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005754 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005755 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005756 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005757 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005758
Josef Bacika1b075d2010-11-19 20:36:11 +00005759 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005760
Yan, Zhenga5719522009-09-24 09:17:31 -04005761 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005762 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005763 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005764 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005765 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005766 if (err)
5767 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005768 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005769 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005770 }
Chris Mason39279cc2007-06-12 06:35:45 -04005771
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005772 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005773fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005774 if (drop_inode) {
5775 inode_dec_link_count(inode);
5776 iput(inode);
5777 }
Liu Bob53d3f52012-11-14 14:34:34 +00005778 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005779 return err;
5780}
5781
Al Viro18bb1db2011-07-26 01:41:39 -04005782static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005783{
Chris Masonb9d86662008-05-02 16:13:49 -04005784 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005785 struct btrfs_trans_handle *trans;
5786 struct btrfs_root *root = BTRFS_I(dir)->root;
5787 int err = 0;
5788 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005789 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005790 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005791
Josef Bacik9ed74f22009-09-11 16:12:44 -04005792 /*
5793 * 2 items for inode and ref
5794 * 2 items for dir items
5795 * 1 for xattr if selinux is on
5796 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005797 trans = btrfs_start_transaction(root, 5);
5798 if (IS_ERR(trans))
5799 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005800
Li Zefan581bb052011-04-20 10:06:11 +08005801 err = btrfs_find_free_ino(root, &objectid);
5802 if (err)
5803 goto out_fail;
5804
Josef Bacikaec74772008-07-24 12:12:38 -04005805 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005806 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005807 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005808 if (IS_ERR(inode)) {
5809 err = PTR_ERR(inode);
5810 goto out_fail;
5811 }
Chris Mason5f39d392007-10-15 16:14:19 -04005812
Chris Mason39279cc2007-06-12 06:35:45 -04005813 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005814
Eric Paris2a7dba32011-02-01 11:05:39 -05005815 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005816 if (err)
5817 goto out_fail;
5818
Chris Mason39279cc2007-06-12 06:35:45 -04005819 inode->i_op = &btrfs_dir_inode_operations;
5820 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005821
Chris Masondbe674a2008-07-17 12:54:05 -04005822 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005823 err = btrfs_update_inode(trans, root, inode);
5824 if (err)
5825 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005826
Josef Bacika1b075d2010-11-19 20:36:11 +00005827 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5828 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005829 if (err)
5830 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005831
Chris Mason39279cc2007-06-12 06:35:45 -04005832 d_instantiate(dentry, inode);
5833 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005834
5835out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005836 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005837 if (drop_on_err)
5838 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005839 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005840 return err;
5841}
5842
Chris Masond352ac62008-09-29 15:18:18 -04005843/* helper for btfs_get_extent. Given an existing extent in the tree,
5844 * and an extent that you want to insert, deal with overlap and insert
5845 * the new extent into the tree.
5846 */
Chris Mason3b951512008-04-17 11:29:12 -04005847static int merge_extent_mapping(struct extent_map_tree *em_tree,
5848 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005849 struct extent_map *em,
5850 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005851{
5852 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005853
Chris Masone6dcd2d2008-07-17 12:53:50 -04005854 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5855 start_diff = map_start - em->start;
5856 em->start = map_start;
5857 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005858 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5859 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005860 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005861 em->block_len -= start_diff;
5862 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005863 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005864}
5865
Chris Masonc8b97812008-10-29 14:49:59 -04005866static noinline int uncompress_inline(struct btrfs_path *path,
5867 struct inode *inode, struct page *page,
5868 size_t pg_offset, u64 extent_offset,
5869 struct btrfs_file_extent_item *item)
5870{
5871 int ret;
5872 struct extent_buffer *leaf = path->nodes[0];
5873 char *tmp;
5874 size_t max_size;
5875 unsigned long inline_size;
5876 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005877 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005878
5879 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005880 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005881 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5882 inline_size = btrfs_file_extent_inline_item_len(leaf,
5883 btrfs_item_nr(leaf, path->slots[0]));
5884 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005885 if (!tmp)
5886 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005887 ptr = btrfs_file_extent_inline_start(item);
5888
5889 read_extent_buffer(leaf, tmp, ptr, inline_size);
5890
Chris Mason5b050f02008-11-11 09:34:41 -05005891 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005892 ret = btrfs_decompress(compress_type, tmp, page,
5893 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005894 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005895 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005896 unsigned long copy_size = min_t(u64,
5897 PAGE_CACHE_SIZE - pg_offset,
5898 max_size - extent_offset);
5899 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005900 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005901 }
5902 kfree(tmp);
5903 return 0;
5904}
5905
Chris Masond352ac62008-09-29 15:18:18 -04005906/*
5907 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005908 * the ugly parts come from merging extents from the disk with the in-ram
5909 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005910 * where the in-ram extents might be locked pending data=ordered completion.
5911 *
5912 * This also copies inline extents directly into the page.
5913 */
Chris Masond3977122009-01-05 21:25:51 -05005914
Chris Masona52d9a82007-08-27 16:49:44 -04005915struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005916 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005917 int create)
5918{
5919 int ret;
5920 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005921 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005922 u64 extent_start = 0;
5923 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005924 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005925 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005926 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005927 struct btrfs_root *root = BTRFS_I(inode)->root;
5928 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005929 struct extent_buffer *leaf;
5930 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005931 struct extent_map *em = NULL;
5932 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005933 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005934 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005935 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005936
Chris Masona52d9a82007-08-27 16:49:44 -04005937again:
Chris Mason890871b2009-09-02 16:24:52 -04005938 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005939 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005940 if (em)
5941 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005942 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005943
Chris Masona52d9a82007-08-27 16:49:44 -04005944 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005945 if (em->start > start || em->start + em->len <= start)
5946 free_extent_map(em);
5947 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005948 free_extent_map(em);
5949 else
5950 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005951 }
David Sterba172ddd62011-04-21 00:48:27 +02005952 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005953 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005954 err = -ENOMEM;
5955 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005956 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005957 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005958 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005959 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005960 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005961 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005962
5963 if (!path) {
5964 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005965 if (!path) {
5966 err = -ENOMEM;
5967 goto out;
5968 }
5969 /*
5970 * Chances are we'll be called again, so go ahead and do
5971 * readahead
5972 */
5973 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005974 }
5975
Chris Mason179e29e2007-11-01 11:28:41 -04005976 ret = btrfs_lookup_file_extent(trans, root, path,
5977 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005978 if (ret < 0) {
5979 err = ret;
5980 goto out;
5981 }
5982
5983 if (ret != 0) {
5984 if (path->slots[0] == 0)
5985 goto not_found;
5986 path->slots[0]--;
5987 }
5988
Chris Mason5f39d392007-10-15 16:14:19 -04005989 leaf = path->nodes[0];
5990 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005991 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005992 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005993 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5994 found_type = btrfs_key_type(&found_key);
5995 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005996 found_type != BTRFS_EXTENT_DATA_KEY) {
5997 goto not_found;
5998 }
5999
Chris Mason5f39d392007-10-15 16:14:19 -04006000 found_type = btrfs_file_extent_type(leaf, item);
6001 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006002 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006003 if (found_type == BTRFS_FILE_EXTENT_REG ||
6004 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006005 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006006 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006007 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6008 size_t size;
6009 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006010 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006011 }
6012
6013 if (start >= extent_end) {
6014 path->slots[0]++;
6015 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6016 ret = btrfs_next_leaf(root, path);
6017 if (ret < 0) {
6018 err = ret;
6019 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006020 }
Yan Zheng9036c102008-10-30 14:19:41 -04006021 if (ret > 0)
6022 goto not_found;
6023 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006024 }
Yan Zheng9036c102008-10-30 14:19:41 -04006025 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6026 if (found_key.objectid != objectid ||
6027 found_key.type != BTRFS_EXTENT_DATA_KEY)
6028 goto not_found;
6029 if (start + len <= found_key.offset)
6030 goto not_found;
6031 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006032 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006033 em->len = found_key.offset - start;
6034 goto not_found_em;
6035 }
6036
Josef Bacikcc95bef2013-04-04 14:31:27 -04006037 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006038 if (found_type == BTRFS_FILE_EXTENT_REG ||
6039 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006040 em->start = extent_start;
6041 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006042 em->orig_start = extent_start -
6043 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006044 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6045 item);
Chris Masondb945352007-10-15 16:15:53 -04006046 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6047 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006048 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006049 goto insert;
6050 }
Li Zefan261507a02010-12-17 14:21:50 +08006051 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006052 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006053 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006054 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006055 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006056 } else {
6057 bytenr += btrfs_file_extent_offset(leaf, item);
6058 em->block_start = bytenr;
6059 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006060 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6061 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006062 }
Chris Masona52d9a82007-08-27 16:49:44 -04006063 goto insert;
6064 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006065 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006066 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006067 size_t size;
6068 size_t extent_offset;
6069 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006070
Chris Masona52d9a82007-08-27 16:49:44 -04006071 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006072 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006073 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006074 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006075 goto out;
6076 }
6077
Yan Zheng9036c102008-10-30 14:19:41 -04006078 size = btrfs_file_extent_inline_len(leaf, item);
6079 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006080 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006081 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006082 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006083 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006084 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006085 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006086 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006087 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006088 em->compress_type = compress_type;
6089 }
Yan689f9342007-10-29 11:41:07 -04006090 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006091 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006092 if (btrfs_file_extent_compression(leaf, item) !=
6093 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006094 ret = uncompress_inline(path, inode, page,
6095 pg_offset,
6096 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006097 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006098 } else {
6099 map = kmap(page);
6100 read_extent_buffer(leaf, map + pg_offset, ptr,
6101 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006102 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6103 memset(map + pg_offset + copy_size, 0,
6104 PAGE_CACHE_SIZE - pg_offset -
6105 copy_size);
6106 }
Chris Masonc8b97812008-10-29 14:49:59 -04006107 kunmap(page);
6108 }
Chris Mason179e29e2007-11-01 11:28:41 -04006109 flush_dcache_page(page);
6110 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006111 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006112 if (!trans) {
6113 kunmap(page);
6114 free_extent_map(em);
6115 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006116
David Sterbab3b4aa72011-04-21 01:20:15 +02006117 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006118 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006119
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006120 if (IS_ERR(trans))
6121 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006122 goto again;
6123 }
Chris Masonc8b97812008-10-29 14:49:59 -04006124 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006125 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006126 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006127 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006128 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006129 }
Chris Masond1310b22008-01-24 16:13:08 -05006130 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006131 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006132 goto insert;
6133 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006134 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006135 }
6136not_found:
6137 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006138 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006139 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006140not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006141 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006142 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006143insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006144 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006145 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006146 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6147 (unsigned long long)em->start,
6148 (unsigned long long)em->len,
6149 (unsigned long long)start,
6150 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006151 err = -EIO;
6152 goto out;
6153 }
Chris Masond1310b22008-01-24 16:13:08 -05006154
6155 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006156 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006157 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006158 /* it is possible that someone inserted the extent into the tree
6159 * while we had the lock dropped. It is also possible that
6160 * an overlapping map exists in the tree
6161 */
Chris Masona52d9a82007-08-27 16:49:44 -04006162 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006163 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006164
6165 ret = 0;
6166
Chris Mason3b951512008-04-17 11:29:12 -04006167 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006168 if (existing && (existing->start > start ||
6169 existing->start + existing->len <= start)) {
6170 free_extent_map(existing);
6171 existing = NULL;
6172 }
Chris Mason3b951512008-04-17 11:29:12 -04006173 if (!existing) {
6174 existing = lookup_extent_mapping(em_tree, em->start,
6175 em->len);
6176 if (existing) {
6177 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006178 em, start,
6179 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006180 free_extent_map(existing);
6181 if (err) {
6182 free_extent_map(em);
6183 em = NULL;
6184 }
6185 } else {
6186 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006187 free_extent_map(em);
6188 em = NULL;
6189 }
6190 } else {
6191 free_extent_map(em);
6192 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006193 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006194 }
Chris Masona52d9a82007-08-27 16:49:44 -04006195 }
Chris Mason890871b2009-09-02 16:24:52 -04006196 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006197out:
liubo1abe9b82011-03-24 11:18:59 +00006198
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006199 if (em)
6200 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006201
Chris Masonf4219502008-07-22 11:18:09 -04006202 if (path)
6203 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006204 if (trans) {
6205 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006206 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006207 err = ret;
6208 }
Chris Masona52d9a82007-08-27 16:49:44 -04006209 if (err) {
6210 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006211 return ERR_PTR(err);
6212 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006213 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006214 return em;
6215}
6216
Chris Masonec29ed52011-02-23 16:23:20 -05006217struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6218 size_t pg_offset, u64 start, u64 len,
6219 int create)
6220{
6221 struct extent_map *em;
6222 struct extent_map *hole_em = NULL;
6223 u64 range_start = start;
6224 u64 end;
6225 u64 found;
6226 u64 found_end;
6227 int err = 0;
6228
6229 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6230 if (IS_ERR(em))
6231 return em;
6232 if (em) {
6233 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006234 * if our em maps to
6235 * - a hole or
6236 * - a pre-alloc extent,
6237 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006238 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006239 if (em->block_start != EXTENT_MAP_HOLE &&
6240 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006241 return em;
6242 else
6243 hole_em = em;
6244 }
6245
6246 /* check to see if we've wrapped (len == -1 or similar) */
6247 end = start + len;
6248 if (end < start)
6249 end = (u64)-1;
6250 else
6251 end -= 1;
6252
6253 em = NULL;
6254
6255 /* ok, we didn't find anything, lets look for delalloc */
6256 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6257 end, len, EXTENT_DELALLOC, 1);
6258 found_end = range_start + found;
6259 if (found_end < range_start)
6260 found_end = (u64)-1;
6261
6262 /*
6263 * we didn't find anything useful, return
6264 * the original results from get_extent()
6265 */
6266 if (range_start > end || found_end <= start) {
6267 em = hole_em;
6268 hole_em = NULL;
6269 goto out;
6270 }
6271
6272 /* adjust the range_start to make sure it doesn't
6273 * go backwards from the start they passed in
6274 */
6275 range_start = max(start,range_start);
6276 found = found_end - range_start;
6277
6278 if (found > 0) {
6279 u64 hole_start = start;
6280 u64 hole_len = len;
6281
David Sterba172ddd62011-04-21 00:48:27 +02006282 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006283 if (!em) {
6284 err = -ENOMEM;
6285 goto out;
6286 }
6287 /*
6288 * when btrfs_get_extent can't find anything it
6289 * returns one huge hole
6290 *
6291 * make sure what it found really fits our range, and
6292 * adjust to make sure it is based on the start from
6293 * the caller
6294 */
6295 if (hole_em) {
6296 u64 calc_end = extent_map_end(hole_em);
6297
6298 if (calc_end <= start || (hole_em->start > end)) {
6299 free_extent_map(hole_em);
6300 hole_em = NULL;
6301 } else {
6302 hole_start = max(hole_em->start, start);
6303 hole_len = calc_end - hole_start;
6304 }
6305 }
6306 em->bdev = NULL;
6307 if (hole_em && range_start > hole_start) {
6308 /* our hole starts before our delalloc, so we
6309 * have to return just the parts of the hole
6310 * that go until the delalloc starts
6311 */
6312 em->len = min(hole_len,
6313 range_start - hole_start);
6314 em->start = hole_start;
6315 em->orig_start = hole_start;
6316 /*
6317 * don't adjust block start at all,
6318 * it is fixed at EXTENT_MAP_HOLE
6319 */
6320 em->block_start = hole_em->block_start;
6321 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006322 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6323 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006324 } else {
6325 em->start = range_start;
6326 em->len = found;
6327 em->orig_start = range_start;
6328 em->block_start = EXTENT_MAP_DELALLOC;
6329 em->block_len = found;
6330 }
6331 } else if (hole_em) {
6332 return hole_em;
6333 }
6334out:
6335
6336 free_extent_map(hole_em);
6337 if (err) {
6338 free_extent_map(em);
6339 return ERR_PTR(err);
6340 }
6341 return em;
6342}
6343
Josef Bacik4b46fce2010-05-23 11:00:55 -04006344static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6345 u64 start, u64 len)
6346{
6347 struct btrfs_root *root = BTRFS_I(inode)->root;
6348 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006349 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006350 struct btrfs_key ins;
6351 u64 alloc_hint;
6352 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006353
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006354 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006355 if (IS_ERR(trans))
6356 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006357
6358 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6359
6360 alloc_hint = get_extent_allocation_hint(inode, start, len);
6361 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006362 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006363 if (ret) {
6364 em = ERR_PTR(ret);
6365 goto out;
6366 }
6367
Josef Bacik70c8a912012-10-11 16:54:30 -04006368 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006369 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik70c8a912012-10-11 16:54:30 -04006370 if (IS_ERR(em))
6371 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006372
6373 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6374 ins.offset, ins.offset, 0);
6375 if (ret) {
6376 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6377 em = ERR_PTR(ret);
6378 }
6379out:
6380 btrfs_end_transaction(trans, root);
6381 return em;
6382}
6383
Chris Mason46bfbb52010-05-26 11:04:10 -04006384/*
6385 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6386 * block must be cow'd
6387 */
Josef Bacik7ee9e442013-06-21 16:37:03 -04006388noinline int can_nocow_extent(struct btrfs_trans_handle *trans,
6389 struct inode *inode, u64 offset, u64 *len,
6390 u64 *orig_start, u64 *orig_block_len,
6391 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006392{
6393 struct btrfs_path *path;
6394 int ret;
6395 struct extent_buffer *leaf;
6396 struct btrfs_root *root = BTRFS_I(inode)->root;
6397 struct btrfs_file_extent_item *fi;
6398 struct btrfs_key key;
6399 u64 disk_bytenr;
6400 u64 backref_offset;
6401 u64 extent_end;
6402 u64 num_bytes;
6403 int slot;
6404 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006405 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Chris Mason46bfbb52010-05-26 11:04:10 -04006406 path = btrfs_alloc_path();
6407 if (!path)
6408 return -ENOMEM;
6409
Li Zefan33345d012011-04-20 10:31:50 +08006410 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006411 offset, 0);
6412 if (ret < 0)
6413 goto out;
6414
6415 slot = path->slots[0];
6416 if (ret == 1) {
6417 if (slot == 0) {
6418 /* can't find the item, must cow */
6419 ret = 0;
6420 goto out;
6421 }
6422 slot--;
6423 }
6424 ret = 0;
6425 leaf = path->nodes[0];
6426 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006427 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006428 key.type != BTRFS_EXTENT_DATA_KEY) {
6429 /* not our file or wrong item type, must cow */
6430 goto out;
6431 }
6432
6433 if (key.offset > offset) {
6434 /* Wrong offset, must cow */
6435 goto out;
6436 }
6437
6438 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6439 found_type = btrfs_file_extent_type(leaf, fi);
6440 if (found_type != BTRFS_FILE_EXTENT_REG &&
6441 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6442 /* not a regular extent, must cow */
6443 goto out;
6444 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006445
6446 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6447 goto out;
6448
Chris Mason46bfbb52010-05-26 11:04:10 -04006449 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006450 if (disk_bytenr == 0)
6451 goto out;
6452
6453 if (btrfs_file_extent_compression(leaf, fi) ||
6454 btrfs_file_extent_encryption(leaf, fi) ||
6455 btrfs_file_extent_other_encoding(leaf, fi))
6456 goto out;
6457
Chris Mason46bfbb52010-05-26 11:04:10 -04006458 backref_offset = btrfs_file_extent_offset(leaf, fi);
6459
Josef Bacik7ee9e442013-06-21 16:37:03 -04006460 if (orig_start) {
6461 *orig_start = key.offset - backref_offset;
6462 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6463 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6464 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006465
Chris Mason46bfbb52010-05-26 11:04:10 -04006466 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason46bfbb52010-05-26 11:04:10 -04006467
6468 if (btrfs_extent_readonly(root, disk_bytenr))
6469 goto out;
6470
6471 /*
6472 * look for other files referencing this extent, if we
6473 * find any we must cow
6474 */
Li Zefan33345d012011-04-20 10:31:50 +08006475 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006476 key.offset - backref_offset, disk_bytenr))
6477 goto out;
6478
6479 /*
6480 * adjust disk_bytenr and num_bytes to cover just the bytes
6481 * in this extent we are about to write. If there
6482 * are any csums in that range we have to cow in order
6483 * to keep the csums correct
6484 */
6485 disk_bytenr += backref_offset;
6486 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006487 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006488 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6489 goto out;
6490 /*
6491 * all of the above have passed, it is safe to overwrite this extent
6492 * without cow
6493 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006494 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006495 ret = 1;
6496out:
6497 btrfs_free_path(path);
6498 return ret;
6499}
6500
Josef Bacikeb838e72012-07-31 16:28:48 -04006501static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6502 struct extent_state **cached_state, int writing)
6503{
6504 struct btrfs_ordered_extent *ordered;
6505 int ret = 0;
6506
6507 while (1) {
6508 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6509 0, cached_state);
6510 /*
6511 * We're concerned with the entire range that we're going to be
6512 * doing DIO to, so we need to make sure theres no ordered
6513 * extents in this range.
6514 */
6515 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6516 lockend - lockstart + 1);
6517
6518 /*
6519 * We need to make sure there are no buffered pages in this
6520 * range either, we could have raced between the invalidate in
6521 * generic_file_direct_write and locking the extent. The
6522 * invalidate needs to happen so that reads after a write do not
6523 * get stale data.
6524 */
6525 if (!ordered && (!writing ||
6526 !test_range_bit(&BTRFS_I(inode)->io_tree,
6527 lockstart, lockend, EXTENT_UPTODATE, 0,
6528 *cached_state)))
6529 break;
6530
6531 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6532 cached_state, GFP_NOFS);
6533
6534 if (ordered) {
6535 btrfs_start_ordered_extent(inode, ordered, 1);
6536 btrfs_put_ordered_extent(ordered);
6537 } else {
6538 /* Screw you mmap */
6539 ret = filemap_write_and_wait_range(inode->i_mapping,
6540 lockstart,
6541 lockend);
6542 if (ret)
6543 break;
6544
6545 /*
6546 * If we found a page that couldn't be invalidated just
6547 * fall back to buffered.
6548 */
6549 ret = invalidate_inode_pages2_range(inode->i_mapping,
6550 lockstart >> PAGE_CACHE_SHIFT,
6551 lockend >> PAGE_CACHE_SHIFT);
6552 if (ret)
6553 break;
6554 }
6555
6556 cond_resched();
6557 }
6558
6559 return ret;
6560}
6561
Josef Bacik69ffb542012-09-11 15:40:07 -04006562static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6563 u64 len, u64 orig_start,
6564 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006565 u64 orig_block_len, u64 ram_bytes,
6566 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006567{
6568 struct extent_map_tree *em_tree;
6569 struct extent_map *em;
6570 struct btrfs_root *root = BTRFS_I(inode)->root;
6571 int ret;
6572
6573 em_tree = &BTRFS_I(inode)->extent_tree;
6574 em = alloc_extent_map();
6575 if (!em)
6576 return ERR_PTR(-ENOMEM);
6577
6578 em->start = start;
6579 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006580 em->mod_start = start;
6581 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006582 em->len = len;
6583 em->block_len = block_len;
6584 em->block_start = block_start;
6585 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006586 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006587 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006588 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006589 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6590 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006591 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006592
6593 do {
6594 btrfs_drop_extent_cache(inode, em->start,
6595 em->start + em->len - 1, 0);
6596 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006597 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006598 write_unlock(&em_tree->lock);
6599 } while (ret == -EEXIST);
6600
6601 if (ret) {
6602 free_extent_map(em);
6603 return ERR_PTR(ret);
6604 }
6605
6606 return em;
6607}
6608
6609
Josef Bacik4b46fce2010-05-23 11:00:55 -04006610static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6611 struct buffer_head *bh_result, int create)
6612{
6613 struct extent_map *em;
6614 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006615 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006616 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006617 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006618 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006619 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006620 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006621 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006622
Miao Xie172a5042013-02-21 02:48:22 -07006623 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006624 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006625 else
Josef Bacikc3298612012-08-03 16:49:19 -04006626 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006627
Josef Bacikc3298612012-08-03 16:49:19 -04006628 lockstart = start;
6629 lockend = start + len - 1;
6630
Josef Bacikeb838e72012-07-31 16:28:48 -04006631 /*
6632 * If this errors out it's because we couldn't invalidate pagecache for
6633 * this range and we need to fallback to buffered.
6634 */
6635 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6636 return -ENOTBLK;
6637
Josef Bacik4b46fce2010-05-23 11:00:55 -04006638 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006639 if (IS_ERR(em)) {
6640 ret = PTR_ERR(em);
6641 goto unlock_err;
6642 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006643
6644 /*
6645 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6646 * io. INLINE is special, and we could probably kludge it in here, but
6647 * it's still buffered so for safety lets just fall back to the generic
6648 * buffered path.
6649 *
6650 * For COMPRESSED we _have_ to read the entire extent in so we can
6651 * decompress it, so there will be buffering required no matter what we
6652 * do, so go ahead and fallback to buffered.
6653 *
6654 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6655 * to buffered IO. Don't blame me, this is the price we pay for using
6656 * the generic code.
6657 */
6658 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6659 em->block_start == EXTENT_MAP_INLINE) {
6660 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006661 ret = -ENOTBLK;
6662 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006663 }
6664
6665 /* Just a good old fashioned hole, return */
6666 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6667 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6668 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006669 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006670 }
6671
6672 /*
6673 * We don't allocate a new extent in the following cases
6674 *
6675 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6676 * existing extent.
6677 * 2) The extent is marked as PREALLOC. We're good to go here and can
6678 * just use the extent.
6679 *
6680 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006681 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006682 len = min(len, em->len - (start - em->start));
6683 lockstart = start + len;
6684 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006685 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006686
6687 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6688 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6689 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006690 int type;
6691 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006692 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006693
6694 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6695 type = BTRFS_ORDERED_PREALLOC;
6696 else
6697 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006698 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006699 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006700
6701 /*
6702 * we're not going to log anything, but we do need
6703 * to make sure the current transaction stays open
6704 * while we look for nocow cross refs
6705 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006706 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006707 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006708 goto must_cow;
6709
Josef Bacik7ee9e442013-06-21 16:37:03 -04006710 if (can_nocow_extent(trans, inode, start, &len, &orig_start,
6711 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006712 if (type == BTRFS_ORDERED_PREALLOC) {
6713 free_extent_map(em);
6714 em = create_pinned_em(inode, start, len,
6715 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006716 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006717 orig_block_len,
6718 ram_bytes, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006719 if (IS_ERR(em)) {
6720 btrfs_end_transaction(trans, root);
6721 goto unlock_err;
6722 }
6723 }
6724
Chris Mason46bfbb52010-05-26 11:04:10 -04006725 ret = btrfs_add_ordered_extent_dio(inode, start,
6726 block_start, len, len, type);
6727 btrfs_end_transaction(trans, root);
6728 if (ret) {
6729 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006730 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006731 }
6732 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006733 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006734 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006735 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006736must_cow:
6737 /*
6738 * this will cow the extent, reset the len in case we changed
6739 * it above
6740 */
6741 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006742 free_extent_map(em);
6743 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006744 if (IS_ERR(em)) {
6745 ret = PTR_ERR(em);
6746 goto unlock_err;
6747 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006748 len = min(len, em->len - (start - em->start));
6749unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006750 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6751 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006752 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006753 bh_result->b_bdev = em->bdev;
6754 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006755 if (create) {
6756 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6757 set_buffer_new(bh_result);
6758
6759 /*
6760 * Need to update the i_size under the extent lock so buffered
6761 * readers will get the updated i_size when we unlock.
6762 */
6763 if (start + len > i_size_read(inode))
6764 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006765
Miao Xie172a5042013-02-21 02:48:22 -07006766 spin_lock(&BTRFS_I(inode)->lock);
6767 BTRFS_I(inode)->outstanding_extents++;
6768 spin_unlock(&BTRFS_I(inode)->lock);
6769
Miao Xie09348562013-02-07 10:12:07 +00006770 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6771 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6772 &cached_state, GFP_NOFS);
6773 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006774 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006775
Josef Bacikeb838e72012-07-31 16:28:48 -04006776 /*
6777 * In the case of write we need to clear and unlock the entire range,
6778 * in the case of read we need to unlock only the end area that we
6779 * aren't using if there is any left over space.
6780 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006781 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006782 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6783 lockend, unlock_bits, 1, 0,
6784 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006785 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006786 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006787 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006788
Josef Bacik4b46fce2010-05-23 11:00:55 -04006789 free_extent_map(em);
6790
6791 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006792
6793unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006794 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6795 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6796 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006797}
6798
6799struct btrfs_dio_private {
6800 struct inode *inode;
6801 u64 logical_offset;
6802 u64 disk_bytenr;
6803 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006804 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006805
6806 /* number of bios pending for this dio */
6807 atomic_t pending_bios;
6808
6809 /* IO errors */
6810 int errors;
6811
Chris Mason9be33952013-05-17 18:30:14 -04006812 /* orig_bio is our btrfs_io_bio */
Miao Xiee65e1532010-11-22 03:04:43 +00006813 struct bio *orig_bio;
Chris Mason9be33952013-05-17 18:30:14 -04006814
6815 /* dio_bio came from fs/direct-io.c */
6816 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006817};
6818
6819static void btrfs_endio_direct_read(struct bio *bio, int err)
6820{
Miao Xiee65e1532010-11-22 03:04:43 +00006821 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006822 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6823 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006824 struct inode *inode = dip->inode;
6825 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006826 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006827 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006828
6829 start = dip->logical_offset;
6830 do {
6831 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6832 struct page *page = bvec->bv_page;
6833 char *kaddr;
6834 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006835 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006836 unsigned long flags;
6837
Josef Bacikc3298612012-08-03 16:49:19 -04006838 if (get_state_private(&BTRFS_I(inode)->io_tree,
6839 start, &private))
6840 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006841 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006842 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006843 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006844 csum, bvec->bv_len);
6845 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006846 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006847 local_irq_restore(flags);
6848
6849 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006850 if (csum != private) {
6851failed:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006852 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u private %u",
6853 (unsigned long long)btrfs_ino(inode),
6854 (unsigned long long)start,
6855 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006856 err = -EIO;
6857 }
6858 }
6859
6860 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006861 bvec++;
6862 } while (bvec <= bvec_end);
6863
6864 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006865 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04006866 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006867
Josef Bacik4b46fce2010-05-23 11:00:55 -04006868 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006869
6870 /* If we had a csum failure make sure to clear the uptodate flag */
6871 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006872 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6873 dio_end_io(dio_bio, err);
6874 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006875}
6876
6877static void btrfs_endio_direct_write(struct bio *bio, int err)
6878{
6879 struct btrfs_dio_private *dip = bio->bi_private;
6880 struct inode *inode = dip->inode;
6881 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006882 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006883 u64 ordered_offset = dip->logical_offset;
6884 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04006885 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006886 int ret;
6887
6888 if (err)
6889 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006890again:
6891 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6892 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006893 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006894 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006895 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006896
Josef Bacik5fd02042012-05-02 14:00:54 -04006897 ordered->work.func = finish_ordered_fn;
6898 ordered->work.flags = 0;
6899 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6900 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006901out_test:
6902 /*
6903 * our bio might span multiple ordered extents. If we haven't
6904 * completed the accounting for the whole dio, go back and try again
6905 */
6906 if (ordered_offset < dip->logical_offset + dip->bytes) {
6907 ordered_bytes = dip->logical_offset + dip->bytes -
6908 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006909 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006910 goto again;
6911 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006912out_done:
Chris Mason9be33952013-05-17 18:30:14 -04006913 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006914
Josef Bacik4b46fce2010-05-23 11:00:55 -04006915 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006916
6917 /* If we had an error make sure to clear the uptodate flag */
6918 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006919 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6920 dio_end_io(dio_bio, err);
6921 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006922}
6923
Chris Masoneaf25d92010-05-25 09:48:28 -04006924static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6925 struct bio *bio, int mirror_num,
6926 unsigned long bio_flags, u64 offset)
6927{
6928 int ret;
6929 struct btrfs_root *root = BTRFS_I(inode)->root;
6930 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006931 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006932 return 0;
6933}
6934
Miao Xiee65e1532010-11-22 03:04:43 +00006935static void btrfs_end_dio_bio(struct bio *bio, int err)
6936{
6937 struct btrfs_dio_private *dip = bio->bi_private;
6938
6939 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006940 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006941 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006942 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006943 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006944 dip->errors = 1;
6945
6946 /*
6947 * before atomic variable goto zero, we must make sure
6948 * dip->errors is perceived to be set.
6949 */
6950 smp_mb__before_atomic_dec();
6951 }
6952
6953 /* if there are more bios still pending for this dio, just exit */
6954 if (!atomic_dec_and_test(&dip->pending_bios))
6955 goto out;
6956
Chris Mason9be33952013-05-17 18:30:14 -04006957 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00006958 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04006959 } else {
6960 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00006961 bio_endio(dip->orig_bio, 0);
6962 }
6963out:
6964 bio_put(bio);
6965}
6966
6967static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6968 u64 first_sector, gfp_t gfp_flags)
6969{
6970 int nr_vecs = bio_get_nr_vecs(bdev);
6971 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6972}
6973
6974static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6975 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006976 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006977{
6978 int write = rw & REQ_WRITE;
6979 struct btrfs_root *root = BTRFS_I(inode)->root;
6980 int ret;
6981
Josef Bacikb812ce22012-11-16 13:56:32 -05006982 if (async_submit)
6983 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6984
Miao Xiee65e1532010-11-22 03:04:43 +00006985 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006986
6987 if (!write) {
6988 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6989 if (ret)
6990 goto err;
6991 }
Miao Xiee65e1532010-11-22 03:04:43 +00006992
Josef Bacik1ae39932011-04-06 14:41:34 -04006993 if (skip_sum)
6994 goto map;
6995
6996 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006997 ret = btrfs_wq_submit_bio(root->fs_info,
6998 inode, rw, bio, 0, 0,
6999 file_offset,
7000 __btrfs_submit_bio_start_direct_io,
7001 __btrfs_submit_bio_done);
7002 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007003 } else if (write) {
7004 /*
7005 * If we aren't doing async submit, calculate the csum of the
7006 * bio now.
7007 */
7008 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7009 if (ret)
7010 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007011 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04007012 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007013 if (ret)
7014 goto err;
7015 }
Miao Xiee65e1532010-11-22 03:04:43 +00007016
Josef Bacik1ae39932011-04-06 14:41:34 -04007017map:
7018 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007019err:
7020 bio_put(bio);
7021 return ret;
7022}
7023
7024static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7025 int skip_sum)
7026{
7027 struct inode *inode = dip->inode;
7028 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007029 struct bio *bio;
7030 struct bio *orig_bio = dip->orig_bio;
7031 struct bio_vec *bvec = orig_bio->bi_io_vec;
7032 u64 start_sector = orig_bio->bi_sector;
7033 u64 file_offset = dip->logical_offset;
7034 u64 submit_len = 0;
7035 u64 map_length;
7036 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007037 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007038 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007039
Miao Xiee65e1532010-11-22 03:04:43 +00007040 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007041 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007042 &map_length, NULL, 0);
7043 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007044 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007045 return -EIO;
7046 }
Josef Bacik02f57c72011-04-06 14:25:44 -04007047 if (map_length >= orig_bio->bi_size) {
7048 bio = orig_bio;
7049 goto submit;
7050 }
7051
David Woodhouse53b381b2013-01-29 18:40:14 -05007052 /* async crcs make it difficult to collect full stripe writes. */
7053 if (btrfs_get_alloc_profile(root, 1) &
7054 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7055 async_submit = 0;
7056 else
7057 async_submit = 1;
7058
Josef Bacik02f57c72011-04-06 14:25:44 -04007059 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7060 if (!bio)
7061 return -ENOMEM;
7062 bio->bi_private = dip;
7063 bio->bi_end_io = btrfs_end_dio_bio;
7064 atomic_inc(&dip->pending_bios);
7065
Miao Xiee65e1532010-11-22 03:04:43 +00007066 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7067 if (unlikely(map_length < submit_len + bvec->bv_len ||
7068 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7069 bvec->bv_offset) < bvec->bv_len)) {
7070 /*
7071 * inc the count before we submit the bio so
7072 * we know the end IO handler won't happen before
7073 * we inc the count. Otherwise, the dip might get freed
7074 * before we're done setting it up
7075 */
7076 atomic_inc(&dip->pending_bios);
7077 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7078 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007079 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007080 if (ret) {
7081 bio_put(bio);
7082 atomic_dec(&dip->pending_bios);
7083 goto out_err;
7084 }
7085
Miao Xiee65e1532010-11-22 03:04:43 +00007086 start_sector += submit_len >> 9;
7087 file_offset += submit_len;
7088
7089 submit_len = 0;
7090 nr_pages = 0;
7091
7092 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7093 start_sector, GFP_NOFS);
7094 if (!bio)
7095 goto out_err;
7096 bio->bi_private = dip;
7097 bio->bi_end_io = btrfs_end_dio_bio;
7098
7099 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007100 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007101 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007102 &map_length, NULL, 0);
7103 if (ret) {
7104 bio_put(bio);
7105 goto out_err;
7106 }
7107 } else {
7108 submit_len += bvec->bv_len;
7109 nr_pages ++;
7110 bvec++;
7111 }
7112 }
7113
Josef Bacik02f57c72011-04-06 14:25:44 -04007114submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007115 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007116 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007117 if (!ret)
7118 return 0;
7119
7120 bio_put(bio);
7121out_err:
7122 dip->errors = 1;
7123 /*
7124 * before atomic variable goto zero, we must
7125 * make sure dip->errors is perceived to be set.
7126 */
7127 smp_mb__before_atomic_dec();
7128 if (atomic_dec_and_test(&dip->pending_bios))
7129 bio_io_error(dip->orig_bio);
7130
7131 /* bio_end_io() will handle error, so we needn't return it */
7132 return 0;
7133}
7134
Chris Mason9be33952013-05-17 18:30:14 -04007135static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7136 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007137{
7138 struct btrfs_root *root = BTRFS_I(inode)->root;
7139 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007140 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007141 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007142 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007143 int ret = 0;
7144
7145 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7146
Chris Mason9be33952013-05-17 18:30:14 -04007147 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
7148
7149 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007150 ret = -ENOMEM;
7151 goto free_ordered;
7152 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007153
Chris Mason9be33952013-05-17 18:30:14 -04007154 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7155 if (!dip) {
7156 ret = -ENOMEM;
7157 goto free_io_bio;
7158 }
7159
7160 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007161 dip->inode = inode;
7162 dip->logical_offset = file_offset;
Miao Xiee6da5d22013-06-19 18:19:17 +08007163 dip->bytes = dio_bio->bi_size;
Chris Mason9be33952013-05-17 18:30:14 -04007164 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7165 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007166 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007167 dip->orig_bio = io_bio;
7168 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007169 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007170
7171 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007172 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007173 else
Chris Mason9be33952013-05-17 18:30:14 -04007174 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007175
Miao Xiee65e1532010-11-22 03:04:43 +00007176 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7177 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007178 return;
Chris Mason9be33952013-05-17 18:30:14 -04007179
7180free_io_bio:
7181 bio_put(io_bio);
7182
Josef Bacik4b46fce2010-05-23 11:00:55 -04007183free_ordered:
7184 /*
7185 * If this is a write, we need to clean up the reserved space and kill
7186 * the ordered extent.
7187 */
7188 if (write) {
7189 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007190 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007191 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7192 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7193 btrfs_free_reserved_extent(root, ordered->start,
7194 ordered->disk_len);
7195 btrfs_put_ordered_extent(ordered);
7196 btrfs_put_ordered_extent(ordered);
7197 }
Chris Mason9be33952013-05-17 18:30:14 -04007198 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007199}
7200
Chris Mason5a5f79b2010-05-26 21:33:37 -04007201static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7202 const struct iovec *iov, loff_t offset,
7203 unsigned long nr_segs)
7204{
7205 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007206 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007207 size_t size;
7208 unsigned long addr;
7209 unsigned blocksize_mask = root->sectorsize - 1;
7210 ssize_t retval = -EINVAL;
7211 loff_t end = offset;
7212
7213 if (offset & blocksize_mask)
7214 goto out;
7215
7216 /* Check the memory alignment. Blocks cannot straddle pages */
7217 for (seg = 0; seg < nr_segs; seg++) {
7218 addr = (unsigned long)iov[seg].iov_base;
7219 size = iov[seg].iov_len;
7220 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007221 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007222 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007223
7224 /* If this is a write we don't need to check anymore */
7225 if (rw & WRITE)
7226 continue;
7227
7228 /*
7229 * Check to make sure we don't have duplicate iov_base's in this
7230 * iovec, if so return EINVAL, otherwise we'll get csum errors
7231 * when reading back.
7232 */
7233 for (i = seg + 1; i < nr_segs; i++) {
7234 if (iov[seg].iov_base == iov[i].iov_base)
7235 goto out;
7236 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007237 }
7238 retval = 0;
7239out:
7240 return retval;
7241}
Josef Bacikeb838e72012-07-31 16:28:48 -04007242
Chris Mason16432982008-04-10 10:23:21 -04007243static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7244 const struct iovec *iov, loff_t offset,
7245 unsigned long nr_segs)
7246{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007247 struct file *file = iocb->ki_filp;
7248 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007249 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007250 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007251 bool wakeup = true;
7252 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007253 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007254
Chris Mason5a5f79b2010-05-26 21:33:37 -04007255 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007256 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007257 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007258
Miao Xie38851cc2013-02-08 07:04:11 +00007259 atomic_inc(&inode->i_dio_count);
7260 smp_mb__after_atomic_inc();
7261
Josef Bacik0e267c42013-07-02 10:38:02 -04007262 /*
7263 * The generic stuff only does filemap_write_and_wait_range, which isn't
7264 * enough if we've written compressed pages to this area, so we need to
7265 * call btrfs_wait_ordered_range to make absolutely sure that any
7266 * outstanding dirty pages are on disk.
7267 */
7268 count = iov_length(iov, nr_segs);
7269 btrfs_wait_ordered_range(inode, offset, count);
7270
Miao Xie09348562013-02-07 10:12:07 +00007271 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007272 /*
7273 * If the write DIO is beyond the EOF, we need update
7274 * the isize, but it is protected by i_mutex. So we can
7275 * not unlock the i_mutex at this case.
7276 */
7277 if (offset + count <= inode->i_size) {
7278 mutex_unlock(&inode->i_mutex);
7279 relock = true;
7280 }
Miao Xie09348562013-02-07 10:12:07 +00007281 ret = btrfs_delalloc_reserve_space(inode, count);
7282 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007283 goto out;
7284 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7285 &BTRFS_I(inode)->runtime_flags))) {
7286 inode_dio_done(inode);
7287 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7288 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007289 }
7290
7291 ret = __blockdev_direct_IO(rw, iocb, inode,
7292 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7293 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007294 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007295 if (rw & WRITE) {
7296 if (ret < 0 && ret != -EIOCBQUEUED)
7297 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007298 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007299 btrfs_delalloc_release_space(inode,
7300 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007301 else
7302 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007303 }
Miao Xie38851cc2013-02-08 07:04:11 +00007304out:
Miao Xie2e60a512013-02-08 07:01:08 +00007305 if (wakeup)
7306 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007307 if (relock)
7308 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007309
7310 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007311}
7312
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007313#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7314
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007315static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7316 __u64 start, __u64 len)
7317{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007318 int ret;
7319
7320 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7321 if (ret)
7322 return ret;
7323
Chris Masonec29ed52011-02-23 16:23:20 -05007324 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007325}
7326
Chris Mason9ebefb182007-06-15 13:50:00 -04007327int btrfs_readpage(struct file *file, struct page *page)
7328{
Chris Masond1310b22008-01-24 16:13:08 -05007329 struct extent_io_tree *tree;
7330 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007331 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007332}
Chris Mason1832a6d2007-12-21 16:27:21 -05007333
Chris Mason39279cc2007-06-12 06:35:45 -04007334static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7335{
Chris Masond1310b22008-01-24 16:13:08 -05007336 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007337
7338
7339 if (current->flags & PF_MEMALLOC) {
7340 redirty_page_for_writepage(wbc, page);
7341 unlock_page(page);
7342 return 0;
7343 }
Chris Masond1310b22008-01-24 16:13:08 -05007344 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007345 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7346}
Chris Mason39279cc2007-06-12 06:35:45 -04007347
Eric Sandeen48a3b632013-04-25 20:41:01 +00007348static int btrfs_writepages(struct address_space *mapping,
7349 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007350{
Chris Masond1310b22008-01-24 16:13:08 -05007351 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007352
Chris Masond1310b22008-01-24 16:13:08 -05007353 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007354 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7355}
7356
Chris Mason3ab2fb52007-11-08 10:59:22 -05007357static int
7358btrfs_readpages(struct file *file, struct address_space *mapping,
7359 struct list_head *pages, unsigned nr_pages)
7360{
Chris Masond1310b22008-01-24 16:13:08 -05007361 struct extent_io_tree *tree;
7362 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007363 return extent_readpages(tree, mapping, pages, nr_pages,
7364 btrfs_get_extent);
7365}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007366static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007367{
Chris Masond1310b22008-01-24 16:13:08 -05007368 struct extent_io_tree *tree;
7369 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007370 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007371
Chris Masond1310b22008-01-24 16:13:08 -05007372 tree = &BTRFS_I(page->mapping->host)->io_tree;
7373 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007374 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007375 if (ret == 1) {
7376 ClearPagePrivate(page);
7377 set_page_private(page, 0);
7378 page_cache_release(page);
7379 }
7380 return ret;
7381}
Chris Mason39279cc2007-06-12 06:35:45 -04007382
Chris Masone6dcd2d2008-07-17 12:53:50 -04007383static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7384{
Chris Mason98509cf2008-09-11 15:51:43 -04007385 if (PageWriteback(page) || PageDirty(page))
7386 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007387 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007388}
7389
Lukas Czernerd47992f2013-05-21 23:17:23 -04007390static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7391 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007392{
Josef Bacik5fd02042012-05-02 14:00:54 -04007393 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007394 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007395 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007396 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007397 u64 page_start = page_offset(page);
7398 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007399
Chris Mason8b62b722009-09-02 16:53:46 -04007400 /*
7401 * we have the page locked, so new writeback can't start,
7402 * and the dirty bit won't be cleared while we are here.
7403 *
7404 * Wait for IO on this page so that we can safely clear
7405 * the PagePrivate2 bit and do ordered accounting
7406 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007407 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007408
Josef Bacik5fd02042012-05-02 14:00:54 -04007409 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007410 if (offset) {
7411 btrfs_releasepage(page, GFP_NOFS);
7412 return;
7413 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007414 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007415 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007416 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007417 /*
7418 * IO on this page will never be started, so we need
7419 * to account for any ordered extents now
7420 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007421 clear_extent_bit(tree, page_start, page_end,
7422 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007423 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7424 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007425 /*
7426 * whoever cleared the private bit is responsible
7427 * for the finish_ordered_io
7428 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007429 if (TestClearPagePrivate2(page) &&
7430 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7431 PAGE_CACHE_SIZE, 1)) {
7432 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007433 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007434 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007435 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007436 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007437 }
7438 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007439 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007440 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7441 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007442 __btrfs_releasepage(page, GFP_NOFS);
7443
Chris Mason4a096752008-07-21 10:29:44 -04007444 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007445 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007446 ClearPagePrivate(page);
7447 set_page_private(page, 0);
7448 page_cache_release(page);
7449 }
Chris Mason39279cc2007-06-12 06:35:45 -04007450}
7451
Chris Mason9ebefb182007-06-15 13:50:00 -04007452/*
7453 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7454 * called from a page fault handler when a page is first dirtied. Hence we must
7455 * be careful to check for EOF conditions here. We set the page up correctly
7456 * for a written page which means we get ENOSPC checking when writing into
7457 * holes and correct delalloc and unwritten extent mapping on filesystems that
7458 * support these features.
7459 *
7460 * We are not allowed to take the i_mutex here so we have to play games to
7461 * protect against truncate races as the page could now be beyond EOF. Because
7462 * vmtruncate() writes the inode size before removing pages, once we have the
7463 * page lock we can determine safely if the page is beyond EOF. If it is not
7464 * beyond EOF, then the page is guaranteed safe against truncation until we
7465 * unlock the page.
7466 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007467int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007468{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007469 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007470 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007471 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007472 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7473 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007474 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007475 char *kaddr;
7476 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007477 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007478 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007479 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007480 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007481 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007482
Jan Karab2b5ef52012-06-12 16:20:45 +02007483 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007484 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007485 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007486 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007487 reserved = 1;
7488 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007489 if (ret) {
7490 if (ret == -ENOMEM)
7491 ret = VM_FAULT_OOM;
7492 else /* -ENOSPC, -EIO, etc */
7493 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007494 if (reserved)
7495 goto out;
7496 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007497 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007498
Nick Piggin56a76f82009-03-31 15:23:23 -07007499 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007500again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007501 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007502 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007503 page_start = page_offset(page);
7504 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007505
Chris Mason9ebefb182007-06-15 13:50:00 -04007506 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007507 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007508 /* page got truncated out from underneath us */
7509 goto out_unlock;
7510 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007511 wait_on_page_writeback(page);
7512
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007513 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007514 set_page_extent_mapped(page);
7515
Chris Masoneb84ae02008-07-17 13:53:27 -04007516 /*
7517 * we can't set the delalloc bits if there are pending ordered
7518 * extents. Drop our locks and wait for them to finish
7519 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007520 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7521 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007522 unlock_extent_cached(io_tree, page_start, page_end,
7523 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007524 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007525 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007526 btrfs_put_ordered_extent(ordered);
7527 goto again;
7528 }
7529
Josef Bacikfbf19082009-10-01 17:10:23 -04007530 /*
7531 * XXX - page_mkwrite gets called every time the page is dirtied, even
7532 * if it was already dirty, so for space accounting reasons we need to
7533 * clear any delalloc bits for the range we are fixing to save. There
7534 * is probably a better way to do this, but for now keep consistent with
7535 * prepare_pages in the normal write path.
7536 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007537 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007538 EXTENT_DIRTY | EXTENT_DELALLOC |
7539 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007540 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007541
Josef Bacik2ac55d42010-02-03 19:33:23 +00007542 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7543 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007544 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007545 unlock_extent_cached(io_tree, page_start, page_end,
7546 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007547 ret = VM_FAULT_SIGBUS;
7548 goto out_unlock;
7549 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007550 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007551
7552 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007553 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007554 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007555 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007556 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007557
Chris Masone6dcd2d2008-07-17 12:53:50 -04007558 if (zero_start != PAGE_CACHE_SIZE) {
7559 kaddr = kmap(page);
7560 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7561 flush_dcache_page(page);
7562 kunmap(page);
7563 }
Chris Mason247e7432008-07-17 12:53:51 -04007564 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007565 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007566 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007567
Chris Mason257c62e2009-10-13 13:21:08 -04007568 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7569 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007570 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007571
Josef Bacik2ac55d42010-02-03 19:33:23 +00007572 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007573
7574out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007575 if (!ret) {
7576 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007577 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007578 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007579 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007580out:
Josef Bacikec39e182012-01-12 19:10:12 -05007581 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007582out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007583 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007584 return ret;
7585}
7586
Josef Bacika41ad392011-01-31 15:30:16 -05007587static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007588{
7589 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007590 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007591 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007592 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007593 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007594 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007595 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007596
Chris Mason4a096752008-07-21 10:29:44 -04007597 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007598 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007599
Josef Bacikfcb80c22011-05-03 10:40:22 -04007600 /*
7601 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7602 * 3 things going on here
7603 *
7604 * 1) We need to reserve space for our orphan item and the space to
7605 * delete our orphan item. Lord knows we don't want to have a dangling
7606 * orphan item because we didn't reserve space to remove it.
7607 *
7608 * 2) We need to reserve space to update our inode.
7609 *
7610 * 3) We need to have something to cache all the space that is going to
7611 * be free'd up by the truncate operation, but also have some slack
7612 * space reserved in case it uses space during the truncate (thank you
7613 * very much snapshotting).
7614 *
7615 * And we need these to all be seperate. The fact is we can use alot of
7616 * space doing the truncate, and we have no earthly idea how much space
7617 * we will use, so we need the truncate reservation to be seperate so it
7618 * doesn't end up using space reserved for updating the inode or
7619 * removing the orphan item. We also need to be able to stop the
7620 * transaction and start a new one, which means we need to be able to
7621 * update the inode several times, and we have no idea of knowing how
7622 * many times that will be, so we can't just reserve 1 item for the
7623 * entirety of the opration, so that has to be done seperately as well.
7624 * Then there is the orphan item, which does indeed need to be held on
7625 * to for the whole operation, and we need nobody to touch this reserved
7626 * space except the orphan code.
7627 *
7628 * So that leaves us with
7629 *
7630 * 1) root->orphan_block_rsv - for the orphan deletion.
7631 * 2) rsv - for the truncate reservation, which we will steal from the
7632 * transaction reservation.
7633 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7634 * updating the inode.
7635 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007636 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007637 if (!rsv)
7638 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007639 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007640 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007641
Josef Bacik907cbce2011-08-08 13:46:15 -04007642 /*
Josef Bacik07127182011-08-19 10:29:59 -04007643 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007644 * 1 for updating the inode.
7645 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007646 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007647 if (IS_ERR(trans)) {
7648 err = PTR_ERR(trans);
7649 goto out;
7650 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007651
Josef Bacik907cbce2011-08-08 13:46:15 -04007652 /* Migrate the slack space for the truncate to our reserve */
7653 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7654 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007655 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007656
Chris Mason5a3f23d2009-03-31 13:27:11 -04007657 /*
7658 * setattr is responsible for setting the ordered_data_close flag,
7659 * but that is only tested during the last file release. That
7660 * could happen well after the next commit, leaving a great big
7661 * window where new writes may get lost if someone chooses to write
7662 * to this file after truncating to zero
7663 *
7664 * The inode doesn't have any dirty data here, and so if we commit
7665 * this is a noop. If someone immediately starts writing to the inode
7666 * it is very likely we'll catch some of their writes in this
7667 * transaction, and the commit will find this file on the ordered
7668 * data list with good things to send down.
7669 *
7670 * This is a best effort solution, there is still a window where
7671 * using truncate to replace the contents of the file will
7672 * end up with a zero length file after a crash.
7673 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007674 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7675 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007676 btrfs_add_ordered_operation(trans, root, inode);
7677
Josef Bacik5dc562c2012-08-17 13:14:17 -04007678 /*
7679 * So if we truncate and then write and fsync we normally would just
7680 * write the extents that changed, which is a problem if we need to
7681 * first truncate that entire inode. So set this flag so we write out
7682 * all of the extents in the inode to the sync log so we're completely
7683 * safe.
7684 */
7685 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007686 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007687
Yan, Zheng80825102009-11-12 09:35:36 +00007688 while (1) {
7689 ret = btrfs_truncate_inode_items(trans, root, inode,
7690 inode->i_size,
7691 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007692 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007693 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007694 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007695 }
Chris Mason39279cc2007-06-12 06:35:45 -04007696
Josef Bacikfcb80c22011-05-03 10:40:22 -04007697 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007698 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007699 if (ret) {
7700 err = ret;
7701 break;
7702 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007703
Yan, Zheng80825102009-11-12 09:35:36 +00007704 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007705 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007706
7707 trans = btrfs_start_transaction(root, 2);
7708 if (IS_ERR(trans)) {
7709 ret = err = PTR_ERR(trans);
7710 trans = NULL;
7711 break;
7712 }
7713
7714 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7715 rsv, min_size);
7716 BUG_ON(ret); /* shouldn't happen */
7717 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007718 }
7719
7720 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007721 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007722 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007723 if (ret)
7724 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007725 }
7726
Chris Mason917c16b2011-11-08 14:49:59 -05007727 if (trans) {
7728 trans->block_rsv = &root->fs_info->trans_block_rsv;
7729 ret = btrfs_update_inode(trans, root, inode);
7730 if (ret && !err)
7731 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007732
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007733 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007734 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007735 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007736
7737out:
7738 btrfs_free_block_rsv(root, rsv);
7739
Josef Bacik3893e332011-01-31 16:03:11 -05007740 if (ret && !err)
7741 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007742
Josef Bacik3893e332011-01-31 16:03:11 -05007743 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007744}
7745
Sven Wegener3b963622008-06-09 21:57:42 -04007746/*
Chris Masond352ac62008-09-29 15:18:18 -04007747 * create a new subvolume directory/inode (helper for the ioctl).
7748 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007749int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007750 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007751{
Chris Mason39279cc2007-06-12 06:35:45 -04007752 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007753 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007754 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007755
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007756 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7757 new_dirid, new_dirid,
7758 S_IFDIR | (~current_umask() & S_IRWXUGO),
7759 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007760 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007761 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007762 inode->i_op = &btrfs_dir_inode_operations;
7763 inode->i_fop = &btrfs_dir_file_operations;
7764
Miklos Szeredibfe86842011-10-28 14:13:29 +02007765 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007766 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007767
Yan, Zheng76dda932009-09-21 16:00:26 -04007768 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007769
Yan, Zheng76dda932009-09-21 16:00:26 -04007770 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007771 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007772}
7773
Chris Mason39279cc2007-06-12 06:35:45 -04007774struct inode *btrfs_alloc_inode(struct super_block *sb)
7775{
7776 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007777 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007778
7779 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7780 if (!ei)
7781 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007782
7783 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007784 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007785 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007786 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007787 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007788 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007789 ei->disk_i_size = 0;
7790 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007791 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007792 ei->index_cnt = (u64)-1;
7793 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007794 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007795
Josef Bacik9e0baf62011-07-15 15:16:44 +00007796 spin_lock_init(&ei->lock);
7797 ei->outstanding_extents = 0;
7798 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007799
Josef Bacik72ac3c02012-05-23 14:13:11 -04007800 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007801 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007802
Miao Xie16cdcec2011-04-22 18:12:22 +08007803 ei->delayed_node = NULL;
7804
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007805 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007806 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007807 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7808 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007809 ei->io_tree.track_uptodate = 1;
7810 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007811 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007812 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007813 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007814 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007815 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007816 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007817 RB_CLEAR_NODE(&ei->rb_node);
7818
7819 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007820}
7821
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007822static void btrfs_i_callback(struct rcu_head *head)
7823{
7824 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007825 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7826}
7827
Chris Mason39279cc2007-06-12 06:35:45 -04007828void btrfs_destroy_inode(struct inode *inode)
7829{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007830 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007831 struct btrfs_root *root = BTRFS_I(inode)->root;
7832
Al Virob3d9b7a2012-06-09 13:51:19 -04007833 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007834 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007835 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7836 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007837 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7838 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007839
Chris Mason5a3f23d2009-03-31 13:27:11 -04007840 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007841 * This can happen where we create an inode, but somebody else also
7842 * created the same inode and we need to destroy the one we already
7843 * created.
7844 */
7845 if (!root)
7846 goto free;
7847
7848 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007849 * Make sure we're properly removed from the ordered operation
7850 * lists.
7851 */
7852 smp_mb();
7853 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
Miao Xie199c2a92013-05-15 07:48:23 +00007854 spin_lock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007855 list_del_init(&BTRFS_I(inode)->ordered_operations);
Miao Xie199c2a92013-05-15 07:48:23 +00007856 spin_unlock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007857 }
7858
Josef Bacik8a35d952012-05-23 14:26:42 -04007859 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7860 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007861 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
7862 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007863 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007864 }
Josef Bacik7b128762008-07-24 12:17:14 -04007865
Chris Masond3977122009-01-05 21:25:51 -05007866 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007867 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7868 if (!ordered)
7869 break;
7870 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007871 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
7872 (unsigned long long)ordered->file_offset,
7873 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007874 btrfs_remove_ordered_extent(inode, ordered);
7875 btrfs_put_ordered_extent(ordered);
7876 btrfs_put_ordered_extent(ordered);
7877 }
7878 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007879 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007880 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007881free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007882 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007883}
7884
Al Viro45321ac2010-06-07 13:43:19 -04007885int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007886{
7887 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007888
Naohiro Aota6379ef92013-06-06 09:56:34 +00007889 if (root == NULL)
7890 return 1;
7891
Liu Bofa6ac872013-02-20 14:10:23 +00007892 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007893 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007894 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007895 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007896 else
Al Viro45321ac2010-06-07 13:43:19 -04007897 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007898}
7899
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007900static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007901{
7902 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7903
7904 inode_init_once(&ei->vfs_inode);
7905}
7906
7907void btrfs_destroy_cachep(void)
7908{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007909 /*
7910 * Make sure all delayed rcu free inodes are flushed before we
7911 * destroy cache.
7912 */
7913 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007914 if (btrfs_inode_cachep)
7915 kmem_cache_destroy(btrfs_inode_cachep);
7916 if (btrfs_trans_handle_cachep)
7917 kmem_cache_destroy(btrfs_trans_handle_cachep);
7918 if (btrfs_transaction_cachep)
7919 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007920 if (btrfs_path_cachep)
7921 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007922 if (btrfs_free_space_cachep)
7923 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007924 if (btrfs_delalloc_work_cachep)
7925 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007926}
7927
7928int btrfs_init_cachep(void)
7929{
David Sterba837e1972012-09-07 03:00:48 -06007930 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007931 sizeof(struct btrfs_inode), 0,
7932 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007933 if (!btrfs_inode_cachep)
7934 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007935
David Sterba837e1972012-09-07 03:00:48 -06007936 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007937 sizeof(struct btrfs_trans_handle), 0,
7938 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007939 if (!btrfs_trans_handle_cachep)
7940 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007941
David Sterba837e1972012-09-07 03:00:48 -06007942 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007943 sizeof(struct btrfs_transaction), 0,
7944 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007945 if (!btrfs_transaction_cachep)
7946 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007947
David Sterba837e1972012-09-07 03:00:48 -06007948 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007949 sizeof(struct btrfs_path), 0,
7950 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007951 if (!btrfs_path_cachep)
7952 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007953
David Sterba837e1972012-09-07 03:00:48 -06007954 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007955 sizeof(struct btrfs_free_space), 0,
7956 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7957 if (!btrfs_free_space_cachep)
7958 goto fail;
7959
Miao Xie8ccf6f192012-10-25 09:28:04 +00007960 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7961 sizeof(struct btrfs_delalloc_work), 0,
7962 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7963 NULL);
7964 if (!btrfs_delalloc_work_cachep)
7965 goto fail;
7966
Chris Mason39279cc2007-06-12 06:35:45 -04007967 return 0;
7968fail:
7969 btrfs_destroy_cachep();
7970 return -ENOMEM;
7971}
7972
7973static int btrfs_getattr(struct vfsmount *mnt,
7974 struct dentry *dentry, struct kstat *stat)
7975{
Miao Xiedf0af1a2013-01-29 10:11:59 +00007976 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04007977 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007978 u32 blocksize = inode->i_sb->s_blocksize;
7979
Chris Mason39279cc2007-06-12 06:35:45 -04007980 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007981 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007982 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00007983
7984 spin_lock(&BTRFS_I(inode)->lock);
7985 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
7986 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05007987 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00007988 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007989 return 0;
7990}
7991
Chris Masond3977122009-01-05 21:25:51 -05007992static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7993 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007994{
7995 struct btrfs_trans_handle *trans;
7996 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007997 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007998 struct inode *new_inode = new_dentry->d_inode;
7999 struct inode *old_inode = old_dentry->d_inode;
8000 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008001 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008002 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008003 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008004 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008005
Li Zefan33345d012011-04-20 10:31:50 +08008006 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008007 return -EPERM;
8008
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008009 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008010 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008011 return -EXDEV;
8012
Li Zefan33345d012011-04-20 10:31:50 +08008013 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8014 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008015 return -ENOTEMPTY;
8016
Chris Mason39279cc2007-06-12 06:35:45 -04008017 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008018 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008019 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008020
8021
8022 /* check for collisions, even if the name isn't there */
8023 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8024 new_dentry->d_name.name,
8025 new_dentry->d_name.len);
8026
8027 if (ret) {
8028 if (ret == -EEXIST) {
8029 /* we shouldn't get
8030 * eexist without a new_inode */
8031 if (!new_inode) {
8032 WARN_ON(1);
8033 return ret;
8034 }
8035 } else {
8036 /* maybe -EOVERFLOW */
8037 return ret;
8038 }
8039 }
8040 ret = 0;
8041
Chris Mason5a3f23d2009-03-31 13:27:11 -04008042 /*
8043 * we're using rename to replace one file with another.
8044 * and the replacement file is large. Start IO on it now so
8045 * we don't add too much work to the end of the transaction
8046 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008047 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008048 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8049 filemap_flush(old_inode->i_mapping);
8050
Yan, Zheng76dda932009-09-21 16:00:26 -04008051 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008052 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008053 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008054 /*
8055 * We want to reserve the absolute worst case amount of items. So if
8056 * both inodes are subvols and we need to unlink them then that would
8057 * require 4 item modifications, but if they are both normal inodes it
8058 * would require 5 item modifications, so we'll assume their normal
8059 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8060 * should cover the worst case number of items we'll modify.
8061 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008062 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008063 if (IS_ERR(trans)) {
8064 ret = PTR_ERR(trans);
8065 goto out_notrans;
8066 }
Chris Mason5f39d392007-10-15 16:14:19 -04008067
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008068 if (dest != root)
8069 btrfs_record_root_in_trans(trans, dest);
8070
Yan, Zhenga5719522009-09-24 09:17:31 -04008071 ret = btrfs_set_inode_index(new_dir, &index);
8072 if (ret)
8073 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008074
Li Zefan33345d012011-04-20 10:31:50 +08008075 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008076 /* force full log commit if subvolume involved. */
8077 root->fs_info->last_trans_log_full_commit = trans->transid;
8078 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008079 ret = btrfs_insert_inode_ref(trans, dest,
8080 new_dentry->d_name.name,
8081 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008082 old_ino,
8083 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008084 if (ret)
8085 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008086 /*
8087 * this is an ugly little race, but the rename is required
8088 * to make sure that if we crash, the inode is either at the
8089 * old name or the new one. pinning the log transaction lets
8090 * us make sure we don't allow a log commit to come in after
8091 * we unlink the name but before we add the new name back in.
8092 */
8093 btrfs_pin_log_trans(root);
8094 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008095 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008096 * make sure the inode gets flushed if it is replacing
8097 * something.
8098 */
Li Zefan33345d012011-04-20 10:31:50 +08008099 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008100 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008101
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008102 inode_inc_iversion(old_dir);
8103 inode_inc_iversion(new_dir);
8104 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008105 old_dir->i_ctime = old_dir->i_mtime = ctime;
8106 new_dir->i_ctime = new_dir->i_mtime = ctime;
8107 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008108
Chris Mason12fcfd22009-03-24 10:24:20 -04008109 if (old_dentry->d_parent != new_dentry->d_parent)
8110 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8111
Li Zefan33345d012011-04-20 10:31:50 +08008112 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008113 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8114 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8115 old_dentry->d_name.name,
8116 old_dentry->d_name.len);
8117 } else {
Al Viro92986792011-03-04 17:14:37 +00008118 ret = __btrfs_unlink_inode(trans, root, old_dir,
8119 old_dentry->d_inode,
8120 old_dentry->d_name.name,
8121 old_dentry->d_name.len);
8122 if (!ret)
8123 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008124 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008125 if (ret) {
8126 btrfs_abort_transaction(trans, root, ret);
8127 goto out_fail;
8128 }
Chris Mason39279cc2007-06-12 06:35:45 -04008129
8130 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008131 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008132 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008133 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008134 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8135 root_objectid = BTRFS_I(new_inode)->location.objectid;
8136 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8137 root_objectid,
8138 new_dentry->d_name.name,
8139 new_dentry->d_name.len);
8140 BUG_ON(new_inode->i_nlink == 0);
8141 } else {
8142 ret = btrfs_unlink_inode(trans, dest, new_dir,
8143 new_dentry->d_inode,
8144 new_dentry->d_name.name,
8145 new_dentry->d_name.len);
8146 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008147 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008148 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008149 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008150 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008151 if (ret) {
8152 btrfs_abort_transaction(trans, root, ret);
8153 goto out_fail;
8154 }
Chris Mason39279cc2007-06-12 06:35:45 -04008155 }
Josef Bacikaec74772008-07-24 12:12:38 -04008156
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008157 ret = btrfs_add_link(trans, new_dir, old_inode,
8158 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008159 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008160 if (ret) {
8161 btrfs_abort_transaction(trans, root, ret);
8162 goto out_fail;
8163 }
Chris Mason39279cc2007-06-12 06:35:45 -04008164
Li Zefan33345d012011-04-20 10:31:50 +08008165 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008166 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008167 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008168 btrfs_end_log_trans(root);
8169 }
Chris Mason39279cc2007-06-12 06:35:45 -04008170out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008171 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008172out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008173 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008174 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008175
Chris Mason39279cc2007-06-12 06:35:45 -04008176 return ret;
8177}
8178
Miao Xie8ccf6f192012-10-25 09:28:04 +00008179static void btrfs_run_delalloc_work(struct btrfs_work *work)
8180{
8181 struct btrfs_delalloc_work *delalloc_work;
8182
8183 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8184 work);
8185 if (delalloc_work->wait)
8186 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8187 else
8188 filemap_flush(delalloc_work->inode->i_mapping);
8189
8190 if (delalloc_work->delay_iput)
8191 btrfs_add_delayed_iput(delalloc_work->inode);
8192 else
8193 iput(delalloc_work->inode);
8194 complete(&delalloc_work->completion);
8195}
8196
8197struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8198 int wait, int delay_iput)
8199{
8200 struct btrfs_delalloc_work *work;
8201
8202 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8203 if (!work)
8204 return NULL;
8205
8206 init_completion(&work->completion);
8207 INIT_LIST_HEAD(&work->list);
8208 work->inode = inode;
8209 work->wait = wait;
8210 work->delay_iput = delay_iput;
8211 work->work.func = btrfs_run_delalloc_work;
8212
8213 return work;
8214}
8215
8216void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8217{
8218 wait_for_completion(&work->completion);
8219 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8220}
8221
Chris Masond352ac62008-09-29 15:18:18 -04008222/*
8223 * some fairly slow code that needs optimization. This walks the list
8224 * of all the inodes with pending delalloc and forces them to disk.
8225 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00008226static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008227{
Chris Masonea8c2812008-08-04 23:17:27 -04008228 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008229 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008230 struct btrfs_delalloc_work *work, *next;
8231 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008232 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008233 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008234
Miao Xie8ccf6f192012-10-25 09:28:04 +00008235 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008236 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008237
Miao Xieeb73c1b2013-05-15 07:48:22 +00008238 spin_lock(&root->delalloc_lock);
8239 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008240 while (!list_empty(&splice)) {
8241 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008242 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008243
Miao Xieeb73c1b2013-05-15 07:48:22 +00008244 list_move_tail(&binode->delalloc_inodes,
8245 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008246 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008247 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008248 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008249 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008250 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008251 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008252
8253 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8254 if (unlikely(!work)) {
8255 ret = -ENOMEM;
8256 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008257 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008258 list_add_tail(&work->list, &works);
8259 btrfs_queue_worker(&root->fs_info->flush_workers,
8260 &work->work);
8261
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008262 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008263 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008264 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008265 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee1d2008-09-29 11:19:10 -04008266
Miao Xie1eafa6c2013-01-22 10:49:00 +00008267 list_for_each_entry_safe(work, next, &works, list) {
8268 list_del_init(&work->list);
8269 btrfs_wait_and_free_delalloc_work(work);
8270 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008271 return 0;
8272out:
8273 list_for_each_entry_safe(work, next, &works, list) {
8274 list_del_init(&work->list);
8275 btrfs_wait_and_free_delalloc_work(work);
8276 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008277
Miao Xieeb73c1b2013-05-15 07:48:22 +00008278 if (!list_empty_careful(&splice)) {
8279 spin_lock(&root->delalloc_lock);
8280 list_splice_tail(&splice, &root->delalloc_inodes);
8281 spin_unlock(&root->delalloc_lock);
8282 }
8283 return ret;
8284}
8285
8286int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8287{
8288 int ret;
8289
8290 if (root->fs_info->sb->s_flags & MS_RDONLY)
8291 return -EROFS;
8292
8293 ret = __start_delalloc_inodes(root, delay_iput);
8294 /*
8295 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee1d2008-09-29 11:19:10 -04008296 * we have to make sure the IO is actually started and that
8297 * ordered extents get created before we return
8298 */
8299 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008300 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008301 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee1d2008-09-29 11:19:10 -04008302 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008303 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8304 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee1d2008-09-29 11:19:10 -04008305 }
8306 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008307 return ret;
8308}
8309
8310int btrfs_start_all_delalloc_inodes(struct btrfs_fs_info *fs_info,
8311 int delay_iput)
8312{
8313 struct btrfs_root *root;
8314 struct list_head splice;
8315 int ret;
8316
8317 if (fs_info->sb->s_flags & MS_RDONLY)
8318 return -EROFS;
8319
8320 INIT_LIST_HEAD(&splice);
8321
8322 spin_lock(&fs_info->delalloc_root_lock);
8323 list_splice_init(&fs_info->delalloc_roots, &splice);
8324 while (!list_empty(&splice)) {
8325 root = list_first_entry(&splice, struct btrfs_root,
8326 delalloc_root);
8327 root = btrfs_grab_fs_root(root);
8328 BUG_ON(!root);
8329 list_move_tail(&root->delalloc_root,
8330 &fs_info->delalloc_roots);
8331 spin_unlock(&fs_info->delalloc_root_lock);
8332
8333 ret = __start_delalloc_inodes(root, delay_iput);
8334 btrfs_put_fs_root(root);
8335 if (ret)
8336 goto out;
8337
8338 spin_lock(&fs_info->delalloc_root_lock);
8339 }
8340 spin_unlock(&fs_info->delalloc_root_lock);
8341
8342 atomic_inc(&fs_info->async_submit_draining);
8343 while (atomic_read(&fs_info->nr_async_submits) ||
8344 atomic_read(&fs_info->async_delalloc_pages)) {
8345 wait_event(fs_info->async_submit_wait,
8346 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8347 atomic_read(&fs_info->async_delalloc_pages) == 0));
8348 }
8349 atomic_dec(&fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008350 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008351out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008352 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008353 spin_lock(&fs_info->delalloc_root_lock);
8354 list_splice_tail(&splice, &fs_info->delalloc_roots);
8355 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008356 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008357 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008358}
8359
Chris Mason39279cc2007-06-12 06:35:45 -04008360static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8361 const char *symname)
8362{
8363 struct btrfs_trans_handle *trans;
8364 struct btrfs_root *root = BTRFS_I(dir)->root;
8365 struct btrfs_path *path;
8366 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008367 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008368 int err;
8369 int drop_inode = 0;
8370 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008371 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008372 int name_len;
8373 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008374 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008375 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008376 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008377
8378 name_len = strlen(symname) + 1;
8379 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8380 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008381
Josef Bacik9ed74f22009-09-11 16:12:44 -04008382 /*
8383 * 2 items for inode item and ref
8384 * 2 items for dir items
8385 * 1 item for xattr if selinux is on
8386 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008387 trans = btrfs_start_transaction(root, 5);
8388 if (IS_ERR(trans))
8389 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008390
Li Zefan581bb052011-04-20 10:06:11 +08008391 err = btrfs_find_free_ino(root, &objectid);
8392 if (err)
8393 goto out_unlock;
8394
Josef Bacikaec74772008-07-24 12:12:38 -04008395 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008396 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008397 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008398 if (IS_ERR(inode)) {
8399 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008400 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008401 }
Chris Mason39279cc2007-06-12 06:35:45 -04008402
Eric Paris2a7dba32011-02-01 11:05:39 -05008403 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008404 if (err) {
8405 drop_inode = 1;
8406 goto out_unlock;
8407 }
8408
Casey Schauflerad19db72011-12-15 10:09:07 -05008409 /*
8410 * If the active LSM wants to access the inode during
8411 * d_instantiate it needs these. Smack checks to see
8412 * if the filesystem supports xattrs by looking at the
8413 * ops vector.
8414 */
8415 inode->i_fop = &btrfs_file_operations;
8416 inode->i_op = &btrfs_file_inode_operations;
8417
Josef Bacika1b075d2010-11-19 20:36:11 +00008418 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008419 if (err)
8420 drop_inode = 1;
8421 else {
8422 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008423 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008424 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008425 }
Chris Mason39279cc2007-06-12 06:35:45 -04008426 if (drop_inode)
8427 goto out_unlock;
8428
8429 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008430 if (!path) {
8431 err = -ENOMEM;
8432 drop_inode = 1;
8433 goto out_unlock;
8434 }
Li Zefan33345d012011-04-20 10:31:50 +08008435 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008436 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008437 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8438 datasize = btrfs_file_extent_calc_inline_size(name_len);
8439 err = btrfs_insert_empty_item(trans, root, path, &key,
8440 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008441 if (err) {
8442 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008443 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008444 goto out_unlock;
8445 }
Chris Mason5f39d392007-10-15 16:14:19 -04008446 leaf = path->nodes[0];
8447 ei = btrfs_item_ptr(leaf, path->slots[0],
8448 struct btrfs_file_extent_item);
8449 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8450 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008451 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008452 btrfs_set_file_extent_encryption(leaf, ei, 0);
8453 btrfs_set_file_extent_compression(leaf, ei, 0);
8454 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8455 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8456
Chris Mason39279cc2007-06-12 06:35:45 -04008457 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008458 write_extent_buffer(leaf, symname, ptr, name_len);
8459 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008460 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008461
Chris Mason39279cc2007-06-12 06:35:45 -04008462 inode->i_op = &btrfs_symlink_inode_operations;
8463 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008464 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008465 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008466 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008467 err = btrfs_update_inode(trans, root, inode);
8468 if (err)
8469 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008470
8471out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008472 if (!err)
8473 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008474 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008475 if (drop_inode) {
8476 inode_dec_link_count(inode);
8477 iput(inode);
8478 }
Liu Bob53d3f52012-11-14 14:34:34 +00008479 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008480 return err;
8481}
Chris Mason16432982008-04-10 10:23:21 -04008482
Josef Bacik0af3d002010-06-21 14:48:16 -04008483static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8484 u64 start, u64 num_bytes, u64 min_size,
8485 loff_t actual_len, u64 *alloc_hint,
8486 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008487{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008488 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8489 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008490 struct btrfs_root *root = BTRFS_I(inode)->root;
8491 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008492 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008493 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008494 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008495 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008496 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008497
Josef Bacik0af3d002010-06-21 14:48:16 -04008498 if (trans)
8499 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008500 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008501 if (own_trans) {
8502 trans = btrfs_start_transaction(root, 3);
8503 if (IS_ERR(trans)) {
8504 ret = PTR_ERR(trans);
8505 break;
8506 }
Yan Zhengd899e052008-10-30 14:25:28 -04008507 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008508
Chris Mason154ea282013-03-05 11:11:26 -05008509 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8510 cur_bytes = max(cur_bytes, min_size);
8511 ret = btrfs_reserve_extent(trans, root, cur_bytes,
Zach Brown24542bf2012-11-16 00:04:43 +00008512 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008513 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008514 if (own_trans)
8515 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008516 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008517 }
8518
Yan Zhengd899e052008-10-30 14:25:28 -04008519 ret = insert_reserved_file_extent(trans, inode,
8520 cur_offset, ins.objectid,
8521 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008522 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008523 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008524 if (ret) {
8525 btrfs_abort_transaction(trans, root, ret);
8526 if (own_trans)
8527 btrfs_end_transaction(trans, root);
8528 break;
8529 }
Chris Masona1ed8352009-09-11 12:27:37 -04008530 btrfs_drop_extent_cache(inode, cur_offset,
8531 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008532
Josef Bacik5dc562c2012-08-17 13:14:17 -04008533 em = alloc_extent_map();
8534 if (!em) {
8535 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8536 &BTRFS_I(inode)->runtime_flags);
8537 goto next;
8538 }
8539
8540 em->start = cur_offset;
8541 em->orig_start = cur_offset;
8542 em->len = ins.offset;
8543 em->block_start = ins.objectid;
8544 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008545 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008546 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008547 em->bdev = root->fs_info->fs_devices->latest_bdev;
8548 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8549 em->generation = trans->transid;
8550
8551 while (1) {
8552 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008553 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008554 write_unlock(&em_tree->lock);
8555 if (ret != -EEXIST)
8556 break;
8557 btrfs_drop_extent_cache(inode, cur_offset,
8558 cur_offset + ins.offset - 1,
8559 0);
8560 }
8561 free_extent_map(em);
8562next:
Yan Zhengd899e052008-10-30 14:25:28 -04008563 num_bytes -= ins.offset;
8564 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008565 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008566
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008567 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008568 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008569 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008570 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008571 (actual_len > inode->i_size) &&
8572 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008573 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008574 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008575 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008576 i_size = cur_offset;
8577 i_size_write(inode, i_size);
8578 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008579 }
8580
Yan Zhengd899e052008-10-30 14:25:28 -04008581 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008582
8583 if (ret) {
8584 btrfs_abort_transaction(trans, root, ret);
8585 if (own_trans)
8586 btrfs_end_transaction(trans, root);
8587 break;
8588 }
Yan Zhengd899e052008-10-30 14:25:28 -04008589
Josef Bacik0af3d002010-06-21 14:48:16 -04008590 if (own_trans)
8591 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008592 }
Yan Zhengd899e052008-10-30 14:25:28 -04008593 return ret;
8594}
8595
Josef Bacik0af3d002010-06-21 14:48:16 -04008596int btrfs_prealloc_file_range(struct inode *inode, int mode,
8597 u64 start, u64 num_bytes, u64 min_size,
8598 loff_t actual_len, u64 *alloc_hint)
8599{
8600 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8601 min_size, actual_len, alloc_hint,
8602 NULL);
8603}
8604
8605int btrfs_prealloc_file_range_trans(struct inode *inode,
8606 struct btrfs_trans_handle *trans, int mode,
8607 u64 start, u64 num_bytes, u64 min_size,
8608 loff_t actual_len, u64 *alloc_hint)
8609{
8610 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8611 min_size, actual_len, alloc_hint, trans);
8612}
8613
Chris Masone6dcd2d2008-07-17 12:53:50 -04008614static int btrfs_set_page_dirty(struct page *page)
8615{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008616 return __set_page_dirty_nobuffers(page);
8617}
8618
Al Viro10556cb2011-06-20 19:28:19 -04008619static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008620{
Li Zefanb83cc962010-12-20 16:04:08 +08008621 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008622 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008623
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008624 if (mask & MAY_WRITE &&
8625 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8626 if (btrfs_root_readonly(root))
8627 return -EROFS;
8628 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8629 return -EACCES;
8630 }
Al Viro2830ba72011-06-20 19:16:29 -04008631 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008632}
Chris Mason39279cc2007-06-12 06:35:45 -04008633
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008634static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008635 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008636 .lookup = btrfs_lookup,
8637 .create = btrfs_create,
8638 .unlink = btrfs_unlink,
8639 .link = btrfs_link,
8640 .mkdir = btrfs_mkdir,
8641 .rmdir = btrfs_rmdir,
8642 .rename = btrfs_rename,
8643 .symlink = btrfs_symlink,
8644 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008645 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008646 .setxattr = btrfs_setxattr,
8647 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008648 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008649 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008650 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008651 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008652};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008653static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008654 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008655 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008656 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008657};
Yan, Zheng76dda932009-09-21 16:00:26 -04008658
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008659static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008660 .llseek = generic_file_llseek,
8661 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04008662 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008663 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008664#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008665 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008666#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008667 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008668 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008669};
8670
Chris Masond1310b22008-01-24 16:13:08 -05008671static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008672 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008673 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008674 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008675 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008676 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008677 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008678 .set_bit_hook = btrfs_set_bit_hook,
8679 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008680 .merge_extent_hook = btrfs_merge_extent_hook,
8681 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008682};
8683
Chris Mason35054392009-01-21 13:11:13 -05008684/*
8685 * btrfs doesn't support the bmap operation because swapfiles
8686 * use bmap to make a mapping of extents in the file. They assume
8687 * these extents won't change over the life of the file and they
8688 * use the bmap result to do IO directly to the drive.
8689 *
8690 * the btrfs bmap call would return logical addresses that aren't
8691 * suitable for IO and they also will change frequently as COW
8692 * operations happen. So, swapfile + btrfs == corruption.
8693 *
8694 * For now we're avoiding this by dropping bmap.
8695 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008696static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008697 .readpage = btrfs_readpage,
8698 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008699 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008700 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008701 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008702 .invalidatepage = btrfs_invalidatepage,
8703 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008704 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008705 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008706};
8707
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008708static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008709 .readpage = btrfs_readpage,
8710 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008711 .invalidatepage = btrfs_invalidatepage,
8712 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008713};
8714
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008715static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008716 .getattr = btrfs_getattr,
8717 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008718 .setxattr = btrfs_setxattr,
8719 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008720 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008721 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008722 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008723 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008724 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008725 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008726};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008727static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008728 .getattr = btrfs_getattr,
8729 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008730 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008731 .setxattr = btrfs_setxattr,
8732 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008733 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008734 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008735 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008736 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008737};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008738static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008739 .readlink = generic_readlink,
8740 .follow_link = page_follow_link_light,
8741 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008742 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008743 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008744 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008745 .setxattr = btrfs_setxattr,
8746 .getxattr = btrfs_getxattr,
8747 .listxattr = btrfs_listxattr,
8748 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008749 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008750 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008751};
Yan, Zheng76dda932009-09-21 16:00:26 -04008752
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008753const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008754 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008755 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008756};