blob: 6d1b93c8aafb8a4d7b832cab8585ebf1ac1ced42 [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
2169 extent_offset = btrfs_file_extent_offset(leaf, extent);
2170 if (key.offset - extent_offset != offset)
2171 continue;
2172
2173 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2174 if (extent_offset >= old->extent_offset + old->offset +
2175 old->len || extent_offset + num_bytes <=
2176 old->extent_offset + old->offset)
2177 continue;
2178
2179 break;
2180 }
2181
2182 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2183 if (!backref) {
2184 ret = -ENOENT;
2185 goto out;
2186 }
2187
2188 backref->root_id = root_id;
2189 backref->inum = inum;
2190 backref->file_pos = offset + extent_offset;
2191 backref->num_bytes = num_bytes;
2192 backref->extent_offset = extent_offset;
2193 backref->generation = btrfs_file_extent_generation(leaf, extent);
2194 backref->old = old;
2195 backref_insert(&new->root, backref);
2196 old->count++;
2197out:
2198 btrfs_release_path(path);
2199 WARN_ON(ret);
2200 return ret;
2201}
2202
2203static noinline bool record_extent_backrefs(struct btrfs_path *path,
2204 struct new_sa_defrag_extent *new)
2205{
2206 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2207 struct old_sa_defrag_extent *old, *tmp;
2208 int ret;
2209
2210 new->path = path;
2211
2212 list_for_each_entry_safe(old, tmp, &new->head, list) {
2213 ret = iterate_inodes_from_logical(old->bytenr, fs_info,
2214 path, record_one_backref,
2215 old);
2216 BUG_ON(ret < 0 && ret != -ENOENT);
2217
2218 /* no backref to be processed for this extent */
2219 if (!old->count) {
2220 list_del(&old->list);
2221 kfree(old);
2222 }
2223 }
2224
2225 if (list_empty(&new->head))
2226 return false;
2227
2228 return true;
2229}
2230
2231static int relink_is_mergable(struct extent_buffer *leaf,
2232 struct btrfs_file_extent_item *fi,
2233 u64 disk_bytenr)
2234{
2235 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2236 return 0;
2237
2238 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2239 return 0;
2240
2241 if (btrfs_file_extent_compression(leaf, fi) ||
2242 btrfs_file_extent_encryption(leaf, fi) ||
2243 btrfs_file_extent_other_encoding(leaf, fi))
2244 return 0;
2245
2246 return 1;
2247}
2248
2249/*
2250 * Note the backref might has changed, and in this case we just return 0.
2251 */
2252static noinline int relink_extent_backref(struct btrfs_path *path,
2253 struct sa_defrag_extent_backref *prev,
2254 struct sa_defrag_extent_backref *backref)
2255{
2256 struct btrfs_file_extent_item *extent;
2257 struct btrfs_file_extent_item *item;
2258 struct btrfs_ordered_extent *ordered;
2259 struct btrfs_trans_handle *trans;
2260 struct btrfs_fs_info *fs_info;
2261 struct btrfs_root *root;
2262 struct btrfs_key key;
2263 struct extent_buffer *leaf;
2264 struct old_sa_defrag_extent *old = backref->old;
2265 struct new_sa_defrag_extent *new = old->new;
2266 struct inode *src_inode = new->inode;
2267 struct inode *inode;
2268 struct extent_state *cached = NULL;
2269 int ret = 0;
2270 u64 start;
2271 u64 len;
2272 u64 lock_start;
2273 u64 lock_end;
2274 bool merge = false;
2275 int index;
2276
2277 if (prev && prev->root_id == backref->root_id &&
2278 prev->inum == backref->inum &&
2279 prev->file_pos + prev->num_bytes == backref->file_pos)
2280 merge = true;
2281
2282 /* step 1: get root */
2283 key.objectid = backref->root_id;
2284 key.type = BTRFS_ROOT_ITEM_KEY;
2285 key.offset = (u64)-1;
2286
2287 fs_info = BTRFS_I(src_inode)->root->fs_info;
2288 index = srcu_read_lock(&fs_info->subvol_srcu);
2289
2290 root = btrfs_read_fs_root_no_name(fs_info, &key);
2291 if (IS_ERR(root)) {
2292 srcu_read_unlock(&fs_info->subvol_srcu, index);
2293 if (PTR_ERR(root) == -ENOENT)
2294 return 0;
2295 return PTR_ERR(root);
2296 }
Liu Bo38c227d2013-01-29 03:18:40 +00002297
2298 /* step 2: get inode */
2299 key.objectid = backref->inum;
2300 key.type = BTRFS_INODE_ITEM_KEY;
2301 key.offset = 0;
2302
2303 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2304 if (IS_ERR(inode)) {
2305 srcu_read_unlock(&fs_info->subvol_srcu, index);
2306 return 0;
2307 }
2308
2309 srcu_read_unlock(&fs_info->subvol_srcu, index);
2310
2311 /* step 3: relink backref */
2312 lock_start = backref->file_pos;
2313 lock_end = backref->file_pos + backref->num_bytes - 1;
2314 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2315 0, &cached);
2316
2317 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2318 if (ordered) {
2319 btrfs_put_ordered_extent(ordered);
2320 goto out_unlock;
2321 }
2322
2323 trans = btrfs_join_transaction(root);
2324 if (IS_ERR(trans)) {
2325 ret = PTR_ERR(trans);
2326 goto out_unlock;
2327 }
2328
2329 key.objectid = backref->inum;
2330 key.type = BTRFS_EXTENT_DATA_KEY;
2331 key.offset = backref->file_pos;
2332
2333 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2334 if (ret < 0) {
2335 goto out_free_path;
2336 } else if (ret > 0) {
2337 ret = 0;
2338 goto out_free_path;
2339 }
2340
2341 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2342 struct btrfs_file_extent_item);
2343
2344 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2345 backref->generation)
2346 goto out_free_path;
2347
2348 btrfs_release_path(path);
2349
2350 start = backref->file_pos;
2351 if (backref->extent_offset < old->extent_offset + old->offset)
2352 start += old->extent_offset + old->offset -
2353 backref->extent_offset;
2354
2355 len = min(backref->extent_offset + backref->num_bytes,
2356 old->extent_offset + old->offset + old->len);
2357 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2358
2359 ret = btrfs_drop_extents(trans, root, inode, start,
2360 start + len, 1);
2361 if (ret)
2362 goto out_free_path;
2363again:
2364 key.objectid = btrfs_ino(inode);
2365 key.type = BTRFS_EXTENT_DATA_KEY;
2366 key.offset = start;
2367
Liu Boa09a0a72013-03-11 09:20:58 +00002368 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002369 if (merge) {
2370 struct btrfs_file_extent_item *fi;
2371 u64 extent_len;
2372 struct btrfs_key found_key;
2373
2374 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2375 if (ret < 0)
2376 goto out_free_path;
2377
2378 path->slots[0]--;
2379 leaf = path->nodes[0];
2380 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2381
2382 fi = btrfs_item_ptr(leaf, path->slots[0],
2383 struct btrfs_file_extent_item);
2384 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2385
2386 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2387 extent_len + found_key.offset == start) {
2388 btrfs_set_file_extent_num_bytes(leaf, fi,
2389 extent_len + len);
2390 btrfs_mark_buffer_dirty(leaf);
2391 inode_add_bytes(inode, len);
2392
2393 ret = 1;
2394 goto out_free_path;
2395 } else {
2396 merge = false;
2397 btrfs_release_path(path);
2398 goto again;
2399 }
2400 }
2401
2402 ret = btrfs_insert_empty_item(trans, root, path, &key,
2403 sizeof(*extent));
2404 if (ret) {
2405 btrfs_abort_transaction(trans, root, ret);
2406 goto out_free_path;
2407 }
2408
2409 leaf = path->nodes[0];
2410 item = btrfs_item_ptr(leaf, path->slots[0],
2411 struct btrfs_file_extent_item);
2412 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2413 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2414 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2415 btrfs_set_file_extent_num_bytes(leaf, item, len);
2416 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2417 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2418 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2419 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2420 btrfs_set_file_extent_encryption(leaf, item, 0);
2421 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2422
2423 btrfs_mark_buffer_dirty(leaf);
2424 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002425 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002426
2427 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2428 new->disk_len, 0,
2429 backref->root_id, backref->inum,
2430 new->file_pos, 0); /* start - extent_offset */
2431 if (ret) {
2432 btrfs_abort_transaction(trans, root, ret);
2433 goto out_free_path;
2434 }
2435
2436 ret = 1;
2437out_free_path:
2438 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002439 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002440 btrfs_end_transaction(trans, root);
2441out_unlock:
2442 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2443 &cached, GFP_NOFS);
2444 iput(inode);
2445 return ret;
2446}
2447
2448static void relink_file_extents(struct new_sa_defrag_extent *new)
2449{
2450 struct btrfs_path *path;
2451 struct old_sa_defrag_extent *old, *tmp;
2452 struct sa_defrag_extent_backref *backref;
2453 struct sa_defrag_extent_backref *prev = NULL;
2454 struct inode *inode;
2455 struct btrfs_root *root;
2456 struct rb_node *node;
2457 int ret;
2458
2459 inode = new->inode;
2460 root = BTRFS_I(inode)->root;
2461
2462 path = btrfs_alloc_path();
2463 if (!path)
2464 return;
2465
2466 if (!record_extent_backrefs(path, new)) {
2467 btrfs_free_path(path);
2468 goto out;
2469 }
2470 btrfs_release_path(path);
2471
2472 while (1) {
2473 node = rb_first(&new->root);
2474 if (!node)
2475 break;
2476 rb_erase(node, &new->root);
2477
2478 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2479
2480 ret = relink_extent_backref(path, prev, backref);
2481 WARN_ON(ret < 0);
2482
2483 kfree(prev);
2484
2485 if (ret == 1)
2486 prev = backref;
2487 else
2488 prev = NULL;
2489 cond_resched();
2490 }
2491 kfree(prev);
2492
2493 btrfs_free_path(path);
2494
2495 list_for_each_entry_safe(old, tmp, &new->head, list) {
2496 list_del(&old->list);
2497 kfree(old);
2498 }
2499out:
2500 atomic_dec(&root->fs_info->defrag_running);
2501 wake_up(&root->fs_info->transaction_wait);
2502
2503 kfree(new);
2504}
2505
2506static struct new_sa_defrag_extent *
2507record_old_file_extents(struct inode *inode,
2508 struct btrfs_ordered_extent *ordered)
2509{
2510 struct btrfs_root *root = BTRFS_I(inode)->root;
2511 struct btrfs_path *path;
2512 struct btrfs_key key;
2513 struct old_sa_defrag_extent *old, *tmp;
2514 struct new_sa_defrag_extent *new;
2515 int ret;
2516
2517 new = kmalloc(sizeof(*new), GFP_NOFS);
2518 if (!new)
2519 return NULL;
2520
2521 new->inode = inode;
2522 new->file_pos = ordered->file_offset;
2523 new->len = ordered->len;
2524 new->bytenr = ordered->start;
2525 new->disk_len = ordered->disk_len;
2526 new->compress_type = ordered->compress_type;
2527 new->root = RB_ROOT;
2528 INIT_LIST_HEAD(&new->head);
2529
2530 path = btrfs_alloc_path();
2531 if (!path)
2532 goto out_kfree;
2533
2534 key.objectid = btrfs_ino(inode);
2535 key.type = BTRFS_EXTENT_DATA_KEY;
2536 key.offset = new->file_pos;
2537
2538 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2539 if (ret < 0)
2540 goto out_free_path;
2541 if (ret > 0 && path->slots[0] > 0)
2542 path->slots[0]--;
2543
2544 /* find out all the old extents for the file range */
2545 while (1) {
2546 struct btrfs_file_extent_item *extent;
2547 struct extent_buffer *l;
2548 int slot;
2549 u64 num_bytes;
2550 u64 offset;
2551 u64 end;
2552 u64 disk_bytenr;
2553 u64 extent_offset;
2554
2555 l = path->nodes[0];
2556 slot = path->slots[0];
2557
2558 if (slot >= btrfs_header_nritems(l)) {
2559 ret = btrfs_next_leaf(root, path);
2560 if (ret < 0)
2561 goto out_free_list;
2562 else if (ret > 0)
2563 break;
2564 continue;
2565 }
2566
2567 btrfs_item_key_to_cpu(l, &key, slot);
2568
2569 if (key.objectid != btrfs_ino(inode))
2570 break;
2571 if (key.type != BTRFS_EXTENT_DATA_KEY)
2572 break;
2573 if (key.offset >= new->file_pos + new->len)
2574 break;
2575
2576 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2577
2578 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2579 if (key.offset + num_bytes < new->file_pos)
2580 goto next;
2581
2582 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2583 if (!disk_bytenr)
2584 goto next;
2585
2586 extent_offset = btrfs_file_extent_offset(l, extent);
2587
2588 old = kmalloc(sizeof(*old), GFP_NOFS);
2589 if (!old)
2590 goto out_free_list;
2591
2592 offset = max(new->file_pos, key.offset);
2593 end = min(new->file_pos + new->len, key.offset + num_bytes);
2594
2595 old->bytenr = disk_bytenr;
2596 old->extent_offset = extent_offset;
2597 old->offset = offset - key.offset;
2598 old->len = end - offset;
2599 old->new = new;
2600 old->count = 0;
2601 list_add_tail(&old->list, &new->head);
2602next:
2603 path->slots[0]++;
2604 cond_resched();
2605 }
2606
2607 btrfs_free_path(path);
2608 atomic_inc(&root->fs_info->defrag_running);
2609
2610 return new;
2611
2612out_free_list:
2613 list_for_each_entry_safe(old, tmp, &new->head, list) {
2614 list_del(&old->list);
2615 kfree(old);
2616 }
2617out_free_path:
2618 btrfs_free_path(path);
2619out_kfree:
2620 kfree(new);
2621 return NULL;
2622}
2623
Chris Mason5d13a982009-03-13 11:41:46 -04002624/*
2625 * helper function for btrfs_finish_ordered_io, this
2626 * just reads in some of the csum leaves to prime them into ram
2627 * before we start the transaction. It limits the amount of btree
2628 * reads required while inside the transaction.
2629 */
Chris Masond352ac62008-09-29 15:18:18 -04002630/* as ordered data IO finishes, this gets called so we can finish
2631 * an ordered extent if the range of bytes in the file it covers are
2632 * fully written.
2633 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002634static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002635{
Josef Bacik5fd02042012-05-02 14:00:54 -04002636 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002637 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002638 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002639 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002640 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002641 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002642 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002643 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002644 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002645
Liu Bo83eea1f2012-07-10 05:28:39 -06002646 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002647
Josef Bacik5fd02042012-05-02 14:00:54 -04002648 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2649 ret = -EIO;
2650 goto out;
2651 }
2652
Yan, Zhengc2167752009-11-12 09:34:21 +00002653 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002654 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002655 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2656 if (nolock)
2657 trans = btrfs_join_transaction_nolock(root);
2658 else
2659 trans = btrfs_join_transaction(root);
2660 if (IS_ERR(trans)) {
2661 ret = PTR_ERR(trans);
2662 trans = NULL;
2663 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002664 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002665 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2666 ret = btrfs_update_inode_fallback(trans, root, inode);
2667 if (ret) /* -ENOMEM or corruption */
2668 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002669 goto out;
2670 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002671
Josef Bacik2ac55d42010-02-03 19:33:23 +00002672 lock_extent_bits(io_tree, ordered_extent->file_offset,
2673 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002674 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002675
Liu Bo38c227d2013-01-29 03:18:40 +00002676 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2677 ordered_extent->file_offset + ordered_extent->len - 1,
2678 EXTENT_DEFRAG, 1, cached_state);
2679 if (ret) {
2680 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2681 if (last_snapshot >= BTRFS_I(inode)->generation)
2682 /* the inode is shared */
2683 new = record_old_file_extents(inode, ordered_extent);
2684
2685 clear_extent_bit(io_tree, ordered_extent->file_offset,
2686 ordered_extent->file_offset + ordered_extent->len - 1,
2687 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2688 }
2689
Josef Bacik0cb59c92010-07-02 12:14:14 -04002690 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002691 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002692 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002693 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002694 if (IS_ERR(trans)) {
2695 ret = PTR_ERR(trans);
2696 trans = NULL;
2697 goto out_unlock;
2698 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002699 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002700
Chris Masonc8b97812008-10-29 14:49:59 -04002701 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002702 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002703 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002704 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002705 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002706 ordered_extent->file_offset,
2707 ordered_extent->file_offset +
2708 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002709 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002710 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002711 ret = insert_reserved_file_extent(trans, inode,
2712 ordered_extent->file_offset,
2713 ordered_extent->start,
2714 ordered_extent->disk_len,
2715 ordered_extent->len,
2716 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002717 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002718 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002719 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002720 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2721 ordered_extent->file_offset, ordered_extent->len,
2722 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002723 if (ret < 0) {
2724 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002725 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002726 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002727
Chris Masone6dcd2d2008-07-17 12:53:50 -04002728 add_pending_csums(trans, inode, ordered_extent->file_offset,
2729 &ordered_extent->list);
2730
Josef Bacik6c760c02012-11-09 10:53:21 -05002731 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2732 ret = btrfs_update_inode_fallback(trans, root, inode);
2733 if (ret) { /* -ENOMEM or corruption */
2734 btrfs_abort_transaction(trans, root, ret);
2735 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002736 }
2737 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002738out_unlock:
2739 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2740 ordered_extent->file_offset +
2741 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002742out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002743 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002744 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002745 if (trans)
2746 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002747
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002748 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002749 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2750 ordered_extent->file_offset +
2751 ordered_extent->len - 1, NULL, GFP_NOFS);
2752
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002753 /*
2754 * If the ordered extent had an IOERR or something else went
2755 * wrong we need to return the space for this ordered extent
2756 * back to the allocator.
2757 */
2758 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2759 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2760 btrfs_free_reserved_extent(root, ordered_extent->start,
2761 ordered_extent->disk_len);
2762 }
2763
2764
Josef Bacik5fd02042012-05-02 14:00:54 -04002765 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002766 * This needs to be done to make sure anybody waiting knows we are done
2767 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002768 */
2769 btrfs_remove_ordered_extent(inode, ordered_extent);
2770
Liu Bo38c227d2013-01-29 03:18:40 +00002771 /* for snapshot-aware defrag */
2772 if (new)
2773 relink_file_extents(new);
2774
Chris Masone6dcd2d2008-07-17 12:53:50 -04002775 /* once for us */
2776 btrfs_put_ordered_extent(ordered_extent);
2777 /* once for the tree */
2778 btrfs_put_ordered_extent(ordered_extent);
2779
Josef Bacik5fd02042012-05-02 14:00:54 -04002780 return ret;
2781}
2782
2783static void finish_ordered_fn(struct btrfs_work *work)
2784{
2785 struct btrfs_ordered_extent *ordered_extent;
2786 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2787 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002788}
2789
Christoph Hellwigb2950862008-12-02 09:54:17 -05002790static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002791 struct extent_state *state, int uptodate)
2792{
Josef Bacik5fd02042012-05-02 14:00:54 -04002793 struct inode *inode = page->mapping->host;
2794 struct btrfs_root *root = BTRFS_I(inode)->root;
2795 struct btrfs_ordered_extent *ordered_extent = NULL;
2796 struct btrfs_workers *workers;
2797
liubo1abe9b82011-03-24 11:18:59 +00002798 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2799
Chris Mason8b62b722009-09-02 16:53:46 -04002800 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002801 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2802 end - start + 1, uptodate))
2803 return 0;
2804
2805 ordered_extent->work.func = finish_ordered_fn;
2806 ordered_extent->work.flags = 0;
2807
Liu Bo83eea1f2012-07-10 05:28:39 -06002808 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002809 workers = &root->fs_info->endio_freespace_worker;
2810 else
2811 workers = &root->fs_info->endio_write_workers;
2812 btrfs_queue_worker(workers, &ordered_extent->work);
2813
2814 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002815}
2816
Chris Masond352ac62008-09-29 15:18:18 -04002817/*
Chris Masond352ac62008-09-29 15:18:18 -04002818 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002819 * if there's a match, we allow the bio to finish. If not, the code in
2820 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002821 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002822static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002823 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002824{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002825 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002826 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002827 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002828 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002829 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002830 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002831 struct btrfs_root *root = BTRFS_I(inode)->root;
2832 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002833 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2834 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002835
Chris Masond20f7042008-12-08 16:58:54 -05002836 if (PageChecked(page)) {
2837 ClearPageChecked(page);
2838 goto good;
2839 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002840
2841 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002842 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002843
Yan Zheng17d217f2008-12-12 10:03:38 -05002844 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002845 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002846 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2847 GFP_NOFS);
2848 return 0;
2849 }
2850
Yanc2e639f2008-02-04 08:57:25 -05002851 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002852 private = state->private;
2853 ret = 0;
2854 } else {
2855 ret = get_state_private(io_tree, start, &private);
2856 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002857 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002858 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002859 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002860
Liu Bob0496682013-03-14 14:57:45 +00002861 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002862 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002863 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002864 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002865
Cong Wang7ac687d2011-11-25 23:14:28 +08002866 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002867good:
Chris Mason07157aa2007-08-30 08:50:51 -04002868 return 0;
2869
2870zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002871 if (__ratelimit(&_rs))
2872 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u private %llu",
2873 (unsigned long long)btrfs_ino(page->mapping->host),
2874 (unsigned long long)start, csum,
2875 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002876 memset(kaddr + offset, 1, end - start + 1);
2877 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002878 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002879 if (private == 0)
2880 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002881 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002882}
Chris Masonb888db22007-08-27 16:49:44 -04002883
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002884struct delayed_iput {
2885 struct list_head list;
2886 struct inode *inode;
2887};
2888
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002889/* JDM: If this is fs-wide, why can't we add a pointer to
2890 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002891void btrfs_add_delayed_iput(struct inode *inode)
2892{
2893 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2894 struct delayed_iput *delayed;
2895
2896 if (atomic_add_unless(&inode->i_count, -1, 1))
2897 return;
2898
2899 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2900 delayed->inode = inode;
2901
2902 spin_lock(&fs_info->delayed_iput_lock);
2903 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2904 spin_unlock(&fs_info->delayed_iput_lock);
2905}
2906
2907void btrfs_run_delayed_iputs(struct btrfs_root *root)
2908{
2909 LIST_HEAD(list);
2910 struct btrfs_fs_info *fs_info = root->fs_info;
2911 struct delayed_iput *delayed;
2912 int empty;
2913
2914 spin_lock(&fs_info->delayed_iput_lock);
2915 empty = list_empty(&fs_info->delayed_iputs);
2916 spin_unlock(&fs_info->delayed_iput_lock);
2917 if (empty)
2918 return;
2919
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002920 spin_lock(&fs_info->delayed_iput_lock);
2921 list_splice_init(&fs_info->delayed_iputs, &list);
2922 spin_unlock(&fs_info->delayed_iput_lock);
2923
2924 while (!list_empty(&list)) {
2925 delayed = list_entry(list.next, struct delayed_iput, list);
2926 list_del(&delayed->list);
2927 iput(delayed->inode);
2928 kfree(delayed);
2929 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002930}
2931
Yan, Zhengd68fc572010-05-16 10:49:58 -04002932/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002933 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002934 * files in the subvolume, it removes orphan item and frees block_rsv
2935 * structure.
2936 */
2937void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2938 struct btrfs_root *root)
2939{
Josef Bacik90290e12011-12-02 15:44:12 -05002940 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002941 int ret;
2942
Josef Bacik8a35d952012-05-23 14:26:42 -04002943 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002944 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2945 return;
2946
Josef Bacik90290e12011-12-02 15:44:12 -05002947 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002948 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002949 spin_unlock(&root->orphan_lock);
2950 return;
2951 }
2952
2953 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2954 spin_unlock(&root->orphan_lock);
2955 return;
2956 }
2957
2958 block_rsv = root->orphan_block_rsv;
2959 root->orphan_block_rsv = NULL;
2960 spin_unlock(&root->orphan_lock);
2961
Yan, Zhengd68fc572010-05-16 10:49:58 -04002962 if (root->orphan_item_inserted &&
2963 btrfs_root_refs(&root->root_item) > 0) {
2964 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2965 root->root_key.objectid);
2966 BUG_ON(ret);
2967 root->orphan_item_inserted = 0;
2968 }
2969
Josef Bacik90290e12011-12-02 15:44:12 -05002970 if (block_rsv) {
2971 WARN_ON(block_rsv->size > 0);
2972 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002973 }
2974}
2975
2976/*
Josef Bacik7b128762008-07-24 12:17:14 -04002977 * This creates an orphan entry for the given inode in case something goes
2978 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002979 *
2980 * NOTE: caller of this function should reserve 5 units of metadata for
2981 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002982 */
2983int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2984{
2985 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002986 struct btrfs_block_rsv *block_rsv = NULL;
2987 int reserve = 0;
2988 int insert = 0;
2989 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002990
Yan, Zhengd68fc572010-05-16 10:49:58 -04002991 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002992 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002993 if (!block_rsv)
2994 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002995 }
2996
Yan, Zhengd68fc572010-05-16 10:49:58 -04002997 spin_lock(&root->orphan_lock);
2998 if (!root->orphan_block_rsv) {
2999 root->orphan_block_rsv = block_rsv;
3000 } else if (block_rsv) {
3001 btrfs_free_block_rsv(root, block_rsv);
3002 block_rsv = NULL;
3003 }
Josef Bacik7b128762008-07-24 12:17:14 -04003004
Josef Bacik8a35d952012-05-23 14:26:42 -04003005 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3006 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003007#if 0
3008 /*
3009 * For proper ENOSPC handling, we should do orphan
3010 * cleanup when mounting. But this introduces backward
3011 * compatibility issue.
3012 */
3013 if (!xchg(&root->orphan_item_inserted, 1))
3014 insert = 2;
3015 else
3016 insert = 1;
3017#endif
3018 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003019 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003020 }
Josef Bacik7b128762008-07-24 12:17:14 -04003021
Josef Bacik72ac3c02012-05-23 14:13:11 -04003022 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3023 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003024 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003025 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003026
Yan, Zhengd68fc572010-05-16 10:49:58 -04003027 /* grab metadata reservation from transaction handle */
3028 if (reserve) {
3029 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003030 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003031 }
3032
3033 /* insert an orphan item to track this unlinked/truncated file */
3034 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003035 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003036 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003037 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3038 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003039 btrfs_abort_transaction(trans, root, ret);
3040 return ret;
3041 }
3042 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003043 }
3044
3045 /* insert an orphan item to track subvolume contains orphan files */
3046 if (insert >= 2) {
3047 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3048 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003049 if (ret && ret != -EEXIST) {
3050 btrfs_abort_transaction(trans, root, ret);
3051 return ret;
3052 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003053 }
3054 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003055}
3056
3057/*
3058 * We have done the truncate/delete so we can go ahead and remove the orphan
3059 * item for this particular inode.
3060 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003061static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3062 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003063{
3064 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003065 int delete_item = 0;
3066 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003067 int ret = 0;
3068
Yan, Zhengd68fc572010-05-16 10:49:58 -04003069 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003070 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3071 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003072 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003073
Josef Bacik72ac3c02012-05-23 14:13:11 -04003074 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3075 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003076 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003077 spin_unlock(&root->orphan_lock);
3078
3079 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003080 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003081 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003082 }
3083
Josef Bacik8a35d952012-05-23 14:26:42 -04003084 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003085 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003086 atomic_dec(&root->orphan_inodes);
3087 }
Josef Bacik7b128762008-07-24 12:17:14 -04003088
Yan, Zhengd68fc572010-05-16 10:49:58 -04003089 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003090}
3091
3092/*
3093 * this cleans up any orphans that may be left on the list from the last use
3094 * of this root.
3095 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003096int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003097{
3098 struct btrfs_path *path;
3099 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003100 struct btrfs_key key, found_key;
3101 struct btrfs_trans_handle *trans;
3102 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003103 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003104 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3105
Yan, Zhengd68fc572010-05-16 10:49:58 -04003106 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003107 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003108
3109 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003110 if (!path) {
3111 ret = -ENOMEM;
3112 goto out;
3113 }
Josef Bacik7b128762008-07-24 12:17:14 -04003114 path->reada = -1;
3115
3116 key.objectid = BTRFS_ORPHAN_OBJECTID;
3117 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3118 key.offset = (u64)-1;
3119
Josef Bacik7b128762008-07-24 12:17:14 -04003120 while (1) {
3121 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003122 if (ret < 0)
3123 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003124
3125 /*
3126 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003127 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003128 * find the key and see if we have stuff that matches
3129 */
3130 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003131 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003132 if (path->slots[0] == 0)
3133 break;
3134 path->slots[0]--;
3135 }
3136
3137 /* pull out the item */
3138 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003139 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3140
3141 /* make sure the item matches what we want */
3142 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3143 break;
3144 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3145 break;
3146
3147 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003148 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003149
3150 /*
3151 * this is where we are basically btrfs_lookup, without the
3152 * crossing root thing. we store the inode number in the
3153 * offset of the orphan item.
3154 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003155
3156 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003157 btrfs_err(root->fs_info,
3158 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003159 ret = -EINVAL;
3160 goto out;
3161 }
3162
3163 last_objectid = found_key.offset;
3164
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003165 found_key.objectid = found_key.offset;
3166 found_key.type = BTRFS_INODE_ITEM_KEY;
3167 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003168 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003169 ret = PTR_RET(inode);
3170 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003171 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003172
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003173 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3174 struct btrfs_root *dead_root;
3175 struct btrfs_fs_info *fs_info = root->fs_info;
3176 int is_dead_root = 0;
3177
3178 /*
3179 * this is an orphan in the tree root. Currently these
3180 * could come from 2 sources:
3181 * a) a snapshot deletion in progress
3182 * b) a free space cache inode
3183 * We need to distinguish those two, as the snapshot
3184 * orphan must not get deleted.
3185 * find_dead_roots already ran before us, so if this
3186 * is a snapshot deletion, we should find the root
3187 * in the dead_roots list
3188 */
3189 spin_lock(&fs_info->trans_lock);
3190 list_for_each_entry(dead_root, &fs_info->dead_roots,
3191 root_list) {
3192 if (dead_root->root_key.objectid ==
3193 found_key.objectid) {
3194 is_dead_root = 1;
3195 break;
3196 }
3197 }
3198 spin_unlock(&fs_info->trans_lock);
3199 if (is_dead_root) {
3200 /* prevent this orphan from being found again */
3201 key.offset = found_key.objectid - 1;
3202 continue;
3203 }
3204 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003205 /*
3206 * Inode is already gone but the orphan item is still there,
3207 * kill the orphan item.
3208 */
3209 if (ret == -ESTALE) {
3210 trans = btrfs_start_transaction(root, 1);
3211 if (IS_ERR(trans)) {
3212 ret = PTR_ERR(trans);
3213 goto out;
3214 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003215 btrfs_debug(root->fs_info, "auto deleting %Lu",
3216 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003217 ret = btrfs_del_orphan_item(trans, root,
3218 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003219 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003220 btrfs_end_transaction(trans, root);
3221 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003222 }
Josef Bacik7b128762008-07-24 12:17:14 -04003223
Josef Bacik7b128762008-07-24 12:17:14 -04003224 /*
3225 * add this inode to the orphan list so btrfs_orphan_del does
3226 * the proper thing when we hit it
3227 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003228 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3229 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003230 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003231
Josef Bacik7b128762008-07-24 12:17:14 -04003232 /* if we have links, this was a truncate, lets do that */
3233 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003234 if (!S_ISREG(inode->i_mode)) {
3235 WARN_ON(1);
3236 iput(inode);
3237 continue;
3238 }
Josef Bacik7b128762008-07-24 12:17:14 -04003239 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003240
3241 /* 1 for the orphan item deletion. */
3242 trans = btrfs_start_transaction(root, 1);
3243 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003244 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003245 ret = PTR_ERR(trans);
3246 goto out;
3247 }
3248 ret = btrfs_orphan_add(trans, inode);
3249 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003250 if (ret) {
3251 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003252 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003253 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003254
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003255 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003256 if (ret)
3257 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003258 } else {
3259 nr_unlink++;
3260 }
3261
3262 /* this will do delete_inode and everything for us */
3263 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003264 if (ret)
3265 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003266 }
Miao Xie3254c872011-11-10 20:45:05 -05003267 /* release the path since we're done with it */
3268 btrfs_release_path(path);
3269
Yan, Zhengd68fc572010-05-16 10:49:58 -04003270 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3271
3272 if (root->orphan_block_rsv)
3273 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3274 (u64)-1);
3275
3276 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003277 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003278 if (!IS_ERR(trans))
3279 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003280 }
Josef Bacik7b128762008-07-24 12:17:14 -04003281
3282 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003283 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003284 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003285 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003286
3287out:
3288 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003289 btrfs_crit(root->fs_info,
3290 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003291 btrfs_free_path(path);
3292 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003293}
3294
Chris Masond352ac62008-09-29 15:18:18 -04003295/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003296 * very simple check to peek ahead in the leaf looking for xattrs. If we
3297 * don't find any xattrs, we know there can't be any acls.
3298 *
3299 * slot is the slot the inode is in, objectid is the objectid of the inode
3300 */
3301static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3302 int slot, u64 objectid)
3303{
3304 u32 nritems = btrfs_header_nritems(leaf);
3305 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003306 static u64 xattr_access = 0;
3307 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003308 int scanned = 0;
3309
Josef Bacikf23b5a52013-06-19 10:16:26 -04003310 if (!xattr_access) {
3311 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3312 strlen(POSIX_ACL_XATTR_ACCESS));
3313 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3314 strlen(POSIX_ACL_XATTR_DEFAULT));
3315 }
3316
Chris Mason46a53cc2009-04-27 11:47:50 -04003317 slot++;
3318 while (slot < nritems) {
3319 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3320
3321 /* we found a different objectid, there must not be acls */
3322 if (found_key.objectid != objectid)
3323 return 0;
3324
3325 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003326 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3327 if (found_key.offset == xattr_access ||
3328 found_key.offset == xattr_default)
3329 return 1;
3330 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003331
3332 /*
3333 * we found a key greater than an xattr key, there can't
3334 * be any acls later on
3335 */
3336 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3337 return 0;
3338
3339 slot++;
3340 scanned++;
3341
3342 /*
3343 * it goes inode, inode backrefs, xattrs, extents,
3344 * so if there are a ton of hard links to an inode there can
3345 * be a lot of backrefs. Don't waste time searching too hard,
3346 * this is just an optimization
3347 */
3348 if (scanned >= 8)
3349 break;
3350 }
3351 /* we hit the end of the leaf before we found an xattr or
3352 * something larger than an xattr. We have to assume the inode
3353 * has acls
3354 */
3355 return 1;
3356}
3357
3358/*
Chris Masond352ac62008-09-29 15:18:18 -04003359 * read an inode from the btree into the in-memory inode
3360 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003361static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003362{
3363 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003364 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003365 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003366 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003367 struct btrfs_root *root = BTRFS_I(inode)->root;
3368 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003369 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003370 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003371 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003372 bool filled = false;
3373
3374 ret = btrfs_fill_inode(inode, &rdev);
3375 if (!ret)
3376 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003377
3378 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003379 if (!path)
3380 goto make_bad;
3381
Josef Bacikd90c7322011-05-17 09:50:54 -04003382 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003383 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003384
Chris Mason39279cc2007-06-12 06:35:45 -04003385 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003386 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003387 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003388
Chris Mason5f39d392007-10-15 16:14:19 -04003389 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003390
3391 if (filled)
3392 goto cache_acl;
3393
Chris Mason5f39d392007-10-15 16:14:19 -04003394 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3395 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003396 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003397 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003398 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3399 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003400 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003401
3402 tspec = btrfs_inode_atime(inode_item);
3403 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3404 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3405
3406 tspec = btrfs_inode_mtime(inode_item);
3407 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3408 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3409
3410 tspec = btrfs_inode_ctime(inode_item);
3411 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3412 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3413
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003414 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003415 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003416 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3417
3418 /*
3419 * If we were modified in the current generation and evicted from memory
3420 * and then re-read we need to do a full sync since we don't have any
3421 * idea about which extents were modified before we were evicted from
3422 * cache.
3423 */
3424 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3425 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3426 &BTRFS_I(inode)->runtime_flags);
3427
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003428 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003429 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003430 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003431 rdev = btrfs_inode_rdev(leaf, inode_item);
3432
Josef Bacikaec74772008-07-24 12:12:38 -04003433 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003434 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003435cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003436 /*
3437 * try to precache a NULL acl entry for files that don't have
3438 * any xattrs or acls
3439 */
Li Zefan33345d012011-04-20 10:31:50 +08003440 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3441 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003442 if (!maybe_acls)
3443 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003444
Chris Mason39279cc2007-06-12 06:35:45 -04003445 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003446
Chris Mason39279cc2007-06-12 06:35:45 -04003447 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003448 case S_IFREG:
3449 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003450 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003451 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003452 inode->i_fop = &btrfs_file_operations;
3453 inode->i_op = &btrfs_file_inode_operations;
3454 break;
3455 case S_IFDIR:
3456 inode->i_fop = &btrfs_dir_file_operations;
3457 if (root == root->fs_info->tree_root)
3458 inode->i_op = &btrfs_dir_ro_inode_operations;
3459 else
3460 inode->i_op = &btrfs_dir_inode_operations;
3461 break;
3462 case S_IFLNK:
3463 inode->i_op = &btrfs_symlink_inode_operations;
3464 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003465 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003466 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003467 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003468 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003469 init_special_inode(inode, inode->i_mode, rdev);
3470 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003471 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003472
3473 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003474 return;
3475
3476make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003477 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003478 make_bad_inode(inode);
3479}
3480
Chris Masond352ac62008-09-29 15:18:18 -04003481/*
3482 * given a leaf and an inode, copy the inode fields into the leaf
3483 */
Chris Masone02119d2008-09-05 16:13:11 -04003484static void fill_inode_item(struct btrfs_trans_handle *trans,
3485 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003486 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003487 struct inode *inode)
3488{
Liu Bo51fab692012-12-27 09:01:21 +00003489 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003490
Liu Bo51fab692012-12-27 09:01:21 +00003491 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003492
Liu Bo51fab692012-12-27 09:01:21 +00003493 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3494 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3495 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3496 &token);
3497 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3498 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003499
Liu Bo51fab692012-12-27 09:01:21 +00003500 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3501 inode->i_atime.tv_sec, &token);
3502 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3503 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003504
Liu Bo51fab692012-12-27 09:01:21 +00003505 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3506 inode->i_mtime.tv_sec, &token);
3507 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3508 inode->i_mtime.tv_nsec, &token);
3509
3510 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3511 inode->i_ctime.tv_sec, &token);
3512 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3513 inode->i_ctime.tv_nsec, &token);
3514
3515 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3516 &token);
3517 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3518 &token);
3519 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3520 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3521 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3522 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3523 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003524}
3525
Chris Masond352ac62008-09-29 15:18:18 -04003526/*
3527 * copy everything in the in-memory inode into the btree.
3528 */
Chris Mason21151332011-11-10 20:39:08 -05003529static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003530 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003531{
3532 struct btrfs_inode_item *inode_item;
3533 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003534 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003535 int ret;
3536
3537 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003538 if (!path)
3539 return -ENOMEM;
3540
Chris Masonb9473432009-03-13 11:00:37 -04003541 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003542 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3543 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003544 if (ret) {
3545 if (ret > 0)
3546 ret = -ENOENT;
3547 goto failed;
3548 }
3549
Chris Masonb4ce94d2009-02-04 09:25:08 -05003550 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003551 leaf = path->nodes[0];
3552 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003553 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003554
Chris Masone02119d2008-09-05 16:13:11 -04003555 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003556 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003557 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003558 ret = 0;
3559failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003560 btrfs_free_path(path);
3561 return ret;
3562}
3563
Chris Masond352ac62008-09-29 15:18:18 -04003564/*
Chris Mason21151332011-11-10 20:39:08 -05003565 * copy everything in the in-memory inode into the btree.
3566 */
3567noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3568 struct btrfs_root *root, struct inode *inode)
3569{
3570 int ret;
3571
3572 /*
3573 * If the inode is a free space inode, we can deadlock during commit
3574 * if we put it into the delayed code.
3575 *
3576 * The data relocation inode should also be directly updated
3577 * without delay
3578 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003579 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003580 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003581 btrfs_update_root_times(trans, root);
3582
Chris Mason21151332011-11-10 20:39:08 -05003583 ret = btrfs_delayed_update_inode(trans, root, inode);
3584 if (!ret)
3585 btrfs_set_inode_last_trans(trans, inode);
3586 return ret;
3587 }
3588
3589 return btrfs_update_inode_item(trans, root, inode);
3590}
3591
Josef Bacikbe6aef62012-10-22 15:43:12 -04003592noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3593 struct btrfs_root *root,
3594 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003595{
3596 int ret;
3597
3598 ret = btrfs_update_inode(trans, root, inode);
3599 if (ret == -ENOSPC)
3600 return btrfs_update_inode_item(trans, root, inode);
3601 return ret;
3602}
3603
3604/*
Chris Masond352ac62008-09-29 15:18:18 -04003605 * unlink helper that gets used here in inode.c and in the tree logging
3606 * recovery code. It remove a link in a directory with a given name, and
3607 * also drops the back refs in the inode to the directory
3608 */
Al Viro92986792011-03-04 17:14:37 +00003609static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3610 struct btrfs_root *root,
3611 struct inode *dir, struct inode *inode,
3612 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003613{
3614 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003615 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003616 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003617 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003618 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003619 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003620 u64 ino = btrfs_ino(inode);
3621 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003622
3623 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003624 if (!path) {
3625 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003626 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003627 }
3628
Chris Masonb9473432009-03-13 11:00:37 -04003629 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003630 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003631 name, name_len, -1);
3632 if (IS_ERR(di)) {
3633 ret = PTR_ERR(di);
3634 goto err;
3635 }
3636 if (!di) {
3637 ret = -ENOENT;
3638 goto err;
3639 }
Chris Mason5f39d392007-10-15 16:14:19 -04003640 leaf = path->nodes[0];
3641 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003642 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003643 if (ret)
3644 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003645 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003646
Li Zefan33345d012011-04-20 10:31:50 +08003647 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3648 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003649 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003650 btrfs_info(root->fs_info,
3651 "failed to delete reference to %.*s, inode %llu parent %llu",
3652 name_len, name,
3653 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003654 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003655 goto err;
3656 }
3657
Miao Xie16cdcec2011-04-22 18:12:22 +08003658 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003659 if (ret) {
3660 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003661 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003662 }
Chris Mason39279cc2007-06-12 06:35:45 -04003663
Chris Masone02119d2008-09-05 16:13:11 -04003664 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003665 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003666 if (ret != 0 && ret != -ENOENT) {
3667 btrfs_abort_transaction(trans, root, ret);
3668 goto err;
3669 }
Chris Masone02119d2008-09-05 16:13:11 -04003670
3671 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3672 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003673 if (ret == -ENOENT)
3674 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003675 else if (ret)
3676 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003677err:
3678 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003679 if (ret)
3680 goto out;
3681
3682 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003683 inode_inc_iversion(inode);
3684 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003685 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003686 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003687out:
Chris Mason39279cc2007-06-12 06:35:45 -04003688 return ret;
3689}
3690
Al Viro92986792011-03-04 17:14:37 +00003691int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3692 struct btrfs_root *root,
3693 struct inode *dir, struct inode *inode,
3694 const char *name, int name_len)
3695{
3696 int ret;
3697 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3698 if (!ret) {
3699 btrfs_drop_nlink(inode);
3700 ret = btrfs_update_inode(trans, root, inode);
3701 }
3702 return ret;
3703}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003704
3705/*
3706 * helper to start transaction for unlink and rmdir.
3707 *
Josef Bacikd52be812013-05-29 14:54:47 -04003708 * unlink and rmdir are special in btrfs, they do not always free space, so
3709 * if we cannot make our reservations the normal way try and see if there is
3710 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3711 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003712 */
Josef Bacikd52be812013-05-29 14:54:47 -04003713static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003714{
3715 struct btrfs_trans_handle *trans;
3716 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003717 int ret;
3718
Josef Bacike70bea52011-10-11 14:18:24 -04003719 /*
3720 * 1 for the possible orphan item
3721 * 1 for the dir item
3722 * 1 for the dir index
3723 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003724 * 1 for the inode
3725 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003726 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003727 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3728 return trans;
3729
Josef Bacikd52be812013-05-29 14:54:47 -04003730 if (PTR_ERR(trans) == -ENOSPC) {
3731 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003732
Josef Bacikd52be812013-05-29 14:54:47 -04003733 trans = btrfs_start_transaction(root, 0);
3734 if (IS_ERR(trans))
3735 return trans;
3736 ret = btrfs_cond_migrate_bytes(root->fs_info,
3737 &root->fs_info->trans_block_rsv,
3738 num_bytes, 5);
3739 if (ret) {
3740 btrfs_end_transaction(trans, root);
3741 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003742 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003743 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003744 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003745 }
Josef Bacikd52be812013-05-29 14:54:47 -04003746 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003747}
3748
Chris Mason39279cc2007-06-12 06:35:45 -04003749static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3750{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003751 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003752 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003753 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003754 int ret;
3755
Josef Bacikd52be812013-05-29 14:54:47 -04003756 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003757 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003758 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003759
Chris Mason12fcfd22009-03-24 10:24:20 -04003760 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3761
Chris Masone02119d2008-09-05 16:13:11 -04003762 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3763 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003764 if (ret)
3765 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003766
Yan, Zhenga22285a2010-05-16 10:48:46 -04003767 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003768 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003769 if (ret)
3770 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003771 }
Josef Bacik7b128762008-07-24 12:17:14 -04003772
Tsutomu Itohb5324022011-07-19 07:27:20 +00003773out:
Josef Bacikd52be812013-05-29 14:54:47 -04003774 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003775 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003776 return ret;
3777}
3778
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003779int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3780 struct btrfs_root *root,
3781 struct inode *dir, u64 objectid,
3782 const char *name, int name_len)
3783{
3784 struct btrfs_path *path;
3785 struct extent_buffer *leaf;
3786 struct btrfs_dir_item *di;
3787 struct btrfs_key key;
3788 u64 index;
3789 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003790 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003791
3792 path = btrfs_alloc_path();
3793 if (!path)
3794 return -ENOMEM;
3795
Li Zefan33345d012011-04-20 10:31:50 +08003796 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003797 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003798 if (IS_ERR_OR_NULL(di)) {
3799 if (!di)
3800 ret = -ENOENT;
3801 else
3802 ret = PTR_ERR(di);
3803 goto out;
3804 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003805
3806 leaf = path->nodes[0];
3807 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3808 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3809 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003810 if (ret) {
3811 btrfs_abort_transaction(trans, root, ret);
3812 goto out;
3813 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003814 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003815
3816 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3817 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003818 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003819 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003820 if (ret != -ENOENT) {
3821 btrfs_abort_transaction(trans, root, ret);
3822 goto out;
3823 }
Li Zefan33345d012011-04-20 10:31:50 +08003824 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003825 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003826 if (IS_ERR_OR_NULL(di)) {
3827 if (!di)
3828 ret = -ENOENT;
3829 else
3830 ret = PTR_ERR(di);
3831 btrfs_abort_transaction(trans, root, ret);
3832 goto out;
3833 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003834
3835 leaf = path->nodes[0];
3836 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003837 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003838 index = key.offset;
3839 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003840 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003841
Miao Xie16cdcec2011-04-22 18:12:22 +08003842 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003843 if (ret) {
3844 btrfs_abort_transaction(trans, root, ret);
3845 goto out;
3846 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003847
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003848 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003849 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003850 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003851 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003852 if (ret)
3853 btrfs_abort_transaction(trans, root, ret);
3854out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003855 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003856 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003857}
3858
Chris Mason39279cc2007-06-12 06:35:45 -04003859static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3860{
3861 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003862 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003863 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003864 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003865
David Sterbab3ae2442012-09-13 16:04:34 -06003866 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003867 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003868 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3869 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003870
Josef Bacikd52be812013-05-29 14:54:47 -04003871 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003872 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003873 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003874
Li Zefan33345d012011-04-20 10:31:50 +08003875 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003876 err = btrfs_unlink_subvol(trans, root, dir,
3877 BTRFS_I(inode)->location.objectid,
3878 dentry->d_name.name,
3879 dentry->d_name.len);
3880 goto out;
3881 }
3882
Josef Bacik7b128762008-07-24 12:17:14 -04003883 err = btrfs_orphan_add(trans, inode);
3884 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003885 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003886
Chris Mason39279cc2007-06-12 06:35:45 -04003887 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003888 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3889 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003890 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003891 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003892out:
Josef Bacikd52be812013-05-29 14:54:47 -04003893 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003894 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003895
Chris Mason39279cc2007-06-12 06:35:45 -04003896 return err;
3897}
3898
Chris Mason323ac952008-10-01 19:05:46 -04003899/*
Chris Mason39279cc2007-06-12 06:35:45 -04003900 * this can truncate away extent items, csum items and directory items.
3901 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003902 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003903 *
3904 * csum items that cross the new i_size are truncated to the new size
3905 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003906 *
3907 * min_type is the minimum key type to truncate down to. If set to 0, this
3908 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003909 */
Yan, Zheng80825102009-11-12 09:35:36 +00003910int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3911 struct btrfs_root *root,
3912 struct inode *inode,
3913 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003914{
Chris Mason39279cc2007-06-12 06:35:45 -04003915 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003916 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003917 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003918 struct btrfs_key key;
3919 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003920 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003921 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003922 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003923 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003924 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003925 int found_extent;
3926 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003927 int pending_del_nr = 0;
3928 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003929 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003930 int ret;
3931 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003932 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003933
3934 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003935
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003936 path = btrfs_alloc_path();
3937 if (!path)
3938 return -ENOMEM;
3939 path->reada = -1;
3940
Josef Bacik5dc562c2012-08-17 13:14:17 -04003941 /*
3942 * We want to drop from the next block forward in case this new size is
3943 * not block aligned since we will be keeping the last block of the
3944 * extent just the way it is.
3945 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003946 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00003947 btrfs_drop_extent_cache(inode, ALIGN(new_size,
3948 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003949
Miao Xie16cdcec2011-04-22 18:12:22 +08003950 /*
3951 * This function is also used to drop the items in the log tree before
3952 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3953 * it is used to drop the loged items. So we shouldn't kill the delayed
3954 * items.
3955 */
3956 if (min_type == 0 && root == BTRFS_I(inode)->root)
3957 btrfs_kill_delayed_inode_items(inode);
3958
Li Zefan33345d012011-04-20 10:31:50 +08003959 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003960 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003961 key.type = (u8)-1;
3962
Chris Mason85e21ba2008-01-29 15:11:36 -05003963search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003964 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003965 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003966 if (ret < 0) {
3967 err = ret;
3968 goto out;
3969 }
Chris Masond3977122009-01-05 21:25:51 -05003970
Chris Mason85e21ba2008-01-29 15:11:36 -05003971 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003972 /* there are no items in the tree for us to truncate, we're
3973 * done
3974 */
Yan, Zheng80825102009-11-12 09:35:36 +00003975 if (path->slots[0] == 0)
3976 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003977 path->slots[0]--;
3978 }
3979
Chris Masond3977122009-01-05 21:25:51 -05003980 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003981 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003982 leaf = path->nodes[0];
3983 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3984 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003985
Li Zefan33345d012011-04-20 10:31:50 +08003986 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003987 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003988
Chris Mason85e21ba2008-01-29 15:11:36 -05003989 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003990 break;
3991
Chris Mason5f39d392007-10-15 16:14:19 -04003992 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003993 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003994 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003995 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003996 extent_type = btrfs_file_extent_type(leaf, fi);
3997 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003998 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003999 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004000 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004001 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04004002 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004003 }
Yan008630c2007-11-07 13:31:09 -05004004 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004005 }
Yan, Zheng80825102009-11-12 09:35:36 +00004006 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004007 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004008 } else {
4009 if (item_end < new_size)
4010 break;
4011 if (found_key.offset >= new_size)
4012 del_item = 1;
4013 else
4014 del_item = 0;
4015 }
Chris Mason39279cc2007-06-12 06:35:45 -04004016 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004017 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004018 if (found_type != BTRFS_EXTENT_DATA_KEY)
4019 goto delete;
4020
4021 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004022 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004023 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004024 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004025 u64 orig_num_bytes =
4026 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004027 extent_num_bytes = ALIGN(new_size -
4028 found_key.offset,
4029 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004030 btrfs_set_file_extent_num_bytes(leaf, fi,
4031 extent_num_bytes);
4032 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004033 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004034 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004035 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004036 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004037 } else {
Chris Masondb945352007-10-15 16:15:53 -04004038 extent_num_bytes =
4039 btrfs_file_extent_disk_num_bytes(leaf,
4040 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004041 extent_offset = found_key.offset -
4042 btrfs_file_extent_offset(leaf, fi);
4043
Chris Mason39279cc2007-06-12 06:35:45 -04004044 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004045 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004046 if (extent_start != 0) {
4047 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004048 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004049 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004050 }
4051 }
Chris Mason90692182008-02-08 13:49:28 -05004052 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004053 /*
4054 * we can't truncate inline items that have had
4055 * special encodings
4056 */
4057 if (!del_item &&
4058 btrfs_file_extent_compression(leaf, fi) == 0 &&
4059 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4060 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004061 u32 size = new_size - found_key.offset;
4062
4063 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004064 inode_sub_bytes(inode, item_end + 1 -
4065 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004066 }
4067 size =
4068 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004069 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004070 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004071 inode_sub_bytes(inode, item_end + 1 -
4072 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004073 }
Chris Mason39279cc2007-06-12 06:35:45 -04004074 }
Chris Mason179e29e2007-11-01 11:28:41 -04004075delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004076 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004077 if (!pending_del_nr) {
4078 /* no pending yet, add ourselves */
4079 pending_del_slot = path->slots[0];
4080 pending_del_nr = 1;
4081 } else if (pending_del_nr &&
4082 path->slots[0] + 1 == pending_del_slot) {
4083 /* hop on the pending chunk */
4084 pending_del_nr++;
4085 pending_del_slot = path->slots[0];
4086 } else {
Chris Masond3977122009-01-05 21:25:51 -05004087 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004088 }
Chris Mason39279cc2007-06-12 06:35:45 -04004089 } else {
4090 break;
4091 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004092 if (found_extent && (root->ref_cows ||
4093 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004094 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004095 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004096 extent_num_bytes, 0,
4097 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004098 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004099 BUG_ON(ret);
4100 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004101
Yan, Zheng80825102009-11-12 09:35:36 +00004102 if (found_type == BTRFS_INODE_ITEM_KEY)
4103 break;
4104
4105 if (path->slots[0] == 0 ||
4106 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004107 if (pending_del_nr) {
4108 ret = btrfs_del_items(trans, root, path,
4109 pending_del_slot,
4110 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004111 if (ret) {
4112 btrfs_abort_transaction(trans,
4113 root, ret);
4114 goto error;
4115 }
Yan, Zheng80825102009-11-12 09:35:36 +00004116 pending_del_nr = 0;
4117 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004118 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004119 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004120 } else {
4121 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004122 }
Chris Mason39279cc2007-06-12 06:35:45 -04004123 }
Yan, Zheng80825102009-11-12 09:35:36 +00004124out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004125 if (pending_del_nr) {
4126 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4127 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004128 if (ret)
4129 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004130 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004131error:
Chris Mason39279cc2007-06-12 06:35:45 -04004132 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004133 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004134}
4135
Chris Masona52d9a82007-08-27 16:49:44 -04004136/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004137 * btrfs_truncate_page - read, zero a chunk and write a page
4138 * @inode - inode that we're zeroing
4139 * @from - the offset to start zeroing
4140 * @len - the length to zero, 0 to zero the entire range respective to the
4141 * offset
4142 * @front - zero up to the offset instead of from the offset on
4143 *
4144 * This will find the page for the "from" offset and cow the page and zero the
4145 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004146 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004147int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4148 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004149{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004150 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004151 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004152 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4153 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004154 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004155 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004156 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004157 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4158 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4159 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004160 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004161 int ret = 0;
4162 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004163 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004164
Josef Bacik2aaa6652012-08-29 14:27:18 -04004165 if ((offset & (blocksize - 1)) == 0 &&
4166 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004167 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004168 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004169 if (ret)
4170 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004171
Chris Mason211c17f2008-05-15 09:13:45 -04004172again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004173 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004174 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004175 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004176 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004177 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004178 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004179
4180 page_start = page_offset(page);
4181 page_end = page_start + PAGE_CACHE_SIZE - 1;
4182
Chris Masona52d9a82007-08-27 16:49:44 -04004183 if (!PageUptodate(page)) {
4184 ret = btrfs_readpage(NULL, page);
4185 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004186 if (page->mapping != mapping) {
4187 unlock_page(page);
4188 page_cache_release(page);
4189 goto again;
4190 }
Chris Masona52d9a82007-08-27 16:49:44 -04004191 if (!PageUptodate(page)) {
4192 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004193 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004194 }
4195 }
Chris Mason211c17f2008-05-15 09:13:45 -04004196 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004197
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004198 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004199 set_page_extent_mapped(page);
4200
4201 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4202 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004203 unlock_extent_cached(io_tree, page_start, page_end,
4204 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004205 unlock_page(page);
4206 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004207 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004208 btrfs_put_ordered_extent(ordered);
4209 goto again;
4210 }
4211
Josef Bacik2ac55d42010-02-03 19:33:23 +00004212 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004213 EXTENT_DIRTY | EXTENT_DELALLOC |
4214 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004215 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004216
Josef Bacik2ac55d42010-02-03 19:33:23 +00004217 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4218 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004219 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004220 unlock_extent_cached(io_tree, page_start, page_end,
4221 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004222 goto out_unlock;
4223 }
4224
Chris Masone6dcd2d2008-07-17 12:53:50 -04004225 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004226 if (!len)
4227 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004228 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004229 if (front)
4230 memset(kaddr, 0, offset);
4231 else
4232 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004233 flush_dcache_page(page);
4234 kunmap(page);
4235 }
Chris Mason247e7432008-07-17 12:53:51 -04004236 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004237 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004238 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4239 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004240
Chris Mason89642222008-07-24 09:41:53 -04004241out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004242 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004243 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004244 unlock_page(page);
4245 page_cache_release(page);
4246out:
4247 return ret;
4248}
4249
Josef Bacik695a0d02011-03-04 15:46:53 -05004250/*
4251 * This function puts in dummy file extents for the area we're creating a hole
4252 * for. So if we are truncating this file to a larger size we need to insert
4253 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4254 * the range between oldsize and size
4255 */
Josef Bacika41ad392011-01-31 15:30:16 -05004256int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004257{
4258 struct btrfs_trans_handle *trans;
4259 struct btrfs_root *root = BTRFS_I(inode)->root;
4260 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004261 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004262 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004263 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004264 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4265 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004266 u64 last_byte;
4267 u64 cur_offset;
4268 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004269 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004270
Josef Bacika71754f2013-06-17 17:14:39 -04004271 /*
4272 * If our size started in the middle of a page we need to zero out the
4273 * rest of the page before we expand the i_size, otherwise we could
4274 * expose stale data.
4275 */
4276 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4277 if (err)
4278 return err;
4279
Yan Zheng9036c102008-10-30 14:19:41 -04004280 if (size <= hole_start)
4281 return 0;
4282
Yan Zheng9036c102008-10-30 14:19:41 -04004283 while (1) {
4284 struct btrfs_ordered_extent *ordered;
4285 btrfs_wait_ordered_range(inode, hole_start,
4286 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004287 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004288 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004289 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4290 if (!ordered)
4291 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004292 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4293 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004294 btrfs_put_ordered_extent(ordered);
4295 }
4296
Yan Zheng9036c102008-10-30 14:19:41 -04004297 cur_offset = hole_start;
4298 while (1) {
4299 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4300 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004301 if (IS_ERR(em)) {
4302 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004303 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004304 break;
4305 }
Yan Zheng9036c102008-10-30 14:19:41 -04004306 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004307 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004308 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004309 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004310 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004311
Miao Xie36423202011-12-14 20:12:02 -05004312 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004313 if (IS_ERR(trans)) {
4314 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004315 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004316 }
Yan, Zheng80825102009-11-12 09:35:36 +00004317
Josef Bacik5dc562c2012-08-17 13:14:17 -04004318 err = btrfs_drop_extents(trans, root, inode,
4319 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004320 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004321 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004322 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004323 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004324 break;
Miao Xie5b397372011-09-11 10:52:24 -04004325 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004326
Yan Zheng9036c102008-10-30 14:19:41 -04004327 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004328 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004329 0, hole_size, 0, hole_size,
4330 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004331 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004332 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004333 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004334 break;
Miao Xie5b397372011-09-11 10:52:24 -04004335 }
Yan, Zheng80825102009-11-12 09:35:36 +00004336
Josef Bacik5dc562c2012-08-17 13:14:17 -04004337 btrfs_drop_extent_cache(inode, cur_offset,
4338 cur_offset + hole_size - 1, 0);
4339 hole_em = alloc_extent_map();
4340 if (!hole_em) {
4341 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4342 &BTRFS_I(inode)->runtime_flags);
4343 goto next;
4344 }
4345 hole_em->start = cur_offset;
4346 hole_em->len = hole_size;
4347 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004348
Josef Bacik5dc562c2012-08-17 13:14:17 -04004349 hole_em->block_start = EXTENT_MAP_HOLE;
4350 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004351 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004352 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004353 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4354 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4355 hole_em->generation = trans->transid;
4356
4357 while (1) {
4358 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004359 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004360 write_unlock(&em_tree->lock);
4361 if (err != -EEXIST)
4362 break;
4363 btrfs_drop_extent_cache(inode, cur_offset,
4364 cur_offset +
4365 hole_size - 1, 0);
4366 }
4367 free_extent_map(hole_em);
4368next:
Miao Xie36423202011-12-14 20:12:02 -05004369 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004370 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004371 }
4372 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004373 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004374 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004375 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004376 break;
4377 }
4378
Yan, Zhenga22285a2010-05-16 10:48:46 -04004379 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004380 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4381 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004382 return err;
4383}
4384
Eric Sandeen3972f262013-01-12 02:57:22 +00004385static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004386{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004387 struct btrfs_root *root = BTRFS_I(inode)->root;
4388 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004389 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004390 loff_t newsize = attr->ia_size;
4391 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004392 int ret;
4393
Josef Bacika41ad392011-01-31 15:30:16 -05004394 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00004395 return 0;
4396
Eric Sandeen3972f262013-01-12 02:57:22 +00004397 /*
4398 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4399 * special case where we need to update the times despite not having
4400 * these flags set. For all other operations the VFS set these flags
4401 * explicitly if it wants a timestamp update.
4402 */
4403 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4404 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4405
Josef Bacika41ad392011-01-31 15:30:16 -05004406 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004407 truncate_pagecache(inode, oldsize, newsize);
4408 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004409 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004410 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004411
Miao Xief4a2f4c2011-12-14 20:12:01 -05004412 trans = btrfs_start_transaction(root, 1);
4413 if (IS_ERR(trans))
4414 return PTR_ERR(trans);
4415
4416 i_size_write(inode, newsize);
4417 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4418 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004419 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004420 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004421
Josef Bacika41ad392011-01-31 15:30:16 -05004422 /*
4423 * We're truncating a file that used to have good data down to
4424 * zero. Make sure it gets into the ordered flush list so that
4425 * any new writes get down to disk quickly.
4426 */
4427 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004428 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4429 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004430
Josef Bacikf3fe8202013-01-07 17:03:21 -05004431 /*
4432 * 1 for the orphan item we're going to add
4433 * 1 for the orphan item deletion.
4434 */
4435 trans = btrfs_start_transaction(root, 2);
4436 if (IS_ERR(trans))
4437 return PTR_ERR(trans);
4438
4439 /*
4440 * We need to do this in case we fail at _any_ point during the
4441 * actual truncate. Once we do the truncate_setsize we could
4442 * invalidate pages which forces any outstanding ordered io to
4443 * be instantly completed which will give us extents that need
4444 * to be truncated. If we fail to get an orphan inode down we
4445 * could have left over extents that were never meant to live,
4446 * so we need to garuntee from this point on that everything
4447 * will be consistent.
4448 */
4449 ret = btrfs_orphan_add(trans, inode);
4450 btrfs_end_transaction(trans, root);
4451 if (ret)
4452 return ret;
4453
Josef Bacika41ad392011-01-31 15:30:16 -05004454 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4455 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004456
4457 /* Disable nonlocked read DIO to avoid the end less truncate */
4458 btrfs_inode_block_unlocked_dio(inode);
4459 inode_dio_wait(inode);
4460 btrfs_inode_resume_unlocked_dio(inode);
4461
Josef Bacika41ad392011-01-31 15:30:16 -05004462 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004463 if (ret && inode->i_nlink)
4464 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004465 }
4466
Josef Bacika41ad392011-01-31 15:30:16 -05004467 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004468}
4469
Chris Mason39279cc2007-06-12 06:35:45 -04004470static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4471{
4472 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004473 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004474 int err;
4475
Li Zefanb83cc962010-12-20 16:04:08 +08004476 if (btrfs_root_readonly(root))
4477 return -EROFS;
4478
Chris Mason39279cc2007-06-12 06:35:45 -04004479 err = inode_change_ok(inode, attr);
4480 if (err)
4481 return err;
4482
Chris Mason5a3f23d2009-03-31 13:27:11 -04004483 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004484 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004485 if (err)
4486 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004487 }
Yan Zheng9036c102008-10-30 14:19:41 -04004488
Christoph Hellwig10257742010-06-04 11:30:02 +02004489 if (attr->ia_valid) {
4490 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004491 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004492 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004493
Josef Bacik22c44fe2011-11-30 10:45:38 -05004494 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004495 err = btrfs_acl_chmod(inode);
4496 }
4497
Chris Mason39279cc2007-06-12 06:35:45 -04004498 return err;
4499}
Chris Mason61295eb2008-01-14 16:24:38 -05004500
Al Virobd555972010-06-07 11:35:40 -04004501void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004502{
4503 struct btrfs_trans_handle *trans;
4504 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004505 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004506 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004507 int ret;
4508
liubo1abe9b82011-03-24 11:18:59 +00004509 trace_btrfs_inode_evict(inode);
4510
Chris Mason39279cc2007-06-12 06:35:45 -04004511 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004512 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004513 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004514 goto no_delete;
4515
Chris Mason39279cc2007-06-12 06:35:45 -04004516 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004517 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004518 goto no_delete;
4519 }
Al Virobd555972010-06-07 11:35:40 -04004520 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004521 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004522
Yan, Zhengc71bf092009-11-12 09:34:40 +00004523 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004524 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004525 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004526 goto no_delete;
4527 }
4528
Yan, Zheng76dda932009-09-21 16:00:26 -04004529 if (inode->i_nlink > 0) {
4530 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4531 goto no_delete;
4532 }
4533
Miao Xie0e8c36a2012-12-19 06:59:51 +00004534 ret = btrfs_commit_inode_delayed_inode(inode);
4535 if (ret) {
4536 btrfs_orphan_del(NULL, inode);
4537 goto no_delete;
4538 }
4539
Miao Xie66d8f3d2012-09-06 04:02:28 -06004540 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004541 if (!rsv) {
4542 btrfs_orphan_del(NULL, inode);
4543 goto no_delete;
4544 }
Josef Bacik4a338542011-08-29 11:01:31 -04004545 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004546 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004547 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004548
Chris Masondbe674a2008-07-17 12:54:05 -04004549 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004550
Josef Bacik4289a662011-08-05 13:22:24 -04004551 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004552 * This is a bit simpler than btrfs_truncate since we've already
4553 * reserved our space for our orphan item in the unlink, so we just
4554 * need to reserve some slack space in case we add bytes and update
4555 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004556 */
Yan, Zheng80825102009-11-12 09:35:36 +00004557 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004558 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4559 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004560
Josef Bacik726c35f2011-09-26 15:46:06 -04004561 /*
4562 * Try and steal from the global reserve since we will
4563 * likely not use this space anyway, we want to try as
4564 * hard as possible to get this to work.
4565 */
4566 if (ret)
4567 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4568
Yan, Zhengd68fc572010-05-16 10:49:58 -04004569 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004570 btrfs_warn(root->fs_info,
4571 "Could not get space for a delete, will truncate on mount %d",
4572 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004573 btrfs_orphan_del(NULL, inode);
4574 btrfs_free_block_rsv(root, rsv);
4575 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004576 }
4577
Miao Xie0e8c36a2012-12-19 06:59:51 +00004578 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004579 if (IS_ERR(trans)) {
4580 btrfs_orphan_del(NULL, inode);
4581 btrfs_free_block_rsv(root, rsv);
4582 goto no_delete;
4583 }
4584
4585 trans->block_rsv = rsv;
4586
Yan, Zhengd68fc572010-05-16 10:49:58 -04004587 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004588 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004589 break;
4590
Miao Xie8407aa42012-09-07 01:43:32 -06004591 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004592 btrfs_end_transaction(trans, root);
4593 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004594 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004595 }
4596
Josef Bacik4289a662011-08-05 13:22:24 -04004597 btrfs_free_block_rsv(root, rsv);
4598
Yan, Zheng80825102009-11-12 09:35:36 +00004599 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004600 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004601 ret = btrfs_orphan_del(trans, inode);
4602 BUG_ON(ret);
4603 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004604
Josef Bacik4289a662011-08-05 13:22:24 -04004605 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004606 if (!(root == root->fs_info->tree_root ||
4607 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004608 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004609
Chris Mason54aa1f42007-06-22 14:16:25 -04004610 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004611 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004612no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004613 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004614 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004615 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004616}
4617
4618/*
4619 * this returns the key found in the dir entry in the location pointer.
4620 * If no dir entries were found, location->objectid is 0.
4621 */
4622static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4623 struct btrfs_key *location)
4624{
4625 const char *name = dentry->d_name.name;
4626 int namelen = dentry->d_name.len;
4627 struct btrfs_dir_item *di;
4628 struct btrfs_path *path;
4629 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004630 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004631
4632 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004633 if (!path)
4634 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004635
Li Zefan33345d012011-04-20 10:31:50 +08004636 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004637 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004638 if (IS_ERR(di))
4639 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004640
David Sterbac7040052011-04-19 18:00:01 +02004641 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004642 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004643
Chris Mason5f39d392007-10-15 16:14:19 -04004644 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004645out:
Chris Mason39279cc2007-06-12 06:35:45 -04004646 btrfs_free_path(path);
4647 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004648out_err:
4649 location->objectid = 0;
4650 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004651}
4652
4653/*
4654 * when we hit a tree root in a directory, the btrfs part of the inode
4655 * needs to be changed to reflect the root directory of the tree root. This
4656 * is kind of like crossing a mount point.
4657 */
4658static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004659 struct inode *dir,
4660 struct dentry *dentry,
4661 struct btrfs_key *location,
4662 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004663{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004664 struct btrfs_path *path;
4665 struct btrfs_root *new_root;
4666 struct btrfs_root_ref *ref;
4667 struct extent_buffer *leaf;
4668 int ret;
4669 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004670
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004671 path = btrfs_alloc_path();
4672 if (!path) {
4673 err = -ENOMEM;
4674 goto out;
4675 }
Chris Mason39279cc2007-06-12 06:35:45 -04004676
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004677 err = -ENOENT;
4678 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4679 BTRFS_I(dir)->root->root_key.objectid,
4680 location->objectid);
4681 if (ret) {
4682 if (ret < 0)
4683 err = ret;
4684 goto out;
4685 }
Chris Mason39279cc2007-06-12 06:35:45 -04004686
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004687 leaf = path->nodes[0];
4688 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004689 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004690 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4691 goto out;
4692
4693 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4694 (unsigned long)(ref + 1),
4695 dentry->d_name.len);
4696 if (ret)
4697 goto out;
4698
David Sterbab3b4aa72011-04-21 01:20:15 +02004699 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004700
4701 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4702 if (IS_ERR(new_root)) {
4703 err = PTR_ERR(new_root);
4704 goto out;
4705 }
4706
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004707 *sub_root = new_root;
4708 location->objectid = btrfs_root_dirid(&new_root->root_item);
4709 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004710 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004711 err = 0;
4712out:
4713 btrfs_free_path(path);
4714 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004715}
4716
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004717static void inode_tree_add(struct inode *inode)
4718{
4719 struct btrfs_root *root = BTRFS_I(inode)->root;
4720 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004721 struct rb_node **p;
4722 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004723 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004724
Al Viro1d3382cb2010-10-23 15:19:20 -04004725 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004726 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004727again:
4728 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004729 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004730 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004731 while (*p) {
4732 parent = *p;
4733 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4734
Li Zefan33345d012011-04-20 10:31:50 +08004735 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004736 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004737 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004738 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004739 else {
4740 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004741 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004742 rb_erase(parent, &root->inode_tree);
4743 RB_CLEAR_NODE(parent);
4744 spin_unlock(&root->inode_lock);
4745 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004746 }
4747 }
4748 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4749 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4750 spin_unlock(&root->inode_lock);
4751}
4752
4753static void inode_tree_del(struct inode *inode)
4754{
4755 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004756 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004757
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004758 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004759 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004760 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004761 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004762 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004763 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004764 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004765
Josef Bacik0af3d002010-06-21 14:48:16 -04004766 /*
4767 * Free space cache has inodes in the tree root, but the tree root has a
4768 * root_refs of 0, so this could end up dropping the tree root as a
4769 * snapshot, so we need the extra !root->fs_info->tree_root check to
4770 * make sure we don't drop it.
4771 */
4772 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4773 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004774 synchronize_srcu(&root->fs_info->subvol_srcu);
4775 spin_lock(&root->inode_lock);
4776 empty = RB_EMPTY_ROOT(&root->inode_tree);
4777 spin_unlock(&root->inode_lock);
4778 if (empty)
4779 btrfs_add_dead_root(root);
4780 }
4781}
4782
Jeff Mahoney143bede2012-03-01 14:56:26 +01004783void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004784{
4785 struct rb_node *node;
4786 struct rb_node *prev;
4787 struct btrfs_inode *entry;
4788 struct inode *inode;
4789 u64 objectid = 0;
4790
4791 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4792
4793 spin_lock(&root->inode_lock);
4794again:
4795 node = root->inode_tree.rb_node;
4796 prev = NULL;
4797 while (node) {
4798 prev = node;
4799 entry = rb_entry(node, struct btrfs_inode, rb_node);
4800
Li Zefan33345d012011-04-20 10:31:50 +08004801 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004802 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004803 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004804 node = node->rb_right;
4805 else
4806 break;
4807 }
4808 if (!node) {
4809 while (prev) {
4810 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004811 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004812 node = prev;
4813 break;
4814 }
4815 prev = rb_next(prev);
4816 }
4817 }
4818 while (node) {
4819 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004820 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004821 inode = igrab(&entry->vfs_inode);
4822 if (inode) {
4823 spin_unlock(&root->inode_lock);
4824 if (atomic_read(&inode->i_count) > 1)
4825 d_prune_aliases(inode);
4826 /*
Al Viro45321ac2010-06-07 13:43:19 -04004827 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004828 * the inode cache when its usage count
4829 * hits zero.
4830 */
4831 iput(inode);
4832 cond_resched();
4833 spin_lock(&root->inode_lock);
4834 goto again;
4835 }
4836
4837 if (cond_resched_lock(&root->inode_lock))
4838 goto again;
4839
4840 node = rb_next(node);
4841 }
4842 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004843}
4844
Chris Masone02119d2008-09-05 16:13:11 -04004845static int btrfs_init_locked_inode(struct inode *inode, void *p)
4846{
4847 struct btrfs_iget_args *args = p;
4848 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004849 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004850 return 0;
4851}
4852
4853static int btrfs_find_actor(struct inode *inode, void *opaque)
4854{
4855 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004856 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004857 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004858}
4859
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004860static struct inode *btrfs_iget_locked(struct super_block *s,
4861 u64 objectid,
4862 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004863{
4864 struct inode *inode;
4865 struct btrfs_iget_args args;
4866 args.ino = objectid;
4867 args.root = root;
4868
4869 inode = iget5_locked(s, objectid, btrfs_find_actor,
4870 btrfs_init_locked_inode,
4871 (void *)&args);
4872 return inode;
4873}
4874
Balaji Rao1a54ef82008-07-21 02:01:04 +05304875/* Get an inode object given its location and corresponding root.
4876 * Returns in *is_new if the inode was read from disk
4877 */
4878struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004879 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304880{
4881 struct inode *inode;
4882
4883 inode = btrfs_iget_locked(s, location->objectid, root);
4884 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004885 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304886
4887 if (inode->i_state & I_NEW) {
4888 BTRFS_I(inode)->root = root;
4889 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4890 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004891 if (!is_bad_inode(inode)) {
4892 inode_tree_add(inode);
4893 unlock_new_inode(inode);
4894 if (new)
4895 *new = 1;
4896 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004897 unlock_new_inode(inode);
4898 iput(inode);
4899 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004900 }
4901 }
4902
Balaji Rao1a54ef82008-07-21 02:01:04 +05304903 return inode;
4904}
4905
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004906static struct inode *new_simple_dir(struct super_block *s,
4907 struct btrfs_key *key,
4908 struct btrfs_root *root)
4909{
4910 struct inode *inode = new_inode(s);
4911
4912 if (!inode)
4913 return ERR_PTR(-ENOMEM);
4914
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004915 BTRFS_I(inode)->root = root;
4916 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004917 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004918
4919 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004920 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004921 inode->i_fop = &simple_dir_operations;
4922 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4923 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4924
4925 return inode;
4926}
4927
Chris Mason3de45862008-11-17 21:02:50 -05004928struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004929{
Chris Masond3977122009-01-05 21:25:51 -05004930 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004931 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004932 struct btrfs_root *sub_root = root;
4933 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004934 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004935 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004936
4937 if (dentry->d_name.len > BTRFS_NAME_LEN)
4938 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004939
Jeff Layton39e3c952012-11-28 11:30:53 -05004940 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04004941 if (ret < 0)
4942 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004943
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004944 if (location.objectid == 0)
4945 return NULL;
4946
4947 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004948 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004949 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004950 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004951
4952 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4953
Yan, Zheng76dda932009-09-21 16:00:26 -04004954 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004955 ret = fixup_tree_root_location(root, dir, dentry,
4956 &location, &sub_root);
4957 if (ret < 0) {
4958 if (ret != -ENOENT)
4959 inode = ERR_PTR(ret);
4960 else
4961 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4962 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004963 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004964 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004965 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4966
Julia Lawall34d19ba2011-01-24 19:55:19 +00004967 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004968 down_read(&root->fs_info->cleanup_work_sem);
4969 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004970 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004971 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04004972 if (ret) {
4973 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004974 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04004975 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00004976 }
4977
Chris Mason3de45862008-11-17 21:02:50 -05004978 return inode;
4979}
4980
Nick Pigginfe15ce42011-01-07 17:49:23 +11004981static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004982{
4983 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004984 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004985
Li Zefan848cce02012-02-21 17:04:28 +08004986 if (!inode && !IS_ROOT(dentry))
4987 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004988
Li Zefan848cce02012-02-21 17:04:28 +08004989 if (inode) {
4990 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004991 if (btrfs_root_refs(&root->root_item) == 0)
4992 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004993
4994 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4995 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004996 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004997 return 0;
4998}
4999
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005000static void btrfs_dentry_release(struct dentry *dentry)
5001{
5002 if (dentry->d_fsdata)
5003 kfree(dentry->d_fsdata);
5004}
5005
Chris Mason3de45862008-11-17 21:02:50 -05005006static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005007 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005008{
Josef Bacika66e7cc2011-09-18 10:34:03 -04005009 struct dentry *ret;
5010
5011 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005012 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005013}
5014
Miao Xie16cdcec2011-04-22 18:12:22 +08005015unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005016 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5017};
5018
Al Viro9cdda8d2013-05-22 16:48:09 -04005019static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005020{
Al Viro9cdda8d2013-05-22 16:48:09 -04005021 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005022 struct btrfs_root *root = BTRFS_I(inode)->root;
5023 struct btrfs_item *item;
5024 struct btrfs_dir_item *di;
5025 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005026 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005027 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005028 struct list_head ins_list;
5029 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005030 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005031 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005032 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005033 unsigned char d_type;
5034 int over = 0;
5035 u32 di_cur;
5036 u32 di_total;
5037 u32 di_len;
5038 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005039 char tmp_name[32];
5040 char *name_ptr;
5041 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005042 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005043
5044 /* FIXME, use a real flag for deciding about the key type */
5045 if (root->fs_info->tree_root == root)
5046 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005047
Al Viro9cdda8d2013-05-22 16:48:09 -04005048 if (!dir_emit_dots(file, ctx))
5049 return 0;
5050
David Woodhouse49593bf2008-08-17 17:08:36 +01005051 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005052 if (!path)
5053 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005054
Josef Bacik026fd312011-05-13 10:32:11 -04005055 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005056
Miao Xie16cdcec2011-04-22 18:12:22 +08005057 if (key_type == BTRFS_DIR_INDEX_KEY) {
5058 INIT_LIST_HEAD(&ins_list);
5059 INIT_LIST_HEAD(&del_list);
5060 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5061 }
5062
Chris Mason39279cc2007-06-12 06:35:45 -04005063 btrfs_set_key_type(&key, key_type);
Al Viro9cdda8d2013-05-22 16:48:09 -04005064 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005065 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005066
Chris Mason39279cc2007-06-12 06:35:45 -04005067 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5068 if (ret < 0)
5069 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005070
5071 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005072 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005073 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005074 if (slot >= btrfs_header_nritems(leaf)) {
5075 ret = btrfs_next_leaf(root, path);
5076 if (ret < 0)
5077 goto err;
5078 else if (ret > 0)
5079 break;
5080 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005081 }
Chris Mason3de45862008-11-17 21:02:50 -05005082
Chris Mason5f39d392007-10-15 16:14:19 -04005083 item = btrfs_item_nr(leaf, slot);
5084 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5085
5086 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005087 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005088 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005089 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005090 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005091 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005092 if (key_type == BTRFS_DIR_INDEX_KEY &&
5093 btrfs_should_delete_dir_index(&del_list,
5094 found_key.offset))
5095 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005096
Al Viro9cdda8d2013-05-22 16:48:09 -04005097 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005098 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005099
Chris Mason39279cc2007-06-12 06:35:45 -04005100 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5101 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005102 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005103
5104 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005105 struct btrfs_key location;
5106
Josef Bacik22a94d42011-03-16 16:47:17 -04005107 if (verify_dir_item(root, leaf, di))
5108 break;
5109
Chris Mason5f39d392007-10-15 16:14:19 -04005110 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005111 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005112 name_ptr = tmp_name;
5113 } else {
5114 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005115 if (!name_ptr) {
5116 ret = -ENOMEM;
5117 goto err;
5118 }
Chris Mason5f39d392007-10-15 16:14:19 -04005119 }
5120 read_extent_buffer(leaf, name_ptr,
5121 (unsigned long)(di + 1), name_len);
5122
5123 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5124 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005125
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005126
Chris Mason3de45862008-11-17 21:02:50 -05005127 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005128 * skip it.
5129 *
5130 * In contrast to old kernels, we insert the snapshot's
5131 * dir item and dir index after it has been created, so
5132 * we won't find a reference to our own snapshot. We
5133 * still keep the following code for backward
5134 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005135 */
5136 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5137 location.objectid == root->root_key.objectid) {
5138 over = 0;
5139 goto skip;
5140 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005141 over = !dir_emit(ctx, name_ptr, name_len,
5142 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005143
Chris Mason3de45862008-11-17 21:02:50 -05005144skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005145 if (name_ptr != tmp_name)
5146 kfree(name_ptr);
5147
Chris Mason39279cc2007-06-12 06:35:45 -04005148 if (over)
5149 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005150 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005151 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005152 di_cur += di_len;
5153 di = (struct btrfs_dir_item *)((char *)di + di_len);
5154 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005155next:
5156 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005157 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005158
Miao Xie16cdcec2011-04-22 18:12:22 +08005159 if (key_type == BTRFS_DIR_INDEX_KEY) {
5160 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005161 ctx->pos++;
5162 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005163 if (ret)
5164 goto nopos;
5165 }
5166
David Woodhouse49593bf2008-08-17 17:08:36 +01005167 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05005168 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00005169 /*
5170 * 32-bit glibc will use getdents64, but then strtol -
5171 * so the last number we can serve is this.
5172 */
Al Viro9cdda8d2013-05-22 16:48:09 -04005173 ctx->pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05005174 else
Al Viro9cdda8d2013-05-22 16:48:09 -04005175 ctx->pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04005176nopos:
5177 ret = 0;
5178err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005179 if (key_type == BTRFS_DIR_INDEX_KEY)
5180 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005181 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005182 return ret;
5183}
5184
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005185int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005186{
5187 struct btrfs_root *root = BTRFS_I(inode)->root;
5188 struct btrfs_trans_handle *trans;
5189 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005190 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005191
Josef Bacik72ac3c02012-05-23 14:13:11 -04005192 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005193 return 0;
5194
Liu Bo83eea1f2012-07-10 05:28:39 -06005195 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005196 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005197
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005198 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005199 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005200 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005201 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005202 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005203 if (IS_ERR(trans))
5204 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005205 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005206 }
5207 return ret;
5208}
5209
5210/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005211 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005212 * inode changes. But, it is most likely to find the inode in cache.
5213 * FIXME, needs more benchmarking...there are no reasons other than performance
5214 * to keep or drop this code.
5215 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005216static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005217{
5218 struct btrfs_root *root = BTRFS_I(inode)->root;
5219 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005220 int ret;
5221
Josef Bacik72ac3c02012-05-23 14:13:11 -04005222 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005223 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005224
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005225 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005226 if (IS_ERR(trans))
5227 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005228
5229 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005230 if (ret && ret == -ENOSPC) {
5231 /* whoops, lets try again with the full transaction */
5232 btrfs_end_transaction(trans, root);
5233 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005234 if (IS_ERR(trans))
5235 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005236
Chris Mason94b60442010-05-26 11:02:00 -04005237 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005238 }
Chris Mason39279cc2007-06-12 06:35:45 -04005239 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005240 if (BTRFS_I(inode)->delayed_node)
5241 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005242
5243 return ret;
5244}
5245
5246/*
5247 * This is a copy of file_update_time. We need this so we can return error on
5248 * ENOSPC for updating the inode in the case of file write and mmap writes.
5249 */
Josef Bacike41f9412012-03-26 09:46:47 -04005250static int btrfs_update_time(struct inode *inode, struct timespec *now,
5251 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005252{
Alexander Block2bc5565282012-06-15 09:49:33 +02005253 struct btrfs_root *root = BTRFS_I(inode)->root;
5254
5255 if (btrfs_root_readonly(root))
5256 return -EROFS;
5257
Josef Bacike41f9412012-03-26 09:46:47 -04005258 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005259 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005260 if (flags & S_CTIME)
5261 inode->i_ctime = *now;
5262 if (flags & S_MTIME)
5263 inode->i_mtime = *now;
5264 if (flags & S_ATIME)
5265 inode->i_atime = *now;
5266 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005267}
5268
Chris Masond352ac62008-09-29 15:18:18 -04005269/*
5270 * find the highest existing sequence number in a directory
5271 * and then set the in-memory index_cnt variable to reflect
5272 * free sequence numbers
5273 */
Josef Bacikaec74772008-07-24 12:12:38 -04005274static int btrfs_set_inode_index_count(struct inode *inode)
5275{
5276 struct btrfs_root *root = BTRFS_I(inode)->root;
5277 struct btrfs_key key, found_key;
5278 struct btrfs_path *path;
5279 struct extent_buffer *leaf;
5280 int ret;
5281
Li Zefan33345d012011-04-20 10:31:50 +08005282 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005283 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5284 key.offset = (u64)-1;
5285
5286 path = btrfs_alloc_path();
5287 if (!path)
5288 return -ENOMEM;
5289
5290 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5291 if (ret < 0)
5292 goto out;
5293 /* FIXME: we should be able to handle this */
5294 if (ret == 0)
5295 goto out;
5296 ret = 0;
5297
5298 /*
5299 * MAGIC NUMBER EXPLANATION:
5300 * since we search a directory based on f_pos we have to start at 2
5301 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5302 * else has to start at 2
5303 */
5304 if (path->slots[0] == 0) {
5305 BTRFS_I(inode)->index_cnt = 2;
5306 goto out;
5307 }
5308
5309 path->slots[0]--;
5310
5311 leaf = path->nodes[0];
5312 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5313
Li Zefan33345d012011-04-20 10:31:50 +08005314 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005315 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5316 BTRFS_I(inode)->index_cnt = 2;
5317 goto out;
5318 }
5319
5320 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5321out:
5322 btrfs_free_path(path);
5323 return ret;
5324}
5325
Chris Masond352ac62008-09-29 15:18:18 -04005326/*
5327 * helper to find a free sequence number in a given directory. This current
5328 * code is very simple, later versions will do smarter things in the btree
5329 */
Chris Mason3de45862008-11-17 21:02:50 -05005330int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005331{
5332 int ret = 0;
5333
5334 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005335 ret = btrfs_inode_delayed_dir_index_count(dir);
5336 if (ret) {
5337 ret = btrfs_set_inode_index_count(dir);
5338 if (ret)
5339 return ret;
5340 }
Josef Bacikaec74772008-07-24 12:12:38 -04005341 }
5342
Chris Mason00e4e6b2008-08-05 11:18:09 -04005343 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005344 BTRFS_I(dir)->index_cnt++;
5345
5346 return ret;
5347}
5348
Chris Mason39279cc2007-06-12 06:35:45 -04005349static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5350 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005351 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005352 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005353 u64 ref_objectid, u64 objectid,
5354 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005355{
5356 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005357 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005358 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005359 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005360 struct btrfs_inode_ref *ref;
5361 struct btrfs_key key[2];
5362 u32 sizes[2];
5363 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005364 int ret;
5365 int owner;
5366
Chris Mason5f39d392007-10-15 16:14:19 -04005367 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005368 if (!path)
5369 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005370
Chris Mason39279cc2007-06-12 06:35:45 -04005371 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005372 if (!inode) {
5373 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005374 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005375 }
Chris Mason39279cc2007-06-12 06:35:45 -04005376
Li Zefan581bb052011-04-20 10:06:11 +08005377 /*
5378 * we have to initialize this early, so we can reclaim the inode
5379 * number if we fail afterwards in this function.
5380 */
5381 inode->i_ino = objectid;
5382
Josef Bacikaec74772008-07-24 12:12:38 -04005383 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005384 trace_btrfs_inode_request(dir);
5385
Chris Mason3de45862008-11-17 21:02:50 -05005386 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005387 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005388 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005389 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005390 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005391 }
Josef Bacikaec74772008-07-24 12:12:38 -04005392 }
5393 /*
5394 * index_cnt is ignored for everything but a dir,
5395 * btrfs_get_inode_index_count has an explanation for the magic
5396 * number
5397 */
5398 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005399 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005400 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005401 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005402
Josef Bacik5dc562c2012-08-17 13:14:17 -04005403 /*
5404 * We could have gotten an inode number from somebody who was fsynced
5405 * and then removed in this same transaction, so let's just set full
5406 * sync since it will be a full sync anyway and this will blow away the
5407 * old info in the log.
5408 */
5409 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5410
Al Viro569254b2011-07-24 17:08:40 -04005411 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005412 owner = 0;
5413 else
5414 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005415
5416 key[0].objectid = objectid;
5417 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5418 key[0].offset = 0;
5419
Mark Fashehf1863732012-08-08 11:32:27 -07005420 /*
5421 * Start new inodes with an inode_ref. This is slightly more
5422 * efficient for small numbers of hard links since they will
5423 * be packed into one item. Extended refs will kick in if we
5424 * add more hard links than can fit in the ref item.
5425 */
Chris Mason9c583092008-01-29 15:15:18 -05005426 key[1].objectid = objectid;
5427 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5428 key[1].offset = ref_objectid;
5429
5430 sizes[0] = sizeof(struct btrfs_inode_item);
5431 sizes[1] = name_len + sizeof(*ref);
5432
Chris Masonb9473432009-03-13 11:00:37 -04005433 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005434 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5435 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005436 goto fail;
5437
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005438 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005439 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005440 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005441 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5442 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005443 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5444 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005445 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005446
5447 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5448 struct btrfs_inode_ref);
5449 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005450 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005451 ptr = (unsigned long)(ref + 1);
5452 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5453
Chris Mason5f39d392007-10-15 16:14:19 -04005454 btrfs_mark_buffer_dirty(path->nodes[0]);
5455 btrfs_free_path(path);
5456
Chris Mason39279cc2007-06-12 06:35:45 -04005457 location = &BTRFS_I(inode)->location;
5458 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005459 location->offset = 0;
5460 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5461
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005462 btrfs_inherit_iflags(inode, dir);
5463
Al Viro569254b2011-07-24 17:08:40 -04005464 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005465 if (btrfs_test_opt(root, NODATASUM))
5466 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005467 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005468 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5469 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005470 }
5471
Chris Mason39279cc2007-06-12 06:35:45 -04005472 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005473 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005474
5475 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005476 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005477
Alexander Block8ea05e32012-07-25 17:35:53 +02005478 btrfs_update_root_times(trans, root);
5479
Chris Mason39279cc2007-06-12 06:35:45 -04005480 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005481fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005482 if (dir)
5483 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005484 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005485 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005486 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005487}
5488
5489static inline u8 btrfs_inode_type(struct inode *inode)
5490{
5491 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5492}
5493
Chris Masond352ac62008-09-29 15:18:18 -04005494/*
5495 * utility function to add 'inode' into 'parent_inode' with
5496 * a give name and a given sequence number.
5497 * if 'add_backref' is true, also insert a backref from the
5498 * inode to the parent directory.
5499 */
Chris Masone02119d2008-09-05 16:13:11 -04005500int btrfs_add_link(struct btrfs_trans_handle *trans,
5501 struct inode *parent_inode, struct inode *inode,
5502 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005503{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005504 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005505 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005506 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005507 u64 ino = btrfs_ino(inode);
5508 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005509
Li Zefan33345d012011-04-20 10:31:50 +08005510 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005511 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5512 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005513 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005514 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5515 key.offset = 0;
5516 }
Chris Mason39279cc2007-06-12 06:35:45 -04005517
Li Zefan33345d012011-04-20 10:31:50 +08005518 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005519 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5520 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005521 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005522 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005523 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5524 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005525 }
5526
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005527 /* Nothing to clean up yet */
5528 if (ret)
5529 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005530
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005531 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5532 parent_inode, &key,
5533 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005534 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005535 goto fail_dir_item;
5536 else if (ret) {
5537 btrfs_abort_transaction(trans, root, ret);
5538 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005539 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005540
5541 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5542 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005543 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005544 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5545 ret = btrfs_update_inode(trans, root, parent_inode);
5546 if (ret)
5547 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005548 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005549
5550fail_dir_item:
5551 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5552 u64 local_index;
5553 int err;
5554 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5555 key.objectid, root->root_key.objectid,
5556 parent_ino, &local_index, name, name_len);
5557
5558 } else if (add_backref) {
5559 u64 local_index;
5560 int err;
5561
5562 err = btrfs_del_inode_ref(trans, root, name, name_len,
5563 ino, parent_ino, &local_index);
5564 }
5565 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005566}
5567
5568static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005569 struct inode *dir, struct dentry *dentry,
5570 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005571{
Josef Bacika1b075d2010-11-19 20:36:11 +00005572 int err = btrfs_add_link(trans, dir, inode,
5573 dentry->d_name.name, dentry->d_name.len,
5574 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005575 if (err > 0)
5576 err = -EEXIST;
5577 return err;
5578}
5579
Josef Bacik618e21d2007-07-11 10:18:17 -04005580static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005581 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005582{
5583 struct btrfs_trans_handle *trans;
5584 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005585 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005586 int err;
5587 int drop_inode = 0;
5588 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005589 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005590
5591 if (!new_valid_dev(rdev))
5592 return -EINVAL;
5593
Josef Bacik9ed74f22009-09-11 16:12:44 -04005594 /*
5595 * 2 for inode item and ref
5596 * 2 for dir items
5597 * 1 for xattr if selinux is on
5598 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005599 trans = btrfs_start_transaction(root, 5);
5600 if (IS_ERR(trans))
5601 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005602
Li Zefan581bb052011-04-20 10:06:11 +08005603 err = btrfs_find_free_ino(root, &objectid);
5604 if (err)
5605 goto out_unlock;
5606
Josef Bacikaec74772008-07-24 12:12:38 -04005607 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005608 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005609 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005610 if (IS_ERR(inode)) {
5611 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005612 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005613 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005614
Eric Paris2a7dba32011-02-01 11:05:39 -05005615 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005616 if (err) {
5617 drop_inode = 1;
5618 goto out_unlock;
5619 }
5620
Casey Schauflerad19db72011-12-15 10:09:07 -05005621 /*
5622 * If the active LSM wants to access the inode during
5623 * d_instantiate it needs these. Smack checks to see
5624 * if the filesystem supports xattrs by looking at the
5625 * ops vector.
5626 */
5627
5628 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005629 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005630 if (err)
5631 drop_inode = 1;
5632 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005633 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005634 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005635 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005636 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005637out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005638 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005639 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005640 if (drop_inode) {
5641 inode_dec_link_count(inode);
5642 iput(inode);
5643 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005644 return err;
5645}
5646
Chris Mason39279cc2007-06-12 06:35:45 -04005647static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005648 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005649{
5650 struct btrfs_trans_handle *trans;
5651 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005652 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005653 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005654 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005655 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005656 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005657
Josef Bacik9ed74f22009-09-11 16:12:44 -04005658 /*
5659 * 2 for inode item and ref
5660 * 2 for dir items
5661 * 1 for xattr if selinux is on
5662 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005663 trans = btrfs_start_transaction(root, 5);
5664 if (IS_ERR(trans))
5665 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005666
Li Zefan581bb052011-04-20 10:06:11 +08005667 err = btrfs_find_free_ino(root, &objectid);
5668 if (err)
5669 goto out_unlock;
5670
Josef Bacikaec74772008-07-24 12:12:38 -04005671 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005672 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005673 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005674 if (IS_ERR(inode)) {
5675 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005676 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005677 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005678 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005679
Eric Paris2a7dba32011-02-01 11:05:39 -05005680 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005681 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005682 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005683
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005684 err = btrfs_update_inode(trans, root, inode);
5685 if (err)
5686 goto out_unlock;
5687
Casey Schauflerad19db72011-12-15 10:09:07 -05005688 /*
5689 * If the active LSM wants to access the inode during
5690 * d_instantiate it needs these. Smack checks to see
5691 * if the filesystem supports xattrs by looking at the
5692 * ops vector.
5693 */
5694 inode->i_fop = &btrfs_file_operations;
5695 inode->i_op = &btrfs_file_inode_operations;
5696
Josef Bacika1b075d2010-11-19 20:36:11 +00005697 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005698 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005699 goto out_unlock;
5700
5701 inode->i_mapping->a_ops = &btrfs_aops;
5702 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5703 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5704 d_instantiate(dentry, inode);
5705
Chris Mason39279cc2007-06-12 06:35:45 -04005706out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005707 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005708 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005709 inode_dec_link_count(inode);
5710 iput(inode);
5711 }
Liu Bob53d3f52012-11-14 14:34:34 +00005712 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005713 return err;
5714}
5715
5716static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5717 struct dentry *dentry)
5718{
5719 struct btrfs_trans_handle *trans;
5720 struct btrfs_root *root = BTRFS_I(dir)->root;
5721 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005722 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005723 int err;
5724 int drop_inode = 0;
5725
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005726 /* do not allow sys_link's with other subvols of the same device */
5727 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005728 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005729
Mark Fashehf1863732012-08-08 11:32:27 -07005730 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005731 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005732
Chris Mason3de45862008-11-17 21:02:50 -05005733 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005734 if (err)
5735 goto fail;
5736
Yan, Zhenga22285a2010-05-16 10:48:46 -04005737 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005738 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005739 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005740 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005741 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005742 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005743 if (IS_ERR(trans)) {
5744 err = PTR_ERR(trans);
5745 goto fail;
5746 }
Chris Mason5f39d392007-10-15 16:14:19 -04005747
Miao Xie31534952011-04-13 13:19:21 +08005748 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005749 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005750 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005751 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005752 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005753
Josef Bacika1b075d2010-11-19 20:36:11 +00005754 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005755
Yan, Zhenga5719522009-09-24 09:17:31 -04005756 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005757 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005758 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005759 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005760 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005761 if (err)
5762 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005763 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005764 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005765 }
Chris Mason39279cc2007-06-12 06:35:45 -04005766
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005767 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005768fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005769 if (drop_inode) {
5770 inode_dec_link_count(inode);
5771 iput(inode);
5772 }
Liu Bob53d3f52012-11-14 14:34:34 +00005773 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005774 return err;
5775}
5776
Al Viro18bb1db2011-07-26 01:41:39 -04005777static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005778{
Chris Masonb9d86662008-05-02 16:13:49 -04005779 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005780 struct btrfs_trans_handle *trans;
5781 struct btrfs_root *root = BTRFS_I(dir)->root;
5782 int err = 0;
5783 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005784 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005785 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005786
Josef Bacik9ed74f22009-09-11 16:12:44 -04005787 /*
5788 * 2 items for inode and ref
5789 * 2 items for dir items
5790 * 1 for xattr if selinux is on
5791 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005792 trans = btrfs_start_transaction(root, 5);
5793 if (IS_ERR(trans))
5794 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005795
Li Zefan581bb052011-04-20 10:06:11 +08005796 err = btrfs_find_free_ino(root, &objectid);
5797 if (err)
5798 goto out_fail;
5799
Josef Bacikaec74772008-07-24 12:12:38 -04005800 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005801 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005802 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005803 if (IS_ERR(inode)) {
5804 err = PTR_ERR(inode);
5805 goto out_fail;
5806 }
Chris Mason5f39d392007-10-15 16:14:19 -04005807
Chris Mason39279cc2007-06-12 06:35:45 -04005808 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005809
Eric Paris2a7dba32011-02-01 11:05:39 -05005810 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005811 if (err)
5812 goto out_fail;
5813
Chris Mason39279cc2007-06-12 06:35:45 -04005814 inode->i_op = &btrfs_dir_inode_operations;
5815 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005816
Chris Masondbe674a2008-07-17 12:54:05 -04005817 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005818 err = btrfs_update_inode(trans, root, inode);
5819 if (err)
5820 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005821
Josef Bacika1b075d2010-11-19 20:36:11 +00005822 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5823 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005824 if (err)
5825 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005826
Chris Mason39279cc2007-06-12 06:35:45 -04005827 d_instantiate(dentry, inode);
5828 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005829
5830out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005831 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005832 if (drop_on_err)
5833 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005834 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005835 return err;
5836}
5837
Chris Masond352ac62008-09-29 15:18:18 -04005838/* helper for btfs_get_extent. Given an existing extent in the tree,
5839 * and an extent that you want to insert, deal with overlap and insert
5840 * the new extent into the tree.
5841 */
Chris Mason3b951512008-04-17 11:29:12 -04005842static int merge_extent_mapping(struct extent_map_tree *em_tree,
5843 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005844 struct extent_map *em,
5845 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005846{
5847 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005848
Chris Masone6dcd2d2008-07-17 12:53:50 -04005849 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5850 start_diff = map_start - em->start;
5851 em->start = map_start;
5852 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005853 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5854 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005855 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005856 em->block_len -= start_diff;
5857 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005858 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005859}
5860
Chris Masonc8b97812008-10-29 14:49:59 -04005861static noinline int uncompress_inline(struct btrfs_path *path,
5862 struct inode *inode, struct page *page,
5863 size_t pg_offset, u64 extent_offset,
5864 struct btrfs_file_extent_item *item)
5865{
5866 int ret;
5867 struct extent_buffer *leaf = path->nodes[0];
5868 char *tmp;
5869 size_t max_size;
5870 unsigned long inline_size;
5871 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005872 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005873
5874 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005875 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005876 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5877 inline_size = btrfs_file_extent_inline_item_len(leaf,
5878 btrfs_item_nr(leaf, path->slots[0]));
5879 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005880 if (!tmp)
5881 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005882 ptr = btrfs_file_extent_inline_start(item);
5883
5884 read_extent_buffer(leaf, tmp, ptr, inline_size);
5885
Chris Mason5b050f02008-11-11 09:34:41 -05005886 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005887 ret = btrfs_decompress(compress_type, tmp, page,
5888 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005889 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005890 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005891 unsigned long copy_size = min_t(u64,
5892 PAGE_CACHE_SIZE - pg_offset,
5893 max_size - extent_offset);
5894 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005895 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005896 }
5897 kfree(tmp);
5898 return 0;
5899}
5900
Chris Masond352ac62008-09-29 15:18:18 -04005901/*
5902 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005903 * the ugly parts come from merging extents from the disk with the in-ram
5904 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005905 * where the in-ram extents might be locked pending data=ordered completion.
5906 *
5907 * This also copies inline extents directly into the page.
5908 */
Chris Masond3977122009-01-05 21:25:51 -05005909
Chris Masona52d9a82007-08-27 16:49:44 -04005910struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005911 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005912 int create)
5913{
5914 int ret;
5915 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005916 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005917 u64 extent_start = 0;
5918 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005919 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005920 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005921 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005922 struct btrfs_root *root = BTRFS_I(inode)->root;
5923 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005924 struct extent_buffer *leaf;
5925 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005926 struct extent_map *em = NULL;
5927 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005928 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005929 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005930 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005931
Chris Masona52d9a82007-08-27 16:49:44 -04005932again:
Chris Mason890871b2009-09-02 16:24:52 -04005933 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005934 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005935 if (em)
5936 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005937 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005938
Chris Masona52d9a82007-08-27 16:49:44 -04005939 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005940 if (em->start > start || em->start + em->len <= start)
5941 free_extent_map(em);
5942 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005943 free_extent_map(em);
5944 else
5945 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005946 }
David Sterba172ddd62011-04-21 00:48:27 +02005947 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005948 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005949 err = -ENOMEM;
5950 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005951 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005952 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005953 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005954 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005955 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005956 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005957
5958 if (!path) {
5959 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005960 if (!path) {
5961 err = -ENOMEM;
5962 goto out;
5963 }
5964 /*
5965 * Chances are we'll be called again, so go ahead and do
5966 * readahead
5967 */
5968 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005969 }
5970
Chris Mason179e29e2007-11-01 11:28:41 -04005971 ret = btrfs_lookup_file_extent(trans, root, path,
5972 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005973 if (ret < 0) {
5974 err = ret;
5975 goto out;
5976 }
5977
5978 if (ret != 0) {
5979 if (path->slots[0] == 0)
5980 goto not_found;
5981 path->slots[0]--;
5982 }
5983
Chris Mason5f39d392007-10-15 16:14:19 -04005984 leaf = path->nodes[0];
5985 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005986 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005987 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005988 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5989 found_type = btrfs_key_type(&found_key);
5990 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005991 found_type != BTRFS_EXTENT_DATA_KEY) {
5992 goto not_found;
5993 }
5994
Chris Mason5f39d392007-10-15 16:14:19 -04005995 found_type = btrfs_file_extent_type(leaf, item);
5996 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005997 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005998 if (found_type == BTRFS_FILE_EXTENT_REG ||
5999 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006000 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006001 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006002 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6003 size_t size;
6004 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006005 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006006 }
6007
6008 if (start >= extent_end) {
6009 path->slots[0]++;
6010 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6011 ret = btrfs_next_leaf(root, path);
6012 if (ret < 0) {
6013 err = ret;
6014 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006015 }
Yan Zheng9036c102008-10-30 14:19:41 -04006016 if (ret > 0)
6017 goto not_found;
6018 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006019 }
Yan Zheng9036c102008-10-30 14:19:41 -04006020 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6021 if (found_key.objectid != objectid ||
6022 found_key.type != BTRFS_EXTENT_DATA_KEY)
6023 goto not_found;
6024 if (start + len <= found_key.offset)
6025 goto not_found;
6026 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006027 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006028 em->len = found_key.offset - start;
6029 goto not_found_em;
6030 }
6031
Josef Bacikcc95bef2013-04-04 14:31:27 -04006032 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006033 if (found_type == BTRFS_FILE_EXTENT_REG ||
6034 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006035 em->start = extent_start;
6036 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006037 em->orig_start = extent_start -
6038 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006039 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6040 item);
Chris Masondb945352007-10-15 16:15:53 -04006041 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6042 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006043 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006044 goto insert;
6045 }
Li Zefan261507a02010-12-17 14:21:50 +08006046 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006047 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006048 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006049 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006050 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006051 } else {
6052 bytenr += btrfs_file_extent_offset(leaf, item);
6053 em->block_start = bytenr;
6054 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006055 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6056 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006057 }
Chris Masona52d9a82007-08-27 16:49:44 -04006058 goto insert;
6059 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006060 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006061 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006062 size_t size;
6063 size_t extent_offset;
6064 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006065
Chris Masona52d9a82007-08-27 16:49:44 -04006066 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006067 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006068 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006069 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006070 goto out;
6071 }
6072
Yan Zheng9036c102008-10-30 14:19:41 -04006073 size = btrfs_file_extent_inline_len(leaf, item);
6074 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006075 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006076 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006077 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006078 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006079 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006080 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006081 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006082 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006083 em->compress_type = compress_type;
6084 }
Yan689f9342007-10-29 11:41:07 -04006085 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006086 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006087 if (btrfs_file_extent_compression(leaf, item) !=
6088 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006089 ret = uncompress_inline(path, inode, page,
6090 pg_offset,
6091 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006092 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006093 } else {
6094 map = kmap(page);
6095 read_extent_buffer(leaf, map + pg_offset, ptr,
6096 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006097 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6098 memset(map + pg_offset + copy_size, 0,
6099 PAGE_CACHE_SIZE - pg_offset -
6100 copy_size);
6101 }
Chris Masonc8b97812008-10-29 14:49:59 -04006102 kunmap(page);
6103 }
Chris Mason179e29e2007-11-01 11:28:41 -04006104 flush_dcache_page(page);
6105 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006106 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006107 if (!trans) {
6108 kunmap(page);
6109 free_extent_map(em);
6110 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006111
David Sterbab3b4aa72011-04-21 01:20:15 +02006112 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006113 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006114
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006115 if (IS_ERR(trans))
6116 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006117 goto again;
6118 }
Chris Masonc8b97812008-10-29 14:49:59 -04006119 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006120 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006121 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006122 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006123 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006124 }
Chris Masond1310b22008-01-24 16:13:08 -05006125 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006126 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006127 goto insert;
6128 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006129 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006130 }
6131not_found:
6132 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006133 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006134 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006135not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006136 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006137 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006138insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006139 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006140 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006141 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6142 (unsigned long long)em->start,
6143 (unsigned long long)em->len,
6144 (unsigned long long)start,
6145 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006146 err = -EIO;
6147 goto out;
6148 }
Chris Masond1310b22008-01-24 16:13:08 -05006149
6150 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006151 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006152 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006153 /* it is possible that someone inserted the extent into the tree
6154 * while we had the lock dropped. It is also possible that
6155 * an overlapping map exists in the tree
6156 */
Chris Masona52d9a82007-08-27 16:49:44 -04006157 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006158 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006159
6160 ret = 0;
6161
Chris Mason3b951512008-04-17 11:29:12 -04006162 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006163 if (existing && (existing->start > start ||
6164 existing->start + existing->len <= start)) {
6165 free_extent_map(existing);
6166 existing = NULL;
6167 }
Chris Mason3b951512008-04-17 11:29:12 -04006168 if (!existing) {
6169 existing = lookup_extent_mapping(em_tree, em->start,
6170 em->len);
6171 if (existing) {
6172 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006173 em, start,
6174 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006175 free_extent_map(existing);
6176 if (err) {
6177 free_extent_map(em);
6178 em = NULL;
6179 }
6180 } else {
6181 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006182 free_extent_map(em);
6183 em = NULL;
6184 }
6185 } else {
6186 free_extent_map(em);
6187 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006188 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006189 }
Chris Masona52d9a82007-08-27 16:49:44 -04006190 }
Chris Mason890871b2009-09-02 16:24:52 -04006191 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006192out:
liubo1abe9b82011-03-24 11:18:59 +00006193
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006194 if (em)
6195 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006196
Chris Masonf4219502008-07-22 11:18:09 -04006197 if (path)
6198 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006199 if (trans) {
6200 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006201 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006202 err = ret;
6203 }
Chris Masona52d9a82007-08-27 16:49:44 -04006204 if (err) {
6205 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006206 return ERR_PTR(err);
6207 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006208 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006209 return em;
6210}
6211
Chris Masonec29ed52011-02-23 16:23:20 -05006212struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6213 size_t pg_offset, u64 start, u64 len,
6214 int create)
6215{
6216 struct extent_map *em;
6217 struct extent_map *hole_em = NULL;
6218 u64 range_start = start;
6219 u64 end;
6220 u64 found;
6221 u64 found_end;
6222 int err = 0;
6223
6224 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6225 if (IS_ERR(em))
6226 return em;
6227 if (em) {
6228 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006229 * if our em maps to
6230 * - a hole or
6231 * - a pre-alloc extent,
6232 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006233 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006234 if (em->block_start != EXTENT_MAP_HOLE &&
6235 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006236 return em;
6237 else
6238 hole_em = em;
6239 }
6240
6241 /* check to see if we've wrapped (len == -1 or similar) */
6242 end = start + len;
6243 if (end < start)
6244 end = (u64)-1;
6245 else
6246 end -= 1;
6247
6248 em = NULL;
6249
6250 /* ok, we didn't find anything, lets look for delalloc */
6251 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6252 end, len, EXTENT_DELALLOC, 1);
6253 found_end = range_start + found;
6254 if (found_end < range_start)
6255 found_end = (u64)-1;
6256
6257 /*
6258 * we didn't find anything useful, return
6259 * the original results from get_extent()
6260 */
6261 if (range_start > end || found_end <= start) {
6262 em = hole_em;
6263 hole_em = NULL;
6264 goto out;
6265 }
6266
6267 /* adjust the range_start to make sure it doesn't
6268 * go backwards from the start they passed in
6269 */
6270 range_start = max(start,range_start);
6271 found = found_end - range_start;
6272
6273 if (found > 0) {
6274 u64 hole_start = start;
6275 u64 hole_len = len;
6276
David Sterba172ddd62011-04-21 00:48:27 +02006277 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006278 if (!em) {
6279 err = -ENOMEM;
6280 goto out;
6281 }
6282 /*
6283 * when btrfs_get_extent can't find anything it
6284 * returns one huge hole
6285 *
6286 * make sure what it found really fits our range, and
6287 * adjust to make sure it is based on the start from
6288 * the caller
6289 */
6290 if (hole_em) {
6291 u64 calc_end = extent_map_end(hole_em);
6292
6293 if (calc_end <= start || (hole_em->start > end)) {
6294 free_extent_map(hole_em);
6295 hole_em = NULL;
6296 } else {
6297 hole_start = max(hole_em->start, start);
6298 hole_len = calc_end - hole_start;
6299 }
6300 }
6301 em->bdev = NULL;
6302 if (hole_em && range_start > hole_start) {
6303 /* our hole starts before our delalloc, so we
6304 * have to return just the parts of the hole
6305 * that go until the delalloc starts
6306 */
6307 em->len = min(hole_len,
6308 range_start - hole_start);
6309 em->start = hole_start;
6310 em->orig_start = hole_start;
6311 /*
6312 * don't adjust block start at all,
6313 * it is fixed at EXTENT_MAP_HOLE
6314 */
6315 em->block_start = hole_em->block_start;
6316 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006317 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6318 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006319 } else {
6320 em->start = range_start;
6321 em->len = found;
6322 em->orig_start = range_start;
6323 em->block_start = EXTENT_MAP_DELALLOC;
6324 em->block_len = found;
6325 }
6326 } else if (hole_em) {
6327 return hole_em;
6328 }
6329out:
6330
6331 free_extent_map(hole_em);
6332 if (err) {
6333 free_extent_map(em);
6334 return ERR_PTR(err);
6335 }
6336 return em;
6337}
6338
Josef Bacik4b46fce2010-05-23 11:00:55 -04006339static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6340 u64 start, u64 len)
6341{
6342 struct btrfs_root *root = BTRFS_I(inode)->root;
6343 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006344 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006345 struct btrfs_key ins;
6346 u64 alloc_hint;
6347 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006348
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006349 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006350 if (IS_ERR(trans))
6351 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006352
6353 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6354
6355 alloc_hint = get_extent_allocation_hint(inode, start, len);
6356 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006357 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006358 if (ret) {
6359 em = ERR_PTR(ret);
6360 goto out;
6361 }
6362
Josef Bacik70c8a912012-10-11 16:54:30 -04006363 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006364 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik70c8a912012-10-11 16:54:30 -04006365 if (IS_ERR(em))
6366 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006367
6368 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6369 ins.offset, ins.offset, 0);
6370 if (ret) {
6371 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6372 em = ERR_PTR(ret);
6373 }
6374out:
6375 btrfs_end_transaction(trans, root);
6376 return em;
6377}
6378
Chris Mason46bfbb52010-05-26 11:04:10 -04006379/*
6380 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6381 * block must be cow'd
6382 */
Josef Bacik7ee9e442013-06-21 16:37:03 -04006383noinline int can_nocow_extent(struct btrfs_trans_handle *trans,
6384 struct inode *inode, u64 offset, u64 *len,
6385 u64 *orig_start, u64 *orig_block_len,
6386 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006387{
6388 struct btrfs_path *path;
6389 int ret;
6390 struct extent_buffer *leaf;
6391 struct btrfs_root *root = BTRFS_I(inode)->root;
6392 struct btrfs_file_extent_item *fi;
6393 struct btrfs_key key;
6394 u64 disk_bytenr;
6395 u64 backref_offset;
6396 u64 extent_end;
6397 u64 num_bytes;
6398 int slot;
6399 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006400 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Chris Mason46bfbb52010-05-26 11:04:10 -04006401 path = btrfs_alloc_path();
6402 if (!path)
6403 return -ENOMEM;
6404
Li Zefan33345d012011-04-20 10:31:50 +08006405 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006406 offset, 0);
6407 if (ret < 0)
6408 goto out;
6409
6410 slot = path->slots[0];
6411 if (ret == 1) {
6412 if (slot == 0) {
6413 /* can't find the item, must cow */
6414 ret = 0;
6415 goto out;
6416 }
6417 slot--;
6418 }
6419 ret = 0;
6420 leaf = path->nodes[0];
6421 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006422 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006423 key.type != BTRFS_EXTENT_DATA_KEY) {
6424 /* not our file or wrong item type, must cow */
6425 goto out;
6426 }
6427
6428 if (key.offset > offset) {
6429 /* Wrong offset, must cow */
6430 goto out;
6431 }
6432
6433 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6434 found_type = btrfs_file_extent_type(leaf, fi);
6435 if (found_type != BTRFS_FILE_EXTENT_REG &&
6436 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6437 /* not a regular extent, must cow */
6438 goto out;
6439 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006440
6441 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6442 goto out;
6443
Chris Mason46bfbb52010-05-26 11:04:10 -04006444 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006445 if (disk_bytenr == 0)
6446 goto out;
6447
6448 if (btrfs_file_extent_compression(leaf, fi) ||
6449 btrfs_file_extent_encryption(leaf, fi) ||
6450 btrfs_file_extent_other_encoding(leaf, fi))
6451 goto out;
6452
Chris Mason46bfbb52010-05-26 11:04:10 -04006453 backref_offset = btrfs_file_extent_offset(leaf, fi);
6454
Josef Bacik7ee9e442013-06-21 16:37:03 -04006455 if (orig_start) {
6456 *orig_start = key.offset - backref_offset;
6457 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6458 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6459 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006460
Chris Mason46bfbb52010-05-26 11:04:10 -04006461 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason46bfbb52010-05-26 11:04:10 -04006462
6463 if (btrfs_extent_readonly(root, disk_bytenr))
6464 goto out;
6465
6466 /*
6467 * look for other files referencing this extent, if we
6468 * find any we must cow
6469 */
Li Zefan33345d012011-04-20 10:31:50 +08006470 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006471 key.offset - backref_offset, disk_bytenr))
6472 goto out;
6473
6474 /*
6475 * adjust disk_bytenr and num_bytes to cover just the bytes
6476 * in this extent we are about to write. If there
6477 * are any csums in that range we have to cow in order
6478 * to keep the csums correct
6479 */
6480 disk_bytenr += backref_offset;
6481 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006482 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006483 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6484 goto out;
6485 /*
6486 * all of the above have passed, it is safe to overwrite this extent
6487 * without cow
6488 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006489 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006490 ret = 1;
6491out:
6492 btrfs_free_path(path);
6493 return ret;
6494}
6495
Josef Bacikeb838e72012-07-31 16:28:48 -04006496static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6497 struct extent_state **cached_state, int writing)
6498{
6499 struct btrfs_ordered_extent *ordered;
6500 int ret = 0;
6501
6502 while (1) {
6503 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6504 0, cached_state);
6505 /*
6506 * We're concerned with the entire range that we're going to be
6507 * doing DIO to, so we need to make sure theres no ordered
6508 * extents in this range.
6509 */
6510 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6511 lockend - lockstart + 1);
6512
6513 /*
6514 * We need to make sure there are no buffered pages in this
6515 * range either, we could have raced between the invalidate in
6516 * generic_file_direct_write and locking the extent. The
6517 * invalidate needs to happen so that reads after a write do not
6518 * get stale data.
6519 */
6520 if (!ordered && (!writing ||
6521 !test_range_bit(&BTRFS_I(inode)->io_tree,
6522 lockstart, lockend, EXTENT_UPTODATE, 0,
6523 *cached_state)))
6524 break;
6525
6526 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6527 cached_state, GFP_NOFS);
6528
6529 if (ordered) {
6530 btrfs_start_ordered_extent(inode, ordered, 1);
6531 btrfs_put_ordered_extent(ordered);
6532 } else {
6533 /* Screw you mmap */
6534 ret = filemap_write_and_wait_range(inode->i_mapping,
6535 lockstart,
6536 lockend);
6537 if (ret)
6538 break;
6539
6540 /*
6541 * If we found a page that couldn't be invalidated just
6542 * fall back to buffered.
6543 */
6544 ret = invalidate_inode_pages2_range(inode->i_mapping,
6545 lockstart >> PAGE_CACHE_SHIFT,
6546 lockend >> PAGE_CACHE_SHIFT);
6547 if (ret)
6548 break;
6549 }
6550
6551 cond_resched();
6552 }
6553
6554 return ret;
6555}
6556
Josef Bacik69ffb542012-09-11 15:40:07 -04006557static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6558 u64 len, u64 orig_start,
6559 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006560 u64 orig_block_len, u64 ram_bytes,
6561 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006562{
6563 struct extent_map_tree *em_tree;
6564 struct extent_map *em;
6565 struct btrfs_root *root = BTRFS_I(inode)->root;
6566 int ret;
6567
6568 em_tree = &BTRFS_I(inode)->extent_tree;
6569 em = alloc_extent_map();
6570 if (!em)
6571 return ERR_PTR(-ENOMEM);
6572
6573 em->start = start;
6574 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006575 em->mod_start = start;
6576 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006577 em->len = len;
6578 em->block_len = block_len;
6579 em->block_start = block_start;
6580 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006581 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006582 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006583 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006584 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6585 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006586 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006587
6588 do {
6589 btrfs_drop_extent_cache(inode, em->start,
6590 em->start + em->len - 1, 0);
6591 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006592 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006593 write_unlock(&em_tree->lock);
6594 } while (ret == -EEXIST);
6595
6596 if (ret) {
6597 free_extent_map(em);
6598 return ERR_PTR(ret);
6599 }
6600
6601 return em;
6602}
6603
6604
Josef Bacik4b46fce2010-05-23 11:00:55 -04006605static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6606 struct buffer_head *bh_result, int create)
6607{
6608 struct extent_map *em;
6609 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006610 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006611 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006612 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006613 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006614 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006615 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006616 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006617
Miao Xie172a5042013-02-21 02:48:22 -07006618 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006619 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006620 else
Josef Bacikc3298612012-08-03 16:49:19 -04006621 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006622
Josef Bacikc3298612012-08-03 16:49:19 -04006623 lockstart = start;
6624 lockend = start + len - 1;
6625
Josef Bacikeb838e72012-07-31 16:28:48 -04006626 /*
6627 * If this errors out it's because we couldn't invalidate pagecache for
6628 * this range and we need to fallback to buffered.
6629 */
6630 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6631 return -ENOTBLK;
6632
Josef Bacik4b46fce2010-05-23 11:00:55 -04006633 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006634 if (IS_ERR(em)) {
6635 ret = PTR_ERR(em);
6636 goto unlock_err;
6637 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006638
6639 /*
6640 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6641 * io. INLINE is special, and we could probably kludge it in here, but
6642 * it's still buffered so for safety lets just fall back to the generic
6643 * buffered path.
6644 *
6645 * For COMPRESSED we _have_ to read the entire extent in so we can
6646 * decompress it, so there will be buffering required no matter what we
6647 * do, so go ahead and fallback to buffered.
6648 *
6649 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6650 * to buffered IO. Don't blame me, this is the price we pay for using
6651 * the generic code.
6652 */
6653 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6654 em->block_start == EXTENT_MAP_INLINE) {
6655 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006656 ret = -ENOTBLK;
6657 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006658 }
6659
6660 /* Just a good old fashioned hole, return */
6661 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6662 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6663 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006664 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006665 }
6666
6667 /*
6668 * We don't allocate a new extent in the following cases
6669 *
6670 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6671 * existing extent.
6672 * 2) The extent is marked as PREALLOC. We're good to go here and can
6673 * just use the extent.
6674 *
6675 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006676 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006677 len = min(len, em->len - (start - em->start));
6678 lockstart = start + len;
6679 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006680 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006681
6682 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6683 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6684 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006685 int type;
6686 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006687 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006688
6689 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6690 type = BTRFS_ORDERED_PREALLOC;
6691 else
6692 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006693 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006694 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006695
6696 /*
6697 * we're not going to log anything, but we do need
6698 * to make sure the current transaction stays open
6699 * while we look for nocow cross refs
6700 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006701 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006702 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006703 goto must_cow;
6704
Josef Bacik7ee9e442013-06-21 16:37:03 -04006705 if (can_nocow_extent(trans, inode, start, &len, &orig_start,
6706 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006707 if (type == BTRFS_ORDERED_PREALLOC) {
6708 free_extent_map(em);
6709 em = create_pinned_em(inode, start, len,
6710 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006711 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006712 orig_block_len,
6713 ram_bytes, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006714 if (IS_ERR(em)) {
6715 btrfs_end_transaction(trans, root);
6716 goto unlock_err;
6717 }
6718 }
6719
Chris Mason46bfbb52010-05-26 11:04:10 -04006720 ret = btrfs_add_ordered_extent_dio(inode, start,
6721 block_start, len, len, type);
6722 btrfs_end_transaction(trans, root);
6723 if (ret) {
6724 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006725 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006726 }
6727 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006728 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006729 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006730 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006731must_cow:
6732 /*
6733 * this will cow the extent, reset the len in case we changed
6734 * it above
6735 */
6736 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006737 free_extent_map(em);
6738 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006739 if (IS_ERR(em)) {
6740 ret = PTR_ERR(em);
6741 goto unlock_err;
6742 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006743 len = min(len, em->len - (start - em->start));
6744unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006745 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6746 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006747 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006748 bh_result->b_bdev = em->bdev;
6749 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006750 if (create) {
6751 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6752 set_buffer_new(bh_result);
6753
6754 /*
6755 * Need to update the i_size under the extent lock so buffered
6756 * readers will get the updated i_size when we unlock.
6757 */
6758 if (start + len > i_size_read(inode))
6759 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006760
Miao Xie172a5042013-02-21 02:48:22 -07006761 spin_lock(&BTRFS_I(inode)->lock);
6762 BTRFS_I(inode)->outstanding_extents++;
6763 spin_unlock(&BTRFS_I(inode)->lock);
6764
Miao Xie09348562013-02-07 10:12:07 +00006765 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6766 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6767 &cached_state, GFP_NOFS);
6768 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006769 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006770
Josef Bacikeb838e72012-07-31 16:28:48 -04006771 /*
6772 * In the case of write we need to clear and unlock the entire range,
6773 * in the case of read we need to unlock only the end area that we
6774 * aren't using if there is any left over space.
6775 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006776 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006777 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6778 lockend, unlock_bits, 1, 0,
6779 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006780 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006781 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006782 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006783
Josef Bacik4b46fce2010-05-23 11:00:55 -04006784 free_extent_map(em);
6785
6786 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006787
6788unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006789 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6790 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6791 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006792}
6793
6794struct btrfs_dio_private {
6795 struct inode *inode;
6796 u64 logical_offset;
6797 u64 disk_bytenr;
6798 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006799 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006800
6801 /* number of bios pending for this dio */
6802 atomic_t pending_bios;
6803
6804 /* IO errors */
6805 int errors;
6806
Chris Mason9be33952013-05-17 18:30:14 -04006807 /* orig_bio is our btrfs_io_bio */
Miao Xiee65e1532010-11-22 03:04:43 +00006808 struct bio *orig_bio;
Chris Mason9be33952013-05-17 18:30:14 -04006809
6810 /* dio_bio came from fs/direct-io.c */
6811 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006812};
6813
6814static void btrfs_endio_direct_read(struct bio *bio, int err)
6815{
Miao Xiee65e1532010-11-22 03:04:43 +00006816 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006817 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6818 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006819 struct inode *inode = dip->inode;
6820 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006821 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006822 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006823
6824 start = dip->logical_offset;
6825 do {
6826 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6827 struct page *page = bvec->bv_page;
6828 char *kaddr;
6829 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006830 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006831 unsigned long flags;
6832
Josef Bacikc3298612012-08-03 16:49:19 -04006833 if (get_state_private(&BTRFS_I(inode)->io_tree,
6834 start, &private))
6835 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006836 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006837 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006838 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006839 csum, bvec->bv_len);
6840 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006841 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006842 local_irq_restore(flags);
6843
6844 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006845 if (csum != private) {
6846failed:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006847 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u private %u",
6848 (unsigned long long)btrfs_ino(inode),
6849 (unsigned long long)start,
6850 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006851 err = -EIO;
6852 }
6853 }
6854
6855 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006856 bvec++;
6857 } while (bvec <= bvec_end);
6858
6859 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006860 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04006861 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006862
Josef Bacik4b46fce2010-05-23 11:00:55 -04006863 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006864
6865 /* If we had a csum failure make sure to clear the uptodate flag */
6866 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006867 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6868 dio_end_io(dio_bio, err);
6869 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006870}
6871
6872static void btrfs_endio_direct_write(struct bio *bio, int err)
6873{
6874 struct btrfs_dio_private *dip = bio->bi_private;
6875 struct inode *inode = dip->inode;
6876 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006877 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006878 u64 ordered_offset = dip->logical_offset;
6879 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04006880 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006881 int ret;
6882
6883 if (err)
6884 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006885again:
6886 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6887 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006888 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006889 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006890 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006891
Josef Bacik5fd02042012-05-02 14:00:54 -04006892 ordered->work.func = finish_ordered_fn;
6893 ordered->work.flags = 0;
6894 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6895 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006896out_test:
6897 /*
6898 * our bio might span multiple ordered extents. If we haven't
6899 * completed the accounting for the whole dio, go back and try again
6900 */
6901 if (ordered_offset < dip->logical_offset + dip->bytes) {
6902 ordered_bytes = dip->logical_offset + dip->bytes -
6903 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006904 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006905 goto again;
6906 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006907out_done:
Chris Mason9be33952013-05-17 18:30:14 -04006908 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006909
Josef Bacik4b46fce2010-05-23 11:00:55 -04006910 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006911
6912 /* If we had an error make sure to clear the uptodate flag */
6913 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006914 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6915 dio_end_io(dio_bio, err);
6916 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006917}
6918
Chris Masoneaf25d92010-05-25 09:48:28 -04006919static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6920 struct bio *bio, int mirror_num,
6921 unsigned long bio_flags, u64 offset)
6922{
6923 int ret;
6924 struct btrfs_root *root = BTRFS_I(inode)->root;
6925 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006926 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006927 return 0;
6928}
6929
Miao Xiee65e1532010-11-22 03:04:43 +00006930static void btrfs_end_dio_bio(struct bio *bio, int err)
6931{
6932 struct btrfs_dio_private *dip = bio->bi_private;
6933
6934 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006935 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006936 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006937 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006938 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006939 dip->errors = 1;
6940
6941 /*
6942 * before atomic variable goto zero, we must make sure
6943 * dip->errors is perceived to be set.
6944 */
6945 smp_mb__before_atomic_dec();
6946 }
6947
6948 /* if there are more bios still pending for this dio, just exit */
6949 if (!atomic_dec_and_test(&dip->pending_bios))
6950 goto out;
6951
Chris Mason9be33952013-05-17 18:30:14 -04006952 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00006953 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04006954 } else {
6955 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00006956 bio_endio(dip->orig_bio, 0);
6957 }
6958out:
6959 bio_put(bio);
6960}
6961
6962static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6963 u64 first_sector, gfp_t gfp_flags)
6964{
6965 int nr_vecs = bio_get_nr_vecs(bdev);
6966 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6967}
6968
6969static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6970 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006971 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006972{
6973 int write = rw & REQ_WRITE;
6974 struct btrfs_root *root = BTRFS_I(inode)->root;
6975 int ret;
6976
Josef Bacikb812ce22012-11-16 13:56:32 -05006977 if (async_submit)
6978 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6979
Miao Xiee65e1532010-11-22 03:04:43 +00006980 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006981
6982 if (!write) {
6983 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6984 if (ret)
6985 goto err;
6986 }
Miao Xiee65e1532010-11-22 03:04:43 +00006987
Josef Bacik1ae39932011-04-06 14:41:34 -04006988 if (skip_sum)
6989 goto map;
6990
6991 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006992 ret = btrfs_wq_submit_bio(root->fs_info,
6993 inode, rw, bio, 0, 0,
6994 file_offset,
6995 __btrfs_submit_bio_start_direct_io,
6996 __btrfs_submit_bio_done);
6997 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006998 } else if (write) {
6999 /*
7000 * If we aren't doing async submit, calculate the csum of the
7001 * bio now.
7002 */
7003 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7004 if (ret)
7005 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007006 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04007007 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007008 if (ret)
7009 goto err;
7010 }
Miao Xiee65e1532010-11-22 03:04:43 +00007011
Josef Bacik1ae39932011-04-06 14:41:34 -04007012map:
7013 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007014err:
7015 bio_put(bio);
7016 return ret;
7017}
7018
7019static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7020 int skip_sum)
7021{
7022 struct inode *inode = dip->inode;
7023 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007024 struct bio *bio;
7025 struct bio *orig_bio = dip->orig_bio;
7026 struct bio_vec *bvec = orig_bio->bi_io_vec;
7027 u64 start_sector = orig_bio->bi_sector;
7028 u64 file_offset = dip->logical_offset;
7029 u64 submit_len = 0;
7030 u64 map_length;
7031 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007032 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007033 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007034
Miao Xiee65e1532010-11-22 03:04:43 +00007035 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007036 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007037 &map_length, NULL, 0);
7038 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007039 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007040 return -EIO;
7041 }
Josef Bacik02f57c72011-04-06 14:25:44 -04007042 if (map_length >= orig_bio->bi_size) {
7043 bio = orig_bio;
7044 goto submit;
7045 }
7046
David Woodhouse53b381b2013-01-29 18:40:14 -05007047 /* async crcs make it difficult to collect full stripe writes. */
7048 if (btrfs_get_alloc_profile(root, 1) &
7049 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7050 async_submit = 0;
7051 else
7052 async_submit = 1;
7053
Josef Bacik02f57c72011-04-06 14:25:44 -04007054 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7055 if (!bio)
7056 return -ENOMEM;
7057 bio->bi_private = dip;
7058 bio->bi_end_io = btrfs_end_dio_bio;
7059 atomic_inc(&dip->pending_bios);
7060
Miao Xiee65e1532010-11-22 03:04:43 +00007061 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7062 if (unlikely(map_length < submit_len + bvec->bv_len ||
7063 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7064 bvec->bv_offset) < bvec->bv_len)) {
7065 /*
7066 * inc the count before we submit the bio so
7067 * we know the end IO handler won't happen before
7068 * we inc the count. Otherwise, the dip might get freed
7069 * before we're done setting it up
7070 */
7071 atomic_inc(&dip->pending_bios);
7072 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7073 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007074 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007075 if (ret) {
7076 bio_put(bio);
7077 atomic_dec(&dip->pending_bios);
7078 goto out_err;
7079 }
7080
Miao Xiee65e1532010-11-22 03:04:43 +00007081 start_sector += submit_len >> 9;
7082 file_offset += submit_len;
7083
7084 submit_len = 0;
7085 nr_pages = 0;
7086
7087 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7088 start_sector, GFP_NOFS);
7089 if (!bio)
7090 goto out_err;
7091 bio->bi_private = dip;
7092 bio->bi_end_io = btrfs_end_dio_bio;
7093
7094 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007095 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007096 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007097 &map_length, NULL, 0);
7098 if (ret) {
7099 bio_put(bio);
7100 goto out_err;
7101 }
7102 } else {
7103 submit_len += bvec->bv_len;
7104 nr_pages ++;
7105 bvec++;
7106 }
7107 }
7108
Josef Bacik02f57c72011-04-06 14:25:44 -04007109submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007110 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007111 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007112 if (!ret)
7113 return 0;
7114
7115 bio_put(bio);
7116out_err:
7117 dip->errors = 1;
7118 /*
7119 * before atomic variable goto zero, we must
7120 * make sure dip->errors is perceived to be set.
7121 */
7122 smp_mb__before_atomic_dec();
7123 if (atomic_dec_and_test(&dip->pending_bios))
7124 bio_io_error(dip->orig_bio);
7125
7126 /* bio_end_io() will handle error, so we needn't return it */
7127 return 0;
7128}
7129
Chris Mason9be33952013-05-17 18:30:14 -04007130static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7131 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007132{
7133 struct btrfs_root *root = BTRFS_I(inode)->root;
7134 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007135 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007136 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007137 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007138 int ret = 0;
7139
7140 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7141
Chris Mason9be33952013-05-17 18:30:14 -04007142 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
7143
7144 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007145 ret = -ENOMEM;
7146 goto free_ordered;
7147 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007148
Chris Mason9be33952013-05-17 18:30:14 -04007149 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7150 if (!dip) {
7151 ret = -ENOMEM;
7152 goto free_io_bio;
7153 }
7154
7155 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007156 dip->inode = inode;
7157 dip->logical_offset = file_offset;
Miao Xiee6da5d22013-06-19 18:19:17 +08007158 dip->bytes = dio_bio->bi_size;
Chris Mason9be33952013-05-17 18:30:14 -04007159 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7160 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007161 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007162 dip->orig_bio = io_bio;
7163 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007164 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007165
7166 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007167 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007168 else
Chris Mason9be33952013-05-17 18:30:14 -04007169 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007170
Miao Xiee65e1532010-11-22 03:04:43 +00007171 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7172 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007173 return;
Chris Mason9be33952013-05-17 18:30:14 -04007174
7175free_io_bio:
7176 bio_put(io_bio);
7177
Josef Bacik4b46fce2010-05-23 11:00:55 -04007178free_ordered:
7179 /*
7180 * If this is a write, we need to clean up the reserved space and kill
7181 * the ordered extent.
7182 */
7183 if (write) {
7184 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007185 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007186 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7187 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7188 btrfs_free_reserved_extent(root, ordered->start,
7189 ordered->disk_len);
7190 btrfs_put_ordered_extent(ordered);
7191 btrfs_put_ordered_extent(ordered);
7192 }
Chris Mason9be33952013-05-17 18:30:14 -04007193 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007194}
7195
Chris Mason5a5f79b2010-05-26 21:33:37 -04007196static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7197 const struct iovec *iov, loff_t offset,
7198 unsigned long nr_segs)
7199{
7200 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007201 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007202 size_t size;
7203 unsigned long addr;
7204 unsigned blocksize_mask = root->sectorsize - 1;
7205 ssize_t retval = -EINVAL;
7206 loff_t end = offset;
7207
7208 if (offset & blocksize_mask)
7209 goto out;
7210
7211 /* Check the memory alignment. Blocks cannot straddle pages */
7212 for (seg = 0; seg < nr_segs; seg++) {
7213 addr = (unsigned long)iov[seg].iov_base;
7214 size = iov[seg].iov_len;
7215 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007216 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007217 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007218
7219 /* If this is a write we don't need to check anymore */
7220 if (rw & WRITE)
7221 continue;
7222
7223 /*
7224 * Check to make sure we don't have duplicate iov_base's in this
7225 * iovec, if so return EINVAL, otherwise we'll get csum errors
7226 * when reading back.
7227 */
7228 for (i = seg + 1; i < nr_segs; i++) {
7229 if (iov[seg].iov_base == iov[i].iov_base)
7230 goto out;
7231 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007232 }
7233 retval = 0;
7234out:
7235 return retval;
7236}
Josef Bacikeb838e72012-07-31 16:28:48 -04007237
Chris Mason16432982008-04-10 10:23:21 -04007238static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7239 const struct iovec *iov, loff_t offset,
7240 unsigned long nr_segs)
7241{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007242 struct file *file = iocb->ki_filp;
7243 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007244 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007245 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007246 bool wakeup = true;
7247 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007248 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007249
Chris Mason5a5f79b2010-05-26 21:33:37 -04007250 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007251 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007252 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007253
Miao Xie38851cc2013-02-08 07:04:11 +00007254 atomic_inc(&inode->i_dio_count);
7255 smp_mb__after_atomic_inc();
7256
Josef Bacik0e267c42013-07-02 10:38:02 -04007257 /*
7258 * The generic stuff only does filemap_write_and_wait_range, which isn't
7259 * enough if we've written compressed pages to this area, so we need to
7260 * call btrfs_wait_ordered_range to make absolutely sure that any
7261 * outstanding dirty pages are on disk.
7262 */
7263 count = iov_length(iov, nr_segs);
7264 btrfs_wait_ordered_range(inode, offset, count);
7265
Miao Xie09348562013-02-07 10:12:07 +00007266 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007267 /*
7268 * If the write DIO is beyond the EOF, we need update
7269 * the isize, but it is protected by i_mutex. So we can
7270 * not unlock the i_mutex at this case.
7271 */
7272 if (offset + count <= inode->i_size) {
7273 mutex_unlock(&inode->i_mutex);
7274 relock = true;
7275 }
Miao Xie09348562013-02-07 10:12:07 +00007276 ret = btrfs_delalloc_reserve_space(inode, count);
7277 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007278 goto out;
7279 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7280 &BTRFS_I(inode)->runtime_flags))) {
7281 inode_dio_done(inode);
7282 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7283 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007284 }
7285
7286 ret = __blockdev_direct_IO(rw, iocb, inode,
7287 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7288 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007289 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007290 if (rw & WRITE) {
7291 if (ret < 0 && ret != -EIOCBQUEUED)
7292 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007293 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007294 btrfs_delalloc_release_space(inode,
7295 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007296 else
7297 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007298 }
Miao Xie38851cc2013-02-08 07:04:11 +00007299out:
Miao Xie2e60a512013-02-08 07:01:08 +00007300 if (wakeup)
7301 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007302 if (relock)
7303 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007304
7305 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007306}
7307
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007308#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7309
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007310static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7311 __u64 start, __u64 len)
7312{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007313 int ret;
7314
7315 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7316 if (ret)
7317 return ret;
7318
Chris Masonec29ed52011-02-23 16:23:20 -05007319 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007320}
7321
Chris Mason9ebefb182007-06-15 13:50:00 -04007322int btrfs_readpage(struct file *file, struct page *page)
7323{
Chris Masond1310b22008-01-24 16:13:08 -05007324 struct extent_io_tree *tree;
7325 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007326 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007327}
Chris Mason1832a6d2007-12-21 16:27:21 -05007328
Chris Mason39279cc2007-06-12 06:35:45 -04007329static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7330{
Chris Masond1310b22008-01-24 16:13:08 -05007331 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007332
7333
7334 if (current->flags & PF_MEMALLOC) {
7335 redirty_page_for_writepage(wbc, page);
7336 unlock_page(page);
7337 return 0;
7338 }
Chris Masond1310b22008-01-24 16:13:08 -05007339 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007340 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7341}
Chris Mason39279cc2007-06-12 06:35:45 -04007342
Eric Sandeen48a3b632013-04-25 20:41:01 +00007343static int btrfs_writepages(struct address_space *mapping,
7344 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007345{
Chris Masond1310b22008-01-24 16:13:08 -05007346 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007347
Chris Masond1310b22008-01-24 16:13:08 -05007348 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007349 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7350}
7351
Chris Mason3ab2fb52007-11-08 10:59:22 -05007352static int
7353btrfs_readpages(struct file *file, struct address_space *mapping,
7354 struct list_head *pages, unsigned nr_pages)
7355{
Chris Masond1310b22008-01-24 16:13:08 -05007356 struct extent_io_tree *tree;
7357 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007358 return extent_readpages(tree, mapping, pages, nr_pages,
7359 btrfs_get_extent);
7360}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007361static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007362{
Chris Masond1310b22008-01-24 16:13:08 -05007363 struct extent_io_tree *tree;
7364 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007365 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007366
Chris Masond1310b22008-01-24 16:13:08 -05007367 tree = &BTRFS_I(page->mapping->host)->io_tree;
7368 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007369 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007370 if (ret == 1) {
7371 ClearPagePrivate(page);
7372 set_page_private(page, 0);
7373 page_cache_release(page);
7374 }
7375 return ret;
7376}
Chris Mason39279cc2007-06-12 06:35:45 -04007377
Chris Masone6dcd2d2008-07-17 12:53:50 -04007378static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7379{
Chris Mason98509cf2008-09-11 15:51:43 -04007380 if (PageWriteback(page) || PageDirty(page))
7381 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007382 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007383}
7384
Lukas Czernerd47992f2013-05-21 23:17:23 -04007385static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7386 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007387{
Josef Bacik5fd02042012-05-02 14:00:54 -04007388 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007389 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007390 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007391 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007392 u64 page_start = page_offset(page);
7393 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007394
Chris Mason8b62b722009-09-02 16:53:46 -04007395 /*
7396 * we have the page locked, so new writeback can't start,
7397 * and the dirty bit won't be cleared while we are here.
7398 *
7399 * Wait for IO on this page so that we can safely clear
7400 * the PagePrivate2 bit and do ordered accounting
7401 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007402 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007403
Josef Bacik5fd02042012-05-02 14:00:54 -04007404 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007405 if (offset) {
7406 btrfs_releasepage(page, GFP_NOFS);
7407 return;
7408 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007409 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007410 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007411 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007412 /*
7413 * IO on this page will never be started, so we need
7414 * to account for any ordered extents now
7415 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007416 clear_extent_bit(tree, page_start, page_end,
7417 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007418 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7419 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007420 /*
7421 * whoever cleared the private bit is responsible
7422 * for the finish_ordered_io
7423 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007424 if (TestClearPagePrivate2(page) &&
7425 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7426 PAGE_CACHE_SIZE, 1)) {
7427 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007428 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007429 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007430 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007431 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007432 }
7433 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007434 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007435 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7436 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007437 __btrfs_releasepage(page, GFP_NOFS);
7438
Chris Mason4a096752008-07-21 10:29:44 -04007439 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007440 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007441 ClearPagePrivate(page);
7442 set_page_private(page, 0);
7443 page_cache_release(page);
7444 }
Chris Mason39279cc2007-06-12 06:35:45 -04007445}
7446
Chris Mason9ebefb182007-06-15 13:50:00 -04007447/*
7448 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7449 * called from a page fault handler when a page is first dirtied. Hence we must
7450 * be careful to check for EOF conditions here. We set the page up correctly
7451 * for a written page which means we get ENOSPC checking when writing into
7452 * holes and correct delalloc and unwritten extent mapping on filesystems that
7453 * support these features.
7454 *
7455 * We are not allowed to take the i_mutex here so we have to play games to
7456 * protect against truncate races as the page could now be beyond EOF. Because
7457 * vmtruncate() writes the inode size before removing pages, once we have the
7458 * page lock we can determine safely if the page is beyond EOF. If it is not
7459 * beyond EOF, then the page is guaranteed safe against truncation until we
7460 * unlock the page.
7461 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007462int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007463{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007464 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007465 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007466 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007467 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7468 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007469 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007470 char *kaddr;
7471 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007472 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007473 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007474 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007475 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007476 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007477
Jan Karab2b5ef52012-06-12 16:20:45 +02007478 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007479 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007480 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007481 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007482 reserved = 1;
7483 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007484 if (ret) {
7485 if (ret == -ENOMEM)
7486 ret = VM_FAULT_OOM;
7487 else /* -ENOSPC, -EIO, etc */
7488 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007489 if (reserved)
7490 goto out;
7491 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007492 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007493
Nick Piggin56a76f82009-03-31 15:23:23 -07007494 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007495again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007496 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007497 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007498 page_start = page_offset(page);
7499 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007500
Chris Mason9ebefb182007-06-15 13:50:00 -04007501 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007502 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007503 /* page got truncated out from underneath us */
7504 goto out_unlock;
7505 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007506 wait_on_page_writeback(page);
7507
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007508 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007509 set_page_extent_mapped(page);
7510
Chris Masoneb84ae02008-07-17 13:53:27 -04007511 /*
7512 * we can't set the delalloc bits if there are pending ordered
7513 * extents. Drop our locks and wait for them to finish
7514 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007515 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7516 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007517 unlock_extent_cached(io_tree, page_start, page_end,
7518 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007519 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007520 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007521 btrfs_put_ordered_extent(ordered);
7522 goto again;
7523 }
7524
Josef Bacikfbf19082009-10-01 17:10:23 -04007525 /*
7526 * XXX - page_mkwrite gets called every time the page is dirtied, even
7527 * if it was already dirty, so for space accounting reasons we need to
7528 * clear any delalloc bits for the range we are fixing to save. There
7529 * is probably a better way to do this, but for now keep consistent with
7530 * prepare_pages in the normal write path.
7531 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007532 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007533 EXTENT_DIRTY | EXTENT_DELALLOC |
7534 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007535 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007536
Josef Bacik2ac55d42010-02-03 19:33:23 +00007537 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7538 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007539 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007540 unlock_extent_cached(io_tree, page_start, page_end,
7541 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007542 ret = VM_FAULT_SIGBUS;
7543 goto out_unlock;
7544 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007545 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007546
7547 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007548 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007549 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007550 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007551 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007552
Chris Masone6dcd2d2008-07-17 12:53:50 -04007553 if (zero_start != PAGE_CACHE_SIZE) {
7554 kaddr = kmap(page);
7555 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7556 flush_dcache_page(page);
7557 kunmap(page);
7558 }
Chris Mason247e7432008-07-17 12:53:51 -04007559 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007560 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007561 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007562
Chris Mason257c62e2009-10-13 13:21:08 -04007563 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7564 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007565 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007566
Josef Bacik2ac55d42010-02-03 19:33:23 +00007567 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007568
7569out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007570 if (!ret) {
7571 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007572 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007573 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007574 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007575out:
Josef Bacikec39e182012-01-12 19:10:12 -05007576 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007577out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007578 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007579 return ret;
7580}
7581
Josef Bacika41ad392011-01-31 15:30:16 -05007582static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007583{
7584 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007585 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007586 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007587 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007588 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007589 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007590 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007591
Chris Mason4a096752008-07-21 10:29:44 -04007592 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007593 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007594
Josef Bacikfcb80c22011-05-03 10:40:22 -04007595 /*
7596 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7597 * 3 things going on here
7598 *
7599 * 1) We need to reserve space for our orphan item and the space to
7600 * delete our orphan item. Lord knows we don't want to have a dangling
7601 * orphan item because we didn't reserve space to remove it.
7602 *
7603 * 2) We need to reserve space to update our inode.
7604 *
7605 * 3) We need to have something to cache all the space that is going to
7606 * be free'd up by the truncate operation, but also have some slack
7607 * space reserved in case it uses space during the truncate (thank you
7608 * very much snapshotting).
7609 *
7610 * And we need these to all be seperate. The fact is we can use alot of
7611 * space doing the truncate, and we have no earthly idea how much space
7612 * we will use, so we need the truncate reservation to be seperate so it
7613 * doesn't end up using space reserved for updating the inode or
7614 * removing the orphan item. We also need to be able to stop the
7615 * transaction and start a new one, which means we need to be able to
7616 * update the inode several times, and we have no idea of knowing how
7617 * many times that will be, so we can't just reserve 1 item for the
7618 * entirety of the opration, so that has to be done seperately as well.
7619 * Then there is the orphan item, which does indeed need to be held on
7620 * to for the whole operation, and we need nobody to touch this reserved
7621 * space except the orphan code.
7622 *
7623 * So that leaves us with
7624 *
7625 * 1) root->orphan_block_rsv - for the orphan deletion.
7626 * 2) rsv - for the truncate reservation, which we will steal from the
7627 * transaction reservation.
7628 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7629 * updating the inode.
7630 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007631 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007632 if (!rsv)
7633 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007634 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007635 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007636
Josef Bacik907cbce2011-08-08 13:46:15 -04007637 /*
Josef Bacik07127182011-08-19 10:29:59 -04007638 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007639 * 1 for updating the inode.
7640 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007641 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007642 if (IS_ERR(trans)) {
7643 err = PTR_ERR(trans);
7644 goto out;
7645 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007646
Josef Bacik907cbce2011-08-08 13:46:15 -04007647 /* Migrate the slack space for the truncate to our reserve */
7648 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7649 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007650 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007651
Chris Mason5a3f23d2009-03-31 13:27:11 -04007652 /*
7653 * setattr is responsible for setting the ordered_data_close flag,
7654 * but that is only tested during the last file release. That
7655 * could happen well after the next commit, leaving a great big
7656 * window where new writes may get lost if someone chooses to write
7657 * to this file after truncating to zero
7658 *
7659 * The inode doesn't have any dirty data here, and so if we commit
7660 * this is a noop. If someone immediately starts writing to the inode
7661 * it is very likely we'll catch some of their writes in this
7662 * transaction, and the commit will find this file on the ordered
7663 * data list with good things to send down.
7664 *
7665 * This is a best effort solution, there is still a window where
7666 * using truncate to replace the contents of the file will
7667 * end up with a zero length file after a crash.
7668 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007669 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7670 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007671 btrfs_add_ordered_operation(trans, root, inode);
7672
Josef Bacik5dc562c2012-08-17 13:14:17 -04007673 /*
7674 * So if we truncate and then write and fsync we normally would just
7675 * write the extents that changed, which is a problem if we need to
7676 * first truncate that entire inode. So set this flag so we write out
7677 * all of the extents in the inode to the sync log so we're completely
7678 * safe.
7679 */
7680 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007681 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007682
Yan, Zheng80825102009-11-12 09:35:36 +00007683 while (1) {
7684 ret = btrfs_truncate_inode_items(trans, root, inode,
7685 inode->i_size,
7686 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007687 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007688 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007689 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007690 }
Chris Mason39279cc2007-06-12 06:35:45 -04007691
Josef Bacikfcb80c22011-05-03 10:40:22 -04007692 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007693 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007694 if (ret) {
7695 err = ret;
7696 break;
7697 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007698
Yan, Zheng80825102009-11-12 09:35:36 +00007699 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007700 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007701
7702 trans = btrfs_start_transaction(root, 2);
7703 if (IS_ERR(trans)) {
7704 ret = err = PTR_ERR(trans);
7705 trans = NULL;
7706 break;
7707 }
7708
7709 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7710 rsv, min_size);
7711 BUG_ON(ret); /* shouldn't happen */
7712 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007713 }
7714
7715 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007716 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007717 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007718 if (ret)
7719 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007720 }
7721
Chris Mason917c16b2011-11-08 14:49:59 -05007722 if (trans) {
7723 trans->block_rsv = &root->fs_info->trans_block_rsv;
7724 ret = btrfs_update_inode(trans, root, inode);
7725 if (ret && !err)
7726 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007727
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007728 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007729 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007730 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007731
7732out:
7733 btrfs_free_block_rsv(root, rsv);
7734
Josef Bacik3893e332011-01-31 16:03:11 -05007735 if (ret && !err)
7736 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007737
Josef Bacik3893e332011-01-31 16:03:11 -05007738 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007739}
7740
Sven Wegener3b963622008-06-09 21:57:42 -04007741/*
Chris Masond352ac62008-09-29 15:18:18 -04007742 * create a new subvolume directory/inode (helper for the ioctl).
7743 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007744int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007745 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007746{
Chris Mason39279cc2007-06-12 06:35:45 -04007747 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007748 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007749 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007750
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007751 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7752 new_dirid, new_dirid,
7753 S_IFDIR | (~current_umask() & S_IRWXUGO),
7754 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007755 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007756 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007757 inode->i_op = &btrfs_dir_inode_operations;
7758 inode->i_fop = &btrfs_dir_file_operations;
7759
Miklos Szeredibfe86842011-10-28 14:13:29 +02007760 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007761 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007762
Yan, Zheng76dda932009-09-21 16:00:26 -04007763 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007764
Yan, Zheng76dda932009-09-21 16:00:26 -04007765 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007766 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007767}
7768
Chris Mason39279cc2007-06-12 06:35:45 -04007769struct inode *btrfs_alloc_inode(struct super_block *sb)
7770{
7771 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007772 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007773
7774 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7775 if (!ei)
7776 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007777
7778 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007779 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007780 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007781 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007782 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007783 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007784 ei->disk_i_size = 0;
7785 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007786 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007787 ei->index_cnt = (u64)-1;
7788 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007789 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007790
Josef Bacik9e0baf62011-07-15 15:16:44 +00007791 spin_lock_init(&ei->lock);
7792 ei->outstanding_extents = 0;
7793 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007794
Josef Bacik72ac3c02012-05-23 14:13:11 -04007795 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007796 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007797
Miao Xie16cdcec2011-04-22 18:12:22 +08007798 ei->delayed_node = NULL;
7799
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007800 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007801 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007802 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7803 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007804 ei->io_tree.track_uptodate = 1;
7805 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007806 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007807 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007808 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007809 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007810 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007811 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007812 RB_CLEAR_NODE(&ei->rb_node);
7813
7814 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007815}
7816
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007817static void btrfs_i_callback(struct rcu_head *head)
7818{
7819 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007820 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7821}
7822
Chris Mason39279cc2007-06-12 06:35:45 -04007823void btrfs_destroy_inode(struct inode *inode)
7824{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007825 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007826 struct btrfs_root *root = BTRFS_I(inode)->root;
7827
Al Virob3d9b7a2012-06-09 13:51:19 -04007828 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007829 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007830 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7831 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007832 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7833 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007834
Chris Mason5a3f23d2009-03-31 13:27:11 -04007835 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007836 * This can happen where we create an inode, but somebody else also
7837 * created the same inode and we need to destroy the one we already
7838 * created.
7839 */
7840 if (!root)
7841 goto free;
7842
7843 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007844 * Make sure we're properly removed from the ordered operation
7845 * lists.
7846 */
7847 smp_mb();
7848 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
Miao Xie199c2a92013-05-15 07:48:23 +00007849 spin_lock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007850 list_del_init(&BTRFS_I(inode)->ordered_operations);
Miao Xie199c2a92013-05-15 07:48:23 +00007851 spin_unlock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007852 }
7853
Josef Bacik8a35d952012-05-23 14:26:42 -04007854 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7855 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007856 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
7857 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007858 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007859 }
Josef Bacik7b128762008-07-24 12:17:14 -04007860
Chris Masond3977122009-01-05 21:25:51 -05007861 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007862 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7863 if (!ordered)
7864 break;
7865 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007866 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
7867 (unsigned long long)ordered->file_offset,
7868 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007869 btrfs_remove_ordered_extent(inode, ordered);
7870 btrfs_put_ordered_extent(ordered);
7871 btrfs_put_ordered_extent(ordered);
7872 }
7873 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007874 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007875 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007876free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007877 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007878}
7879
Al Viro45321ac2010-06-07 13:43:19 -04007880int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007881{
7882 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007883
Naohiro Aota6379ef92013-06-06 09:56:34 +00007884 if (root == NULL)
7885 return 1;
7886
Liu Bofa6ac872013-02-20 14:10:23 +00007887 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007888 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007889 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007890 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007891 else
Al Viro45321ac2010-06-07 13:43:19 -04007892 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007893}
7894
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007895static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007896{
7897 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7898
7899 inode_init_once(&ei->vfs_inode);
7900}
7901
7902void btrfs_destroy_cachep(void)
7903{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007904 /*
7905 * Make sure all delayed rcu free inodes are flushed before we
7906 * destroy cache.
7907 */
7908 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007909 if (btrfs_inode_cachep)
7910 kmem_cache_destroy(btrfs_inode_cachep);
7911 if (btrfs_trans_handle_cachep)
7912 kmem_cache_destroy(btrfs_trans_handle_cachep);
7913 if (btrfs_transaction_cachep)
7914 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007915 if (btrfs_path_cachep)
7916 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007917 if (btrfs_free_space_cachep)
7918 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007919 if (btrfs_delalloc_work_cachep)
7920 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007921}
7922
7923int btrfs_init_cachep(void)
7924{
David Sterba837e1972012-09-07 03:00:48 -06007925 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007926 sizeof(struct btrfs_inode), 0,
7927 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007928 if (!btrfs_inode_cachep)
7929 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007930
David Sterba837e1972012-09-07 03:00:48 -06007931 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007932 sizeof(struct btrfs_trans_handle), 0,
7933 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007934 if (!btrfs_trans_handle_cachep)
7935 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007936
David Sterba837e1972012-09-07 03:00:48 -06007937 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007938 sizeof(struct btrfs_transaction), 0,
7939 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007940 if (!btrfs_transaction_cachep)
7941 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007942
David Sterba837e1972012-09-07 03:00:48 -06007943 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007944 sizeof(struct btrfs_path), 0,
7945 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007946 if (!btrfs_path_cachep)
7947 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007948
David Sterba837e1972012-09-07 03:00:48 -06007949 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007950 sizeof(struct btrfs_free_space), 0,
7951 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7952 if (!btrfs_free_space_cachep)
7953 goto fail;
7954
Miao Xie8ccf6f192012-10-25 09:28:04 +00007955 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7956 sizeof(struct btrfs_delalloc_work), 0,
7957 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7958 NULL);
7959 if (!btrfs_delalloc_work_cachep)
7960 goto fail;
7961
Chris Mason39279cc2007-06-12 06:35:45 -04007962 return 0;
7963fail:
7964 btrfs_destroy_cachep();
7965 return -ENOMEM;
7966}
7967
7968static int btrfs_getattr(struct vfsmount *mnt,
7969 struct dentry *dentry, struct kstat *stat)
7970{
Miao Xiedf0af1a2013-01-29 10:11:59 +00007971 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04007972 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007973 u32 blocksize = inode->i_sb->s_blocksize;
7974
Chris Mason39279cc2007-06-12 06:35:45 -04007975 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007976 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007977 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00007978
7979 spin_lock(&BTRFS_I(inode)->lock);
7980 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
7981 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05007982 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00007983 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007984 return 0;
7985}
7986
Chris Masond3977122009-01-05 21:25:51 -05007987static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7988 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007989{
7990 struct btrfs_trans_handle *trans;
7991 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007992 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007993 struct inode *new_inode = new_dentry->d_inode;
7994 struct inode *old_inode = old_dentry->d_inode;
7995 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007996 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007997 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007998 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007999 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008000
Li Zefan33345d012011-04-20 10:31:50 +08008001 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008002 return -EPERM;
8003
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008004 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008005 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008006 return -EXDEV;
8007
Li Zefan33345d012011-04-20 10:31:50 +08008008 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8009 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008010 return -ENOTEMPTY;
8011
Chris Mason39279cc2007-06-12 06:35:45 -04008012 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008013 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008014 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008015
8016
8017 /* check for collisions, even if the name isn't there */
8018 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8019 new_dentry->d_name.name,
8020 new_dentry->d_name.len);
8021
8022 if (ret) {
8023 if (ret == -EEXIST) {
8024 /* we shouldn't get
8025 * eexist without a new_inode */
8026 if (!new_inode) {
8027 WARN_ON(1);
8028 return ret;
8029 }
8030 } else {
8031 /* maybe -EOVERFLOW */
8032 return ret;
8033 }
8034 }
8035 ret = 0;
8036
Chris Mason5a3f23d2009-03-31 13:27:11 -04008037 /*
8038 * we're using rename to replace one file with another.
8039 * and the replacement file is large. Start IO on it now so
8040 * we don't add too much work to the end of the transaction
8041 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008042 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008043 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8044 filemap_flush(old_inode->i_mapping);
8045
Yan, Zheng76dda932009-09-21 16:00:26 -04008046 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008047 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008048 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008049 /*
8050 * We want to reserve the absolute worst case amount of items. So if
8051 * both inodes are subvols and we need to unlink them then that would
8052 * require 4 item modifications, but if they are both normal inodes it
8053 * would require 5 item modifications, so we'll assume their normal
8054 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8055 * should cover the worst case number of items we'll modify.
8056 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008057 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008058 if (IS_ERR(trans)) {
8059 ret = PTR_ERR(trans);
8060 goto out_notrans;
8061 }
Chris Mason5f39d392007-10-15 16:14:19 -04008062
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008063 if (dest != root)
8064 btrfs_record_root_in_trans(trans, dest);
8065
Yan, Zhenga5719522009-09-24 09:17:31 -04008066 ret = btrfs_set_inode_index(new_dir, &index);
8067 if (ret)
8068 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008069
Li Zefan33345d012011-04-20 10:31:50 +08008070 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008071 /* force full log commit if subvolume involved. */
8072 root->fs_info->last_trans_log_full_commit = trans->transid;
8073 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008074 ret = btrfs_insert_inode_ref(trans, dest,
8075 new_dentry->d_name.name,
8076 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008077 old_ino,
8078 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008079 if (ret)
8080 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008081 /*
8082 * this is an ugly little race, but the rename is required
8083 * to make sure that if we crash, the inode is either at the
8084 * old name or the new one. pinning the log transaction lets
8085 * us make sure we don't allow a log commit to come in after
8086 * we unlink the name but before we add the new name back in.
8087 */
8088 btrfs_pin_log_trans(root);
8089 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008090 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008091 * make sure the inode gets flushed if it is replacing
8092 * something.
8093 */
Li Zefan33345d012011-04-20 10:31:50 +08008094 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008095 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008096
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008097 inode_inc_iversion(old_dir);
8098 inode_inc_iversion(new_dir);
8099 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008100 old_dir->i_ctime = old_dir->i_mtime = ctime;
8101 new_dir->i_ctime = new_dir->i_mtime = ctime;
8102 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008103
Chris Mason12fcfd22009-03-24 10:24:20 -04008104 if (old_dentry->d_parent != new_dentry->d_parent)
8105 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8106
Li Zefan33345d012011-04-20 10:31:50 +08008107 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008108 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8109 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8110 old_dentry->d_name.name,
8111 old_dentry->d_name.len);
8112 } else {
Al Viro92986792011-03-04 17:14:37 +00008113 ret = __btrfs_unlink_inode(trans, root, old_dir,
8114 old_dentry->d_inode,
8115 old_dentry->d_name.name,
8116 old_dentry->d_name.len);
8117 if (!ret)
8118 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008119 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008120 if (ret) {
8121 btrfs_abort_transaction(trans, root, ret);
8122 goto out_fail;
8123 }
Chris Mason39279cc2007-06-12 06:35:45 -04008124
8125 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008126 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008127 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008128 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008129 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8130 root_objectid = BTRFS_I(new_inode)->location.objectid;
8131 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8132 root_objectid,
8133 new_dentry->d_name.name,
8134 new_dentry->d_name.len);
8135 BUG_ON(new_inode->i_nlink == 0);
8136 } else {
8137 ret = btrfs_unlink_inode(trans, dest, new_dir,
8138 new_dentry->d_inode,
8139 new_dentry->d_name.name,
8140 new_dentry->d_name.len);
8141 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008142 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008143 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008144 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008145 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008146 if (ret) {
8147 btrfs_abort_transaction(trans, root, ret);
8148 goto out_fail;
8149 }
Chris Mason39279cc2007-06-12 06:35:45 -04008150 }
Josef Bacikaec74772008-07-24 12:12:38 -04008151
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008152 ret = btrfs_add_link(trans, new_dir, old_inode,
8153 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008154 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008155 if (ret) {
8156 btrfs_abort_transaction(trans, root, ret);
8157 goto out_fail;
8158 }
Chris Mason39279cc2007-06-12 06:35:45 -04008159
Li Zefan33345d012011-04-20 10:31:50 +08008160 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008161 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008162 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008163 btrfs_end_log_trans(root);
8164 }
Chris Mason39279cc2007-06-12 06:35:45 -04008165out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008166 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008167out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008168 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008169 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008170
Chris Mason39279cc2007-06-12 06:35:45 -04008171 return ret;
8172}
8173
Miao Xie8ccf6f192012-10-25 09:28:04 +00008174static void btrfs_run_delalloc_work(struct btrfs_work *work)
8175{
8176 struct btrfs_delalloc_work *delalloc_work;
8177
8178 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8179 work);
8180 if (delalloc_work->wait)
8181 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8182 else
8183 filemap_flush(delalloc_work->inode->i_mapping);
8184
8185 if (delalloc_work->delay_iput)
8186 btrfs_add_delayed_iput(delalloc_work->inode);
8187 else
8188 iput(delalloc_work->inode);
8189 complete(&delalloc_work->completion);
8190}
8191
8192struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8193 int wait, int delay_iput)
8194{
8195 struct btrfs_delalloc_work *work;
8196
8197 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8198 if (!work)
8199 return NULL;
8200
8201 init_completion(&work->completion);
8202 INIT_LIST_HEAD(&work->list);
8203 work->inode = inode;
8204 work->wait = wait;
8205 work->delay_iput = delay_iput;
8206 work->work.func = btrfs_run_delalloc_work;
8207
8208 return work;
8209}
8210
8211void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8212{
8213 wait_for_completion(&work->completion);
8214 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8215}
8216
Chris Masond352ac62008-09-29 15:18:18 -04008217/*
8218 * some fairly slow code that needs optimization. This walks the list
8219 * of all the inodes with pending delalloc and forces them to disk.
8220 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00008221static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008222{
Chris Masonea8c2812008-08-04 23:17:27 -04008223 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008224 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008225 struct btrfs_delalloc_work *work, *next;
8226 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008227 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008228 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008229
Miao Xie8ccf6f192012-10-25 09:28:04 +00008230 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008231 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008232
Miao Xieeb73c1b2013-05-15 07:48:22 +00008233 spin_lock(&root->delalloc_lock);
8234 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008235 while (!list_empty(&splice)) {
8236 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008237 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008238
Miao Xieeb73c1b2013-05-15 07:48:22 +00008239 list_move_tail(&binode->delalloc_inodes,
8240 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008241 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008242 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008243 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008244 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008245 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008246 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008247
8248 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8249 if (unlikely(!work)) {
8250 ret = -ENOMEM;
8251 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008252 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008253 list_add_tail(&work->list, &works);
8254 btrfs_queue_worker(&root->fs_info->flush_workers,
8255 &work->work);
8256
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008257 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008258 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008259 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008260 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008261
Miao Xie1eafa6c2013-01-22 10:49:00 +00008262 list_for_each_entry_safe(work, next, &works, list) {
8263 list_del_init(&work->list);
8264 btrfs_wait_and_free_delalloc_work(work);
8265 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008266 return 0;
8267out:
8268 list_for_each_entry_safe(work, next, &works, list) {
8269 list_del_init(&work->list);
8270 btrfs_wait_and_free_delalloc_work(work);
8271 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008272
Miao Xieeb73c1b2013-05-15 07:48:22 +00008273 if (!list_empty_careful(&splice)) {
8274 spin_lock(&root->delalloc_lock);
8275 list_splice_tail(&splice, &root->delalloc_inodes);
8276 spin_unlock(&root->delalloc_lock);
8277 }
8278 return ret;
8279}
8280
8281int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8282{
8283 int ret;
8284
8285 if (root->fs_info->sb->s_flags & MS_RDONLY)
8286 return -EROFS;
8287
8288 ret = __start_delalloc_inodes(root, delay_iput);
8289 /*
8290 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008291 * we have to make sure the IO is actually started and that
8292 * ordered extents get created before we return
8293 */
8294 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008295 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008296 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008297 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008298 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8299 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008300 }
8301 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008302 return ret;
8303}
8304
8305int btrfs_start_all_delalloc_inodes(struct btrfs_fs_info *fs_info,
8306 int delay_iput)
8307{
8308 struct btrfs_root *root;
8309 struct list_head splice;
8310 int ret;
8311
8312 if (fs_info->sb->s_flags & MS_RDONLY)
8313 return -EROFS;
8314
8315 INIT_LIST_HEAD(&splice);
8316
8317 spin_lock(&fs_info->delalloc_root_lock);
8318 list_splice_init(&fs_info->delalloc_roots, &splice);
8319 while (!list_empty(&splice)) {
8320 root = list_first_entry(&splice, struct btrfs_root,
8321 delalloc_root);
8322 root = btrfs_grab_fs_root(root);
8323 BUG_ON(!root);
8324 list_move_tail(&root->delalloc_root,
8325 &fs_info->delalloc_roots);
8326 spin_unlock(&fs_info->delalloc_root_lock);
8327
8328 ret = __start_delalloc_inodes(root, delay_iput);
8329 btrfs_put_fs_root(root);
8330 if (ret)
8331 goto out;
8332
8333 spin_lock(&fs_info->delalloc_root_lock);
8334 }
8335 spin_unlock(&fs_info->delalloc_root_lock);
8336
8337 atomic_inc(&fs_info->async_submit_draining);
8338 while (atomic_read(&fs_info->nr_async_submits) ||
8339 atomic_read(&fs_info->async_delalloc_pages)) {
8340 wait_event(fs_info->async_submit_wait,
8341 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8342 atomic_read(&fs_info->async_delalloc_pages) == 0));
8343 }
8344 atomic_dec(&fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008345 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008346out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008347 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008348 spin_lock(&fs_info->delalloc_root_lock);
8349 list_splice_tail(&splice, &fs_info->delalloc_roots);
8350 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008351 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008352 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008353}
8354
Chris Mason39279cc2007-06-12 06:35:45 -04008355static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8356 const char *symname)
8357{
8358 struct btrfs_trans_handle *trans;
8359 struct btrfs_root *root = BTRFS_I(dir)->root;
8360 struct btrfs_path *path;
8361 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008362 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008363 int err;
8364 int drop_inode = 0;
8365 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008366 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008367 int name_len;
8368 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008369 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008370 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008371 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008372
8373 name_len = strlen(symname) + 1;
8374 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8375 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008376
Josef Bacik9ed74f22009-09-11 16:12:44 -04008377 /*
8378 * 2 items for inode item and ref
8379 * 2 items for dir items
8380 * 1 item for xattr if selinux is on
8381 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008382 trans = btrfs_start_transaction(root, 5);
8383 if (IS_ERR(trans))
8384 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008385
Li Zefan581bb052011-04-20 10:06:11 +08008386 err = btrfs_find_free_ino(root, &objectid);
8387 if (err)
8388 goto out_unlock;
8389
Josef Bacikaec74772008-07-24 12:12:38 -04008390 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008391 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008392 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008393 if (IS_ERR(inode)) {
8394 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008395 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008396 }
Chris Mason39279cc2007-06-12 06:35:45 -04008397
Eric Paris2a7dba32011-02-01 11:05:39 -05008398 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008399 if (err) {
8400 drop_inode = 1;
8401 goto out_unlock;
8402 }
8403
Casey Schauflerad19db72011-12-15 10:09:07 -05008404 /*
8405 * If the active LSM wants to access the inode during
8406 * d_instantiate it needs these. Smack checks to see
8407 * if the filesystem supports xattrs by looking at the
8408 * ops vector.
8409 */
8410 inode->i_fop = &btrfs_file_operations;
8411 inode->i_op = &btrfs_file_inode_operations;
8412
Josef Bacika1b075d2010-11-19 20:36:11 +00008413 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008414 if (err)
8415 drop_inode = 1;
8416 else {
8417 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008418 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008419 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008420 }
Chris Mason39279cc2007-06-12 06:35:45 -04008421 if (drop_inode)
8422 goto out_unlock;
8423
8424 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008425 if (!path) {
8426 err = -ENOMEM;
8427 drop_inode = 1;
8428 goto out_unlock;
8429 }
Li Zefan33345d012011-04-20 10:31:50 +08008430 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008431 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008432 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8433 datasize = btrfs_file_extent_calc_inline_size(name_len);
8434 err = btrfs_insert_empty_item(trans, root, path, &key,
8435 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008436 if (err) {
8437 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008438 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008439 goto out_unlock;
8440 }
Chris Mason5f39d392007-10-15 16:14:19 -04008441 leaf = path->nodes[0];
8442 ei = btrfs_item_ptr(leaf, path->slots[0],
8443 struct btrfs_file_extent_item);
8444 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8445 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008446 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008447 btrfs_set_file_extent_encryption(leaf, ei, 0);
8448 btrfs_set_file_extent_compression(leaf, ei, 0);
8449 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8450 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8451
Chris Mason39279cc2007-06-12 06:35:45 -04008452 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008453 write_extent_buffer(leaf, symname, ptr, name_len);
8454 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008455 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008456
Chris Mason39279cc2007-06-12 06:35:45 -04008457 inode->i_op = &btrfs_symlink_inode_operations;
8458 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008459 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008460 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008461 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008462 err = btrfs_update_inode(trans, root, inode);
8463 if (err)
8464 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008465
8466out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008467 if (!err)
8468 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008469 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008470 if (drop_inode) {
8471 inode_dec_link_count(inode);
8472 iput(inode);
8473 }
Liu Bob53d3f52012-11-14 14:34:34 +00008474 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008475 return err;
8476}
Chris Mason16432982008-04-10 10:23:21 -04008477
Josef Bacik0af3d002010-06-21 14:48:16 -04008478static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8479 u64 start, u64 num_bytes, u64 min_size,
8480 loff_t actual_len, u64 *alloc_hint,
8481 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008482{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008483 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8484 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008485 struct btrfs_root *root = BTRFS_I(inode)->root;
8486 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008487 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008488 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008489 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008490 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008491 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008492
Josef Bacik0af3d002010-06-21 14:48:16 -04008493 if (trans)
8494 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008495 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008496 if (own_trans) {
8497 trans = btrfs_start_transaction(root, 3);
8498 if (IS_ERR(trans)) {
8499 ret = PTR_ERR(trans);
8500 break;
8501 }
Yan Zhengd899e052008-10-30 14:25:28 -04008502 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008503
Chris Mason154ea282013-03-05 11:11:26 -05008504 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8505 cur_bytes = max(cur_bytes, min_size);
8506 ret = btrfs_reserve_extent(trans, root, cur_bytes,
Zach Brown24542bf2012-11-16 00:04:43 +00008507 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008508 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008509 if (own_trans)
8510 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008511 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008512 }
8513
Yan Zhengd899e052008-10-30 14:25:28 -04008514 ret = insert_reserved_file_extent(trans, inode,
8515 cur_offset, ins.objectid,
8516 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008517 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008518 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008519 if (ret) {
8520 btrfs_abort_transaction(trans, root, ret);
8521 if (own_trans)
8522 btrfs_end_transaction(trans, root);
8523 break;
8524 }
Chris Masona1ed8352009-09-11 12:27:37 -04008525 btrfs_drop_extent_cache(inode, cur_offset,
8526 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008527
Josef Bacik5dc562c2012-08-17 13:14:17 -04008528 em = alloc_extent_map();
8529 if (!em) {
8530 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8531 &BTRFS_I(inode)->runtime_flags);
8532 goto next;
8533 }
8534
8535 em->start = cur_offset;
8536 em->orig_start = cur_offset;
8537 em->len = ins.offset;
8538 em->block_start = ins.objectid;
8539 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008540 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008541 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008542 em->bdev = root->fs_info->fs_devices->latest_bdev;
8543 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8544 em->generation = trans->transid;
8545
8546 while (1) {
8547 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008548 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008549 write_unlock(&em_tree->lock);
8550 if (ret != -EEXIST)
8551 break;
8552 btrfs_drop_extent_cache(inode, cur_offset,
8553 cur_offset + ins.offset - 1,
8554 0);
8555 }
8556 free_extent_map(em);
8557next:
Yan Zhengd899e052008-10-30 14:25:28 -04008558 num_bytes -= ins.offset;
8559 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008560 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008561
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008562 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008563 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008564 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008565 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008566 (actual_len > inode->i_size) &&
8567 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008568 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008569 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008570 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008571 i_size = cur_offset;
8572 i_size_write(inode, i_size);
8573 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008574 }
8575
Yan Zhengd899e052008-10-30 14:25:28 -04008576 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008577
8578 if (ret) {
8579 btrfs_abort_transaction(trans, root, ret);
8580 if (own_trans)
8581 btrfs_end_transaction(trans, root);
8582 break;
8583 }
Yan Zhengd899e052008-10-30 14:25:28 -04008584
Josef Bacik0af3d002010-06-21 14:48:16 -04008585 if (own_trans)
8586 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008587 }
Yan Zhengd899e052008-10-30 14:25:28 -04008588 return ret;
8589}
8590
Josef Bacik0af3d002010-06-21 14:48:16 -04008591int btrfs_prealloc_file_range(struct inode *inode, int mode,
8592 u64 start, u64 num_bytes, u64 min_size,
8593 loff_t actual_len, u64 *alloc_hint)
8594{
8595 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8596 min_size, actual_len, alloc_hint,
8597 NULL);
8598}
8599
8600int btrfs_prealloc_file_range_trans(struct inode *inode,
8601 struct btrfs_trans_handle *trans, int mode,
8602 u64 start, u64 num_bytes, u64 min_size,
8603 loff_t actual_len, u64 *alloc_hint)
8604{
8605 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8606 min_size, actual_len, alloc_hint, trans);
8607}
8608
Chris Masone6dcd2d2008-07-17 12:53:50 -04008609static int btrfs_set_page_dirty(struct page *page)
8610{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008611 return __set_page_dirty_nobuffers(page);
8612}
8613
Al Viro10556cb2011-06-20 19:28:19 -04008614static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008615{
Li Zefanb83cc962010-12-20 16:04:08 +08008616 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008617 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008618
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008619 if (mask & MAY_WRITE &&
8620 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8621 if (btrfs_root_readonly(root))
8622 return -EROFS;
8623 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8624 return -EACCES;
8625 }
Al Viro2830ba72011-06-20 19:16:29 -04008626 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008627}
Chris Mason39279cc2007-06-12 06:35:45 -04008628
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008629static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008630 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008631 .lookup = btrfs_lookup,
8632 .create = btrfs_create,
8633 .unlink = btrfs_unlink,
8634 .link = btrfs_link,
8635 .mkdir = btrfs_mkdir,
8636 .rmdir = btrfs_rmdir,
8637 .rename = btrfs_rename,
8638 .symlink = btrfs_symlink,
8639 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008640 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008641 .setxattr = btrfs_setxattr,
8642 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008643 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008644 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008645 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008646 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008647};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008648static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008649 .lookup = btrfs_lookup,
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};
Yan, Zheng76dda932009-09-21 16:00:26 -04008653
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008654static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008655 .llseek = generic_file_llseek,
8656 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04008657 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008658 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008659#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008660 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008661#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008662 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008663 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008664};
8665
Chris Masond1310b22008-01-24 16:13:08 -05008666static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008667 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008668 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008669 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008670 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008671 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008672 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008673 .set_bit_hook = btrfs_set_bit_hook,
8674 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008675 .merge_extent_hook = btrfs_merge_extent_hook,
8676 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008677};
8678
Chris Mason35054392009-01-21 13:11:13 -05008679/*
8680 * btrfs doesn't support the bmap operation because swapfiles
8681 * use bmap to make a mapping of extents in the file. They assume
8682 * these extents won't change over the life of the file and they
8683 * use the bmap result to do IO directly to the drive.
8684 *
8685 * the btrfs bmap call would return logical addresses that aren't
8686 * suitable for IO and they also will change frequently as COW
8687 * operations happen. So, swapfile + btrfs == corruption.
8688 *
8689 * For now we're avoiding this by dropping bmap.
8690 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008691static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008692 .readpage = btrfs_readpage,
8693 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008694 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008695 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008696 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008697 .invalidatepage = btrfs_invalidatepage,
8698 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008699 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008700 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008701};
8702
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008703static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008704 .readpage = btrfs_readpage,
8705 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008706 .invalidatepage = btrfs_invalidatepage,
8707 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008708};
8709
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008710static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008711 .getattr = btrfs_getattr,
8712 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008713 .setxattr = btrfs_setxattr,
8714 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008715 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008716 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008717 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008718 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008719 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008720 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008721};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008722static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008723 .getattr = btrfs_getattr,
8724 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008725 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008726 .setxattr = btrfs_setxattr,
8727 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008728 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008729 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008730 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008731 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008732};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008733static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008734 .readlink = generic_readlink,
8735 .follow_link = page_follow_link_light,
8736 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008737 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008738 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008739 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008740 .setxattr = btrfs_setxattr,
8741 .getxattr = btrfs_getxattr,
8742 .listxattr = btrfs_listxattr,
8743 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008744 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008745 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008746};
Yan, Zheng76dda932009-09-21 16:00:26 -04008747
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008748const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008749 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008750 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008751};