blob: 0a43d42268f7ccc2dbdc4682b518df7545ed509a [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>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040037#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040038#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020040#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050041#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000042#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050043#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040044#include <linux/posix_acl_xattr.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050045#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040060#include "hash.h"
Chris Mason39279cc2007-06-12 06:35:45 -040061
62struct btrfs_iget_args {
63 u64 ino;
64 struct btrfs_root *root;
65};
66
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070067static const struct inode_operations btrfs_dir_inode_operations;
68static const struct inode_operations btrfs_symlink_inode_operations;
69static const struct inode_operations btrfs_dir_ro_inode_operations;
70static const struct inode_operations btrfs_special_inode_operations;
71static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070072static const struct address_space_operations btrfs_aops;
73static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070074static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050075static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040076
77static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000078static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079struct kmem_cache *btrfs_trans_handle_cachep;
80struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040081struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050082struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040083
84#define S_SHIFT 12
85static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
86 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
87 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
88 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
89 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
90 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
91 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
92 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
93};
94
Eric Sandeen3972f262013-01-12 02:57:22 +000095static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050096static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040097static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050098static noinline int cow_file_range(struct inode *inode,
99 struct page *locked_page,
100 u64 start, u64 end, int *page_started,
101 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -0400102static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
103 u64 len, u64 orig_start,
104 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400105 u64 orig_block_len, u64 ram_bytes,
106 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400107
Eric Sandeen48a3b632013-04-25 20:41:01 +0000108static int btrfs_dirty_inode(struct inode *inode);
109
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000110static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500111 struct inode *inode, struct inode *dir,
112 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500113{
114 int err;
115
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000116 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500117 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500118 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500119 return err;
120}
121
Chris Masond352ac62008-09-29 15:18:18 -0400122/*
Chris Masonc8b97812008-10-29 14:49:59 -0400123 * this does all the hard work for inserting an inline extent into
124 * the btree. The caller should have done a btrfs_drop_extents so that
125 * no overlapping inline items exist in the btree
126 */
Chris Masond3977122009-01-05 21:25:51 -0500127static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400128 struct btrfs_root *root, struct inode *inode,
129 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000130 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400131 struct page **compressed_pages)
132{
133 struct btrfs_key key;
134 struct btrfs_path *path;
135 struct extent_buffer *leaf;
136 struct page *page = NULL;
137 char *kaddr;
138 unsigned long ptr;
139 struct btrfs_file_extent_item *ei;
140 int err = 0;
141 int ret;
142 size_t cur_size = size;
143 size_t datasize;
144 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400145
Li Zefanfe3f5662011-03-28 08:30:38 +0000146 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400147 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400148
Chris Masond3977122009-01-05 21:25:51 -0500149 path = btrfs_alloc_path();
150 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400151 return -ENOMEM;
152
Chris Masonb9473432009-03-13 11:00:37 -0400153 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400154
Li Zefan33345d012011-04-20 10:31:50 +0800155 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400156 key.offset = start;
157 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400158 datasize = btrfs_file_extent_calc_inline_size(cur_size);
159
160 inode_add_bytes(inode, size);
161 ret = btrfs_insert_empty_item(trans, root, path, &key,
162 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400163 if (ret) {
164 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400165 goto fail;
166 }
167 leaf = path->nodes[0];
168 ei = btrfs_item_ptr(leaf, path->slots[0],
169 struct btrfs_file_extent_item);
170 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
171 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
172 btrfs_set_file_extent_encryption(leaf, ei, 0);
173 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
174 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
175 ptr = btrfs_file_extent_inline_start(ei);
176
Li Zefan261507a02010-12-17 14:21:50 +0800177 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400178 struct page *cpage;
179 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500180 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400181 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500182 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400183 PAGE_CACHE_SIZE);
184
Cong Wang7ac687d2011-11-25 23:14:28 +0800185 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400186 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800187 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400188
189 i++;
190 ptr += cur_size;
191 compressed_size -= cur_size;
192 }
193 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800194 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400195 } else {
196 page = find_get_page(inode->i_mapping,
197 start >> PAGE_CACHE_SHIFT);
198 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800199 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400200 offset = start & (PAGE_CACHE_SIZE - 1);
201 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800202 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400203 page_cache_release(page);
204 }
205 btrfs_mark_buffer_dirty(leaf);
206 btrfs_free_path(path);
207
Yan, Zhengc2167752009-11-12 09:34:21 +0000208 /*
209 * we're an inline extent, so nobody can
210 * extend the file past i_size without locking
211 * a page we already have locked.
212 *
213 * We must do any isize and inode updates
214 * before we unlock the pages. Otherwise we
215 * could end up racing with unlink.
216 */
Chris Masonc8b97812008-10-29 14:49:59 -0400217 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100218 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000219
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100220 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400221fail:
222 btrfs_free_path(path);
223 return err;
224}
225
226
227/*
228 * conditionally insert an inline extent into the file. This
229 * does the checks required to make sure the data is small enough
230 * to fit as an inline extent.
231 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400232static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400233 struct btrfs_root *root,
234 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000235 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400236 struct page **compressed_pages)
237{
238 u64 isize = i_size_read(inode);
239 u64 actual_end = min(end + 1, isize);
240 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000241 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400242 u64 data_len = inline_len;
243 int ret;
244
245 if (compressed_size)
246 data_len = compressed_size;
247
248 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400249 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400250 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
251 (!compressed_size &&
252 (actual_end & (root->sectorsize - 1)) == 0) ||
253 end + 1 < isize ||
254 data_len > root->fs_info->max_inline) {
255 return 1;
256 }
257
Josef Bacik26714852012-08-29 12:24:27 -0400258 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100259 if (ret)
260 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400261
262 if (isize > actual_end)
263 inline_len = min_t(u64, isize, actual_end);
264 ret = insert_inline_extent(trans, root, inode, start,
265 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000266 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400267 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100268 btrfs_abort_transaction(trans, root, ret);
269 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400270 } else if (ret == -ENOSPC) {
271 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100272 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400273
Josef Bacikbdc20e62013-02-28 13:23:38 -0500274 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400275 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400276 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400277 return 0;
278}
279
Chris Mason771ed682008-11-06 22:02:51 -0500280struct async_extent {
281 u64 start;
282 u64 ram_size;
283 u64 compressed_size;
284 struct page **pages;
285 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800286 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500287 struct list_head list;
288};
289
290struct async_cow {
291 struct inode *inode;
292 struct btrfs_root *root;
293 struct page *locked_page;
294 u64 start;
295 u64 end;
296 struct list_head extents;
297 struct btrfs_work work;
298};
299
300static noinline int add_async_extent(struct async_cow *cow,
301 u64 start, u64 ram_size,
302 u64 compressed_size,
303 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800304 unsigned long nr_pages,
305 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500306{
307 struct async_extent *async_extent;
308
309 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100310 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500311 async_extent->start = start;
312 async_extent->ram_size = ram_size;
313 async_extent->compressed_size = compressed_size;
314 async_extent->pages = pages;
315 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800316 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500317 list_add_tail(&async_extent->list, &cow->extents);
318 return 0;
319}
320
Chris Masonc8b97812008-10-29 14:49:59 -0400321/*
Chris Mason771ed682008-11-06 22:02:51 -0500322 * we create compressed extents in two phases. The first
323 * phase compresses a range of pages that have already been
324 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400325 *
Chris Mason771ed682008-11-06 22:02:51 -0500326 * This is done inside an ordered work queue, and the compression
327 * is spread across many cpus. The actual IO submission is step
328 * two, and the ordered work queue takes care of making sure that
329 * happens in the same order things were put onto the queue by
330 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400331 *
Chris Mason771ed682008-11-06 22:02:51 -0500332 * If this code finds it can't get good compression, it puts an
333 * entry onto the work queue to write the uncompressed bytes. This
334 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300335 * are written in the same order that the flusher thread sent them
336 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400337 */
Chris Mason771ed682008-11-06 22:02:51 -0500338static noinline int compress_file_range(struct inode *inode,
339 struct page *locked_page,
340 u64 start, u64 end,
341 struct async_cow *async_cow,
342 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400343{
344 struct btrfs_root *root = BTRFS_I(inode)->root;
345 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400346 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400347 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400348 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500349 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400350 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400351 struct page **pages = NULL;
352 unsigned long nr_pages;
353 unsigned long nr_pages_ret = 0;
354 unsigned long total_compressed = 0;
355 unsigned long total_in = 0;
356 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500357 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400358 int i;
359 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800360 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400361 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400362
Liu Bo4cb13e52012-03-29 09:57:45 -0400363 /* if this is a small write inside eof, kick off a defrag */
364 if ((end - start + 1) < 16 * 1024 &&
365 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400366 btrfs_add_inode_defrag(NULL, inode);
367
Chris Mason42dc7ba2008-12-15 11:44:56 -0500368 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400369again:
370 will_compress = 0;
371 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
372 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
373
Chris Masonf03d9302009-02-04 09:31:06 -0500374 /*
375 * we don't want to send crud past the end of i_size through
376 * compression, that's just a waste of CPU time. So, if the
377 * end of the file is before the start of our current
378 * requested range of bytes, we bail out to the uncompressed
379 * cleanup code that can deal with all of this.
380 *
381 * It isn't really the fastest way to fix things, but this is a
382 * very uncommon corner.
383 */
384 if (actual_end <= start)
385 goto cleanup_and_bail_uncompressed;
386
Chris Masonc8b97812008-10-29 14:49:59 -0400387 total_compressed = actual_end - start;
388
389 /* we want to make sure that amount of ram required to uncompress
390 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500391 * of a compressed extent to 128k. This is a crucial number
392 * because it also controls how easily we can spread reads across
393 * cpus for decompression.
394 *
395 * We also want to make sure the amount of IO required to do
396 * a random read is reasonably small, so we limit the size of
397 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400398 */
399 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000400 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500401 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400402 total_in = 0;
403 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400404
Chris Mason771ed682008-11-06 22:02:51 -0500405 /*
406 * we do compression for mount -o compress and when the
407 * inode has not been flagged as nocompress. This flag can
408 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400409 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200410 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500411 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000412 (BTRFS_I(inode)->force_compress) ||
413 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400414 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400415 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800416 if (!pages) {
417 /* just bail out to the uncompressed code */
418 goto cont;
419 }
Chris Mason179e29e2007-11-01 11:28:41 -0400420
Li Zefan261507a02010-12-17 14:21:50 +0800421 if (BTRFS_I(inode)->force_compress)
422 compress_type = BTRFS_I(inode)->force_compress;
423
Chris Mason4adaa612013-03-26 13:07:00 -0400424 /*
425 * we need to call clear_page_dirty_for_io on each
426 * page in the range. Otherwise applications with the file
427 * mmap'd can wander in and change the page contents while
428 * we are compressing them.
429 *
430 * If the compression fails for any reason, we set the pages
431 * dirty again later on.
432 */
433 extent_range_clear_dirty_for_io(inode, start, end);
434 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800435 ret = btrfs_compress_pages(compress_type,
436 inode->i_mapping, start,
437 total_compressed, pages,
438 nr_pages, &nr_pages_ret,
439 &total_in,
440 &total_compressed,
441 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400442
443 if (!ret) {
444 unsigned long offset = total_compressed &
445 (PAGE_CACHE_SIZE - 1);
446 struct page *page = pages[nr_pages_ret - 1];
447 char *kaddr;
448
449 /* zero the tail end of the last page, we might be
450 * sending it down to disk
451 */
452 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800453 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400454 memset(kaddr + offset, 0,
455 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800456 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400457 }
458 will_compress = 1;
459 }
460 }
Li Zefan560f7d72011-09-08 10:22:01 +0800461cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400462 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400463 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100464 if (IS_ERR(trans)) {
465 ret = PTR_ERR(trans);
466 trans = NULL;
467 goto cleanup_and_out;
468 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400469 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500470
Chris Masonc8b97812008-10-29 14:49:59 -0400471 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500472 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400473 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500474 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400475 */
476 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000477 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400478 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500479 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400480 ret = cow_file_range_inline(trans, root, inode,
481 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000482 total_compressed,
483 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400484 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100485 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500486 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100487 * inline extent creation worked or returned error,
488 * we don't need to create any more async work items.
489 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500490 */
Chris Masonc8b97812008-10-29 14:49:59 -0400491 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400492 &BTRFS_I(inode)->io_tree,
493 start, end, NULL,
494 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400495 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400496 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000497
498 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400499 goto free_pages_out;
500 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000501 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400502 }
503
504 if (will_compress) {
505 /*
506 * we aren't doing an inline extent round the compressed size
507 * up to a block size boundary so the allocator does sane
508 * things
509 */
Qu Wenruofda28322013-02-26 08:10:22 +0000510 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400511
512 /*
513 * one last check to make sure the compression is really a
514 * win, compare the page count read with the blocks on disk
515 */
Qu Wenruofda28322013-02-26 08:10:22 +0000516 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400517 if (total_compressed >= total_in) {
518 will_compress = 0;
519 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400520 num_bytes = total_in;
521 }
522 }
523 if (!will_compress && pages) {
524 /*
525 * the compression code ran but failed to make things smaller,
526 * free any pages it allocated and our page pointer array
527 */
528 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400529 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400530 page_cache_release(pages[i]);
531 }
532 kfree(pages);
533 pages = NULL;
534 total_compressed = 0;
535 nr_pages_ret = 0;
536
537 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500538 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
539 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500540 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500541 }
Chris Masonc8b97812008-10-29 14:49:59 -0400542 }
Chris Mason771ed682008-11-06 22:02:51 -0500543 if (will_compress) {
544 *num_added += 1;
545
546 /* the async work queues will take care of doing actual
547 * allocation on disk for these compressed pages,
548 * and will submit them to the elevator.
549 */
550 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800551 total_compressed, pages, nr_pages_ret,
552 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500553
Yan, Zheng24ae6362010-12-06 07:02:36 +0000554 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500555 start += num_bytes;
556 pages = NULL;
557 cond_resched();
558 goto again;
559 }
560 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500561cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500562 /*
563 * No compression, but we still need to write the pages in
564 * the file we've been given so far. redirty the locked
565 * page if it corresponds to our extent and set things up
566 * for the async work queue to run cow_file_range to do
567 * the normal delalloc dance
568 */
569 if (page_offset(locked_page) >= start &&
570 page_offset(locked_page) <= end) {
571 __set_page_dirty_nobuffers(locked_page);
572 /* unlocked later on in the async handlers */
573 }
Chris Mason4adaa612013-03-26 13:07:00 -0400574 if (redirty)
575 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800576 add_async_extent(async_cow, start, end - start + 1,
577 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500578 *num_added += 1;
579 }
580
581out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100582 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500583
584free_pages_out:
585 for (i = 0; i < nr_pages_ret; i++) {
586 WARN_ON(pages[i]->mapping);
587 page_cache_release(pages[i]);
588 }
Chris Masond3977122009-01-05 21:25:51 -0500589 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500590
591 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100592
593cleanup_and_out:
594 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
595 start, end, NULL,
596 EXTENT_CLEAR_UNLOCK_PAGE |
597 EXTENT_CLEAR_DIRTY |
598 EXTENT_CLEAR_DELALLOC |
599 EXTENT_SET_WRITEBACK |
600 EXTENT_END_WRITEBACK);
601 if (!trans || IS_ERR(trans))
602 btrfs_error(root->fs_info, ret, "Failed to join transaction");
603 else
604 btrfs_abort_transaction(trans, root, ret);
605 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500606}
607
608/*
609 * phase two of compressed writeback. This is the ordered portion
610 * of the code, which only gets called in the order the work was
611 * queued. We walk all the async extents created by compress_file_range
612 * and send them down to the disk.
613 */
614static noinline int submit_compressed_extents(struct inode *inode,
615 struct async_cow *async_cow)
616{
617 struct async_extent *async_extent;
618 u64 alloc_hint = 0;
619 struct btrfs_trans_handle *trans;
620 struct btrfs_key ins;
621 struct extent_map *em;
622 struct btrfs_root *root = BTRFS_I(inode)->root;
623 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
624 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500625 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500626
627 if (list_empty(&async_cow->extents))
628 return 0;
629
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500630again:
Chris Masond3977122009-01-05 21:25:51 -0500631 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500632 async_extent = list_entry(async_cow->extents.next,
633 struct async_extent, list);
634 list_del(&async_extent->list);
635
636 io_tree = &BTRFS_I(inode)->io_tree;
637
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500638retry:
Chris Mason771ed682008-11-06 22:02:51 -0500639 /* did the compression code fall back to uncompressed IO? */
640 if (!async_extent->pages) {
641 int page_started = 0;
642 unsigned long nr_written = 0;
643
644 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000645 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100646 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500647
648 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500649 ret = cow_file_range(inode, async_cow->locked_page,
650 async_extent->start,
651 async_extent->start +
652 async_extent->ram_size - 1,
653 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500654
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100655 /* JDM XXX */
656
Chris Mason771ed682008-11-06 22:02:51 -0500657 /*
658 * if page_started, cow_file_range inserted an
659 * inline extent and took care of all the unlocking
660 * and IO for us. Otherwise, we need to submit
661 * all those pages down to the drive.
662 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500663 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500664 extent_write_locked_range(io_tree,
665 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500666 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500667 async_extent->ram_size - 1,
668 btrfs_get_extent,
669 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500670 else if (ret)
671 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500672 kfree(async_extent);
673 cond_resched();
674 continue;
675 }
676
677 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100678 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500679
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400680 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100681 if (IS_ERR(trans)) {
682 ret = PTR_ERR(trans);
683 } else {
684 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
685 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500686 async_extent->compressed_size,
687 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500688 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600689 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100690 btrfs_abort_transaction(trans, root, ret);
691 btrfs_end_transaction(trans, root);
692 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000693
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500694 if (ret) {
695 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500696
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500697 for (i = 0; i < async_extent->nr_pages; i++) {
698 WARN_ON(async_extent->pages[i]->mapping);
699 page_cache_release(async_extent->pages[i]);
700 }
701 kfree(async_extent->pages);
702 async_extent->nr_pages = 0;
703 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500704
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400705 if (ret == -ENOSPC) {
706 unlock_extent(io_tree, async_extent->start,
707 async_extent->start +
708 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100709 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400710 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500711 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500712 }
713
Yan, Zhengc2167752009-11-12 09:34:21 +0000714 /*
715 * here we're doing allocation and writeback of the
716 * compressed pages
717 */
718 btrfs_drop_extent_cache(inode, async_extent->start,
719 async_extent->start +
720 async_extent->ram_size - 1, 0);
721
David Sterba172ddd62011-04-21 00:48:27 +0200722 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000723 if (!em) {
724 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500725 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000726 }
Chris Mason771ed682008-11-06 22:02:51 -0500727 em->start = async_extent->start;
728 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500729 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400730 em->mod_start = em->start;
731 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500732
733 em->block_start = ins.objectid;
734 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500735 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400736 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500737 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800738 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500739 set_bit(EXTENT_FLAG_PINNED, &em->flags);
740 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400741 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500742
Chris Masond3977122009-01-05 21:25:51 -0500743 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400744 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400745 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400746 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500747 if (ret != -EEXIST) {
748 free_extent_map(em);
749 break;
750 }
751 btrfs_drop_extent_cache(inode, async_extent->start,
752 async_extent->start +
753 async_extent->ram_size - 1, 0);
754 }
755
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500756 if (ret)
757 goto out_free_reserve;
758
Li Zefan261507a02010-12-17 14:21:50 +0800759 ret = btrfs_add_ordered_extent_compress(inode,
760 async_extent->start,
761 ins.objectid,
762 async_extent->ram_size,
763 ins.offset,
764 BTRFS_ORDERED_COMPRESSED,
765 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500766 if (ret)
767 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500768
Chris Mason771ed682008-11-06 22:02:51 -0500769 /*
770 * clear dirty, set writeback and unlock the pages.
771 */
772 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400773 &BTRFS_I(inode)->io_tree,
774 async_extent->start,
775 async_extent->start +
776 async_extent->ram_size - 1,
777 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
778 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400779 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400780 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500781
782 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500783 async_extent->start,
784 async_extent->ram_size,
785 ins.objectid,
786 ins.offset, async_extent->pages,
787 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500788 alloc_hint = ins.objectid + ins.offset;
789 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500790 if (ret)
791 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500792 cond_resched();
793 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100794 ret = 0;
795out:
796 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500797out_free_reserve:
798 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100799out_free:
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500800 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
801 async_extent->start,
802 async_extent->start +
803 async_extent->ram_size - 1,
804 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
805 EXTENT_CLEAR_UNLOCK |
806 EXTENT_CLEAR_DELALLOC |
807 EXTENT_CLEAR_DIRTY |
808 EXTENT_SET_WRITEBACK |
809 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100810 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500811 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500812}
813
Josef Bacik4b46fce2010-05-23 11:00:55 -0400814static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
815 u64 num_bytes)
816{
817 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
818 struct extent_map *em;
819 u64 alloc_hint = 0;
820
821 read_lock(&em_tree->lock);
822 em = search_extent_mapping(em_tree, start, num_bytes);
823 if (em) {
824 /*
825 * if block start isn't an actual block number then find the
826 * first block in this inode and use that as a hint. If that
827 * block is also bogus then just don't worry about it.
828 */
829 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
830 free_extent_map(em);
831 em = search_extent_mapping(em_tree, 0, 0);
832 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
833 alloc_hint = em->block_start;
834 if (em)
835 free_extent_map(em);
836 } else {
837 alloc_hint = em->block_start;
838 free_extent_map(em);
839 }
840 }
841 read_unlock(&em_tree->lock);
842
843 return alloc_hint;
844}
845
Chris Mason771ed682008-11-06 22:02:51 -0500846/*
847 * when extent_io.c finds a delayed allocation range in the file,
848 * the call backs end up in this code. The basic idea is to
849 * allocate extents on disk for the range, and create ordered data structs
850 * in ram to track those extents.
851 *
852 * locked_page is the page that writepage had locked already. We use
853 * it to make sure we don't do extra locks or unlocks.
854 *
855 * *page_started is set to one if we unlock locked_page and do everything
856 * required to start IO on it. It may be clean and already done with
857 * IO when we return.
858 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000859static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
860 struct inode *inode,
861 struct btrfs_root *root,
862 struct page *locked_page,
863 u64 start, u64 end, int *page_started,
864 unsigned long *nr_written,
865 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500866{
Chris Mason771ed682008-11-06 22:02:51 -0500867 u64 alloc_hint = 0;
868 u64 num_bytes;
869 unsigned long ram_size;
870 u64 disk_num_bytes;
871 u64 cur_alloc_size;
872 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500873 struct btrfs_key ins;
874 struct extent_map *em;
875 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
876 int ret = 0;
877
Liu Bo83eea1f2012-07-10 05:28:39 -0600878 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500879
Qu Wenruofda28322013-02-26 08:10:22 +0000880 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500881 num_bytes = max(blocksize, num_bytes);
882 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500883
Chris Mason4cb53002011-05-24 15:35:30 -0400884 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400885 if (num_bytes < 64 * 1024 &&
886 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400887 btrfs_add_inode_defrag(trans, inode);
888
Chris Mason771ed682008-11-06 22:02:51 -0500889 if (start == 0) {
890 /* lets try to make an inline extent */
891 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000892 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500893 if (ret == 0) {
894 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400895 &BTRFS_I(inode)->io_tree,
896 start, end, NULL,
897 EXTENT_CLEAR_UNLOCK_PAGE |
898 EXTENT_CLEAR_UNLOCK |
899 EXTENT_CLEAR_DELALLOC |
900 EXTENT_CLEAR_DIRTY |
901 EXTENT_SET_WRITEBACK |
902 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000903
Chris Mason771ed682008-11-06 22:02:51 -0500904 *nr_written = *nr_written +
905 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
906 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500907 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100908 } else if (ret < 0) {
909 btrfs_abort_transaction(trans, root, ret);
910 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500911 }
912 }
Chris Masonc8b97812008-10-29 14:49:59 -0400913
914 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200915 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400916
Josef Bacik4b46fce2010-05-23 11:00:55 -0400917 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400918 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400919
Chris Masond3977122009-01-05 21:25:51 -0500920 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400921 unsigned long op;
922
Josef Bacik287a0ab2010-03-19 18:07:23 +0000923 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400924 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500925 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500926 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100927 if (ret < 0) {
928 btrfs_abort_transaction(trans, root, ret);
929 goto out_unlock;
930 }
Chris Masond3977122009-01-05 21:25:51 -0500931
David Sterba172ddd62011-04-21 00:48:27 +0200932 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000933 if (!em) {
934 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000935 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000936 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400937 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500938 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500939 ram_size = ins.offset;
940 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400941 em->mod_start = em->start;
942 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400943
Chris Masone6dcd2d2008-07-17 12:53:50 -0400944 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400945 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500946 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400947 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400948 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400949 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400950 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400951
Chris Masond3977122009-01-05 21:25:51 -0500952 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400953 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400954 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400955 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400956 if (ret != -EEXIST) {
957 free_extent_map(em);
958 break;
959 }
960 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400961 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400962 }
Liu Boace68ba2013-04-22 10:53:47 +0000963 if (ret)
964 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400965
Chris Mason98d20f62008-04-14 09:46:10 -0400966 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400967 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500968 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000969 if (ret)
970 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400971
Yan Zheng17d217f2008-12-12 10:03:38 -0500972 if (root->root_key.objectid ==
973 BTRFS_DATA_RELOC_TREE_OBJECTID) {
974 ret = btrfs_reloc_clone_csums(inode, start,
975 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100976 if (ret) {
977 btrfs_abort_transaction(trans, root, ret);
Liu Boace68ba2013-04-22 10:53:47 +0000978 goto out_reserve;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100979 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500980 }
981
Chris Masond3977122009-01-05 21:25:51 -0500982 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400983 break;
Chris Masond3977122009-01-05 21:25:51 -0500984
Chris Masonc8b97812008-10-29 14:49:59 -0400985 /* we're not doing compressed IO, don't unlock the first
986 * page (which the caller expects to stay locked), don't
987 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400988 *
989 * Do set the Private2 bit so we know this page was properly
990 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400991 */
Chris Masona791e352009-10-08 11:27:10 -0400992 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
993 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
994 EXTENT_SET_PRIVATE2;
995
Chris Masonc8b97812008-10-29 14:49:59 -0400996 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
997 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400998 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400999 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001000 num_bytes -= cur_alloc_size;
1001 alloc_hint = ins.objectid + ins.offset;
1002 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001003 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001004out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001005 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001006
Liu Boace68ba2013-04-22 10:53:47 +00001007out_reserve:
1008 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001009out_unlock:
1010 extent_clear_unlock_delalloc(inode,
1011 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -04001012 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001013 EXTENT_CLEAR_UNLOCK_PAGE |
1014 EXTENT_CLEAR_UNLOCK |
1015 EXTENT_CLEAR_DELALLOC |
1016 EXTENT_CLEAR_DIRTY |
1017 EXTENT_SET_WRITEBACK |
1018 EXTENT_END_WRITEBACK);
1019
1020 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001021}
Chris Masonc8b97812008-10-29 14:49:59 -04001022
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001023static noinline int cow_file_range(struct inode *inode,
1024 struct page *locked_page,
1025 u64 start, u64 end, int *page_started,
1026 unsigned long *nr_written,
1027 int unlock)
1028{
1029 struct btrfs_trans_handle *trans;
1030 struct btrfs_root *root = BTRFS_I(inode)->root;
1031 int ret;
1032
1033 trans = btrfs_join_transaction(root);
1034 if (IS_ERR(trans)) {
1035 extent_clear_unlock_delalloc(inode,
1036 &BTRFS_I(inode)->io_tree,
1037 start, end, locked_page,
1038 EXTENT_CLEAR_UNLOCK_PAGE |
1039 EXTENT_CLEAR_UNLOCK |
1040 EXTENT_CLEAR_DELALLOC |
1041 EXTENT_CLEAR_DIRTY |
1042 EXTENT_SET_WRITEBACK |
1043 EXTENT_END_WRITEBACK);
1044 return PTR_ERR(trans);
1045 }
1046 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1047
1048 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1049 page_started, nr_written, unlock);
1050
1051 btrfs_end_transaction(trans, root);
1052
1053 return ret;
1054}
1055
Chris Mason771ed682008-11-06 22:02:51 -05001056/*
1057 * work queue call back to started compression on a file and pages
1058 */
1059static noinline void async_cow_start(struct btrfs_work *work)
1060{
1061 struct async_cow *async_cow;
1062 int num_added = 0;
1063 async_cow = container_of(work, struct async_cow, work);
1064
1065 compress_file_range(async_cow->inode, async_cow->locked_page,
1066 async_cow->start, async_cow->end, async_cow,
1067 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001068 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001069 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001070 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001071 }
Chris Mason771ed682008-11-06 22:02:51 -05001072}
1073
1074/*
1075 * work queue call back to submit previously compressed pages
1076 */
1077static noinline void async_cow_submit(struct btrfs_work *work)
1078{
1079 struct async_cow *async_cow;
1080 struct btrfs_root *root;
1081 unsigned long nr_pages;
1082
1083 async_cow = container_of(work, struct async_cow, work);
1084
1085 root = async_cow->root;
1086 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1087 PAGE_CACHE_SHIFT;
1088
Josef Bacik66657b32012-08-01 15:36:24 -04001089 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001090 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001091 waitqueue_active(&root->fs_info->async_submit_wait))
1092 wake_up(&root->fs_info->async_submit_wait);
1093
Chris Masond3977122009-01-05 21:25:51 -05001094 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001095 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001096}
Chris Masonc8b97812008-10-29 14:49:59 -04001097
Chris Mason771ed682008-11-06 22:02:51 -05001098static noinline void async_cow_free(struct btrfs_work *work)
1099{
1100 struct async_cow *async_cow;
1101 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001102 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001103 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001104 kfree(async_cow);
1105}
1106
1107static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1108 u64 start, u64 end, int *page_started,
1109 unsigned long *nr_written)
1110{
1111 struct async_cow *async_cow;
1112 struct btrfs_root *root = BTRFS_I(inode)->root;
1113 unsigned long nr_pages;
1114 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001115 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001116
Chris Masona3429ab2009-10-08 12:30:20 -04001117 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1118 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001119 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001120 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001121 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001122 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001123 async_cow->root = root;
1124 async_cow->locked_page = locked_page;
1125 async_cow->start = start;
1126
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001127 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001128 cur_end = end;
1129 else
1130 cur_end = min(end, start + 512 * 1024 - 1);
1131
1132 async_cow->end = cur_end;
1133 INIT_LIST_HEAD(&async_cow->extents);
1134
1135 async_cow->work.func = async_cow_start;
1136 async_cow->work.ordered_func = async_cow_submit;
1137 async_cow->work.ordered_free = async_cow_free;
1138 async_cow->work.flags = 0;
1139
Chris Mason771ed682008-11-06 22:02:51 -05001140 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1141 PAGE_CACHE_SHIFT;
1142 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1143
1144 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1145 &async_cow->work);
1146
1147 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1148 wait_event(root->fs_info->async_submit_wait,
1149 (atomic_read(&root->fs_info->async_delalloc_pages) <
1150 limit));
1151 }
1152
Chris Masond3977122009-01-05 21:25:51 -05001153 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001154 atomic_read(&root->fs_info->async_delalloc_pages)) {
1155 wait_event(root->fs_info->async_submit_wait,
1156 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1157 0));
1158 }
1159
1160 *nr_written += nr_pages;
1161 start = cur_end + 1;
1162 }
1163 *page_started = 1;
1164 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001165}
1166
Chris Masond3977122009-01-05 21:25:51 -05001167static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001168 u64 bytenr, u64 num_bytes)
1169{
1170 int ret;
1171 struct btrfs_ordered_sum *sums;
1172 LIST_HEAD(list);
1173
Yan Zheng07d400a2009-01-06 11:42:00 -05001174 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001175 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001176 if (ret == 0 && list_empty(&list))
1177 return 0;
1178
1179 while (!list_empty(&list)) {
1180 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1181 list_del(&sums->list);
1182 kfree(sums);
1183 }
1184 return 1;
1185}
1186
Chris Masond352ac62008-09-29 15:18:18 -04001187/*
1188 * when nowcow writeback call back. This checks for snapshots or COW copies
1189 * of the extents that exist in the file, and COWs the file as required.
1190 *
1191 * If no cow copies or snapshots exist, we write directly to the existing
1192 * blocks on disk
1193 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001194static noinline int run_delalloc_nocow(struct inode *inode,
1195 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001196 u64 start, u64 end, int *page_started, int force,
1197 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001198{
Chris Masonbe20aa92007-12-17 20:14:01 -05001199 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001200 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001201 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001202 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001203 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001204 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001205 u64 cow_start;
1206 u64 cur_offset;
1207 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001208 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001209 u64 disk_bytenr;
1210 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001211 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001212 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001213 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001214 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001215 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001216 int nocow;
1217 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001218 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001219 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001220
1221 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001222 if (!path) {
1223 extent_clear_unlock_delalloc(inode,
1224 &BTRFS_I(inode)->io_tree,
1225 start, end, locked_page,
1226 EXTENT_CLEAR_UNLOCK_PAGE |
1227 EXTENT_CLEAR_UNLOCK |
1228 EXTENT_CLEAR_DELALLOC |
1229 EXTENT_CLEAR_DIRTY |
1230 EXTENT_SET_WRITEBACK |
1231 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001232 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001233 }
Li Zefan82d59022011-04-20 10:33:24 +08001234
Liu Bo83eea1f2012-07-10 05:28:39 -06001235 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001236
1237 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001238 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001239 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001240 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001241
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001242 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001243 extent_clear_unlock_delalloc(inode,
1244 &BTRFS_I(inode)->io_tree,
1245 start, end, locked_page,
1246 EXTENT_CLEAR_UNLOCK_PAGE |
1247 EXTENT_CLEAR_UNLOCK |
1248 EXTENT_CLEAR_DELALLOC |
1249 EXTENT_CLEAR_DIRTY |
1250 EXTENT_SET_WRITEBACK |
1251 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001252 btrfs_free_path(path);
1253 return PTR_ERR(trans);
1254 }
1255
Josef Bacik74b21072011-04-13 12:02:53 -04001256 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001257
Yan Zheng80ff3852008-10-30 14:20:02 -04001258 cow_start = (u64)-1;
1259 cur_offset = start;
1260 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001261 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001263 if (ret < 0) {
1264 btrfs_abort_transaction(trans, root, ret);
1265 goto error;
1266 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001267 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1268 leaf = path->nodes[0];
1269 btrfs_item_key_to_cpu(leaf, &found_key,
1270 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001271 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001272 found_key.type == BTRFS_EXTENT_DATA_KEY)
1273 path->slots[0]--;
1274 }
1275 check_prev = 0;
1276next_slot:
1277 leaf = path->nodes[0];
1278 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1279 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001280 if (ret < 0) {
1281 btrfs_abort_transaction(trans, root, ret);
1282 goto error;
1283 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001284 if (ret > 0)
1285 break;
1286 leaf = path->nodes[0];
1287 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001288
Yan Zheng80ff3852008-10-30 14:20:02 -04001289 nocow = 0;
1290 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001291 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001292 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001293
Li Zefan33345d012011-04-20 10:31:50 +08001294 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001295 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1296 found_key.offset > end)
1297 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001298
Yan Zheng80ff3852008-10-30 14:20:02 -04001299 if (found_key.offset > cur_offset) {
1300 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001301 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001302 goto out_check;
1303 }
Chris Masonc31f8832008-01-08 15:46:31 -05001304
Yan Zheng80ff3852008-10-30 14:20:02 -04001305 fi = btrfs_item_ptr(leaf, path->slots[0],
1306 struct btrfs_file_extent_item);
1307 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001308
Josef Bacikcc95bef2013-04-04 14:31:27 -04001309 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001310 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1311 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001312 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001313 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001314 extent_end = found_key.offset +
1315 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001316 disk_num_bytes =
1317 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001318 if (extent_end <= start) {
1319 path->slots[0]++;
1320 goto next_slot;
1321 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001322 if (disk_bytenr == 0)
1323 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001324 if (btrfs_file_extent_compression(leaf, fi) ||
1325 btrfs_file_extent_encryption(leaf, fi) ||
1326 btrfs_file_extent_other_encoding(leaf, fi))
1327 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001328 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1329 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001330 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001331 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001332 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001333 found_key.offset -
1334 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001335 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001336 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001337 disk_bytenr += cur_offset - found_key.offset;
1338 num_bytes = min(end + 1, extent_end) - cur_offset;
1339 /*
1340 * force cow if csum exists in the range.
1341 * this ensure that csum for a given extent are
1342 * either valid or do not exist.
1343 */
1344 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1345 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001346 nocow = 1;
1347 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1348 extent_end = found_key.offset +
1349 btrfs_file_extent_inline_len(leaf, fi);
1350 extent_end = ALIGN(extent_end, root->sectorsize);
1351 } else {
1352 BUG_ON(1);
1353 }
1354out_check:
1355 if (extent_end <= start) {
1356 path->slots[0]++;
1357 goto next_slot;
1358 }
1359 if (!nocow) {
1360 if (cow_start == (u64)-1)
1361 cow_start = cur_offset;
1362 cur_offset = extent_end;
1363 if (cur_offset > end)
1364 break;
1365 path->slots[0]++;
1366 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001367 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001368
David Sterbab3b4aa72011-04-21 01:20:15 +02001369 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001370 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001371 ret = __cow_file_range(trans, inode, root, locked_page,
1372 cow_start, found_key.offset - 1,
1373 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001374 if (ret) {
1375 btrfs_abort_transaction(trans, root, ret);
1376 goto error;
1377 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001378 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001379 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001380
Yan Zhengd899e052008-10-30 14:25:28 -04001381 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1382 struct extent_map *em;
1383 struct extent_map_tree *em_tree;
1384 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001385 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001386 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001387 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001388 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001389 em->len = num_bytes;
1390 em->block_len = num_bytes;
1391 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001392 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001393 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001394 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001395 em->mod_start = em->start;
1396 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001397 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001398 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001399 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001400 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001401 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001402 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001403 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001404 if (ret != -EEXIST) {
1405 free_extent_map(em);
1406 break;
1407 }
1408 btrfs_drop_extent_cache(inode, em->start,
1409 em->start + em->len - 1, 0);
1410 }
1411 type = BTRFS_ORDERED_PREALLOC;
1412 } else {
1413 type = BTRFS_ORDERED_NOCOW;
1414 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001415
1416 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001417 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001418 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001419
Yan, Zhengefa56462010-05-16 10:49:59 -04001420 if (root->root_key.objectid ==
1421 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1422 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1423 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001424 if (ret) {
1425 btrfs_abort_transaction(trans, root, ret);
1426 goto error;
1427 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001428 }
1429
Yan Zhengd899e052008-10-30 14:25:28 -04001430 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001431 cur_offset, cur_offset + num_bytes - 1,
1432 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1433 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1434 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001435 cur_offset = extent_end;
1436 if (cur_offset > end)
1437 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001438 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001439 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001440
Josef Bacik17ca04a2012-05-31 15:58:55 -04001441 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001442 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001443 cur_offset = end;
1444 }
1445
Yan Zheng80ff3852008-10-30 14:20:02 -04001446 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001447 ret = __cow_file_range(trans, inode, root, locked_page,
1448 cow_start, end,
1449 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001450 if (ret) {
1451 btrfs_abort_transaction(trans, root, ret);
1452 goto error;
1453 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001454 }
1455
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001456error:
Miao Xiea698d0752012-09-20 01:51:59 -06001457 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001458 if (!ret)
1459 ret = err;
1460
Josef Bacik17ca04a2012-05-31 15:58:55 -04001461 if (ret && cur_offset < end)
1462 extent_clear_unlock_delalloc(inode,
1463 &BTRFS_I(inode)->io_tree,
1464 cur_offset, end, locked_page,
1465 EXTENT_CLEAR_UNLOCK_PAGE |
1466 EXTENT_CLEAR_UNLOCK |
1467 EXTENT_CLEAR_DELALLOC |
1468 EXTENT_CLEAR_DIRTY |
1469 EXTENT_SET_WRITEBACK |
1470 EXTENT_END_WRITEBACK);
1471
Yan Zheng7ea394f2008-08-05 13:05:02 -04001472 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001473 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001474}
1475
Chris Masond352ac62008-09-29 15:18:18 -04001476/*
1477 * extent_io.c call back to do delayed allocation processing
1478 */
Chris Masonc8b97812008-10-29 14:49:59 -04001479static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001480 u64 start, u64 end, int *page_started,
1481 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001482{
Chris Masonbe20aa92007-12-17 20:14:01 -05001483 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001484 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001485
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001486 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001487 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001488 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001489 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001490 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001491 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001492 } else if (!btrfs_test_opt(root, COMPRESS) &&
1493 !(BTRFS_I(inode)->force_compress) &&
1494 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001495 ret = cow_file_range(inode, locked_page, start, end,
1496 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001497 } else {
1498 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1499 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001500 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001501 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001502 }
Chris Masonb888db22007-08-27 16:49:44 -04001503 return ret;
1504}
1505
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001506static void btrfs_split_extent_hook(struct inode *inode,
1507 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001508{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001509 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001510 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001511 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001512
Josef Bacik9e0baf62011-07-15 15:16:44 +00001513 spin_lock(&BTRFS_I(inode)->lock);
1514 BTRFS_I(inode)->outstanding_extents++;
1515 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001516}
1517
1518/*
1519 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1520 * extents so we can keep track of new extents that are just merged onto old
1521 * extents, such as when we are doing sequential writes, so we can properly
1522 * account for the metadata space we'll need.
1523 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001524static void btrfs_merge_extent_hook(struct inode *inode,
1525 struct extent_state *new,
1526 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001527{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001528 /* not delalloc, ignore it */
1529 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001530 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001531
Josef Bacik9e0baf62011-07-15 15:16:44 +00001532 spin_lock(&BTRFS_I(inode)->lock);
1533 BTRFS_I(inode)->outstanding_extents--;
1534 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001535}
1536
Miao Xieeb73c1b2013-05-15 07:48:22 +00001537static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1538 struct inode *inode)
1539{
1540 spin_lock(&root->delalloc_lock);
1541 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1542 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1543 &root->delalloc_inodes);
1544 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1545 &BTRFS_I(inode)->runtime_flags);
1546 root->nr_delalloc_inodes++;
1547 if (root->nr_delalloc_inodes == 1) {
1548 spin_lock(&root->fs_info->delalloc_root_lock);
1549 BUG_ON(!list_empty(&root->delalloc_root));
1550 list_add_tail(&root->delalloc_root,
1551 &root->fs_info->delalloc_roots);
1552 spin_unlock(&root->fs_info->delalloc_root_lock);
1553 }
1554 }
1555 spin_unlock(&root->delalloc_lock);
1556}
1557
1558static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1559 struct inode *inode)
1560{
1561 spin_lock(&root->delalloc_lock);
1562 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1563 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1564 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1565 &BTRFS_I(inode)->runtime_flags);
1566 root->nr_delalloc_inodes--;
1567 if (!root->nr_delalloc_inodes) {
1568 spin_lock(&root->fs_info->delalloc_root_lock);
1569 BUG_ON(list_empty(&root->delalloc_root));
1570 list_del_init(&root->delalloc_root);
1571 spin_unlock(&root->fs_info->delalloc_root_lock);
1572 }
1573 }
1574 spin_unlock(&root->delalloc_lock);
1575}
1576
Chris Masond352ac62008-09-29 15:18:18 -04001577/*
1578 * extent_io.c set_bit_hook, used to track delayed allocation
1579 * bytes in this file, and to maintain the list of inodes that
1580 * have pending delalloc work to be done.
1581 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001582static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001583 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001584{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001585
Chris Mason75eff682008-12-15 15:54:40 -05001586 /*
1587 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001588 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001589 * bit, which is only set or cleared with irqs on
1590 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001591 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001592 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001593 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001594 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001595
Josef Bacik9e0baf62011-07-15 15:16:44 +00001596 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001597 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001598 } else {
1599 spin_lock(&BTRFS_I(inode)->lock);
1600 BTRFS_I(inode)->outstanding_extents++;
1601 spin_unlock(&BTRFS_I(inode)->lock);
1602 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001603
Miao Xie963d6782013-01-29 10:10:51 +00001604 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1605 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001606 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001607 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001608 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001609 &BTRFS_I(inode)->runtime_flags))
1610 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001611 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001612 }
Chris Mason291d6732008-01-29 15:55:23 -05001613}
1614
Chris Masond352ac62008-09-29 15:18:18 -04001615/*
1616 * extent_io.c clear_bit_hook, see set_bit_hook for why
1617 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001618static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001619 struct extent_state *state,
1620 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001621{
Chris Mason75eff682008-12-15 15:54:40 -05001622 /*
1623 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001624 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001625 * bit, which is only set or cleared with irqs on
1626 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001627 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001628 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001629 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001630 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001631
Josef Bacik9e0baf62011-07-15 15:16:44 +00001632 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001633 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001634 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1635 spin_lock(&BTRFS_I(inode)->lock);
1636 BTRFS_I(inode)->outstanding_extents--;
1637 spin_unlock(&BTRFS_I(inode)->lock);
1638 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001639
1640 if (*bits & EXTENT_DO_ACCOUNTING)
1641 btrfs_delalloc_release_metadata(inode, len);
1642
Josef Bacik0cb59c92010-07-02 12:14:14 -04001643 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001644 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001645 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001646
Miao Xie963d6782013-01-29 10:10:51 +00001647 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1648 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001649 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001650 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001651 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001652 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001653 &BTRFS_I(inode)->runtime_flags))
1654 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001655 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001656 }
Chris Mason291d6732008-01-29 15:55:23 -05001657}
1658
Chris Masond352ac62008-09-29 15:18:18 -04001659/*
1660 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1661 * we don't create bios that span stripes or chunks
1662 */
David Woodhouse64a16702009-07-15 23:29:37 +01001663int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001664 size_t size, struct bio *bio,
1665 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001666{
1667 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001668 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001669 u64 length = 0;
1670 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001671 int ret;
1672
Chris Mason771ed682008-11-06 22:02:51 -05001673 if (bio_flags & EXTENT_BIO_COMPRESSED)
1674 return 0;
1675
Chris Masonf2d8d742008-04-21 10:03:05 -04001676 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001677 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001678 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001679 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001680 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001681 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001682 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001683 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001684 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001685}
1686
Chris Masond352ac62008-09-29 15:18:18 -04001687/*
1688 * in order to insert checksums into the metadata in large chunks,
1689 * we wait until bio submission time. All the pages in the bio are
1690 * checksummed and sums are attached onto the ordered extent record.
1691 *
1692 * At IO completion time the cums attached on the ordered extent record
1693 * are inserted into the btree
1694 */
Chris Masond3977122009-01-05 21:25:51 -05001695static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1696 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001697 unsigned long bio_flags,
1698 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001699{
Chris Mason065631f2008-02-20 12:07:25 -05001700 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001701 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001702
Chris Masond20f7042008-12-08 16:58:54 -05001703 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001704 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001705 return 0;
1706}
Chris Masone0156402008-04-16 11:15:20 -04001707
Chris Mason4a69a412008-11-06 22:03:00 -05001708/*
1709 * in order to insert checksums into the metadata in large chunks,
1710 * we wait until bio submission time. All the pages in the bio are
1711 * checksummed and sums are attached onto the ordered extent record.
1712 *
1713 * At IO completion time the cums attached on the ordered extent record
1714 * are inserted into the btree
1715 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001716static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001717 int mirror_num, unsigned long bio_flags,
1718 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001719{
1720 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001721 int ret;
1722
1723 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1724 if (ret)
1725 bio_endio(bio, ret);
1726 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001727}
1728
Chris Masond352ac62008-09-29 15:18:18 -04001729/*
Chris Masoncad321a2008-12-17 14:51:42 -05001730 * extent_io.c submission hook. This does the right thing for csum calculation
1731 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001732 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001733static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001734 int mirror_num, unsigned long bio_flags,
1735 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001736{
1737 struct btrfs_root *root = BTRFS_I(inode)->root;
1738 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001739 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001740 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001741 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001742
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001743 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001744
Liu Bo83eea1f2012-07-10 05:28:39 -06001745 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001746 metadata = 2;
1747
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001748 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001749 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1750 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001751 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001752
Chris Masond20f7042008-12-08 16:58:54 -05001753 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001754 ret = btrfs_submit_compressed_read(inode, bio,
1755 mirror_num,
1756 bio_flags);
1757 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001758 } else if (!skip_sum) {
1759 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1760 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001761 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001762 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001763 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001764 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001765 /* csum items have already been cloned */
1766 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1767 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001768 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001769 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001770 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001771 bio_flags, bio_offset,
1772 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001773 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001774 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001775 } else if (!skip_sum) {
1776 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1777 if (ret)
1778 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001779 }
1780
Chris Mason0b86a832008-03-24 15:01:56 -04001781mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001782 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1783
1784out:
1785 if (ret < 0)
1786 bio_endio(bio, ret);
1787 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001788}
Chris Mason6885f302008-02-20 16:11:05 -05001789
Chris Masond352ac62008-09-29 15:18:18 -04001790/*
1791 * given a list of ordered sums record them in the inode. This happens
1792 * at IO completion time based on sums calculated at bio submission time.
1793 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001794static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001795 struct inode *inode, u64 file_offset,
1796 struct list_head *list)
1797{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001798 struct btrfs_ordered_sum *sum;
1799
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001800 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001801 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001802 btrfs_csum_file_blocks(trans,
1803 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001804 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001805 }
1806 return 0;
1807}
1808
Josef Bacik2ac55d42010-02-03 19:33:23 +00001809int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1810 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001811{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001812 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001813 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001814 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001815}
1816
Chris Masond352ac62008-09-29 15:18:18 -04001817/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001818struct btrfs_writepage_fixup {
1819 struct page *page;
1820 struct btrfs_work work;
1821};
1822
Christoph Hellwigb2950862008-12-02 09:54:17 -05001823static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001824{
1825 struct btrfs_writepage_fixup *fixup;
1826 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001827 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001828 struct page *page;
1829 struct inode *inode;
1830 u64 page_start;
1831 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001832 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001833
1834 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1835 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001836again:
Chris Mason247e7432008-07-17 12:53:51 -04001837 lock_page(page);
1838 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1839 ClearPageChecked(page);
1840 goto out_page;
1841 }
1842
1843 inode = page->mapping->host;
1844 page_start = page_offset(page);
1845 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1846
Josef Bacik2ac55d42010-02-03 19:33:23 +00001847 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001848 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001849
1850 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001851 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001852 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001853
1854 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1855 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001856 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1857 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001858 unlock_page(page);
1859 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001860 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001861 goto again;
1862 }
Chris Mason247e7432008-07-17 12:53:51 -04001863
Jeff Mahoney87826df2012-02-15 16:23:57 +01001864 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1865 if (ret) {
1866 mapping_set_error(page->mapping, ret);
1867 end_extent_writepage(page, ret, page_start, page_end);
1868 ClearPageChecked(page);
1869 goto out;
1870 }
1871
Josef Bacik2ac55d42010-02-03 19:33:23 +00001872 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001873 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001874 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001875out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001876 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1877 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001878out_page:
1879 unlock_page(page);
1880 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001881 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001882}
1883
1884/*
1885 * There are a few paths in the higher layers of the kernel that directly
1886 * set the page dirty bit without asking the filesystem if it is a
1887 * good idea. This causes problems because we want to make sure COW
1888 * properly happens and the data=ordered rules are followed.
1889 *
Chris Masonc8b97812008-10-29 14:49:59 -04001890 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001891 * hasn't been properly setup for IO. We kick off an async process
1892 * to fix it up. The async helper will wait for ordered extents, set
1893 * the delalloc bit and make it safe to write the page.
1894 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001895static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001896{
1897 struct inode *inode = page->mapping->host;
1898 struct btrfs_writepage_fixup *fixup;
1899 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001900
Chris Mason8b62b722009-09-02 16:53:46 -04001901 /* this page is properly in the ordered list */
1902 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001903 return 0;
1904
1905 if (PageChecked(page))
1906 return -EAGAIN;
1907
1908 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1909 if (!fixup)
1910 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001911
Chris Mason247e7432008-07-17 12:53:51 -04001912 SetPageChecked(page);
1913 page_cache_get(page);
1914 fixup->work.func = btrfs_writepage_fixup_worker;
1915 fixup->page = page;
1916 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001917 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001918}
1919
Yan Zhengd899e052008-10-30 14:25:28 -04001920static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1921 struct inode *inode, u64 file_pos,
1922 u64 disk_bytenr, u64 disk_num_bytes,
1923 u64 num_bytes, u64 ram_bytes,
1924 u8 compression, u8 encryption,
1925 u16 other_encoding, int extent_type)
1926{
1927 struct btrfs_root *root = BTRFS_I(inode)->root;
1928 struct btrfs_file_extent_item *fi;
1929 struct btrfs_path *path;
1930 struct extent_buffer *leaf;
1931 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001932 int ret;
1933
1934 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001935 if (!path)
1936 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001937
Chris Masonb9473432009-03-13 11:00:37 -04001938 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001939
1940 /*
1941 * we may be replacing one extent in the tree with another.
1942 * The new extent is pinned in the extent map, and we don't want
1943 * to drop it from the cache until it is completely in the btree.
1944 *
1945 * So, tell btrfs_drop_extents to leave this extent in the cache.
1946 * the caller is expected to unpin it and allow it to be merged
1947 * with the others.
1948 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001949 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001950 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001951 if (ret)
1952 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001953
Li Zefan33345d012011-04-20 10:31:50 +08001954 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001955 ins.offset = file_pos;
1956 ins.type = BTRFS_EXTENT_DATA_KEY;
1957 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001958 if (ret)
1959 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001960 leaf = path->nodes[0];
1961 fi = btrfs_item_ptr(leaf, path->slots[0],
1962 struct btrfs_file_extent_item);
1963 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1964 btrfs_set_file_extent_type(leaf, fi, extent_type);
1965 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1966 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1967 btrfs_set_file_extent_offset(leaf, fi, 0);
1968 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1969 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1970 btrfs_set_file_extent_compression(leaf, fi, compression);
1971 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1972 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001973
Yan Zhengd899e052008-10-30 14:25:28 -04001974 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001975 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001976
1977 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001978
1979 ins.objectid = disk_bytenr;
1980 ins.offset = disk_num_bytes;
1981 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001982 ret = btrfs_alloc_reserved_file_extent(trans, root,
1983 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001984 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001985out:
Yan Zhengd899e052008-10-30 14:25:28 -04001986 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001987
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001988 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001989}
1990
Liu Bo38c227d2013-01-29 03:18:40 +00001991/* snapshot-aware defrag */
1992struct sa_defrag_extent_backref {
1993 struct rb_node node;
1994 struct old_sa_defrag_extent *old;
1995 u64 root_id;
1996 u64 inum;
1997 u64 file_pos;
1998 u64 extent_offset;
1999 u64 num_bytes;
2000 u64 generation;
2001};
2002
2003struct old_sa_defrag_extent {
2004 struct list_head list;
2005 struct new_sa_defrag_extent *new;
2006
2007 u64 extent_offset;
2008 u64 bytenr;
2009 u64 offset;
2010 u64 len;
2011 int count;
2012};
2013
2014struct new_sa_defrag_extent {
2015 struct rb_root root;
2016 struct list_head head;
2017 struct btrfs_path *path;
2018 struct inode *inode;
2019 u64 file_pos;
2020 u64 len;
2021 u64 bytenr;
2022 u64 disk_len;
2023 u8 compress_type;
2024};
2025
2026static int backref_comp(struct sa_defrag_extent_backref *b1,
2027 struct sa_defrag_extent_backref *b2)
2028{
2029 if (b1->root_id < b2->root_id)
2030 return -1;
2031 else if (b1->root_id > b2->root_id)
2032 return 1;
2033
2034 if (b1->inum < b2->inum)
2035 return -1;
2036 else if (b1->inum > b2->inum)
2037 return 1;
2038
2039 if (b1->file_pos < b2->file_pos)
2040 return -1;
2041 else if (b1->file_pos > b2->file_pos)
2042 return 1;
2043
2044 /*
2045 * [------------------------------] ===> (a range of space)
2046 * |<--->| |<---->| =============> (fs/file tree A)
2047 * |<---------------------------->| ===> (fs/file tree B)
2048 *
2049 * A range of space can refer to two file extents in one tree while
2050 * refer to only one file extent in another tree.
2051 *
2052 * So we may process a disk offset more than one time(two extents in A)
2053 * and locate at the same extent(one extent in B), then insert two same
2054 * backrefs(both refer to the extent in B).
2055 */
2056 return 0;
2057}
2058
2059static void backref_insert(struct rb_root *root,
2060 struct sa_defrag_extent_backref *backref)
2061{
2062 struct rb_node **p = &root->rb_node;
2063 struct rb_node *parent = NULL;
2064 struct sa_defrag_extent_backref *entry;
2065 int ret;
2066
2067 while (*p) {
2068 parent = *p;
2069 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2070
2071 ret = backref_comp(backref, entry);
2072 if (ret < 0)
2073 p = &(*p)->rb_left;
2074 else
2075 p = &(*p)->rb_right;
2076 }
2077
2078 rb_link_node(&backref->node, parent, p);
2079 rb_insert_color(&backref->node, root);
2080}
2081
2082/*
2083 * Note the backref might has changed, and in this case we just return 0.
2084 */
2085static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2086 void *ctx)
2087{
2088 struct btrfs_file_extent_item *extent;
2089 struct btrfs_fs_info *fs_info;
2090 struct old_sa_defrag_extent *old = ctx;
2091 struct new_sa_defrag_extent *new = old->new;
2092 struct btrfs_path *path = new->path;
2093 struct btrfs_key key;
2094 struct btrfs_root *root;
2095 struct sa_defrag_extent_backref *backref;
2096 struct extent_buffer *leaf;
2097 struct inode *inode = new->inode;
2098 int slot;
2099 int ret;
2100 u64 extent_offset;
2101 u64 num_bytes;
2102
2103 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2104 inum == btrfs_ino(inode))
2105 return 0;
2106
2107 key.objectid = root_id;
2108 key.type = BTRFS_ROOT_ITEM_KEY;
2109 key.offset = (u64)-1;
2110
2111 fs_info = BTRFS_I(inode)->root->fs_info;
2112 root = btrfs_read_fs_root_no_name(fs_info, &key);
2113 if (IS_ERR(root)) {
2114 if (PTR_ERR(root) == -ENOENT)
2115 return 0;
2116 WARN_ON(1);
2117 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2118 inum, offset, root_id);
2119 return PTR_ERR(root);
2120 }
2121
2122 key.objectid = inum;
2123 key.type = BTRFS_EXTENT_DATA_KEY;
2124 if (offset > (u64)-1 << 32)
2125 key.offset = 0;
2126 else
2127 key.offset = offset;
2128
2129 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2130 if (ret < 0) {
2131 WARN_ON(1);
2132 return ret;
2133 }
2134
2135 while (1) {
2136 cond_resched();
2137
2138 leaf = path->nodes[0];
2139 slot = path->slots[0];
2140
2141 if (slot >= btrfs_header_nritems(leaf)) {
2142 ret = btrfs_next_leaf(root, path);
2143 if (ret < 0) {
2144 goto out;
2145 } else if (ret > 0) {
2146 ret = 0;
2147 goto out;
2148 }
2149 continue;
2150 }
2151
2152 path->slots[0]++;
2153
2154 btrfs_item_key_to_cpu(leaf, &key, slot);
2155
2156 if (key.objectid > inum)
2157 goto out;
2158
2159 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2160 continue;
2161
2162 extent = btrfs_item_ptr(leaf, slot,
2163 struct btrfs_file_extent_item);
2164
2165 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2166 continue;
2167
2168 extent_offset = btrfs_file_extent_offset(leaf, extent);
2169 if (key.offset - extent_offset != offset)
2170 continue;
2171
2172 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2173 if (extent_offset >= old->extent_offset + old->offset +
2174 old->len || extent_offset + num_bytes <=
2175 old->extent_offset + old->offset)
2176 continue;
2177
2178 break;
2179 }
2180
2181 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2182 if (!backref) {
2183 ret = -ENOENT;
2184 goto out;
2185 }
2186
2187 backref->root_id = root_id;
2188 backref->inum = inum;
2189 backref->file_pos = offset + extent_offset;
2190 backref->num_bytes = num_bytes;
2191 backref->extent_offset = extent_offset;
2192 backref->generation = btrfs_file_extent_generation(leaf, extent);
2193 backref->old = old;
2194 backref_insert(&new->root, backref);
2195 old->count++;
2196out:
2197 btrfs_release_path(path);
2198 WARN_ON(ret);
2199 return ret;
2200}
2201
2202static noinline bool record_extent_backrefs(struct btrfs_path *path,
2203 struct new_sa_defrag_extent *new)
2204{
2205 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2206 struct old_sa_defrag_extent *old, *tmp;
2207 int ret;
2208
2209 new->path = path;
2210
2211 list_for_each_entry_safe(old, tmp, &new->head, list) {
2212 ret = iterate_inodes_from_logical(old->bytenr, fs_info,
2213 path, record_one_backref,
2214 old);
2215 BUG_ON(ret < 0 && ret != -ENOENT);
2216
2217 /* no backref to be processed for this extent */
2218 if (!old->count) {
2219 list_del(&old->list);
2220 kfree(old);
2221 }
2222 }
2223
2224 if (list_empty(&new->head))
2225 return false;
2226
2227 return true;
2228}
2229
2230static int relink_is_mergable(struct extent_buffer *leaf,
2231 struct btrfs_file_extent_item *fi,
2232 u64 disk_bytenr)
2233{
2234 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2235 return 0;
2236
2237 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2238 return 0;
2239
2240 if (btrfs_file_extent_compression(leaf, fi) ||
2241 btrfs_file_extent_encryption(leaf, fi) ||
2242 btrfs_file_extent_other_encoding(leaf, fi))
2243 return 0;
2244
2245 return 1;
2246}
2247
2248/*
2249 * Note the backref might has changed, and in this case we just return 0.
2250 */
2251static noinline int relink_extent_backref(struct btrfs_path *path,
2252 struct sa_defrag_extent_backref *prev,
2253 struct sa_defrag_extent_backref *backref)
2254{
2255 struct btrfs_file_extent_item *extent;
2256 struct btrfs_file_extent_item *item;
2257 struct btrfs_ordered_extent *ordered;
2258 struct btrfs_trans_handle *trans;
2259 struct btrfs_fs_info *fs_info;
2260 struct btrfs_root *root;
2261 struct btrfs_key key;
2262 struct extent_buffer *leaf;
2263 struct old_sa_defrag_extent *old = backref->old;
2264 struct new_sa_defrag_extent *new = old->new;
2265 struct inode *src_inode = new->inode;
2266 struct inode *inode;
2267 struct extent_state *cached = NULL;
2268 int ret = 0;
2269 u64 start;
2270 u64 len;
2271 u64 lock_start;
2272 u64 lock_end;
2273 bool merge = false;
2274 int index;
2275
2276 if (prev && prev->root_id == backref->root_id &&
2277 prev->inum == backref->inum &&
2278 prev->file_pos + prev->num_bytes == backref->file_pos)
2279 merge = true;
2280
2281 /* step 1: get root */
2282 key.objectid = backref->root_id;
2283 key.type = BTRFS_ROOT_ITEM_KEY;
2284 key.offset = (u64)-1;
2285
2286 fs_info = BTRFS_I(src_inode)->root->fs_info;
2287 index = srcu_read_lock(&fs_info->subvol_srcu);
2288
2289 root = btrfs_read_fs_root_no_name(fs_info, &key);
2290 if (IS_ERR(root)) {
2291 srcu_read_unlock(&fs_info->subvol_srcu, index);
2292 if (PTR_ERR(root) == -ENOENT)
2293 return 0;
2294 return PTR_ERR(root);
2295 }
Liu Bo38c227d2013-01-29 03:18:40 +00002296
2297 /* step 2: get inode */
2298 key.objectid = backref->inum;
2299 key.type = BTRFS_INODE_ITEM_KEY;
2300 key.offset = 0;
2301
2302 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2303 if (IS_ERR(inode)) {
2304 srcu_read_unlock(&fs_info->subvol_srcu, index);
2305 return 0;
2306 }
2307
2308 srcu_read_unlock(&fs_info->subvol_srcu, index);
2309
2310 /* step 3: relink backref */
2311 lock_start = backref->file_pos;
2312 lock_end = backref->file_pos + backref->num_bytes - 1;
2313 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2314 0, &cached);
2315
2316 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2317 if (ordered) {
2318 btrfs_put_ordered_extent(ordered);
2319 goto out_unlock;
2320 }
2321
2322 trans = btrfs_join_transaction(root);
2323 if (IS_ERR(trans)) {
2324 ret = PTR_ERR(trans);
2325 goto out_unlock;
2326 }
2327
2328 key.objectid = backref->inum;
2329 key.type = BTRFS_EXTENT_DATA_KEY;
2330 key.offset = backref->file_pos;
2331
2332 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2333 if (ret < 0) {
2334 goto out_free_path;
2335 } else if (ret > 0) {
2336 ret = 0;
2337 goto out_free_path;
2338 }
2339
2340 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2341 struct btrfs_file_extent_item);
2342
2343 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2344 backref->generation)
2345 goto out_free_path;
2346
2347 btrfs_release_path(path);
2348
2349 start = backref->file_pos;
2350 if (backref->extent_offset < old->extent_offset + old->offset)
2351 start += old->extent_offset + old->offset -
2352 backref->extent_offset;
2353
2354 len = min(backref->extent_offset + backref->num_bytes,
2355 old->extent_offset + old->offset + old->len);
2356 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2357
2358 ret = btrfs_drop_extents(trans, root, inode, start,
2359 start + len, 1);
2360 if (ret)
2361 goto out_free_path;
2362again:
2363 key.objectid = btrfs_ino(inode);
2364 key.type = BTRFS_EXTENT_DATA_KEY;
2365 key.offset = start;
2366
Liu Boa09a0a72013-03-11 09:20:58 +00002367 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002368 if (merge) {
2369 struct btrfs_file_extent_item *fi;
2370 u64 extent_len;
2371 struct btrfs_key found_key;
2372
2373 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2374 if (ret < 0)
2375 goto out_free_path;
2376
2377 path->slots[0]--;
2378 leaf = path->nodes[0];
2379 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2380
2381 fi = btrfs_item_ptr(leaf, path->slots[0],
2382 struct btrfs_file_extent_item);
2383 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2384
2385 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2386 extent_len + found_key.offset == start) {
2387 btrfs_set_file_extent_num_bytes(leaf, fi,
2388 extent_len + len);
2389 btrfs_mark_buffer_dirty(leaf);
2390 inode_add_bytes(inode, len);
2391
2392 ret = 1;
2393 goto out_free_path;
2394 } else {
2395 merge = false;
2396 btrfs_release_path(path);
2397 goto again;
2398 }
2399 }
2400
2401 ret = btrfs_insert_empty_item(trans, root, path, &key,
2402 sizeof(*extent));
2403 if (ret) {
2404 btrfs_abort_transaction(trans, root, ret);
2405 goto out_free_path;
2406 }
2407
2408 leaf = path->nodes[0];
2409 item = btrfs_item_ptr(leaf, path->slots[0],
2410 struct btrfs_file_extent_item);
2411 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2412 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2413 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2414 btrfs_set_file_extent_num_bytes(leaf, item, len);
2415 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2416 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2417 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2418 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2419 btrfs_set_file_extent_encryption(leaf, item, 0);
2420 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2421
2422 btrfs_mark_buffer_dirty(leaf);
2423 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002424 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002425
2426 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2427 new->disk_len, 0,
2428 backref->root_id, backref->inum,
2429 new->file_pos, 0); /* start - extent_offset */
2430 if (ret) {
2431 btrfs_abort_transaction(trans, root, ret);
2432 goto out_free_path;
2433 }
2434
2435 ret = 1;
2436out_free_path:
2437 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002438 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002439 btrfs_end_transaction(trans, root);
2440out_unlock:
2441 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2442 &cached, GFP_NOFS);
2443 iput(inode);
2444 return ret;
2445}
2446
2447static void relink_file_extents(struct new_sa_defrag_extent *new)
2448{
2449 struct btrfs_path *path;
2450 struct old_sa_defrag_extent *old, *tmp;
2451 struct sa_defrag_extent_backref *backref;
2452 struct sa_defrag_extent_backref *prev = NULL;
2453 struct inode *inode;
2454 struct btrfs_root *root;
2455 struct rb_node *node;
2456 int ret;
2457
2458 inode = new->inode;
2459 root = BTRFS_I(inode)->root;
2460
2461 path = btrfs_alloc_path();
2462 if (!path)
2463 return;
2464
2465 if (!record_extent_backrefs(path, new)) {
2466 btrfs_free_path(path);
2467 goto out;
2468 }
2469 btrfs_release_path(path);
2470
2471 while (1) {
2472 node = rb_first(&new->root);
2473 if (!node)
2474 break;
2475 rb_erase(node, &new->root);
2476
2477 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2478
2479 ret = relink_extent_backref(path, prev, backref);
2480 WARN_ON(ret < 0);
2481
2482 kfree(prev);
2483
2484 if (ret == 1)
2485 prev = backref;
2486 else
2487 prev = NULL;
2488 cond_resched();
2489 }
2490 kfree(prev);
2491
2492 btrfs_free_path(path);
2493
2494 list_for_each_entry_safe(old, tmp, &new->head, list) {
2495 list_del(&old->list);
2496 kfree(old);
2497 }
2498out:
2499 atomic_dec(&root->fs_info->defrag_running);
2500 wake_up(&root->fs_info->transaction_wait);
2501
2502 kfree(new);
2503}
2504
2505static struct new_sa_defrag_extent *
2506record_old_file_extents(struct inode *inode,
2507 struct btrfs_ordered_extent *ordered)
2508{
2509 struct btrfs_root *root = BTRFS_I(inode)->root;
2510 struct btrfs_path *path;
2511 struct btrfs_key key;
2512 struct old_sa_defrag_extent *old, *tmp;
2513 struct new_sa_defrag_extent *new;
2514 int ret;
2515
2516 new = kmalloc(sizeof(*new), GFP_NOFS);
2517 if (!new)
2518 return NULL;
2519
2520 new->inode = inode;
2521 new->file_pos = ordered->file_offset;
2522 new->len = ordered->len;
2523 new->bytenr = ordered->start;
2524 new->disk_len = ordered->disk_len;
2525 new->compress_type = ordered->compress_type;
2526 new->root = RB_ROOT;
2527 INIT_LIST_HEAD(&new->head);
2528
2529 path = btrfs_alloc_path();
2530 if (!path)
2531 goto out_kfree;
2532
2533 key.objectid = btrfs_ino(inode);
2534 key.type = BTRFS_EXTENT_DATA_KEY;
2535 key.offset = new->file_pos;
2536
2537 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2538 if (ret < 0)
2539 goto out_free_path;
2540 if (ret > 0 && path->slots[0] > 0)
2541 path->slots[0]--;
2542
2543 /* find out all the old extents for the file range */
2544 while (1) {
2545 struct btrfs_file_extent_item *extent;
2546 struct extent_buffer *l;
2547 int slot;
2548 u64 num_bytes;
2549 u64 offset;
2550 u64 end;
2551 u64 disk_bytenr;
2552 u64 extent_offset;
2553
2554 l = path->nodes[0];
2555 slot = path->slots[0];
2556
2557 if (slot >= btrfs_header_nritems(l)) {
2558 ret = btrfs_next_leaf(root, path);
2559 if (ret < 0)
2560 goto out_free_list;
2561 else if (ret > 0)
2562 break;
2563 continue;
2564 }
2565
2566 btrfs_item_key_to_cpu(l, &key, slot);
2567
2568 if (key.objectid != btrfs_ino(inode))
2569 break;
2570 if (key.type != BTRFS_EXTENT_DATA_KEY)
2571 break;
2572 if (key.offset >= new->file_pos + new->len)
2573 break;
2574
2575 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2576
2577 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2578 if (key.offset + num_bytes < new->file_pos)
2579 goto next;
2580
2581 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2582 if (!disk_bytenr)
2583 goto next;
2584
2585 extent_offset = btrfs_file_extent_offset(l, extent);
2586
2587 old = kmalloc(sizeof(*old), GFP_NOFS);
2588 if (!old)
2589 goto out_free_list;
2590
2591 offset = max(new->file_pos, key.offset);
2592 end = min(new->file_pos + new->len, key.offset + num_bytes);
2593
2594 old->bytenr = disk_bytenr;
2595 old->extent_offset = extent_offset;
2596 old->offset = offset - key.offset;
2597 old->len = end - offset;
2598 old->new = new;
2599 old->count = 0;
2600 list_add_tail(&old->list, &new->head);
2601next:
2602 path->slots[0]++;
2603 cond_resched();
2604 }
2605
2606 btrfs_free_path(path);
2607 atomic_inc(&root->fs_info->defrag_running);
2608
2609 return new;
2610
2611out_free_list:
2612 list_for_each_entry_safe(old, tmp, &new->head, list) {
2613 list_del(&old->list);
2614 kfree(old);
2615 }
2616out_free_path:
2617 btrfs_free_path(path);
2618out_kfree:
2619 kfree(new);
2620 return NULL;
2621}
2622
Chris Mason5d13a982009-03-13 11:41:46 -04002623/*
2624 * helper function for btrfs_finish_ordered_io, this
2625 * just reads in some of the csum leaves to prime them into ram
2626 * before we start the transaction. It limits the amount of btree
2627 * reads required while inside the transaction.
2628 */
Chris Masond352ac62008-09-29 15:18:18 -04002629/* as ordered data IO finishes, this gets called so we can finish
2630 * an ordered extent if the range of bytes in the file it covers are
2631 * fully written.
2632 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002633static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002634{
Josef Bacik5fd02042012-05-02 14:00:54 -04002635 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002636 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002637 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002638 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002639 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002640 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002641 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002642 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002643 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002644
Liu Bo83eea1f2012-07-10 05:28:39 -06002645 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002646
Josef Bacik5fd02042012-05-02 14:00:54 -04002647 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2648 ret = -EIO;
2649 goto out;
2650 }
2651
Yan, Zhengc2167752009-11-12 09:34:21 +00002652 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002653 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002654 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2655 if (nolock)
2656 trans = btrfs_join_transaction_nolock(root);
2657 else
2658 trans = btrfs_join_transaction(root);
2659 if (IS_ERR(trans)) {
2660 ret = PTR_ERR(trans);
2661 trans = NULL;
2662 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002663 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002664 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2665 ret = btrfs_update_inode_fallback(trans, root, inode);
2666 if (ret) /* -ENOMEM or corruption */
2667 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002668 goto out;
2669 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002670
Josef Bacik2ac55d42010-02-03 19:33:23 +00002671 lock_extent_bits(io_tree, ordered_extent->file_offset,
2672 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002673 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002674
Liu Bo38c227d2013-01-29 03:18:40 +00002675 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2676 ordered_extent->file_offset + ordered_extent->len - 1,
2677 EXTENT_DEFRAG, 1, cached_state);
2678 if (ret) {
2679 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2680 if (last_snapshot >= BTRFS_I(inode)->generation)
2681 /* the inode is shared */
2682 new = record_old_file_extents(inode, ordered_extent);
2683
2684 clear_extent_bit(io_tree, ordered_extent->file_offset,
2685 ordered_extent->file_offset + ordered_extent->len - 1,
2686 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2687 }
2688
Josef Bacik0cb59c92010-07-02 12:14:14 -04002689 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002690 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002691 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002692 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002693 if (IS_ERR(trans)) {
2694 ret = PTR_ERR(trans);
2695 trans = NULL;
2696 goto out_unlock;
2697 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002698 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002699
Chris Masonc8b97812008-10-29 14:49:59 -04002700 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002701 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002702 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002703 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002704 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002705 ordered_extent->file_offset,
2706 ordered_extent->file_offset +
2707 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002708 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002709 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002710 ret = insert_reserved_file_extent(trans, inode,
2711 ordered_extent->file_offset,
2712 ordered_extent->start,
2713 ordered_extent->disk_len,
2714 ordered_extent->len,
2715 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002716 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002717 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002718 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002719 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2720 ordered_extent->file_offset, ordered_extent->len,
2721 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002722 if (ret < 0) {
2723 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002724 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002725 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002726
Chris Masone6dcd2d2008-07-17 12:53:50 -04002727 add_pending_csums(trans, inode, ordered_extent->file_offset,
2728 &ordered_extent->list);
2729
Josef Bacik6c760c02012-11-09 10:53:21 -05002730 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2731 ret = btrfs_update_inode_fallback(trans, root, inode);
2732 if (ret) { /* -ENOMEM or corruption */
2733 btrfs_abort_transaction(trans, root, ret);
2734 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002735 }
2736 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002737out_unlock:
2738 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2739 ordered_extent->file_offset +
2740 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002741out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002742 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002743 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002744 if (trans)
2745 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002746
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002747 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002748 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2749 ordered_extent->file_offset +
2750 ordered_extent->len - 1, NULL, GFP_NOFS);
2751
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002752 /*
2753 * If the ordered extent had an IOERR or something else went
2754 * wrong we need to return the space for this ordered extent
2755 * back to the allocator.
2756 */
2757 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2758 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2759 btrfs_free_reserved_extent(root, ordered_extent->start,
2760 ordered_extent->disk_len);
2761 }
2762
2763
Josef Bacik5fd02042012-05-02 14:00:54 -04002764 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002765 * This needs to be done to make sure anybody waiting knows we are done
2766 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002767 */
2768 btrfs_remove_ordered_extent(inode, ordered_extent);
2769
Liu Bo38c227d2013-01-29 03:18:40 +00002770 /* for snapshot-aware defrag */
2771 if (new)
2772 relink_file_extents(new);
2773
Chris Masone6dcd2d2008-07-17 12:53:50 -04002774 /* once for us */
2775 btrfs_put_ordered_extent(ordered_extent);
2776 /* once for the tree */
2777 btrfs_put_ordered_extent(ordered_extent);
2778
Josef Bacik5fd02042012-05-02 14:00:54 -04002779 return ret;
2780}
2781
2782static void finish_ordered_fn(struct btrfs_work *work)
2783{
2784 struct btrfs_ordered_extent *ordered_extent;
2785 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2786 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002787}
2788
Christoph Hellwigb2950862008-12-02 09:54:17 -05002789static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002790 struct extent_state *state, int uptodate)
2791{
Josef Bacik5fd02042012-05-02 14:00:54 -04002792 struct inode *inode = page->mapping->host;
2793 struct btrfs_root *root = BTRFS_I(inode)->root;
2794 struct btrfs_ordered_extent *ordered_extent = NULL;
2795 struct btrfs_workers *workers;
2796
liubo1abe9b82011-03-24 11:18:59 +00002797 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2798
Chris Mason8b62b722009-09-02 16:53:46 -04002799 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002800 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2801 end - start + 1, uptodate))
2802 return 0;
2803
2804 ordered_extent->work.func = finish_ordered_fn;
2805 ordered_extent->work.flags = 0;
2806
Liu Bo83eea1f2012-07-10 05:28:39 -06002807 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002808 workers = &root->fs_info->endio_freespace_worker;
2809 else
2810 workers = &root->fs_info->endio_write_workers;
2811 btrfs_queue_worker(workers, &ordered_extent->work);
2812
2813 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002814}
2815
Chris Masond352ac62008-09-29 15:18:18 -04002816/*
Chris Masond352ac62008-09-29 15:18:18 -04002817 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002818 * if there's a match, we allow the bio to finish. If not, the code in
2819 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002820 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002821static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002822 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002823{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002824 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002825 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002826 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002827 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002828 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002829 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002830 struct btrfs_root *root = BTRFS_I(inode)->root;
2831 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002832 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2833 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002834
Chris Masond20f7042008-12-08 16:58:54 -05002835 if (PageChecked(page)) {
2836 ClearPageChecked(page);
2837 goto good;
2838 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002839
2840 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002841 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002842
Yan Zheng17d217f2008-12-12 10:03:38 -05002843 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002844 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002845 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2846 GFP_NOFS);
2847 return 0;
2848 }
2849
Yanc2e639f2008-02-04 08:57:25 -05002850 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002851 private = state->private;
2852 ret = 0;
2853 } else {
2854 ret = get_state_private(io_tree, start, &private);
2855 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002856 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002857 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002858 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002859
Liu Bob0496682013-03-14 14:57:45 +00002860 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002861 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002862 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002863 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002864
Cong Wang7ac687d2011-11-25 23:14:28 +08002865 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002866good:
Chris Mason07157aa2007-08-30 08:50:51 -04002867 return 0;
2868
2869zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002870 if (__ratelimit(&_rs))
2871 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u private %llu",
2872 (unsigned long long)btrfs_ino(page->mapping->host),
2873 (unsigned long long)start, csum,
2874 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002875 memset(kaddr + offset, 1, end - start + 1);
2876 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002877 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002878 if (private == 0)
2879 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002880 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002881}
Chris Masonb888db22007-08-27 16:49:44 -04002882
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002883struct delayed_iput {
2884 struct list_head list;
2885 struct inode *inode;
2886};
2887
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002888/* JDM: If this is fs-wide, why can't we add a pointer to
2889 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002890void btrfs_add_delayed_iput(struct inode *inode)
2891{
2892 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2893 struct delayed_iput *delayed;
2894
2895 if (atomic_add_unless(&inode->i_count, -1, 1))
2896 return;
2897
2898 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2899 delayed->inode = inode;
2900
2901 spin_lock(&fs_info->delayed_iput_lock);
2902 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2903 spin_unlock(&fs_info->delayed_iput_lock);
2904}
2905
2906void btrfs_run_delayed_iputs(struct btrfs_root *root)
2907{
2908 LIST_HEAD(list);
2909 struct btrfs_fs_info *fs_info = root->fs_info;
2910 struct delayed_iput *delayed;
2911 int empty;
2912
2913 spin_lock(&fs_info->delayed_iput_lock);
2914 empty = list_empty(&fs_info->delayed_iputs);
2915 spin_unlock(&fs_info->delayed_iput_lock);
2916 if (empty)
2917 return;
2918
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002919 spin_lock(&fs_info->delayed_iput_lock);
2920 list_splice_init(&fs_info->delayed_iputs, &list);
2921 spin_unlock(&fs_info->delayed_iput_lock);
2922
2923 while (!list_empty(&list)) {
2924 delayed = list_entry(list.next, struct delayed_iput, list);
2925 list_del(&delayed->list);
2926 iput(delayed->inode);
2927 kfree(delayed);
2928 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002929}
2930
Yan, Zhengd68fc572010-05-16 10:49:58 -04002931/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002932 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002933 * files in the subvolume, it removes orphan item and frees block_rsv
2934 * structure.
2935 */
2936void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2937 struct btrfs_root *root)
2938{
Josef Bacik90290e12011-12-02 15:44:12 -05002939 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002940 int ret;
2941
Josef Bacik8a35d952012-05-23 14:26:42 -04002942 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002943 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2944 return;
2945
Josef Bacik90290e12011-12-02 15:44:12 -05002946 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002947 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002948 spin_unlock(&root->orphan_lock);
2949 return;
2950 }
2951
2952 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2953 spin_unlock(&root->orphan_lock);
2954 return;
2955 }
2956
2957 block_rsv = root->orphan_block_rsv;
2958 root->orphan_block_rsv = NULL;
2959 spin_unlock(&root->orphan_lock);
2960
Yan, Zhengd68fc572010-05-16 10:49:58 -04002961 if (root->orphan_item_inserted &&
2962 btrfs_root_refs(&root->root_item) > 0) {
2963 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2964 root->root_key.objectid);
2965 BUG_ON(ret);
2966 root->orphan_item_inserted = 0;
2967 }
2968
Josef Bacik90290e12011-12-02 15:44:12 -05002969 if (block_rsv) {
2970 WARN_ON(block_rsv->size > 0);
2971 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002972 }
2973}
2974
2975/*
Josef Bacik7b128762008-07-24 12:17:14 -04002976 * This creates an orphan entry for the given inode in case something goes
2977 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002978 *
2979 * NOTE: caller of this function should reserve 5 units of metadata for
2980 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002981 */
2982int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2983{
2984 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002985 struct btrfs_block_rsv *block_rsv = NULL;
2986 int reserve = 0;
2987 int insert = 0;
2988 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002989
Yan, Zhengd68fc572010-05-16 10:49:58 -04002990 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002991 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002992 if (!block_rsv)
2993 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002994 }
2995
Yan, Zhengd68fc572010-05-16 10:49:58 -04002996 spin_lock(&root->orphan_lock);
2997 if (!root->orphan_block_rsv) {
2998 root->orphan_block_rsv = block_rsv;
2999 } else if (block_rsv) {
3000 btrfs_free_block_rsv(root, block_rsv);
3001 block_rsv = NULL;
3002 }
Josef Bacik7b128762008-07-24 12:17:14 -04003003
Josef Bacik8a35d952012-05-23 14:26:42 -04003004 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3005 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003006#if 0
3007 /*
3008 * For proper ENOSPC handling, we should do orphan
3009 * cleanup when mounting. But this introduces backward
3010 * compatibility issue.
3011 */
3012 if (!xchg(&root->orphan_item_inserted, 1))
3013 insert = 2;
3014 else
3015 insert = 1;
3016#endif
3017 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003018 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003019 }
Josef Bacik7b128762008-07-24 12:17:14 -04003020
Josef Bacik72ac3c02012-05-23 14:13:11 -04003021 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3022 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003023 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003024 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003025
Yan, Zhengd68fc572010-05-16 10:49:58 -04003026 /* grab metadata reservation from transaction handle */
3027 if (reserve) {
3028 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003029 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04003030 }
3031
3032 /* insert an orphan item to track this unlinked/truncated file */
3033 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003034 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003035 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003036 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3037 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003038 btrfs_abort_transaction(trans, root, ret);
3039 return ret;
3040 }
3041 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003042 }
3043
3044 /* insert an orphan item to track subvolume contains orphan files */
3045 if (insert >= 2) {
3046 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3047 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003048 if (ret && ret != -EEXIST) {
3049 btrfs_abort_transaction(trans, root, ret);
3050 return ret;
3051 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003052 }
3053 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003054}
3055
3056/*
3057 * We have done the truncate/delete so we can go ahead and remove the orphan
3058 * item for this particular inode.
3059 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003060static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3061 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003062{
3063 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003064 int delete_item = 0;
3065 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003066 int ret = 0;
3067
Yan, Zhengd68fc572010-05-16 10:49:58 -04003068 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003069 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3070 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003071 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003072
Josef Bacik72ac3c02012-05-23 14:13:11 -04003073 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3074 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003075 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003076 spin_unlock(&root->orphan_lock);
3077
3078 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003079 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003080 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003081 }
3082
Josef Bacik8a35d952012-05-23 14:26:42 -04003083 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003084 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003085 atomic_dec(&root->orphan_inodes);
3086 }
Josef Bacik7b128762008-07-24 12:17:14 -04003087
Yan, Zhengd68fc572010-05-16 10:49:58 -04003088 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003089}
3090
3091/*
3092 * this cleans up any orphans that may be left on the list from the last use
3093 * of this root.
3094 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003095int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003096{
3097 struct btrfs_path *path;
3098 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003099 struct btrfs_key key, found_key;
3100 struct btrfs_trans_handle *trans;
3101 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003102 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003103 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3104
Yan, Zhengd68fc572010-05-16 10:49:58 -04003105 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003106 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003107
3108 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003109 if (!path) {
3110 ret = -ENOMEM;
3111 goto out;
3112 }
Josef Bacik7b128762008-07-24 12:17:14 -04003113 path->reada = -1;
3114
3115 key.objectid = BTRFS_ORPHAN_OBJECTID;
3116 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3117 key.offset = (u64)-1;
3118
Josef Bacik7b128762008-07-24 12:17:14 -04003119 while (1) {
3120 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003121 if (ret < 0)
3122 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003123
3124 /*
3125 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003126 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003127 * find the key and see if we have stuff that matches
3128 */
3129 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003130 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003131 if (path->slots[0] == 0)
3132 break;
3133 path->slots[0]--;
3134 }
3135
3136 /* pull out the item */
3137 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003138 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3139
3140 /* make sure the item matches what we want */
3141 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3142 break;
3143 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3144 break;
3145
3146 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003147 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003148
3149 /*
3150 * this is where we are basically btrfs_lookup, without the
3151 * crossing root thing. we store the inode number in the
3152 * offset of the orphan item.
3153 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003154
3155 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003156 btrfs_err(root->fs_info,
3157 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003158 ret = -EINVAL;
3159 goto out;
3160 }
3161
3162 last_objectid = found_key.offset;
3163
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003164 found_key.objectid = found_key.offset;
3165 found_key.type = BTRFS_INODE_ITEM_KEY;
3166 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003167 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003168 ret = PTR_RET(inode);
3169 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003170 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003171
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003172 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3173 struct btrfs_root *dead_root;
3174 struct btrfs_fs_info *fs_info = root->fs_info;
3175 int is_dead_root = 0;
3176
3177 /*
3178 * this is an orphan in the tree root. Currently these
3179 * could come from 2 sources:
3180 * a) a snapshot deletion in progress
3181 * b) a free space cache inode
3182 * We need to distinguish those two, as the snapshot
3183 * orphan must not get deleted.
3184 * find_dead_roots already ran before us, so if this
3185 * is a snapshot deletion, we should find the root
3186 * in the dead_roots list
3187 */
3188 spin_lock(&fs_info->trans_lock);
3189 list_for_each_entry(dead_root, &fs_info->dead_roots,
3190 root_list) {
3191 if (dead_root->root_key.objectid ==
3192 found_key.objectid) {
3193 is_dead_root = 1;
3194 break;
3195 }
3196 }
3197 spin_unlock(&fs_info->trans_lock);
3198 if (is_dead_root) {
3199 /* prevent this orphan from being found again */
3200 key.offset = found_key.objectid - 1;
3201 continue;
3202 }
3203 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003204 /*
3205 * Inode is already gone but the orphan item is still there,
3206 * kill the orphan item.
3207 */
3208 if (ret == -ESTALE) {
3209 trans = btrfs_start_transaction(root, 1);
3210 if (IS_ERR(trans)) {
3211 ret = PTR_ERR(trans);
3212 goto out;
3213 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003214 btrfs_debug(root->fs_info, "auto deleting %Lu",
3215 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003216 ret = btrfs_del_orphan_item(trans, root,
3217 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003218 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003219 btrfs_end_transaction(trans, root);
3220 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003221 }
Josef Bacik7b128762008-07-24 12:17:14 -04003222
Josef Bacik7b128762008-07-24 12:17:14 -04003223 /*
3224 * add this inode to the orphan list so btrfs_orphan_del does
3225 * the proper thing when we hit it
3226 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003227 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3228 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003229 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003230
Josef Bacik7b128762008-07-24 12:17:14 -04003231 /* if we have links, this was a truncate, lets do that */
3232 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003233 if (!S_ISREG(inode->i_mode)) {
3234 WARN_ON(1);
3235 iput(inode);
3236 continue;
3237 }
Josef Bacik7b128762008-07-24 12:17:14 -04003238 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003239
3240 /* 1 for the orphan item deletion. */
3241 trans = btrfs_start_transaction(root, 1);
3242 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003243 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003244 ret = PTR_ERR(trans);
3245 goto out;
3246 }
3247 ret = btrfs_orphan_add(trans, inode);
3248 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003249 if (ret) {
3250 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003251 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003252 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003253
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003254 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003255 if (ret)
3256 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003257 } else {
3258 nr_unlink++;
3259 }
3260
3261 /* this will do delete_inode and everything for us */
3262 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003263 if (ret)
3264 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003265 }
Miao Xie3254c872011-11-10 20:45:05 -05003266 /* release the path since we're done with it */
3267 btrfs_release_path(path);
3268
Yan, Zhengd68fc572010-05-16 10:49:58 -04003269 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3270
3271 if (root->orphan_block_rsv)
3272 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3273 (u64)-1);
3274
3275 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003276 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003277 if (!IS_ERR(trans))
3278 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003279 }
Josef Bacik7b128762008-07-24 12:17:14 -04003280
3281 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003282 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003283 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003284 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003285
3286out:
3287 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003288 btrfs_crit(root->fs_info,
3289 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003290 btrfs_free_path(path);
3291 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003292}
3293
Chris Masond352ac62008-09-29 15:18:18 -04003294/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003295 * very simple check to peek ahead in the leaf looking for xattrs. If we
3296 * don't find any xattrs, we know there can't be any acls.
3297 *
3298 * slot is the slot the inode is in, objectid is the objectid of the inode
3299 */
3300static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3301 int slot, u64 objectid)
3302{
3303 u32 nritems = btrfs_header_nritems(leaf);
3304 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003305 static u64 xattr_access = 0;
3306 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003307 int scanned = 0;
3308
Josef Bacikf23b5a52013-06-19 10:16:26 -04003309 if (!xattr_access) {
3310 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3311 strlen(POSIX_ACL_XATTR_ACCESS));
3312 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3313 strlen(POSIX_ACL_XATTR_DEFAULT));
3314 }
3315
Chris Mason46a53cc2009-04-27 11:47:50 -04003316 slot++;
3317 while (slot < nritems) {
3318 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3319
3320 /* we found a different objectid, there must not be acls */
3321 if (found_key.objectid != objectid)
3322 return 0;
3323
3324 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003325 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3326 if (found_key.offset == xattr_access ||
3327 found_key.offset == xattr_default)
3328 return 1;
3329 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003330
3331 /*
3332 * we found a key greater than an xattr key, there can't
3333 * be any acls later on
3334 */
3335 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3336 return 0;
3337
3338 slot++;
3339 scanned++;
3340
3341 /*
3342 * it goes inode, inode backrefs, xattrs, extents,
3343 * so if there are a ton of hard links to an inode there can
3344 * be a lot of backrefs. Don't waste time searching too hard,
3345 * this is just an optimization
3346 */
3347 if (scanned >= 8)
3348 break;
3349 }
3350 /* we hit the end of the leaf before we found an xattr or
3351 * something larger than an xattr. We have to assume the inode
3352 * has acls
3353 */
3354 return 1;
3355}
3356
3357/*
Chris Masond352ac62008-09-29 15:18:18 -04003358 * read an inode from the btree into the in-memory inode
3359 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003360static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003361{
3362 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003363 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003364 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003365 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003366 struct btrfs_root *root = BTRFS_I(inode)->root;
3367 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003368 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003369 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003370 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003371 bool filled = false;
3372
3373 ret = btrfs_fill_inode(inode, &rdev);
3374 if (!ret)
3375 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003376
3377 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003378 if (!path)
3379 goto make_bad;
3380
Josef Bacikd90c7322011-05-17 09:50:54 -04003381 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003382 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003383
Chris Mason39279cc2007-06-12 06:35:45 -04003384 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003385 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003386 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003387
Chris Mason5f39d392007-10-15 16:14:19 -04003388 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003389
3390 if (filled)
3391 goto cache_acl;
3392
Chris Mason5f39d392007-10-15 16:14:19 -04003393 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3394 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003395 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003396 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003397 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3398 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003399 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003400
3401 tspec = btrfs_inode_atime(inode_item);
3402 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3403 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3404
3405 tspec = btrfs_inode_mtime(inode_item);
3406 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3407 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3408
3409 tspec = btrfs_inode_ctime(inode_item);
3410 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3411 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3412
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003413 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003414 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003415 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3416
3417 /*
3418 * If we were modified in the current generation and evicted from memory
3419 * and then re-read we need to do a full sync since we don't have any
3420 * idea about which extents were modified before we were evicted from
3421 * cache.
3422 */
3423 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3424 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3425 &BTRFS_I(inode)->runtime_flags);
3426
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003427 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003428 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003429 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003430 rdev = btrfs_inode_rdev(leaf, inode_item);
3431
Josef Bacikaec74772008-07-24 12:12:38 -04003432 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003433 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003434cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003435 /*
3436 * try to precache a NULL acl entry for files that don't have
3437 * any xattrs or acls
3438 */
Li Zefan33345d012011-04-20 10:31:50 +08003439 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3440 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003441 if (!maybe_acls)
3442 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003443
Chris Mason39279cc2007-06-12 06:35:45 -04003444 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003445
Chris Mason39279cc2007-06-12 06:35:45 -04003446 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003447 case S_IFREG:
3448 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003449 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003450 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003451 inode->i_fop = &btrfs_file_operations;
3452 inode->i_op = &btrfs_file_inode_operations;
3453 break;
3454 case S_IFDIR:
3455 inode->i_fop = &btrfs_dir_file_operations;
3456 if (root == root->fs_info->tree_root)
3457 inode->i_op = &btrfs_dir_ro_inode_operations;
3458 else
3459 inode->i_op = &btrfs_dir_inode_operations;
3460 break;
3461 case S_IFLNK:
3462 inode->i_op = &btrfs_symlink_inode_operations;
3463 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003464 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003465 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003466 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003467 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003468 init_special_inode(inode, inode->i_mode, rdev);
3469 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003470 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003471
3472 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003473 return;
3474
3475make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003476 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003477 make_bad_inode(inode);
3478}
3479
Chris Masond352ac62008-09-29 15:18:18 -04003480/*
3481 * given a leaf and an inode, copy the inode fields into the leaf
3482 */
Chris Masone02119d2008-09-05 16:13:11 -04003483static void fill_inode_item(struct btrfs_trans_handle *trans,
3484 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003485 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003486 struct inode *inode)
3487{
Liu Bo51fab692012-12-27 09:01:21 +00003488 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003489
Liu Bo51fab692012-12-27 09:01:21 +00003490 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003491
Liu Bo51fab692012-12-27 09:01:21 +00003492 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3493 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3494 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3495 &token);
3496 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3497 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003498
Liu Bo51fab692012-12-27 09:01:21 +00003499 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3500 inode->i_atime.tv_sec, &token);
3501 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3502 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003503
Liu Bo51fab692012-12-27 09:01:21 +00003504 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3505 inode->i_mtime.tv_sec, &token);
3506 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3507 inode->i_mtime.tv_nsec, &token);
3508
3509 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3510 inode->i_ctime.tv_sec, &token);
3511 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3512 inode->i_ctime.tv_nsec, &token);
3513
3514 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3515 &token);
3516 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3517 &token);
3518 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3519 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3520 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3521 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3522 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003523}
3524
Chris Masond352ac62008-09-29 15:18:18 -04003525/*
3526 * copy everything in the in-memory inode into the btree.
3527 */
Chris Mason21151332011-11-10 20:39:08 -05003528static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003529 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003530{
3531 struct btrfs_inode_item *inode_item;
3532 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003533 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003534 int ret;
3535
3536 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003537 if (!path)
3538 return -ENOMEM;
3539
Chris Masonb9473432009-03-13 11:00:37 -04003540 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003541 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3542 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003543 if (ret) {
3544 if (ret > 0)
3545 ret = -ENOENT;
3546 goto failed;
3547 }
3548
Chris Masonb4ce94d2009-02-04 09:25:08 -05003549 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003550 leaf = path->nodes[0];
3551 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003552 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003553
Chris Masone02119d2008-09-05 16:13:11 -04003554 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003555 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003556 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003557 ret = 0;
3558failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003559 btrfs_free_path(path);
3560 return ret;
3561}
3562
Chris Masond352ac62008-09-29 15:18:18 -04003563/*
Chris Mason21151332011-11-10 20:39:08 -05003564 * copy everything in the in-memory inode into the btree.
3565 */
3566noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3567 struct btrfs_root *root, struct inode *inode)
3568{
3569 int ret;
3570
3571 /*
3572 * If the inode is a free space inode, we can deadlock during commit
3573 * if we put it into the delayed code.
3574 *
3575 * The data relocation inode should also be directly updated
3576 * without delay
3577 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003578 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003579 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003580 btrfs_update_root_times(trans, root);
3581
Chris Mason21151332011-11-10 20:39:08 -05003582 ret = btrfs_delayed_update_inode(trans, root, inode);
3583 if (!ret)
3584 btrfs_set_inode_last_trans(trans, inode);
3585 return ret;
3586 }
3587
3588 return btrfs_update_inode_item(trans, root, inode);
3589}
3590
Josef Bacikbe6aef62012-10-22 15:43:12 -04003591noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3592 struct btrfs_root *root,
3593 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003594{
3595 int ret;
3596
3597 ret = btrfs_update_inode(trans, root, inode);
3598 if (ret == -ENOSPC)
3599 return btrfs_update_inode_item(trans, root, inode);
3600 return ret;
3601}
3602
3603/*
Chris Masond352ac62008-09-29 15:18:18 -04003604 * unlink helper that gets used here in inode.c and in the tree logging
3605 * recovery code. It remove a link in a directory with a given name, and
3606 * also drops the back refs in the inode to the directory
3607 */
Al Viro92986792011-03-04 17:14:37 +00003608static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3609 struct btrfs_root *root,
3610 struct inode *dir, struct inode *inode,
3611 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003612{
3613 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003614 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003615 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003616 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003617 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003618 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003619 u64 ino = btrfs_ino(inode);
3620 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003621
3622 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003623 if (!path) {
3624 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003625 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003626 }
3627
Chris Masonb9473432009-03-13 11:00:37 -04003628 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003629 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003630 name, name_len, -1);
3631 if (IS_ERR(di)) {
3632 ret = PTR_ERR(di);
3633 goto err;
3634 }
3635 if (!di) {
3636 ret = -ENOENT;
3637 goto err;
3638 }
Chris Mason5f39d392007-10-15 16:14:19 -04003639 leaf = path->nodes[0];
3640 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003641 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003642 if (ret)
3643 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003644 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003645
Li Zefan33345d012011-04-20 10:31:50 +08003646 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3647 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003648 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003649 btrfs_info(root->fs_info,
3650 "failed to delete reference to %.*s, inode %llu parent %llu",
3651 name_len, name,
3652 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003653 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003654 goto err;
3655 }
3656
Miao Xie16cdcec2011-04-22 18:12:22 +08003657 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003658 if (ret) {
3659 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003660 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003661 }
Chris Mason39279cc2007-06-12 06:35:45 -04003662
Chris Masone02119d2008-09-05 16:13:11 -04003663 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003664 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003665 if (ret != 0 && ret != -ENOENT) {
3666 btrfs_abort_transaction(trans, root, ret);
3667 goto err;
3668 }
Chris Masone02119d2008-09-05 16:13:11 -04003669
3670 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3671 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003672 if (ret == -ENOENT)
3673 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003674 else if (ret)
3675 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003676err:
3677 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003678 if (ret)
3679 goto out;
3680
3681 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003682 inode_inc_iversion(inode);
3683 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003684 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003685 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003686out:
Chris Mason39279cc2007-06-12 06:35:45 -04003687 return ret;
3688}
3689
Al Viro92986792011-03-04 17:14:37 +00003690int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3691 struct btrfs_root *root,
3692 struct inode *dir, struct inode *inode,
3693 const char *name, int name_len)
3694{
3695 int ret;
3696 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3697 if (!ret) {
3698 btrfs_drop_nlink(inode);
3699 ret = btrfs_update_inode(trans, root, inode);
3700 }
3701 return ret;
3702}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003703
3704/*
3705 * helper to start transaction for unlink and rmdir.
3706 *
Josef Bacikd52be812013-05-29 14:54:47 -04003707 * unlink and rmdir are special in btrfs, they do not always free space, so
3708 * if we cannot make our reservations the normal way try and see if there is
3709 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3710 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003711 */
Josef Bacikd52be812013-05-29 14:54:47 -04003712static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003713{
3714 struct btrfs_trans_handle *trans;
3715 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003716 int ret;
3717
Josef Bacike70bea52011-10-11 14:18:24 -04003718 /*
3719 * 1 for the possible orphan item
3720 * 1 for the dir item
3721 * 1 for the dir index
3722 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003723 * 1 for the inode
3724 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003725 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003726 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3727 return trans;
3728
Josef Bacikd52be812013-05-29 14:54:47 -04003729 if (PTR_ERR(trans) == -ENOSPC) {
3730 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003731
Josef Bacikd52be812013-05-29 14:54:47 -04003732 trans = btrfs_start_transaction(root, 0);
3733 if (IS_ERR(trans))
3734 return trans;
3735 ret = btrfs_cond_migrate_bytes(root->fs_info,
3736 &root->fs_info->trans_block_rsv,
3737 num_bytes, 5);
3738 if (ret) {
3739 btrfs_end_transaction(trans, root);
3740 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003741 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003742 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003743 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003744 }
Josef Bacikd52be812013-05-29 14:54:47 -04003745 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003746}
3747
Chris Mason39279cc2007-06-12 06:35:45 -04003748static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3749{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003750 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003751 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003752 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003753 int ret;
3754
Josef Bacikd52be812013-05-29 14:54:47 -04003755 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003756 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003757 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003758
Chris Mason12fcfd22009-03-24 10:24:20 -04003759 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3760
Chris Masone02119d2008-09-05 16:13:11 -04003761 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3762 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003763 if (ret)
3764 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003765
Yan, Zhenga22285a2010-05-16 10:48:46 -04003766 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003767 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003768 if (ret)
3769 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003770 }
Josef Bacik7b128762008-07-24 12:17:14 -04003771
Tsutomu Itohb5324022011-07-19 07:27:20 +00003772out:
Josef Bacikd52be812013-05-29 14:54:47 -04003773 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003774 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003775 return ret;
3776}
3777
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003778int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3779 struct btrfs_root *root,
3780 struct inode *dir, u64 objectid,
3781 const char *name, int name_len)
3782{
3783 struct btrfs_path *path;
3784 struct extent_buffer *leaf;
3785 struct btrfs_dir_item *di;
3786 struct btrfs_key key;
3787 u64 index;
3788 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003789 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003790
3791 path = btrfs_alloc_path();
3792 if (!path)
3793 return -ENOMEM;
3794
Li Zefan33345d012011-04-20 10:31:50 +08003795 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003796 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003797 if (IS_ERR_OR_NULL(di)) {
3798 if (!di)
3799 ret = -ENOENT;
3800 else
3801 ret = PTR_ERR(di);
3802 goto out;
3803 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003804
3805 leaf = path->nodes[0];
3806 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3807 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3808 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003809 if (ret) {
3810 btrfs_abort_transaction(trans, root, ret);
3811 goto out;
3812 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003813 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003814
3815 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3816 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003817 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003818 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003819 if (ret != -ENOENT) {
3820 btrfs_abort_transaction(trans, root, ret);
3821 goto out;
3822 }
Li Zefan33345d012011-04-20 10:31:50 +08003823 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003824 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003825 if (IS_ERR_OR_NULL(di)) {
3826 if (!di)
3827 ret = -ENOENT;
3828 else
3829 ret = PTR_ERR(di);
3830 btrfs_abort_transaction(trans, root, ret);
3831 goto out;
3832 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003833
3834 leaf = path->nodes[0];
3835 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003836 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003837 index = key.offset;
3838 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003839 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003840
Miao Xie16cdcec2011-04-22 18:12:22 +08003841 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003842 if (ret) {
3843 btrfs_abort_transaction(trans, root, ret);
3844 goto out;
3845 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003846
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003847 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003848 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003849 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003850 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003851 if (ret)
3852 btrfs_abort_transaction(trans, root, ret);
3853out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003854 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003855 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003856}
3857
Chris Mason39279cc2007-06-12 06:35:45 -04003858static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3859{
3860 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003861 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003862 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003863 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003864
David Sterbab3ae2442012-09-13 16:04:34 -06003865 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003866 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003867 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3868 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003869
Josef Bacikd52be812013-05-29 14:54:47 -04003870 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003871 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003872 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003873
Li Zefan33345d012011-04-20 10:31:50 +08003874 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003875 err = btrfs_unlink_subvol(trans, root, dir,
3876 BTRFS_I(inode)->location.objectid,
3877 dentry->d_name.name,
3878 dentry->d_name.len);
3879 goto out;
3880 }
3881
Josef Bacik7b128762008-07-24 12:17:14 -04003882 err = btrfs_orphan_add(trans, inode);
3883 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003884 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003885
Chris Mason39279cc2007-06-12 06:35:45 -04003886 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003887 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3888 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003889 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003890 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003891out:
Josef Bacikd52be812013-05-29 14:54:47 -04003892 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003893 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003894
Chris Mason39279cc2007-06-12 06:35:45 -04003895 return err;
3896}
3897
Chris Mason323ac952008-10-01 19:05:46 -04003898/*
Chris Mason39279cc2007-06-12 06:35:45 -04003899 * this can truncate away extent items, csum items and directory items.
3900 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003901 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003902 *
3903 * csum items that cross the new i_size are truncated to the new size
3904 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003905 *
3906 * min_type is the minimum key type to truncate down to. If set to 0, this
3907 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003908 */
Yan, Zheng80825102009-11-12 09:35:36 +00003909int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3910 struct btrfs_root *root,
3911 struct inode *inode,
3912 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003913{
Chris Mason39279cc2007-06-12 06:35:45 -04003914 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003915 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003916 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003917 struct btrfs_key key;
3918 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003919 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003920 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003921 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003922 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003923 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003924 int found_extent;
3925 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003926 int pending_del_nr = 0;
3927 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003928 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003929 int ret;
3930 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003931 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003932
3933 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003934
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003935 path = btrfs_alloc_path();
3936 if (!path)
3937 return -ENOMEM;
3938 path->reada = -1;
3939
Josef Bacik5dc562c2012-08-17 13:14:17 -04003940 /*
3941 * We want to drop from the next block forward in case this new size is
3942 * not block aligned since we will be keeping the last block of the
3943 * extent just the way it is.
3944 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003945 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00003946 btrfs_drop_extent_cache(inode, ALIGN(new_size,
3947 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003948
Miao Xie16cdcec2011-04-22 18:12:22 +08003949 /*
3950 * This function is also used to drop the items in the log tree before
3951 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3952 * it is used to drop the loged items. So we shouldn't kill the delayed
3953 * items.
3954 */
3955 if (min_type == 0 && root == BTRFS_I(inode)->root)
3956 btrfs_kill_delayed_inode_items(inode);
3957
Li Zefan33345d012011-04-20 10:31:50 +08003958 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003959 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003960 key.type = (u8)-1;
3961
Chris Mason85e21ba2008-01-29 15:11:36 -05003962search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003963 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003964 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003965 if (ret < 0) {
3966 err = ret;
3967 goto out;
3968 }
Chris Masond3977122009-01-05 21:25:51 -05003969
Chris Mason85e21ba2008-01-29 15:11:36 -05003970 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003971 /* there are no items in the tree for us to truncate, we're
3972 * done
3973 */
Yan, Zheng80825102009-11-12 09:35:36 +00003974 if (path->slots[0] == 0)
3975 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003976 path->slots[0]--;
3977 }
3978
Chris Masond3977122009-01-05 21:25:51 -05003979 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003980 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003981 leaf = path->nodes[0];
3982 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3983 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003984
Li Zefan33345d012011-04-20 10:31:50 +08003985 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003986 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003987
Chris Mason85e21ba2008-01-29 15:11:36 -05003988 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003989 break;
3990
Chris Mason5f39d392007-10-15 16:14:19 -04003991 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003992 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003993 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003994 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003995 extent_type = btrfs_file_extent_type(leaf, fi);
3996 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003997 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003998 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003999 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004000 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04004001 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004002 }
Yan008630c2007-11-07 13:31:09 -05004003 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004004 }
Yan, Zheng80825102009-11-12 09:35:36 +00004005 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004006 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004007 } else {
4008 if (item_end < new_size)
4009 break;
4010 if (found_key.offset >= new_size)
4011 del_item = 1;
4012 else
4013 del_item = 0;
4014 }
Chris Mason39279cc2007-06-12 06:35:45 -04004015 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004016 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004017 if (found_type != BTRFS_EXTENT_DATA_KEY)
4018 goto delete;
4019
4020 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004021 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004022 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004023 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004024 u64 orig_num_bytes =
4025 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004026 extent_num_bytes = ALIGN(new_size -
4027 found_key.offset,
4028 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004029 btrfs_set_file_extent_num_bytes(leaf, fi,
4030 extent_num_bytes);
4031 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004032 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004033 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004034 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004035 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004036 } else {
Chris Masondb945352007-10-15 16:15:53 -04004037 extent_num_bytes =
4038 btrfs_file_extent_disk_num_bytes(leaf,
4039 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004040 extent_offset = found_key.offset -
4041 btrfs_file_extent_offset(leaf, fi);
4042
Chris Mason39279cc2007-06-12 06:35:45 -04004043 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004044 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004045 if (extent_start != 0) {
4046 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004047 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004048 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004049 }
4050 }
Chris Mason90692182008-02-08 13:49:28 -05004051 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004052 /*
4053 * we can't truncate inline items that have had
4054 * special encodings
4055 */
4056 if (!del_item &&
4057 btrfs_file_extent_compression(leaf, fi) == 0 &&
4058 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4059 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004060 u32 size = new_size - found_key.offset;
4061
4062 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004063 inode_sub_bytes(inode, item_end + 1 -
4064 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004065 }
4066 size =
4067 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004068 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004069 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004070 inode_sub_bytes(inode, item_end + 1 -
4071 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004072 }
Chris Mason39279cc2007-06-12 06:35:45 -04004073 }
Chris Mason179e29e2007-11-01 11:28:41 -04004074delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004075 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004076 if (!pending_del_nr) {
4077 /* no pending yet, add ourselves */
4078 pending_del_slot = path->slots[0];
4079 pending_del_nr = 1;
4080 } else if (pending_del_nr &&
4081 path->slots[0] + 1 == pending_del_slot) {
4082 /* hop on the pending chunk */
4083 pending_del_nr++;
4084 pending_del_slot = path->slots[0];
4085 } else {
Chris Masond3977122009-01-05 21:25:51 -05004086 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004087 }
Chris Mason39279cc2007-06-12 06:35:45 -04004088 } else {
4089 break;
4090 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004091 if (found_extent && (root->ref_cows ||
4092 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004093 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004094 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004095 extent_num_bytes, 0,
4096 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004097 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004098 BUG_ON(ret);
4099 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004100
Yan, Zheng80825102009-11-12 09:35:36 +00004101 if (found_type == BTRFS_INODE_ITEM_KEY)
4102 break;
4103
4104 if (path->slots[0] == 0 ||
4105 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004106 if (pending_del_nr) {
4107 ret = btrfs_del_items(trans, root, path,
4108 pending_del_slot,
4109 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004110 if (ret) {
4111 btrfs_abort_transaction(trans,
4112 root, ret);
4113 goto error;
4114 }
Yan, Zheng80825102009-11-12 09:35:36 +00004115 pending_del_nr = 0;
4116 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004117 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004118 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004119 } else {
4120 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004121 }
Chris Mason39279cc2007-06-12 06:35:45 -04004122 }
Yan, Zheng80825102009-11-12 09:35:36 +00004123out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004124 if (pending_del_nr) {
4125 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4126 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004127 if (ret)
4128 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004129 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004130error:
Chris Mason39279cc2007-06-12 06:35:45 -04004131 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004132 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004133}
4134
Chris Masona52d9a82007-08-27 16:49:44 -04004135/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004136 * btrfs_truncate_page - read, zero a chunk and write a page
4137 * @inode - inode that we're zeroing
4138 * @from - the offset to start zeroing
4139 * @len - the length to zero, 0 to zero the entire range respective to the
4140 * offset
4141 * @front - zero up to the offset instead of from the offset on
4142 *
4143 * This will find the page for the "from" offset and cow the page and zero the
4144 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004145 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004146int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4147 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004148{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004149 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004150 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004151 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4152 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004153 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004154 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004155 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004156 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4157 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4158 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004159 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004160 int ret = 0;
4161 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004162 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004163
Josef Bacik2aaa6652012-08-29 14:27:18 -04004164 if ((offset & (blocksize - 1)) == 0 &&
4165 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004166 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004167 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004168 if (ret)
4169 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004170
Chris Mason211c17f2008-05-15 09:13:45 -04004171again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004172 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004173 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004174 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004175 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004176 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004177 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004178
4179 page_start = page_offset(page);
4180 page_end = page_start + PAGE_CACHE_SIZE - 1;
4181
Chris Masona52d9a82007-08-27 16:49:44 -04004182 if (!PageUptodate(page)) {
4183 ret = btrfs_readpage(NULL, page);
4184 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004185 if (page->mapping != mapping) {
4186 unlock_page(page);
4187 page_cache_release(page);
4188 goto again;
4189 }
Chris Masona52d9a82007-08-27 16:49:44 -04004190 if (!PageUptodate(page)) {
4191 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004192 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004193 }
4194 }
Chris Mason211c17f2008-05-15 09:13:45 -04004195 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004196
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004197 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004198 set_page_extent_mapped(page);
4199
4200 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4201 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004202 unlock_extent_cached(io_tree, page_start, page_end,
4203 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004204 unlock_page(page);
4205 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004206 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004207 btrfs_put_ordered_extent(ordered);
4208 goto again;
4209 }
4210
Josef Bacik2ac55d42010-02-03 19:33:23 +00004211 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004212 EXTENT_DIRTY | EXTENT_DELALLOC |
4213 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004214 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004215
Josef Bacik2ac55d42010-02-03 19:33:23 +00004216 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4217 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004218 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004219 unlock_extent_cached(io_tree, page_start, page_end,
4220 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004221 goto out_unlock;
4222 }
4223
Chris Masone6dcd2d2008-07-17 12:53:50 -04004224 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004225 if (!len)
4226 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004227 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004228 if (front)
4229 memset(kaddr, 0, offset);
4230 else
4231 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004232 flush_dcache_page(page);
4233 kunmap(page);
4234 }
Chris Mason247e7432008-07-17 12:53:51 -04004235 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004236 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004237 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4238 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004239
Chris Mason89642222008-07-24 09:41:53 -04004240out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004241 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004242 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004243 unlock_page(page);
4244 page_cache_release(page);
4245out:
4246 return ret;
4247}
4248
Josef Bacik695a0d02011-03-04 15:46:53 -05004249/*
4250 * This function puts in dummy file extents for the area we're creating a hole
4251 * for. So if we are truncating this file to a larger size we need to insert
4252 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4253 * the range between oldsize and size
4254 */
Josef Bacika41ad392011-01-31 15:30:16 -05004255int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004256{
4257 struct btrfs_trans_handle *trans;
4258 struct btrfs_root *root = BTRFS_I(inode)->root;
4259 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004260 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004261 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004262 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004263 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4264 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004265 u64 last_byte;
4266 u64 cur_offset;
4267 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004268 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004269
Josef Bacika71754f2013-06-17 17:14:39 -04004270 /*
4271 * If our size started in the middle of a page we need to zero out the
4272 * rest of the page before we expand the i_size, otherwise we could
4273 * expose stale data.
4274 */
4275 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4276 if (err)
4277 return err;
4278
Yan Zheng9036c102008-10-30 14:19:41 -04004279 if (size <= hole_start)
4280 return 0;
4281
Yan Zheng9036c102008-10-30 14:19:41 -04004282 while (1) {
4283 struct btrfs_ordered_extent *ordered;
4284 btrfs_wait_ordered_range(inode, hole_start,
4285 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004286 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004287 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004288 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4289 if (!ordered)
4290 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004291 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4292 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004293 btrfs_put_ordered_extent(ordered);
4294 }
4295
Yan Zheng9036c102008-10-30 14:19:41 -04004296 cur_offset = hole_start;
4297 while (1) {
4298 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4299 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004300 if (IS_ERR(em)) {
4301 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004302 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004303 break;
4304 }
Yan Zheng9036c102008-10-30 14:19:41 -04004305 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004306 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004307 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004308 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004309 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004310
Miao Xie36423202011-12-14 20:12:02 -05004311 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004312 if (IS_ERR(trans)) {
4313 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004314 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004315 }
Yan, Zheng80825102009-11-12 09:35:36 +00004316
Josef Bacik5dc562c2012-08-17 13:14:17 -04004317 err = btrfs_drop_extents(trans, root, inode,
4318 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004319 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004320 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004321 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004322 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004323 break;
Miao Xie5b397372011-09-11 10:52:24 -04004324 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004325
Yan Zheng9036c102008-10-30 14:19:41 -04004326 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004327 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004328 0, hole_size, 0, hole_size,
4329 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004330 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004331 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004332 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004333 break;
Miao Xie5b397372011-09-11 10:52:24 -04004334 }
Yan, Zheng80825102009-11-12 09:35:36 +00004335
Josef Bacik5dc562c2012-08-17 13:14:17 -04004336 btrfs_drop_extent_cache(inode, cur_offset,
4337 cur_offset + hole_size - 1, 0);
4338 hole_em = alloc_extent_map();
4339 if (!hole_em) {
4340 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4341 &BTRFS_I(inode)->runtime_flags);
4342 goto next;
4343 }
4344 hole_em->start = cur_offset;
4345 hole_em->len = hole_size;
4346 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004347
Josef Bacik5dc562c2012-08-17 13:14:17 -04004348 hole_em->block_start = EXTENT_MAP_HOLE;
4349 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004350 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004351 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004352 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4353 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4354 hole_em->generation = trans->transid;
4355
4356 while (1) {
4357 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004358 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004359 write_unlock(&em_tree->lock);
4360 if (err != -EEXIST)
4361 break;
4362 btrfs_drop_extent_cache(inode, cur_offset,
4363 cur_offset +
4364 hole_size - 1, 0);
4365 }
4366 free_extent_map(hole_em);
4367next:
Miao Xie36423202011-12-14 20:12:02 -05004368 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004369 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004370 }
4371 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004372 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004373 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004374 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004375 break;
4376 }
4377
Yan, Zhenga22285a2010-05-16 10:48:46 -04004378 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004379 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4380 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004381 return err;
4382}
4383
Eric Sandeen3972f262013-01-12 02:57:22 +00004384static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004385{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004386 struct btrfs_root *root = BTRFS_I(inode)->root;
4387 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004388 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004389 loff_t newsize = attr->ia_size;
4390 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004391 int ret;
4392
Josef Bacika41ad392011-01-31 15:30:16 -05004393 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00004394 return 0;
4395
Eric Sandeen3972f262013-01-12 02:57:22 +00004396 /*
4397 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4398 * special case where we need to update the times despite not having
4399 * these flags set. For all other operations the VFS set these flags
4400 * explicitly if it wants a timestamp update.
4401 */
4402 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4403 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4404
Josef Bacika41ad392011-01-31 15:30:16 -05004405 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004406 truncate_pagecache(inode, oldsize, newsize);
4407 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004408 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004409 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004410
Miao Xief4a2f4c2011-12-14 20:12:01 -05004411 trans = btrfs_start_transaction(root, 1);
4412 if (IS_ERR(trans))
4413 return PTR_ERR(trans);
4414
4415 i_size_write(inode, newsize);
4416 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4417 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004418 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004419 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004420
Josef Bacika41ad392011-01-31 15:30:16 -05004421 /*
4422 * We're truncating a file that used to have good data down to
4423 * zero. Make sure it gets into the ordered flush list so that
4424 * any new writes get down to disk quickly.
4425 */
4426 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004427 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4428 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004429
Josef Bacikf3fe8202013-01-07 17:03:21 -05004430 /*
4431 * 1 for the orphan item we're going to add
4432 * 1 for the orphan item deletion.
4433 */
4434 trans = btrfs_start_transaction(root, 2);
4435 if (IS_ERR(trans))
4436 return PTR_ERR(trans);
4437
4438 /*
4439 * We need to do this in case we fail at _any_ point during the
4440 * actual truncate. Once we do the truncate_setsize we could
4441 * invalidate pages which forces any outstanding ordered io to
4442 * be instantly completed which will give us extents that need
4443 * to be truncated. If we fail to get an orphan inode down we
4444 * could have left over extents that were never meant to live,
4445 * so we need to garuntee from this point on that everything
4446 * will be consistent.
4447 */
4448 ret = btrfs_orphan_add(trans, inode);
4449 btrfs_end_transaction(trans, root);
4450 if (ret)
4451 return ret;
4452
Josef Bacika41ad392011-01-31 15:30:16 -05004453 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4454 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004455
4456 /* Disable nonlocked read DIO to avoid the end less truncate */
4457 btrfs_inode_block_unlocked_dio(inode);
4458 inode_dio_wait(inode);
4459 btrfs_inode_resume_unlocked_dio(inode);
4460
Josef Bacika41ad392011-01-31 15:30:16 -05004461 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004462 if (ret && inode->i_nlink)
4463 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004464 }
4465
Josef Bacika41ad392011-01-31 15:30:16 -05004466 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004467}
4468
Chris Mason39279cc2007-06-12 06:35:45 -04004469static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4470{
4471 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004472 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004473 int err;
4474
Li Zefanb83cc962010-12-20 16:04:08 +08004475 if (btrfs_root_readonly(root))
4476 return -EROFS;
4477
Chris Mason39279cc2007-06-12 06:35:45 -04004478 err = inode_change_ok(inode, attr);
4479 if (err)
4480 return err;
4481
Chris Mason5a3f23d2009-03-31 13:27:11 -04004482 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004483 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004484 if (err)
4485 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004486 }
Yan Zheng9036c102008-10-30 14:19:41 -04004487
Christoph Hellwig10257742010-06-04 11:30:02 +02004488 if (attr->ia_valid) {
4489 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004490 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004491 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004492
Josef Bacik22c44fe2011-11-30 10:45:38 -05004493 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004494 err = btrfs_acl_chmod(inode);
4495 }
4496
Chris Mason39279cc2007-06-12 06:35:45 -04004497 return err;
4498}
Chris Mason61295eb2008-01-14 16:24:38 -05004499
Al Virobd555972010-06-07 11:35:40 -04004500void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004501{
4502 struct btrfs_trans_handle *trans;
4503 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004504 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004505 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004506 int ret;
4507
liubo1abe9b82011-03-24 11:18:59 +00004508 trace_btrfs_inode_evict(inode);
4509
Chris Mason39279cc2007-06-12 06:35:45 -04004510 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004511 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004512 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004513 goto no_delete;
4514
Chris Mason39279cc2007-06-12 06:35:45 -04004515 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004516 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004517 goto no_delete;
4518 }
Al Virobd555972010-06-07 11:35:40 -04004519 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004520 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004521
Yan, Zhengc71bf092009-11-12 09:34:40 +00004522 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004523 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004524 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004525 goto no_delete;
4526 }
4527
Yan, Zheng76dda932009-09-21 16:00:26 -04004528 if (inode->i_nlink > 0) {
4529 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4530 goto no_delete;
4531 }
4532
Miao Xie0e8c36a2012-12-19 06:59:51 +00004533 ret = btrfs_commit_inode_delayed_inode(inode);
4534 if (ret) {
4535 btrfs_orphan_del(NULL, inode);
4536 goto no_delete;
4537 }
4538
Miao Xie66d8f3d2012-09-06 04:02:28 -06004539 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004540 if (!rsv) {
4541 btrfs_orphan_del(NULL, inode);
4542 goto no_delete;
4543 }
Josef Bacik4a338542011-08-29 11:01:31 -04004544 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004545 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004546 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004547
Chris Masondbe674a2008-07-17 12:54:05 -04004548 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004549
Josef Bacik4289a662011-08-05 13:22:24 -04004550 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004551 * This is a bit simpler than btrfs_truncate since we've already
4552 * reserved our space for our orphan item in the unlink, so we just
4553 * need to reserve some slack space in case we add bytes and update
4554 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004555 */
Yan, Zheng80825102009-11-12 09:35:36 +00004556 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004557 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4558 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004559
Josef Bacik726c35f2011-09-26 15:46:06 -04004560 /*
4561 * Try and steal from the global reserve since we will
4562 * likely not use this space anyway, we want to try as
4563 * hard as possible to get this to work.
4564 */
4565 if (ret)
4566 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4567
Yan, Zhengd68fc572010-05-16 10:49:58 -04004568 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004569 btrfs_warn(root->fs_info,
4570 "Could not get space for a delete, will truncate on mount %d",
4571 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004572 btrfs_orphan_del(NULL, inode);
4573 btrfs_free_block_rsv(root, rsv);
4574 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004575 }
4576
Miao Xie0e8c36a2012-12-19 06:59:51 +00004577 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004578 if (IS_ERR(trans)) {
4579 btrfs_orphan_del(NULL, inode);
4580 btrfs_free_block_rsv(root, rsv);
4581 goto no_delete;
4582 }
4583
4584 trans->block_rsv = rsv;
4585
Yan, Zhengd68fc572010-05-16 10:49:58 -04004586 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004587 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004588 break;
4589
Miao Xie8407aa42012-09-07 01:43:32 -06004590 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004591 btrfs_end_transaction(trans, root);
4592 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004593 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004594 }
4595
Josef Bacik4289a662011-08-05 13:22:24 -04004596 btrfs_free_block_rsv(root, rsv);
4597
Yan, Zheng80825102009-11-12 09:35:36 +00004598 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004599 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004600 ret = btrfs_orphan_del(trans, inode);
4601 BUG_ON(ret);
4602 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004603
Josef Bacik4289a662011-08-05 13:22:24 -04004604 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004605 if (!(root == root->fs_info->tree_root ||
4606 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004607 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004608
Chris Mason54aa1f42007-06-22 14:16:25 -04004609 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004610 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004611no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004612 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004613 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004614 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004615}
4616
4617/*
4618 * this returns the key found in the dir entry in the location pointer.
4619 * If no dir entries were found, location->objectid is 0.
4620 */
4621static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4622 struct btrfs_key *location)
4623{
4624 const char *name = dentry->d_name.name;
4625 int namelen = dentry->d_name.len;
4626 struct btrfs_dir_item *di;
4627 struct btrfs_path *path;
4628 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004629 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004630
4631 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004632 if (!path)
4633 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004634
Li Zefan33345d012011-04-20 10:31:50 +08004635 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004636 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004637 if (IS_ERR(di))
4638 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004639
David Sterbac7040052011-04-19 18:00:01 +02004640 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004641 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004642
Chris Mason5f39d392007-10-15 16:14:19 -04004643 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004644out:
Chris Mason39279cc2007-06-12 06:35:45 -04004645 btrfs_free_path(path);
4646 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004647out_err:
4648 location->objectid = 0;
4649 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004650}
4651
4652/*
4653 * when we hit a tree root in a directory, the btrfs part of the inode
4654 * needs to be changed to reflect the root directory of the tree root. This
4655 * is kind of like crossing a mount point.
4656 */
4657static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004658 struct inode *dir,
4659 struct dentry *dentry,
4660 struct btrfs_key *location,
4661 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004662{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004663 struct btrfs_path *path;
4664 struct btrfs_root *new_root;
4665 struct btrfs_root_ref *ref;
4666 struct extent_buffer *leaf;
4667 int ret;
4668 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004669
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004670 path = btrfs_alloc_path();
4671 if (!path) {
4672 err = -ENOMEM;
4673 goto out;
4674 }
Chris Mason39279cc2007-06-12 06:35:45 -04004675
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004676 err = -ENOENT;
4677 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4678 BTRFS_I(dir)->root->root_key.objectid,
4679 location->objectid);
4680 if (ret) {
4681 if (ret < 0)
4682 err = ret;
4683 goto out;
4684 }
Chris Mason39279cc2007-06-12 06:35:45 -04004685
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004686 leaf = path->nodes[0];
4687 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004688 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004689 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4690 goto out;
4691
4692 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4693 (unsigned long)(ref + 1),
4694 dentry->d_name.len);
4695 if (ret)
4696 goto out;
4697
David Sterbab3b4aa72011-04-21 01:20:15 +02004698 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004699
4700 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4701 if (IS_ERR(new_root)) {
4702 err = PTR_ERR(new_root);
4703 goto out;
4704 }
4705
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004706 *sub_root = new_root;
4707 location->objectid = btrfs_root_dirid(&new_root->root_item);
4708 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004709 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004710 err = 0;
4711out:
4712 btrfs_free_path(path);
4713 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004714}
4715
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004716static void inode_tree_add(struct inode *inode)
4717{
4718 struct btrfs_root *root = BTRFS_I(inode)->root;
4719 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004720 struct rb_node **p;
4721 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004722 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004723
Al Viro1d3382cb2010-10-23 15:19:20 -04004724 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004725 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004726again:
4727 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004728 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004729 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004730 while (*p) {
4731 parent = *p;
4732 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4733
Li Zefan33345d012011-04-20 10:31:50 +08004734 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004735 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004736 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004737 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004738 else {
4739 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004740 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004741 rb_erase(parent, &root->inode_tree);
4742 RB_CLEAR_NODE(parent);
4743 spin_unlock(&root->inode_lock);
4744 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004745 }
4746 }
4747 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4748 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4749 spin_unlock(&root->inode_lock);
4750}
4751
4752static void inode_tree_del(struct inode *inode)
4753{
4754 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004755 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004756
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004757 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004758 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004759 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004760 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004761 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004762 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004763 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004764
Josef Bacik0af3d002010-06-21 14:48:16 -04004765 /*
4766 * Free space cache has inodes in the tree root, but the tree root has a
4767 * root_refs of 0, so this could end up dropping the tree root as a
4768 * snapshot, so we need the extra !root->fs_info->tree_root check to
4769 * make sure we don't drop it.
4770 */
4771 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4772 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004773 synchronize_srcu(&root->fs_info->subvol_srcu);
4774 spin_lock(&root->inode_lock);
4775 empty = RB_EMPTY_ROOT(&root->inode_tree);
4776 spin_unlock(&root->inode_lock);
4777 if (empty)
4778 btrfs_add_dead_root(root);
4779 }
4780}
4781
Jeff Mahoney143bede2012-03-01 14:56:26 +01004782void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004783{
4784 struct rb_node *node;
4785 struct rb_node *prev;
4786 struct btrfs_inode *entry;
4787 struct inode *inode;
4788 u64 objectid = 0;
4789
4790 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4791
4792 spin_lock(&root->inode_lock);
4793again:
4794 node = root->inode_tree.rb_node;
4795 prev = NULL;
4796 while (node) {
4797 prev = node;
4798 entry = rb_entry(node, struct btrfs_inode, rb_node);
4799
Li Zefan33345d012011-04-20 10:31:50 +08004800 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004801 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004802 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004803 node = node->rb_right;
4804 else
4805 break;
4806 }
4807 if (!node) {
4808 while (prev) {
4809 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004810 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004811 node = prev;
4812 break;
4813 }
4814 prev = rb_next(prev);
4815 }
4816 }
4817 while (node) {
4818 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004819 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004820 inode = igrab(&entry->vfs_inode);
4821 if (inode) {
4822 spin_unlock(&root->inode_lock);
4823 if (atomic_read(&inode->i_count) > 1)
4824 d_prune_aliases(inode);
4825 /*
Al Viro45321ac2010-06-07 13:43:19 -04004826 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004827 * the inode cache when its usage count
4828 * hits zero.
4829 */
4830 iput(inode);
4831 cond_resched();
4832 spin_lock(&root->inode_lock);
4833 goto again;
4834 }
4835
4836 if (cond_resched_lock(&root->inode_lock))
4837 goto again;
4838
4839 node = rb_next(node);
4840 }
4841 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004842}
4843
Chris Masone02119d2008-09-05 16:13:11 -04004844static int btrfs_init_locked_inode(struct inode *inode, void *p)
4845{
4846 struct btrfs_iget_args *args = p;
4847 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004848 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004849 return 0;
4850}
4851
4852static int btrfs_find_actor(struct inode *inode, void *opaque)
4853{
4854 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004855 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004856 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004857}
4858
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004859static struct inode *btrfs_iget_locked(struct super_block *s,
4860 u64 objectid,
4861 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004862{
4863 struct inode *inode;
4864 struct btrfs_iget_args args;
4865 args.ino = objectid;
4866 args.root = root;
4867
4868 inode = iget5_locked(s, objectid, btrfs_find_actor,
4869 btrfs_init_locked_inode,
4870 (void *)&args);
4871 return inode;
4872}
4873
Balaji Rao1a54ef82008-07-21 02:01:04 +05304874/* Get an inode object given its location and corresponding root.
4875 * Returns in *is_new if the inode was read from disk
4876 */
4877struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004878 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304879{
4880 struct inode *inode;
4881
4882 inode = btrfs_iget_locked(s, location->objectid, root);
4883 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004884 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304885
4886 if (inode->i_state & I_NEW) {
4887 BTRFS_I(inode)->root = root;
4888 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4889 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004890 if (!is_bad_inode(inode)) {
4891 inode_tree_add(inode);
4892 unlock_new_inode(inode);
4893 if (new)
4894 *new = 1;
4895 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004896 unlock_new_inode(inode);
4897 iput(inode);
4898 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004899 }
4900 }
4901
Balaji Rao1a54ef82008-07-21 02:01:04 +05304902 return inode;
4903}
4904
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004905static struct inode *new_simple_dir(struct super_block *s,
4906 struct btrfs_key *key,
4907 struct btrfs_root *root)
4908{
4909 struct inode *inode = new_inode(s);
4910
4911 if (!inode)
4912 return ERR_PTR(-ENOMEM);
4913
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004914 BTRFS_I(inode)->root = root;
4915 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004916 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004917
4918 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004919 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004920 inode->i_fop = &simple_dir_operations;
4921 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4922 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4923
4924 return inode;
4925}
4926
Chris Mason3de45862008-11-17 21:02:50 -05004927struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004928{
Chris Masond3977122009-01-05 21:25:51 -05004929 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004930 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004931 struct btrfs_root *sub_root = root;
4932 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004933 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004934 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004935
4936 if (dentry->d_name.len > BTRFS_NAME_LEN)
4937 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004938
Jeff Layton39e3c952012-11-28 11:30:53 -05004939 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04004940 if (ret < 0)
4941 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004942
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004943 if (location.objectid == 0)
4944 return NULL;
4945
4946 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004947 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004948 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004949 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004950
4951 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4952
Yan, Zheng76dda932009-09-21 16:00:26 -04004953 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004954 ret = fixup_tree_root_location(root, dir, dentry,
4955 &location, &sub_root);
4956 if (ret < 0) {
4957 if (ret != -ENOENT)
4958 inode = ERR_PTR(ret);
4959 else
4960 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4961 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004962 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004963 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004964 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4965
Julia Lawall34d19ba2011-01-24 19:55:19 +00004966 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004967 down_read(&root->fs_info->cleanup_work_sem);
4968 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004969 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004970 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04004971 if (ret) {
4972 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004973 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04004974 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00004975 }
4976
Chris Mason3de45862008-11-17 21:02:50 -05004977 return inode;
4978}
4979
Nick Pigginfe15ce42011-01-07 17:49:23 +11004980static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004981{
4982 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004983 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004984
Li Zefan848cce02012-02-21 17:04:28 +08004985 if (!inode && !IS_ROOT(dentry))
4986 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004987
Li Zefan848cce02012-02-21 17:04:28 +08004988 if (inode) {
4989 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004990 if (btrfs_root_refs(&root->root_item) == 0)
4991 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004992
4993 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4994 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004995 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004996 return 0;
4997}
4998
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004999static void btrfs_dentry_release(struct dentry *dentry)
5000{
5001 if (dentry->d_fsdata)
5002 kfree(dentry->d_fsdata);
5003}
5004
Chris Mason3de45862008-11-17 21:02:50 -05005005static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005006 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005007{
Josef Bacika66e7cc2011-09-18 10:34:03 -04005008 struct dentry *ret;
5009
5010 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005011 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005012}
5013
Miao Xie16cdcec2011-04-22 18:12:22 +08005014unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005015 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5016};
5017
David Woodhousecbdf5a22008-08-06 19:42:33 +01005018static int btrfs_real_readdir(struct file *filp, void *dirent,
5019 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04005020{
Al Viro496ad9a2013-01-23 17:07:38 -05005021 struct inode *inode = file_inode(filp);
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;
Miao Xie16cdcec2011-04-22 18:12:22 +08005042 int is_curr = 0; /* filp->f_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
Chris Mason39544012007-12-12 14:38:19 -05005048 /* special case for "." */
5049 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005050 over = filldir(dirent, ".", 1,
5051 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005052 if (over)
5053 return 0;
5054 filp->f_pos = 1;
5055 }
Chris Mason39544012007-12-12 14:38:19 -05005056 /* special case for .., just use the back ref */
5057 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01005058 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05005059 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04005060 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05005061 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01005062 return 0;
Chris Mason39544012007-12-12 14:38:19 -05005063 filp->f_pos = 2;
5064 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005065 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005066 if (!path)
5067 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005068
Josef Bacik026fd312011-05-13 10:32:11 -04005069 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005070
Miao Xie16cdcec2011-04-22 18:12:22 +08005071 if (key_type == BTRFS_DIR_INDEX_KEY) {
5072 INIT_LIST_HEAD(&ins_list);
5073 INIT_LIST_HEAD(&del_list);
5074 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5075 }
5076
Chris Mason39279cc2007-06-12 06:35:45 -04005077 btrfs_set_key_type(&key, key_type);
5078 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08005079 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005080
Chris Mason39279cc2007-06-12 06:35:45 -04005081 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5082 if (ret < 0)
5083 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005084
5085 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005086 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005087 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005088 if (slot >= btrfs_header_nritems(leaf)) {
5089 ret = btrfs_next_leaf(root, path);
5090 if (ret < 0)
5091 goto err;
5092 else if (ret > 0)
5093 break;
5094 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005095 }
Chris Mason3de45862008-11-17 21:02:50 -05005096
Chris Mason5f39d392007-10-15 16:14:19 -04005097 item = btrfs_item_nr(leaf, slot);
5098 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5099
5100 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005101 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005102 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005103 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005104 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005105 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005106 if (key_type == BTRFS_DIR_INDEX_KEY &&
5107 btrfs_should_delete_dir_index(&del_list,
5108 found_key.offset))
5109 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005110
5111 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005112 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005113
Chris Mason39279cc2007-06-12 06:35:45 -04005114 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5115 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005116 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005117
5118 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005119 struct btrfs_key location;
5120
Josef Bacik22a94d42011-03-16 16:47:17 -04005121 if (verify_dir_item(root, leaf, di))
5122 break;
5123
Chris Mason5f39d392007-10-15 16:14:19 -04005124 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005125 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005126 name_ptr = tmp_name;
5127 } else {
5128 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005129 if (!name_ptr) {
5130 ret = -ENOMEM;
5131 goto err;
5132 }
Chris Mason5f39d392007-10-15 16:14:19 -04005133 }
5134 read_extent_buffer(leaf, name_ptr,
5135 (unsigned long)(di + 1), name_len);
5136
5137 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5138 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005139
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005140
Chris Mason3de45862008-11-17 21:02:50 -05005141 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005142 * skip it.
5143 *
5144 * In contrast to old kernels, we insert the snapshot's
5145 * dir item and dir index after it has been created, so
5146 * we won't find a reference to our own snapshot. We
5147 * still keep the following code for backward
5148 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005149 */
5150 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5151 location.objectid == root->root_key.objectid) {
5152 over = 0;
5153 goto skip;
5154 }
Chris Mason5f39d392007-10-15 16:14:19 -04005155 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01005156 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04005157 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005158
Chris Mason3de45862008-11-17 21:02:50 -05005159skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005160 if (name_ptr != tmp_name)
5161 kfree(name_ptr);
5162
Chris Mason39279cc2007-06-12 06:35:45 -04005163 if (over)
5164 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005165 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005166 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005167 di_cur += di_len;
5168 di = (struct btrfs_dir_item *)((char *)di + di_len);
5169 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005170next:
5171 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005172 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005173
Miao Xie16cdcec2011-04-22 18:12:22 +08005174 if (key_type == BTRFS_DIR_INDEX_KEY) {
5175 if (is_curr)
5176 filp->f_pos++;
5177 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
5178 &ins_list);
5179 if (ret)
5180 goto nopos;
5181 }
5182
David Woodhouse49593bf2008-08-17 17:08:36 +01005183 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05005184 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00005185 /*
5186 * 32-bit glibc will use getdents64, but then strtol -
5187 * so the last number we can serve is this.
5188 */
5189 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05005190 else
5191 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04005192nopos:
5193 ret = 0;
5194err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005195 if (key_type == BTRFS_DIR_INDEX_KEY)
5196 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005197 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005198 return ret;
5199}
5200
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005201int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005202{
5203 struct btrfs_root *root = BTRFS_I(inode)->root;
5204 struct btrfs_trans_handle *trans;
5205 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005206 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005207
Josef Bacik72ac3c02012-05-23 14:13:11 -04005208 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005209 return 0;
5210
Liu Bo83eea1f2012-07-10 05:28:39 -06005211 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005212 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005213
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005214 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005215 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005216 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005217 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005218 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005219 if (IS_ERR(trans))
5220 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005221 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005222 }
5223 return ret;
5224}
5225
5226/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005227 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005228 * inode changes. But, it is most likely to find the inode in cache.
5229 * FIXME, needs more benchmarking...there are no reasons other than performance
5230 * to keep or drop this code.
5231 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005232static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005233{
5234 struct btrfs_root *root = BTRFS_I(inode)->root;
5235 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005236 int ret;
5237
Josef Bacik72ac3c02012-05-23 14:13:11 -04005238 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005239 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005240
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005241 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005242 if (IS_ERR(trans))
5243 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005244
5245 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005246 if (ret && ret == -ENOSPC) {
5247 /* whoops, lets try again with the full transaction */
5248 btrfs_end_transaction(trans, root);
5249 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005250 if (IS_ERR(trans))
5251 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005252
Chris Mason94b60442010-05-26 11:02:00 -04005253 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005254 }
Chris Mason39279cc2007-06-12 06:35:45 -04005255 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005256 if (BTRFS_I(inode)->delayed_node)
5257 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005258
5259 return ret;
5260}
5261
5262/*
5263 * This is a copy of file_update_time. We need this so we can return error on
5264 * ENOSPC for updating the inode in the case of file write and mmap writes.
5265 */
Josef Bacike41f9412012-03-26 09:46:47 -04005266static int btrfs_update_time(struct inode *inode, struct timespec *now,
5267 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005268{
Alexander Block2bc5565282012-06-15 09:49:33 +02005269 struct btrfs_root *root = BTRFS_I(inode)->root;
5270
5271 if (btrfs_root_readonly(root))
5272 return -EROFS;
5273
Josef Bacike41f9412012-03-26 09:46:47 -04005274 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005275 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005276 if (flags & S_CTIME)
5277 inode->i_ctime = *now;
5278 if (flags & S_MTIME)
5279 inode->i_mtime = *now;
5280 if (flags & S_ATIME)
5281 inode->i_atime = *now;
5282 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005283}
5284
Chris Masond352ac62008-09-29 15:18:18 -04005285/*
5286 * find the highest existing sequence number in a directory
5287 * and then set the in-memory index_cnt variable to reflect
5288 * free sequence numbers
5289 */
Josef Bacikaec74772008-07-24 12:12:38 -04005290static int btrfs_set_inode_index_count(struct inode *inode)
5291{
5292 struct btrfs_root *root = BTRFS_I(inode)->root;
5293 struct btrfs_key key, found_key;
5294 struct btrfs_path *path;
5295 struct extent_buffer *leaf;
5296 int ret;
5297
Li Zefan33345d012011-04-20 10:31:50 +08005298 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005299 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5300 key.offset = (u64)-1;
5301
5302 path = btrfs_alloc_path();
5303 if (!path)
5304 return -ENOMEM;
5305
5306 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5307 if (ret < 0)
5308 goto out;
5309 /* FIXME: we should be able to handle this */
5310 if (ret == 0)
5311 goto out;
5312 ret = 0;
5313
5314 /*
5315 * MAGIC NUMBER EXPLANATION:
5316 * since we search a directory based on f_pos we have to start at 2
5317 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5318 * else has to start at 2
5319 */
5320 if (path->slots[0] == 0) {
5321 BTRFS_I(inode)->index_cnt = 2;
5322 goto out;
5323 }
5324
5325 path->slots[0]--;
5326
5327 leaf = path->nodes[0];
5328 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5329
Li Zefan33345d012011-04-20 10:31:50 +08005330 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005331 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5332 BTRFS_I(inode)->index_cnt = 2;
5333 goto out;
5334 }
5335
5336 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5337out:
5338 btrfs_free_path(path);
5339 return ret;
5340}
5341
Chris Masond352ac62008-09-29 15:18:18 -04005342/*
5343 * helper to find a free sequence number in a given directory. This current
5344 * code is very simple, later versions will do smarter things in the btree
5345 */
Chris Mason3de45862008-11-17 21:02:50 -05005346int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005347{
5348 int ret = 0;
5349
5350 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005351 ret = btrfs_inode_delayed_dir_index_count(dir);
5352 if (ret) {
5353 ret = btrfs_set_inode_index_count(dir);
5354 if (ret)
5355 return ret;
5356 }
Josef Bacikaec74772008-07-24 12:12:38 -04005357 }
5358
Chris Mason00e4e6b2008-08-05 11:18:09 -04005359 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005360 BTRFS_I(dir)->index_cnt++;
5361
5362 return ret;
5363}
5364
Chris Mason39279cc2007-06-12 06:35:45 -04005365static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5366 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005367 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005368 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005369 u64 ref_objectid, u64 objectid,
5370 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005371{
5372 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005373 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005374 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005375 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005376 struct btrfs_inode_ref *ref;
5377 struct btrfs_key key[2];
5378 u32 sizes[2];
5379 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005380 int ret;
5381 int owner;
5382
Chris Mason5f39d392007-10-15 16:14:19 -04005383 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005384 if (!path)
5385 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005386
Chris Mason39279cc2007-06-12 06:35:45 -04005387 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005388 if (!inode) {
5389 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005390 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005391 }
Chris Mason39279cc2007-06-12 06:35:45 -04005392
Li Zefan581bb052011-04-20 10:06:11 +08005393 /*
5394 * we have to initialize this early, so we can reclaim the inode
5395 * number if we fail afterwards in this function.
5396 */
5397 inode->i_ino = objectid;
5398
Josef Bacikaec74772008-07-24 12:12:38 -04005399 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005400 trace_btrfs_inode_request(dir);
5401
Chris Mason3de45862008-11-17 21:02:50 -05005402 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005403 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005404 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005405 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005406 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005407 }
Josef Bacikaec74772008-07-24 12:12:38 -04005408 }
5409 /*
5410 * index_cnt is ignored for everything but a dir,
5411 * btrfs_get_inode_index_count has an explanation for the magic
5412 * number
5413 */
5414 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005415 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005416 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005417 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005418
Josef Bacik5dc562c2012-08-17 13:14:17 -04005419 /*
5420 * We could have gotten an inode number from somebody who was fsynced
5421 * and then removed in this same transaction, so let's just set full
5422 * sync since it will be a full sync anyway and this will blow away the
5423 * old info in the log.
5424 */
5425 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5426
Al Viro569254b2011-07-24 17:08:40 -04005427 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005428 owner = 0;
5429 else
5430 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005431
5432 key[0].objectid = objectid;
5433 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5434 key[0].offset = 0;
5435
Mark Fashehf1863732012-08-08 11:32:27 -07005436 /*
5437 * Start new inodes with an inode_ref. This is slightly more
5438 * efficient for small numbers of hard links since they will
5439 * be packed into one item. Extended refs will kick in if we
5440 * add more hard links than can fit in the ref item.
5441 */
Chris Mason9c583092008-01-29 15:15:18 -05005442 key[1].objectid = objectid;
5443 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5444 key[1].offset = ref_objectid;
5445
5446 sizes[0] = sizeof(struct btrfs_inode_item);
5447 sizes[1] = name_len + sizeof(*ref);
5448
Chris Masonb9473432009-03-13 11:00:37 -04005449 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005450 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5451 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005452 goto fail;
5453
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005454 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005455 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005456 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005457 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5458 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005459 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5460 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005461 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005462
5463 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5464 struct btrfs_inode_ref);
5465 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005466 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005467 ptr = (unsigned long)(ref + 1);
5468 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5469
Chris Mason5f39d392007-10-15 16:14:19 -04005470 btrfs_mark_buffer_dirty(path->nodes[0]);
5471 btrfs_free_path(path);
5472
Chris Mason39279cc2007-06-12 06:35:45 -04005473 location = &BTRFS_I(inode)->location;
5474 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005475 location->offset = 0;
5476 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5477
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005478 btrfs_inherit_iflags(inode, dir);
5479
Al Viro569254b2011-07-24 17:08:40 -04005480 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005481 if (btrfs_test_opt(root, NODATASUM))
5482 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005483 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005484 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5485 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005486 }
5487
Chris Mason39279cc2007-06-12 06:35:45 -04005488 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005489 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005490
5491 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005492 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005493
Alexander Block8ea05e32012-07-25 17:35:53 +02005494 btrfs_update_root_times(trans, root);
5495
Chris Mason39279cc2007-06-12 06:35:45 -04005496 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005497fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005498 if (dir)
5499 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005500 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005501 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005502 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005503}
5504
5505static inline u8 btrfs_inode_type(struct inode *inode)
5506{
5507 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5508}
5509
Chris Masond352ac62008-09-29 15:18:18 -04005510/*
5511 * utility function to add 'inode' into 'parent_inode' with
5512 * a give name and a given sequence number.
5513 * if 'add_backref' is true, also insert a backref from the
5514 * inode to the parent directory.
5515 */
Chris Masone02119d2008-09-05 16:13:11 -04005516int btrfs_add_link(struct btrfs_trans_handle *trans,
5517 struct inode *parent_inode, struct inode *inode,
5518 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005519{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005520 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005521 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005522 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005523 u64 ino = btrfs_ino(inode);
5524 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005525
Li Zefan33345d012011-04-20 10:31:50 +08005526 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005527 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5528 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005529 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005530 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5531 key.offset = 0;
5532 }
Chris Mason39279cc2007-06-12 06:35:45 -04005533
Li Zefan33345d012011-04-20 10:31:50 +08005534 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005535 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5536 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005537 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005538 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005539 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5540 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005541 }
5542
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005543 /* Nothing to clean up yet */
5544 if (ret)
5545 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005546
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005547 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5548 parent_inode, &key,
5549 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005550 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005551 goto fail_dir_item;
5552 else if (ret) {
5553 btrfs_abort_transaction(trans, root, ret);
5554 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005555 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005556
5557 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5558 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005559 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005560 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5561 ret = btrfs_update_inode(trans, root, parent_inode);
5562 if (ret)
5563 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005564 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005565
5566fail_dir_item:
5567 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5568 u64 local_index;
5569 int err;
5570 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5571 key.objectid, root->root_key.objectid,
5572 parent_ino, &local_index, name, name_len);
5573
5574 } else if (add_backref) {
5575 u64 local_index;
5576 int err;
5577
5578 err = btrfs_del_inode_ref(trans, root, name, name_len,
5579 ino, parent_ino, &local_index);
5580 }
5581 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005582}
5583
5584static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005585 struct inode *dir, struct dentry *dentry,
5586 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005587{
Josef Bacika1b075d2010-11-19 20:36:11 +00005588 int err = btrfs_add_link(trans, dir, inode,
5589 dentry->d_name.name, dentry->d_name.len,
5590 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005591 if (err > 0)
5592 err = -EEXIST;
5593 return err;
5594}
5595
Josef Bacik618e21d2007-07-11 10:18:17 -04005596static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005597 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005598{
5599 struct btrfs_trans_handle *trans;
5600 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005601 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005602 int err;
5603 int drop_inode = 0;
5604 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005605 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005606
5607 if (!new_valid_dev(rdev))
5608 return -EINVAL;
5609
Josef Bacik9ed74f22009-09-11 16:12:44 -04005610 /*
5611 * 2 for inode item and ref
5612 * 2 for dir items
5613 * 1 for xattr if selinux is on
5614 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005615 trans = btrfs_start_transaction(root, 5);
5616 if (IS_ERR(trans))
5617 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005618
Li Zefan581bb052011-04-20 10:06:11 +08005619 err = btrfs_find_free_ino(root, &objectid);
5620 if (err)
5621 goto out_unlock;
5622
Josef Bacikaec74772008-07-24 12:12:38 -04005623 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005624 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005625 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005626 if (IS_ERR(inode)) {
5627 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005628 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005629 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005630
Eric Paris2a7dba32011-02-01 11:05:39 -05005631 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005632 if (err) {
5633 drop_inode = 1;
5634 goto out_unlock;
5635 }
5636
Casey Schauflerad19db72011-12-15 10:09:07 -05005637 /*
5638 * If the active LSM wants to access the inode during
5639 * d_instantiate it needs these. Smack checks to see
5640 * if the filesystem supports xattrs by looking at the
5641 * ops vector.
5642 */
5643
5644 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005645 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005646 if (err)
5647 drop_inode = 1;
5648 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005649 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005650 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005651 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005652 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005653out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005654 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005655 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005656 if (drop_inode) {
5657 inode_dec_link_count(inode);
5658 iput(inode);
5659 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005660 return err;
5661}
5662
Chris Mason39279cc2007-06-12 06:35:45 -04005663static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005664 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005665{
5666 struct btrfs_trans_handle *trans;
5667 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005668 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005669 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005670 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005671 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005672 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005673
Josef Bacik9ed74f22009-09-11 16:12:44 -04005674 /*
5675 * 2 for inode item and ref
5676 * 2 for dir items
5677 * 1 for xattr if selinux is on
5678 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005679 trans = btrfs_start_transaction(root, 5);
5680 if (IS_ERR(trans))
5681 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005682
Li Zefan581bb052011-04-20 10:06:11 +08005683 err = btrfs_find_free_ino(root, &objectid);
5684 if (err)
5685 goto out_unlock;
5686
Josef Bacikaec74772008-07-24 12:12:38 -04005687 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005688 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005689 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005690 if (IS_ERR(inode)) {
5691 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005692 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005693 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005694 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005695
Eric Paris2a7dba32011-02-01 11:05:39 -05005696 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005697 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005698 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005699
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005700 err = btrfs_update_inode(trans, root, inode);
5701 if (err)
5702 goto out_unlock;
5703
Casey Schauflerad19db72011-12-15 10:09:07 -05005704 /*
5705 * If the active LSM wants to access the inode during
5706 * d_instantiate it needs these. Smack checks to see
5707 * if the filesystem supports xattrs by looking at the
5708 * ops vector.
5709 */
5710 inode->i_fop = &btrfs_file_operations;
5711 inode->i_op = &btrfs_file_inode_operations;
5712
Josef Bacika1b075d2010-11-19 20:36:11 +00005713 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005714 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005715 goto out_unlock;
5716
5717 inode->i_mapping->a_ops = &btrfs_aops;
5718 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5719 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5720 d_instantiate(dentry, inode);
5721
Chris Mason39279cc2007-06-12 06:35:45 -04005722out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005723 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005724 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005725 inode_dec_link_count(inode);
5726 iput(inode);
5727 }
Liu Bob53d3f52012-11-14 14:34:34 +00005728 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005729 return err;
5730}
5731
5732static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5733 struct dentry *dentry)
5734{
5735 struct btrfs_trans_handle *trans;
5736 struct btrfs_root *root = BTRFS_I(dir)->root;
5737 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005738 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005739 int err;
5740 int drop_inode = 0;
5741
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005742 /* do not allow sys_link's with other subvols of the same device */
5743 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005744 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005745
Mark Fashehf1863732012-08-08 11:32:27 -07005746 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005747 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005748
Chris Mason3de45862008-11-17 21:02:50 -05005749 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005750 if (err)
5751 goto fail;
5752
Yan, Zhenga22285a2010-05-16 10:48:46 -04005753 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005754 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005755 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005756 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005757 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005758 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005759 if (IS_ERR(trans)) {
5760 err = PTR_ERR(trans);
5761 goto fail;
5762 }
Chris Mason5f39d392007-10-15 16:14:19 -04005763
Miao Xie31534952011-04-13 13:19:21 +08005764 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005765 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005766 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005767 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005768 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005769
Josef Bacika1b075d2010-11-19 20:36:11 +00005770 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005771
Yan, Zhenga5719522009-09-24 09:17:31 -04005772 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005773 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005774 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005775 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005776 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005777 if (err)
5778 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005779 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005780 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005781 }
Chris Mason39279cc2007-06-12 06:35:45 -04005782
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005783 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005784fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005785 if (drop_inode) {
5786 inode_dec_link_count(inode);
5787 iput(inode);
5788 }
Liu Bob53d3f52012-11-14 14:34:34 +00005789 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005790 return err;
5791}
5792
Al Viro18bb1db2011-07-26 01:41:39 -04005793static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005794{
Chris Masonb9d86662008-05-02 16:13:49 -04005795 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005796 struct btrfs_trans_handle *trans;
5797 struct btrfs_root *root = BTRFS_I(dir)->root;
5798 int err = 0;
5799 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005800 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005801 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005802
Josef Bacik9ed74f22009-09-11 16:12:44 -04005803 /*
5804 * 2 items for inode and ref
5805 * 2 items for dir items
5806 * 1 for xattr if selinux is on
5807 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005808 trans = btrfs_start_transaction(root, 5);
5809 if (IS_ERR(trans))
5810 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005811
Li Zefan581bb052011-04-20 10:06:11 +08005812 err = btrfs_find_free_ino(root, &objectid);
5813 if (err)
5814 goto out_fail;
5815
Josef Bacikaec74772008-07-24 12:12:38 -04005816 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005817 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005818 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005819 if (IS_ERR(inode)) {
5820 err = PTR_ERR(inode);
5821 goto out_fail;
5822 }
Chris Mason5f39d392007-10-15 16:14:19 -04005823
Chris Mason39279cc2007-06-12 06:35:45 -04005824 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005825
Eric Paris2a7dba32011-02-01 11:05:39 -05005826 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005827 if (err)
5828 goto out_fail;
5829
Chris Mason39279cc2007-06-12 06:35:45 -04005830 inode->i_op = &btrfs_dir_inode_operations;
5831 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005832
Chris Masondbe674a2008-07-17 12:54:05 -04005833 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005834 err = btrfs_update_inode(trans, root, inode);
5835 if (err)
5836 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005837
Josef Bacika1b075d2010-11-19 20:36:11 +00005838 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5839 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005840 if (err)
5841 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005842
Chris Mason39279cc2007-06-12 06:35:45 -04005843 d_instantiate(dentry, inode);
5844 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005845
5846out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005847 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005848 if (drop_on_err)
5849 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005850 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005851 return err;
5852}
5853
Chris Masond352ac62008-09-29 15:18:18 -04005854/* helper for btfs_get_extent. Given an existing extent in the tree,
5855 * and an extent that you want to insert, deal with overlap and insert
5856 * the new extent into the tree.
5857 */
Chris Mason3b951512008-04-17 11:29:12 -04005858static int merge_extent_mapping(struct extent_map_tree *em_tree,
5859 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005860 struct extent_map *em,
5861 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005862{
5863 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005864
Chris Masone6dcd2d2008-07-17 12:53:50 -04005865 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5866 start_diff = map_start - em->start;
5867 em->start = map_start;
5868 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005869 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5870 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005871 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005872 em->block_len -= start_diff;
5873 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005874 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005875}
5876
Chris Masonc8b97812008-10-29 14:49:59 -04005877static noinline int uncompress_inline(struct btrfs_path *path,
5878 struct inode *inode, struct page *page,
5879 size_t pg_offset, u64 extent_offset,
5880 struct btrfs_file_extent_item *item)
5881{
5882 int ret;
5883 struct extent_buffer *leaf = path->nodes[0];
5884 char *tmp;
5885 size_t max_size;
5886 unsigned long inline_size;
5887 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005888 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005889
5890 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005891 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005892 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5893 inline_size = btrfs_file_extent_inline_item_len(leaf,
5894 btrfs_item_nr(leaf, path->slots[0]));
5895 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005896 if (!tmp)
5897 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005898 ptr = btrfs_file_extent_inline_start(item);
5899
5900 read_extent_buffer(leaf, tmp, ptr, inline_size);
5901
Chris Mason5b050f02008-11-11 09:34:41 -05005902 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005903 ret = btrfs_decompress(compress_type, tmp, page,
5904 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005905 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005906 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005907 unsigned long copy_size = min_t(u64,
5908 PAGE_CACHE_SIZE - pg_offset,
5909 max_size - extent_offset);
5910 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005911 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005912 }
5913 kfree(tmp);
5914 return 0;
5915}
5916
Chris Masond352ac62008-09-29 15:18:18 -04005917/*
5918 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005919 * the ugly parts come from merging extents from the disk with the in-ram
5920 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005921 * where the in-ram extents might be locked pending data=ordered completion.
5922 *
5923 * This also copies inline extents directly into the page.
5924 */
Chris Masond3977122009-01-05 21:25:51 -05005925
Chris Masona52d9a82007-08-27 16:49:44 -04005926struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005927 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005928 int create)
5929{
5930 int ret;
5931 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005932 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005933 u64 extent_start = 0;
5934 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005935 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005936 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005937 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005938 struct btrfs_root *root = BTRFS_I(inode)->root;
5939 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005940 struct extent_buffer *leaf;
5941 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005942 struct extent_map *em = NULL;
5943 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005944 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005945 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005946 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005947
Chris Masona52d9a82007-08-27 16:49:44 -04005948again:
Chris Mason890871b2009-09-02 16:24:52 -04005949 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005950 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005951 if (em)
5952 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005953 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005954
Chris Masona52d9a82007-08-27 16:49:44 -04005955 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005956 if (em->start > start || em->start + em->len <= start)
5957 free_extent_map(em);
5958 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005959 free_extent_map(em);
5960 else
5961 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005962 }
David Sterba172ddd62011-04-21 00:48:27 +02005963 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005964 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005965 err = -ENOMEM;
5966 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005967 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005968 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005969 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005970 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005971 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005972 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005973
5974 if (!path) {
5975 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005976 if (!path) {
5977 err = -ENOMEM;
5978 goto out;
5979 }
5980 /*
5981 * Chances are we'll be called again, so go ahead and do
5982 * readahead
5983 */
5984 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005985 }
5986
Chris Mason179e29e2007-11-01 11:28:41 -04005987 ret = btrfs_lookup_file_extent(trans, root, path,
5988 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005989 if (ret < 0) {
5990 err = ret;
5991 goto out;
5992 }
5993
5994 if (ret != 0) {
5995 if (path->slots[0] == 0)
5996 goto not_found;
5997 path->slots[0]--;
5998 }
5999
Chris Mason5f39d392007-10-15 16:14:19 -04006000 leaf = path->nodes[0];
6001 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006002 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006003 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006004 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6005 found_type = btrfs_key_type(&found_key);
6006 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006007 found_type != BTRFS_EXTENT_DATA_KEY) {
6008 goto not_found;
6009 }
6010
Chris Mason5f39d392007-10-15 16:14:19 -04006011 found_type = btrfs_file_extent_type(leaf, item);
6012 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08006013 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006014 if (found_type == BTRFS_FILE_EXTENT_REG ||
6015 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006016 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006017 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006018 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6019 size_t size;
6020 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00006021 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006022 }
6023
6024 if (start >= extent_end) {
6025 path->slots[0]++;
6026 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6027 ret = btrfs_next_leaf(root, path);
6028 if (ret < 0) {
6029 err = ret;
6030 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006031 }
Yan Zheng9036c102008-10-30 14:19:41 -04006032 if (ret > 0)
6033 goto not_found;
6034 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006035 }
Yan Zheng9036c102008-10-30 14:19:41 -04006036 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6037 if (found_key.objectid != objectid ||
6038 found_key.type != BTRFS_EXTENT_DATA_KEY)
6039 goto not_found;
6040 if (start + len <= found_key.offset)
6041 goto not_found;
6042 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006043 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006044 em->len = found_key.offset - start;
6045 goto not_found_em;
6046 }
6047
Josef Bacikcc95bef2013-04-04 14:31:27 -04006048 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006049 if (found_type == BTRFS_FILE_EXTENT_REG ||
6050 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006051 em->start = extent_start;
6052 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006053 em->orig_start = extent_start -
6054 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006055 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6056 item);
Chris Masondb945352007-10-15 16:15:53 -04006057 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6058 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006059 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006060 goto insert;
6061 }
Li Zefan261507a02010-12-17 14:21:50 +08006062 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006063 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006064 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006065 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006066 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006067 } else {
6068 bytenr += btrfs_file_extent_offset(leaf, item);
6069 em->block_start = bytenr;
6070 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006071 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6072 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006073 }
Chris Masona52d9a82007-08-27 16:49:44 -04006074 goto insert;
6075 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006076 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006077 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006078 size_t size;
6079 size_t extent_offset;
6080 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006081
Chris Masona52d9a82007-08-27 16:49:44 -04006082 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006083 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006084 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006085 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006086 goto out;
6087 }
6088
Yan Zheng9036c102008-10-30 14:19:41 -04006089 size = btrfs_file_extent_inline_len(leaf, item);
6090 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006091 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006092 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006093 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006094 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006095 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006096 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006097 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006098 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006099 em->compress_type = compress_type;
6100 }
Yan689f9342007-10-29 11:41:07 -04006101 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006102 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006103 if (btrfs_file_extent_compression(leaf, item) !=
6104 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006105 ret = uncompress_inline(path, inode, page,
6106 pg_offset,
6107 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006108 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006109 } else {
6110 map = kmap(page);
6111 read_extent_buffer(leaf, map + pg_offset, ptr,
6112 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006113 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6114 memset(map + pg_offset + copy_size, 0,
6115 PAGE_CACHE_SIZE - pg_offset -
6116 copy_size);
6117 }
Chris Masonc8b97812008-10-29 14:49:59 -04006118 kunmap(page);
6119 }
Chris Mason179e29e2007-11-01 11:28:41 -04006120 flush_dcache_page(page);
6121 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006122 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006123 if (!trans) {
6124 kunmap(page);
6125 free_extent_map(em);
6126 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006127
David Sterbab3b4aa72011-04-21 01:20:15 +02006128 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006129 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006130
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006131 if (IS_ERR(trans))
6132 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006133 goto again;
6134 }
Chris Masonc8b97812008-10-29 14:49:59 -04006135 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006136 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006137 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006138 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006139 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006140 }
Chris Masond1310b22008-01-24 16:13:08 -05006141 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006142 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006143 goto insert;
6144 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006145 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006146 }
6147not_found:
6148 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006149 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006150 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006151not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006152 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006153 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006154insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006155 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006156 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006157 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6158 (unsigned long long)em->start,
6159 (unsigned long long)em->len,
6160 (unsigned long long)start,
6161 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006162 err = -EIO;
6163 goto out;
6164 }
Chris Masond1310b22008-01-24 16:13:08 -05006165
6166 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006167 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006168 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006169 /* it is possible that someone inserted the extent into the tree
6170 * while we had the lock dropped. It is also possible that
6171 * an overlapping map exists in the tree
6172 */
Chris Masona52d9a82007-08-27 16:49:44 -04006173 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006174 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006175
6176 ret = 0;
6177
Chris Mason3b951512008-04-17 11:29:12 -04006178 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006179 if (existing && (existing->start > start ||
6180 existing->start + existing->len <= start)) {
6181 free_extent_map(existing);
6182 existing = NULL;
6183 }
Chris Mason3b951512008-04-17 11:29:12 -04006184 if (!existing) {
6185 existing = lookup_extent_mapping(em_tree, em->start,
6186 em->len);
6187 if (existing) {
6188 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006189 em, start,
6190 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006191 free_extent_map(existing);
6192 if (err) {
6193 free_extent_map(em);
6194 em = NULL;
6195 }
6196 } else {
6197 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006198 free_extent_map(em);
6199 em = NULL;
6200 }
6201 } else {
6202 free_extent_map(em);
6203 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006204 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006205 }
Chris Masona52d9a82007-08-27 16:49:44 -04006206 }
Chris Mason890871b2009-09-02 16:24:52 -04006207 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006208out:
liubo1abe9b82011-03-24 11:18:59 +00006209
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006210 if (em)
6211 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006212
Chris Masonf4219502008-07-22 11:18:09 -04006213 if (path)
6214 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006215 if (trans) {
6216 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006217 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006218 err = ret;
6219 }
Chris Masona52d9a82007-08-27 16:49:44 -04006220 if (err) {
6221 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006222 return ERR_PTR(err);
6223 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006224 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006225 return em;
6226}
6227
Chris Masonec29ed52011-02-23 16:23:20 -05006228struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6229 size_t pg_offset, u64 start, u64 len,
6230 int create)
6231{
6232 struct extent_map *em;
6233 struct extent_map *hole_em = NULL;
6234 u64 range_start = start;
6235 u64 end;
6236 u64 found;
6237 u64 found_end;
6238 int err = 0;
6239
6240 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6241 if (IS_ERR(em))
6242 return em;
6243 if (em) {
6244 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006245 * if our em maps to
6246 * - a hole or
6247 * - a pre-alloc extent,
6248 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006249 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006250 if (em->block_start != EXTENT_MAP_HOLE &&
6251 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006252 return em;
6253 else
6254 hole_em = em;
6255 }
6256
6257 /* check to see if we've wrapped (len == -1 or similar) */
6258 end = start + len;
6259 if (end < start)
6260 end = (u64)-1;
6261 else
6262 end -= 1;
6263
6264 em = NULL;
6265
6266 /* ok, we didn't find anything, lets look for delalloc */
6267 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6268 end, len, EXTENT_DELALLOC, 1);
6269 found_end = range_start + found;
6270 if (found_end < range_start)
6271 found_end = (u64)-1;
6272
6273 /*
6274 * we didn't find anything useful, return
6275 * the original results from get_extent()
6276 */
6277 if (range_start > end || found_end <= start) {
6278 em = hole_em;
6279 hole_em = NULL;
6280 goto out;
6281 }
6282
6283 /* adjust the range_start to make sure it doesn't
6284 * go backwards from the start they passed in
6285 */
6286 range_start = max(start,range_start);
6287 found = found_end - range_start;
6288
6289 if (found > 0) {
6290 u64 hole_start = start;
6291 u64 hole_len = len;
6292
David Sterba172ddd62011-04-21 00:48:27 +02006293 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006294 if (!em) {
6295 err = -ENOMEM;
6296 goto out;
6297 }
6298 /*
6299 * when btrfs_get_extent can't find anything it
6300 * returns one huge hole
6301 *
6302 * make sure what it found really fits our range, and
6303 * adjust to make sure it is based on the start from
6304 * the caller
6305 */
6306 if (hole_em) {
6307 u64 calc_end = extent_map_end(hole_em);
6308
6309 if (calc_end <= start || (hole_em->start > end)) {
6310 free_extent_map(hole_em);
6311 hole_em = NULL;
6312 } else {
6313 hole_start = max(hole_em->start, start);
6314 hole_len = calc_end - hole_start;
6315 }
6316 }
6317 em->bdev = NULL;
6318 if (hole_em && range_start > hole_start) {
6319 /* our hole starts before our delalloc, so we
6320 * have to return just the parts of the hole
6321 * that go until the delalloc starts
6322 */
6323 em->len = min(hole_len,
6324 range_start - hole_start);
6325 em->start = hole_start;
6326 em->orig_start = hole_start;
6327 /*
6328 * don't adjust block start at all,
6329 * it is fixed at EXTENT_MAP_HOLE
6330 */
6331 em->block_start = hole_em->block_start;
6332 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006333 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6334 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006335 } else {
6336 em->start = range_start;
6337 em->len = found;
6338 em->orig_start = range_start;
6339 em->block_start = EXTENT_MAP_DELALLOC;
6340 em->block_len = found;
6341 }
6342 } else if (hole_em) {
6343 return hole_em;
6344 }
6345out:
6346
6347 free_extent_map(hole_em);
6348 if (err) {
6349 free_extent_map(em);
6350 return ERR_PTR(err);
6351 }
6352 return em;
6353}
6354
Josef Bacik4b46fce2010-05-23 11:00:55 -04006355static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6356 u64 start, u64 len)
6357{
6358 struct btrfs_root *root = BTRFS_I(inode)->root;
6359 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006360 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006361 struct btrfs_key ins;
6362 u64 alloc_hint;
6363 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006364
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006365 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006366 if (IS_ERR(trans))
6367 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006368
6369 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6370
6371 alloc_hint = get_extent_allocation_hint(inode, start, len);
6372 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006373 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006374 if (ret) {
6375 em = ERR_PTR(ret);
6376 goto out;
6377 }
6378
Josef Bacik70c8a912012-10-11 16:54:30 -04006379 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006380 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik70c8a912012-10-11 16:54:30 -04006381 if (IS_ERR(em))
6382 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006383
6384 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6385 ins.offset, ins.offset, 0);
6386 if (ret) {
6387 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6388 em = ERR_PTR(ret);
6389 }
6390out:
6391 btrfs_end_transaction(trans, root);
6392 return em;
6393}
6394
Chris Mason46bfbb52010-05-26 11:04:10 -04006395/*
6396 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6397 * block must be cow'd
6398 */
Josef Bacik7ee9e442013-06-21 16:37:03 -04006399noinline int can_nocow_extent(struct btrfs_trans_handle *trans,
6400 struct inode *inode, u64 offset, u64 *len,
6401 u64 *orig_start, u64 *orig_block_len,
6402 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006403{
6404 struct btrfs_path *path;
6405 int ret;
6406 struct extent_buffer *leaf;
6407 struct btrfs_root *root = BTRFS_I(inode)->root;
6408 struct btrfs_file_extent_item *fi;
6409 struct btrfs_key key;
6410 u64 disk_bytenr;
6411 u64 backref_offset;
6412 u64 extent_end;
6413 u64 num_bytes;
6414 int slot;
6415 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006416 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Chris Mason46bfbb52010-05-26 11:04:10 -04006417 path = btrfs_alloc_path();
6418 if (!path)
6419 return -ENOMEM;
6420
Li Zefan33345d012011-04-20 10:31:50 +08006421 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006422 offset, 0);
6423 if (ret < 0)
6424 goto out;
6425
6426 slot = path->slots[0];
6427 if (ret == 1) {
6428 if (slot == 0) {
6429 /* can't find the item, must cow */
6430 ret = 0;
6431 goto out;
6432 }
6433 slot--;
6434 }
6435 ret = 0;
6436 leaf = path->nodes[0];
6437 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006438 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006439 key.type != BTRFS_EXTENT_DATA_KEY) {
6440 /* not our file or wrong item type, must cow */
6441 goto out;
6442 }
6443
6444 if (key.offset > offset) {
6445 /* Wrong offset, must cow */
6446 goto out;
6447 }
6448
6449 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6450 found_type = btrfs_file_extent_type(leaf, fi);
6451 if (found_type != BTRFS_FILE_EXTENT_REG &&
6452 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6453 /* not a regular extent, must cow */
6454 goto out;
6455 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006456
6457 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6458 goto out;
6459
Chris Mason46bfbb52010-05-26 11:04:10 -04006460 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006461 if (disk_bytenr == 0)
6462 goto out;
6463
6464 if (btrfs_file_extent_compression(leaf, fi) ||
6465 btrfs_file_extent_encryption(leaf, fi) ||
6466 btrfs_file_extent_other_encoding(leaf, fi))
6467 goto out;
6468
Chris Mason46bfbb52010-05-26 11:04:10 -04006469 backref_offset = btrfs_file_extent_offset(leaf, fi);
6470
Josef Bacik7ee9e442013-06-21 16:37:03 -04006471 if (orig_start) {
6472 *orig_start = key.offset - backref_offset;
6473 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6474 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6475 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006476
Chris Mason46bfbb52010-05-26 11:04:10 -04006477 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason46bfbb52010-05-26 11:04:10 -04006478
6479 if (btrfs_extent_readonly(root, disk_bytenr))
6480 goto out;
6481
6482 /*
6483 * look for other files referencing this extent, if we
6484 * find any we must cow
6485 */
Li Zefan33345d012011-04-20 10:31:50 +08006486 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006487 key.offset - backref_offset, disk_bytenr))
6488 goto out;
6489
6490 /*
6491 * adjust disk_bytenr and num_bytes to cover just the bytes
6492 * in this extent we are about to write. If there
6493 * are any csums in that range we have to cow in order
6494 * to keep the csums correct
6495 */
6496 disk_bytenr += backref_offset;
6497 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006498 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006499 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6500 goto out;
6501 /*
6502 * all of the above have passed, it is safe to overwrite this extent
6503 * without cow
6504 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006505 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006506 ret = 1;
6507out:
6508 btrfs_free_path(path);
6509 return ret;
6510}
6511
Josef Bacikeb838e72012-07-31 16:28:48 -04006512static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6513 struct extent_state **cached_state, int writing)
6514{
6515 struct btrfs_ordered_extent *ordered;
6516 int ret = 0;
6517
6518 while (1) {
6519 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6520 0, cached_state);
6521 /*
6522 * We're concerned with the entire range that we're going to be
6523 * doing DIO to, so we need to make sure theres no ordered
6524 * extents in this range.
6525 */
6526 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6527 lockend - lockstart + 1);
6528
6529 /*
6530 * We need to make sure there are no buffered pages in this
6531 * range either, we could have raced between the invalidate in
6532 * generic_file_direct_write and locking the extent. The
6533 * invalidate needs to happen so that reads after a write do not
6534 * get stale data.
6535 */
6536 if (!ordered && (!writing ||
6537 !test_range_bit(&BTRFS_I(inode)->io_tree,
6538 lockstart, lockend, EXTENT_UPTODATE, 0,
6539 *cached_state)))
6540 break;
6541
6542 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6543 cached_state, GFP_NOFS);
6544
6545 if (ordered) {
6546 btrfs_start_ordered_extent(inode, ordered, 1);
6547 btrfs_put_ordered_extent(ordered);
6548 } else {
6549 /* Screw you mmap */
6550 ret = filemap_write_and_wait_range(inode->i_mapping,
6551 lockstart,
6552 lockend);
6553 if (ret)
6554 break;
6555
6556 /*
6557 * If we found a page that couldn't be invalidated just
6558 * fall back to buffered.
6559 */
6560 ret = invalidate_inode_pages2_range(inode->i_mapping,
6561 lockstart >> PAGE_CACHE_SHIFT,
6562 lockend >> PAGE_CACHE_SHIFT);
6563 if (ret)
6564 break;
6565 }
6566
6567 cond_resched();
6568 }
6569
6570 return ret;
6571}
6572
Josef Bacik69ffb542012-09-11 15:40:07 -04006573static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6574 u64 len, u64 orig_start,
6575 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006576 u64 orig_block_len, u64 ram_bytes,
6577 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006578{
6579 struct extent_map_tree *em_tree;
6580 struct extent_map *em;
6581 struct btrfs_root *root = BTRFS_I(inode)->root;
6582 int ret;
6583
6584 em_tree = &BTRFS_I(inode)->extent_tree;
6585 em = alloc_extent_map();
6586 if (!em)
6587 return ERR_PTR(-ENOMEM);
6588
6589 em->start = start;
6590 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006591 em->mod_start = start;
6592 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006593 em->len = len;
6594 em->block_len = block_len;
6595 em->block_start = block_start;
6596 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006597 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006598 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006599 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006600 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6601 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006602 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006603
6604 do {
6605 btrfs_drop_extent_cache(inode, em->start,
6606 em->start + em->len - 1, 0);
6607 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006608 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006609 write_unlock(&em_tree->lock);
6610 } while (ret == -EEXIST);
6611
6612 if (ret) {
6613 free_extent_map(em);
6614 return ERR_PTR(ret);
6615 }
6616
6617 return em;
6618}
6619
6620
Josef Bacik4b46fce2010-05-23 11:00:55 -04006621static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6622 struct buffer_head *bh_result, int create)
6623{
6624 struct extent_map *em;
6625 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006626 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006627 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006628 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006629 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006630 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006631 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006632 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006633
Miao Xie172a5042013-02-21 02:48:22 -07006634 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006635 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006636 else
Josef Bacikc3298612012-08-03 16:49:19 -04006637 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006638
Josef Bacikc3298612012-08-03 16:49:19 -04006639 lockstart = start;
6640 lockend = start + len - 1;
6641
Josef Bacikeb838e72012-07-31 16:28:48 -04006642 /*
6643 * If this errors out it's because we couldn't invalidate pagecache for
6644 * this range and we need to fallback to buffered.
6645 */
6646 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6647 return -ENOTBLK;
6648
Josef Bacik4b46fce2010-05-23 11:00:55 -04006649 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006650 if (IS_ERR(em)) {
6651 ret = PTR_ERR(em);
6652 goto unlock_err;
6653 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006654
6655 /*
6656 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6657 * io. INLINE is special, and we could probably kludge it in here, but
6658 * it's still buffered so for safety lets just fall back to the generic
6659 * buffered path.
6660 *
6661 * For COMPRESSED we _have_ to read the entire extent in so we can
6662 * decompress it, so there will be buffering required no matter what we
6663 * do, so go ahead and fallback to buffered.
6664 *
6665 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6666 * to buffered IO. Don't blame me, this is the price we pay for using
6667 * the generic code.
6668 */
6669 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6670 em->block_start == EXTENT_MAP_INLINE) {
6671 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006672 ret = -ENOTBLK;
6673 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006674 }
6675
6676 /* Just a good old fashioned hole, return */
6677 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6678 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6679 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006680 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006681 }
6682
6683 /*
6684 * We don't allocate a new extent in the following cases
6685 *
6686 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6687 * existing extent.
6688 * 2) The extent is marked as PREALLOC. We're good to go here and can
6689 * just use the extent.
6690 *
6691 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006692 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006693 len = min(len, em->len - (start - em->start));
6694 lockstart = start + len;
6695 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006696 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006697
6698 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6699 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6700 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006701 int type;
6702 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006703 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006704
6705 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6706 type = BTRFS_ORDERED_PREALLOC;
6707 else
6708 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006709 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006710 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006711
6712 /*
6713 * we're not going to log anything, but we do need
6714 * to make sure the current transaction stays open
6715 * while we look for nocow cross refs
6716 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006717 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006718 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006719 goto must_cow;
6720
Josef Bacik7ee9e442013-06-21 16:37:03 -04006721 if (can_nocow_extent(trans, inode, start, &len, &orig_start,
6722 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006723 if (type == BTRFS_ORDERED_PREALLOC) {
6724 free_extent_map(em);
6725 em = create_pinned_em(inode, start, len,
6726 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006727 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006728 orig_block_len,
6729 ram_bytes, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006730 if (IS_ERR(em)) {
6731 btrfs_end_transaction(trans, root);
6732 goto unlock_err;
6733 }
6734 }
6735
Chris Mason46bfbb52010-05-26 11:04:10 -04006736 ret = btrfs_add_ordered_extent_dio(inode, start,
6737 block_start, len, len, type);
6738 btrfs_end_transaction(trans, root);
6739 if (ret) {
6740 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006741 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006742 }
6743 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006744 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006745 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006746 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006747must_cow:
6748 /*
6749 * this will cow the extent, reset the len in case we changed
6750 * it above
6751 */
6752 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006753 free_extent_map(em);
6754 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006755 if (IS_ERR(em)) {
6756 ret = PTR_ERR(em);
6757 goto unlock_err;
6758 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006759 len = min(len, em->len - (start - em->start));
6760unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006761 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6762 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006763 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006764 bh_result->b_bdev = em->bdev;
6765 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006766 if (create) {
6767 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6768 set_buffer_new(bh_result);
6769
6770 /*
6771 * Need to update the i_size under the extent lock so buffered
6772 * readers will get the updated i_size when we unlock.
6773 */
6774 if (start + len > i_size_read(inode))
6775 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006776
Miao Xie172a5042013-02-21 02:48:22 -07006777 spin_lock(&BTRFS_I(inode)->lock);
6778 BTRFS_I(inode)->outstanding_extents++;
6779 spin_unlock(&BTRFS_I(inode)->lock);
6780
Miao Xie09348562013-02-07 10:12:07 +00006781 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6782 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6783 &cached_state, GFP_NOFS);
6784 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006785 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006786
Josef Bacikeb838e72012-07-31 16:28:48 -04006787 /*
6788 * In the case of write we need to clear and unlock the entire range,
6789 * in the case of read we need to unlock only the end area that we
6790 * aren't using if there is any left over space.
6791 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006792 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006793 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6794 lockend, unlock_bits, 1, 0,
6795 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006796 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006797 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006798 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006799
Josef Bacik4b46fce2010-05-23 11:00:55 -04006800 free_extent_map(em);
6801
6802 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006803
6804unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006805 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6806 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6807 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006808}
6809
6810struct btrfs_dio_private {
6811 struct inode *inode;
6812 u64 logical_offset;
6813 u64 disk_bytenr;
6814 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006815 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006816
6817 /* number of bios pending for this dio */
6818 atomic_t pending_bios;
6819
6820 /* IO errors */
6821 int errors;
6822
Chris Mason9be33952013-05-17 18:30:14 -04006823 /* orig_bio is our btrfs_io_bio */
Miao Xiee65e1532010-11-22 03:04:43 +00006824 struct bio *orig_bio;
Chris Mason9be33952013-05-17 18:30:14 -04006825
6826 /* dio_bio came from fs/direct-io.c */
6827 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006828};
6829
6830static void btrfs_endio_direct_read(struct bio *bio, int err)
6831{
Miao Xiee65e1532010-11-22 03:04:43 +00006832 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006833 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6834 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006835 struct inode *inode = dip->inode;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006836 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006837 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006838 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006839
6840 start = dip->logical_offset;
6841 do {
6842 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6843 struct page *page = bvec->bv_page;
6844 char *kaddr;
6845 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006846 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006847 unsigned long flags;
6848
Josef Bacikc3298612012-08-03 16:49:19 -04006849 if (get_state_private(&BTRFS_I(inode)->io_tree,
6850 start, &private))
6851 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006852 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006853 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006854 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006855 csum, bvec->bv_len);
6856 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006857 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006858 local_irq_restore(flags);
6859
6860 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006861 if (csum != private) {
6862failed:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006863 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u private %u",
6864 (unsigned long long)btrfs_ino(inode),
6865 (unsigned long long)start,
6866 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006867 err = -EIO;
6868 }
6869 }
6870
6871 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006872 bvec++;
6873 } while (bvec <= bvec_end);
6874
6875 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006876 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04006877 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006878
Josef Bacik4b46fce2010-05-23 11:00:55 -04006879 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006880
6881 /* If we had a csum failure make sure to clear the uptodate flag */
6882 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006883 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6884 dio_end_io(dio_bio, err);
6885 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006886}
6887
6888static void btrfs_endio_direct_write(struct bio *bio, int err)
6889{
6890 struct btrfs_dio_private *dip = bio->bi_private;
6891 struct inode *inode = dip->inode;
6892 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006893 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006894 u64 ordered_offset = dip->logical_offset;
6895 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04006896 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006897 int ret;
6898
6899 if (err)
6900 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006901again:
6902 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6903 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006904 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006905 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006906 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006907
Josef Bacik5fd02042012-05-02 14:00:54 -04006908 ordered->work.func = finish_ordered_fn;
6909 ordered->work.flags = 0;
6910 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6911 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006912out_test:
6913 /*
6914 * our bio might span multiple ordered extents. If we haven't
6915 * completed the accounting for the whole dio, go back and try again
6916 */
6917 if (ordered_offset < dip->logical_offset + dip->bytes) {
6918 ordered_bytes = dip->logical_offset + dip->bytes -
6919 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006920 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006921 goto again;
6922 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006923out_done:
Chris Mason9be33952013-05-17 18:30:14 -04006924 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006925
Josef Bacik4b46fce2010-05-23 11:00:55 -04006926 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006927
6928 /* If we had an error make sure to clear the uptodate flag */
6929 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006930 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6931 dio_end_io(dio_bio, err);
6932 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006933}
6934
Chris Masoneaf25d92010-05-25 09:48:28 -04006935static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6936 struct bio *bio, int mirror_num,
6937 unsigned long bio_flags, u64 offset)
6938{
6939 int ret;
6940 struct btrfs_root *root = BTRFS_I(inode)->root;
6941 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006942 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006943 return 0;
6944}
6945
Miao Xiee65e1532010-11-22 03:04:43 +00006946static void btrfs_end_dio_bio(struct bio *bio, int err)
6947{
6948 struct btrfs_dio_private *dip = bio->bi_private;
6949
6950 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006951 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006952 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006953 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006954 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006955 dip->errors = 1;
6956
6957 /*
6958 * before atomic variable goto zero, we must make sure
6959 * dip->errors is perceived to be set.
6960 */
6961 smp_mb__before_atomic_dec();
6962 }
6963
6964 /* if there are more bios still pending for this dio, just exit */
6965 if (!atomic_dec_and_test(&dip->pending_bios))
6966 goto out;
6967
Chris Mason9be33952013-05-17 18:30:14 -04006968 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00006969 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04006970 } else {
6971 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00006972 bio_endio(dip->orig_bio, 0);
6973 }
6974out:
6975 bio_put(bio);
6976}
6977
6978static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6979 u64 first_sector, gfp_t gfp_flags)
6980{
6981 int nr_vecs = bio_get_nr_vecs(bdev);
6982 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6983}
6984
6985static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6986 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006987 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006988{
6989 int write = rw & REQ_WRITE;
6990 struct btrfs_root *root = BTRFS_I(inode)->root;
6991 int ret;
6992
Josef Bacikb812ce22012-11-16 13:56:32 -05006993 if (async_submit)
6994 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6995
Miao Xiee65e1532010-11-22 03:04:43 +00006996 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006997
6998 if (!write) {
6999 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7000 if (ret)
7001 goto err;
7002 }
Miao Xiee65e1532010-11-22 03:04:43 +00007003
Josef Bacik1ae39932011-04-06 14:41:34 -04007004 if (skip_sum)
7005 goto map;
7006
7007 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00007008 ret = btrfs_wq_submit_bio(root->fs_info,
7009 inode, rw, bio, 0, 0,
7010 file_offset,
7011 __btrfs_submit_bio_start_direct_io,
7012 __btrfs_submit_bio_done);
7013 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007014 } else if (write) {
7015 /*
7016 * If we aren't doing async submit, calculate the csum of the
7017 * bio now.
7018 */
7019 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7020 if (ret)
7021 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007022 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04007023 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007024 if (ret)
7025 goto err;
7026 }
Miao Xiee65e1532010-11-22 03:04:43 +00007027
Josef Bacik1ae39932011-04-06 14:41:34 -04007028map:
7029 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007030err:
7031 bio_put(bio);
7032 return ret;
7033}
7034
7035static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7036 int skip_sum)
7037{
7038 struct inode *inode = dip->inode;
7039 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00007040 struct bio *bio;
7041 struct bio *orig_bio = dip->orig_bio;
7042 struct bio_vec *bvec = orig_bio->bi_io_vec;
7043 u64 start_sector = orig_bio->bi_sector;
7044 u64 file_offset = dip->logical_offset;
7045 u64 submit_len = 0;
7046 u64 map_length;
7047 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007048 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04007049 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00007050
Miao Xiee65e1532010-11-22 03:04:43 +00007051 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007052 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007053 &map_length, NULL, 0);
7054 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007055 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007056 return -EIO;
7057 }
Josef Bacik02f57c72011-04-06 14:25:44 -04007058 if (map_length >= orig_bio->bi_size) {
7059 bio = orig_bio;
7060 goto submit;
7061 }
7062
David Woodhouse53b381b2013-01-29 18:40:14 -05007063 /* async crcs make it difficult to collect full stripe writes. */
7064 if (btrfs_get_alloc_profile(root, 1) &
7065 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7066 async_submit = 0;
7067 else
7068 async_submit = 1;
7069
Josef Bacik02f57c72011-04-06 14:25:44 -04007070 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7071 if (!bio)
7072 return -ENOMEM;
7073 bio->bi_private = dip;
7074 bio->bi_end_io = btrfs_end_dio_bio;
7075 atomic_inc(&dip->pending_bios);
7076
Miao Xiee65e1532010-11-22 03:04:43 +00007077 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7078 if (unlikely(map_length < submit_len + bvec->bv_len ||
7079 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7080 bvec->bv_offset) < bvec->bv_len)) {
7081 /*
7082 * inc the count before we submit the bio so
7083 * we know the end IO handler won't happen before
7084 * we inc the count. Otherwise, the dip might get freed
7085 * before we're done setting it up
7086 */
7087 atomic_inc(&dip->pending_bios);
7088 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7089 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007090 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007091 if (ret) {
7092 bio_put(bio);
7093 atomic_dec(&dip->pending_bios);
7094 goto out_err;
7095 }
7096
Miao Xiee65e1532010-11-22 03:04:43 +00007097 start_sector += submit_len >> 9;
7098 file_offset += submit_len;
7099
7100 submit_len = 0;
7101 nr_pages = 0;
7102
7103 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7104 start_sector, GFP_NOFS);
7105 if (!bio)
7106 goto out_err;
7107 bio->bi_private = dip;
7108 bio->bi_end_io = btrfs_end_dio_bio;
7109
7110 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007111 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007112 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007113 &map_length, NULL, 0);
7114 if (ret) {
7115 bio_put(bio);
7116 goto out_err;
7117 }
7118 } else {
7119 submit_len += bvec->bv_len;
7120 nr_pages ++;
7121 bvec++;
7122 }
7123 }
7124
Josef Bacik02f57c72011-04-06 14:25:44 -04007125submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007126 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007127 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007128 if (!ret)
7129 return 0;
7130
7131 bio_put(bio);
7132out_err:
7133 dip->errors = 1;
7134 /*
7135 * before atomic variable goto zero, we must
7136 * make sure dip->errors is perceived to be set.
7137 */
7138 smp_mb__before_atomic_dec();
7139 if (atomic_dec_and_test(&dip->pending_bios))
7140 bio_io_error(dip->orig_bio);
7141
7142 /* bio_end_io() will handle error, so we needn't return it */
7143 return 0;
7144}
7145
Chris Mason9be33952013-05-17 18:30:14 -04007146static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7147 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007148{
7149 struct btrfs_root *root = BTRFS_I(inode)->root;
7150 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007151 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007152 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007153 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007154 int ret = 0;
7155
7156 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7157
Chris Mason9be33952013-05-17 18:30:14 -04007158 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
7159
7160 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007161 ret = -ENOMEM;
7162 goto free_ordered;
7163 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007164
Chris Mason9be33952013-05-17 18:30:14 -04007165 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7166 if (!dip) {
7167 ret = -ENOMEM;
7168 goto free_io_bio;
7169 }
7170
7171 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007172 dip->inode = inode;
7173 dip->logical_offset = file_offset;
Miao Xiee6da5d22013-06-19 18:19:17 +08007174 dip->bytes = dio_bio->bi_size;
Chris Mason9be33952013-05-17 18:30:14 -04007175 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7176 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007177 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007178 dip->orig_bio = io_bio;
7179 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007180 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007181
7182 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007183 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007184 else
Chris Mason9be33952013-05-17 18:30:14 -04007185 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007186
Miao Xiee65e1532010-11-22 03:04:43 +00007187 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7188 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007189 return;
Chris Mason9be33952013-05-17 18:30:14 -04007190
7191free_io_bio:
7192 bio_put(io_bio);
7193
Josef Bacik4b46fce2010-05-23 11:00:55 -04007194free_ordered:
7195 /*
7196 * If this is a write, we need to clean up the reserved space and kill
7197 * the ordered extent.
7198 */
7199 if (write) {
7200 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007201 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007202 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7203 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7204 btrfs_free_reserved_extent(root, ordered->start,
7205 ordered->disk_len);
7206 btrfs_put_ordered_extent(ordered);
7207 btrfs_put_ordered_extent(ordered);
7208 }
Chris Mason9be33952013-05-17 18:30:14 -04007209 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007210}
7211
Chris Mason5a5f79b2010-05-26 21:33:37 -04007212static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7213 const struct iovec *iov, loff_t offset,
7214 unsigned long nr_segs)
7215{
7216 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007217 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007218 size_t size;
7219 unsigned long addr;
7220 unsigned blocksize_mask = root->sectorsize - 1;
7221 ssize_t retval = -EINVAL;
7222 loff_t end = offset;
7223
7224 if (offset & blocksize_mask)
7225 goto out;
7226
7227 /* Check the memory alignment. Blocks cannot straddle pages */
7228 for (seg = 0; seg < nr_segs; seg++) {
7229 addr = (unsigned long)iov[seg].iov_base;
7230 size = iov[seg].iov_len;
7231 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007232 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007233 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007234
7235 /* If this is a write we don't need to check anymore */
7236 if (rw & WRITE)
7237 continue;
7238
7239 /*
7240 * Check to make sure we don't have duplicate iov_base's in this
7241 * iovec, if so return EINVAL, otherwise we'll get csum errors
7242 * when reading back.
7243 */
7244 for (i = seg + 1; i < nr_segs; i++) {
7245 if (iov[seg].iov_base == iov[i].iov_base)
7246 goto out;
7247 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007248 }
7249 retval = 0;
7250out:
7251 return retval;
7252}
Josef Bacikeb838e72012-07-31 16:28:48 -04007253
Chris Mason16432982008-04-10 10:23:21 -04007254static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7255 const struct iovec *iov, loff_t offset,
7256 unsigned long nr_segs)
7257{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007258 struct file *file = iocb->ki_filp;
7259 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007260 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007261 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007262 bool wakeup = true;
7263 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007264 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007265
Chris Mason5a5f79b2010-05-26 21:33:37 -04007266 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007267 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007268 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007269
Miao Xie38851cc2013-02-08 07:04:11 +00007270 atomic_inc(&inode->i_dio_count);
7271 smp_mb__after_atomic_inc();
7272
Miao Xie09348562013-02-07 10:12:07 +00007273 if (rw & WRITE) {
7274 count = iov_length(iov, nr_segs);
Miao Xie38851cc2013-02-08 07:04:11 +00007275 /*
7276 * If the write DIO is beyond the EOF, we need update
7277 * the isize, but it is protected by i_mutex. So we can
7278 * not unlock the i_mutex at this case.
7279 */
7280 if (offset + count <= inode->i_size) {
7281 mutex_unlock(&inode->i_mutex);
7282 relock = true;
7283 }
Miao Xie09348562013-02-07 10:12:07 +00007284 ret = btrfs_delalloc_reserve_space(inode, count);
7285 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007286 goto out;
7287 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7288 &BTRFS_I(inode)->runtime_flags))) {
7289 inode_dio_done(inode);
7290 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7291 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007292 }
7293
7294 ret = __blockdev_direct_IO(rw, iocb, inode,
7295 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7296 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007297 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007298 if (rw & WRITE) {
7299 if (ret < 0 && ret != -EIOCBQUEUED)
7300 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007301 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007302 btrfs_delalloc_release_space(inode,
7303 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007304 else
7305 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007306 }
Miao Xie38851cc2013-02-08 07:04:11 +00007307out:
Miao Xie2e60a512013-02-08 07:01:08 +00007308 if (wakeup)
7309 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007310 if (relock)
7311 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007312
7313 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007314}
7315
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007316#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7317
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007318static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7319 __u64 start, __u64 len)
7320{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007321 int ret;
7322
7323 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7324 if (ret)
7325 return ret;
7326
Chris Masonec29ed52011-02-23 16:23:20 -05007327 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007328}
7329
Chris Mason9ebefb182007-06-15 13:50:00 -04007330int btrfs_readpage(struct file *file, struct page *page)
7331{
Chris Masond1310b22008-01-24 16:13:08 -05007332 struct extent_io_tree *tree;
7333 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007334 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007335}
Chris Mason1832a6d2007-12-21 16:27:21 -05007336
Chris Mason39279cc2007-06-12 06:35:45 -04007337static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7338{
Chris Masond1310b22008-01-24 16:13:08 -05007339 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007340
7341
7342 if (current->flags & PF_MEMALLOC) {
7343 redirty_page_for_writepage(wbc, page);
7344 unlock_page(page);
7345 return 0;
7346 }
Chris Masond1310b22008-01-24 16:13:08 -05007347 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007348 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7349}
Chris Mason39279cc2007-06-12 06:35:45 -04007350
Eric Sandeen48a3b632013-04-25 20:41:01 +00007351static int btrfs_writepages(struct address_space *mapping,
7352 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04007353{
Chris Masond1310b22008-01-24 16:13:08 -05007354 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007355
Chris Masond1310b22008-01-24 16:13:08 -05007356 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04007357 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7358}
7359
Chris Mason3ab2fb52007-11-08 10:59:22 -05007360static int
7361btrfs_readpages(struct file *file, struct address_space *mapping,
7362 struct list_head *pages, unsigned nr_pages)
7363{
Chris Masond1310b22008-01-24 16:13:08 -05007364 struct extent_io_tree *tree;
7365 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007366 return extent_readpages(tree, mapping, pages, nr_pages,
7367 btrfs_get_extent);
7368}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007369static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007370{
Chris Masond1310b22008-01-24 16:13:08 -05007371 struct extent_io_tree *tree;
7372 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007373 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007374
Chris Masond1310b22008-01-24 16:13:08 -05007375 tree = &BTRFS_I(page->mapping->host)->io_tree;
7376 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007377 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007378 if (ret == 1) {
7379 ClearPagePrivate(page);
7380 set_page_private(page, 0);
7381 page_cache_release(page);
7382 }
7383 return ret;
7384}
Chris Mason39279cc2007-06-12 06:35:45 -04007385
Chris Masone6dcd2d2008-07-17 12:53:50 -04007386static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7387{
Chris Mason98509cf2008-09-11 15:51:43 -04007388 if (PageWriteback(page) || PageDirty(page))
7389 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007390 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007391}
7392
Chris Masona52d9a82007-08-27 16:49:44 -04007393static void btrfs_invalidatepage(struct page *page, unsigned long offset)
7394{
Josef Bacik5fd02042012-05-02 14:00:54 -04007395 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007396 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007397 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007398 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007399 u64 page_start = page_offset(page);
7400 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007401
Chris Mason8b62b722009-09-02 16:53:46 -04007402 /*
7403 * we have the page locked, so new writeback can't start,
7404 * and the dirty bit won't be cleared while we are here.
7405 *
7406 * Wait for IO on this page so that we can safely clear
7407 * the PagePrivate2 bit and do ordered accounting
7408 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007409 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007410
Josef Bacik5fd02042012-05-02 14:00:54 -04007411 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007412 if (offset) {
7413 btrfs_releasepage(page, GFP_NOFS);
7414 return;
7415 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007416 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007417 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007418 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007419 /*
7420 * IO on this page will never be started, so we need
7421 * to account for any ordered extents now
7422 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007423 clear_extent_bit(tree, page_start, page_end,
7424 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007425 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7426 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007427 /*
7428 * whoever cleared the private bit is responsible
7429 * for the finish_ordered_io
7430 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007431 if (TestClearPagePrivate2(page) &&
7432 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7433 PAGE_CACHE_SIZE, 1)) {
7434 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007435 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007436 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007437 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007438 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007439 }
7440 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007441 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007442 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7443 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007444 __btrfs_releasepage(page, GFP_NOFS);
7445
Chris Mason4a096752008-07-21 10:29:44 -04007446 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007447 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04007448 ClearPagePrivate(page);
7449 set_page_private(page, 0);
7450 page_cache_release(page);
7451 }
Chris Mason39279cc2007-06-12 06:35:45 -04007452}
7453
Chris Mason9ebefb182007-06-15 13:50:00 -04007454/*
7455 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7456 * called from a page fault handler when a page is first dirtied. Hence we must
7457 * be careful to check for EOF conditions here. We set the page up correctly
7458 * for a written page which means we get ENOSPC checking when writing into
7459 * holes and correct delalloc and unwritten extent mapping on filesystems that
7460 * support these features.
7461 *
7462 * We are not allowed to take the i_mutex here so we have to play games to
7463 * protect against truncate races as the page could now be beyond EOF. Because
7464 * vmtruncate() writes the inode size before removing pages, once we have the
7465 * page lock we can determine safely if the page is beyond EOF. If it is not
7466 * beyond EOF, then the page is guaranteed safe against truncation until we
7467 * unlock the page.
7468 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007469int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007470{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007471 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007472 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007473 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007474 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7475 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007476 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007477 char *kaddr;
7478 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007479 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007480 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007481 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007482 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007483 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007484
Jan Karab2b5ef52012-06-12 16:20:45 +02007485 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007486 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007487 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007488 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007489 reserved = 1;
7490 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007491 if (ret) {
7492 if (ret == -ENOMEM)
7493 ret = VM_FAULT_OOM;
7494 else /* -ENOSPC, -EIO, etc */
7495 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007496 if (reserved)
7497 goto out;
7498 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007499 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007500
Nick Piggin56a76f82009-03-31 15:23:23 -07007501 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007502again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007503 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007504 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007505 page_start = page_offset(page);
7506 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007507
Chris Mason9ebefb182007-06-15 13:50:00 -04007508 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007509 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007510 /* page got truncated out from underneath us */
7511 goto out_unlock;
7512 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007513 wait_on_page_writeback(page);
7514
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007515 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007516 set_page_extent_mapped(page);
7517
Chris Masoneb84ae02008-07-17 13:53:27 -04007518 /*
7519 * we can't set the delalloc bits if there are pending ordered
7520 * extents. Drop our locks and wait for them to finish
7521 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007522 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7523 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007524 unlock_extent_cached(io_tree, page_start, page_end,
7525 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007526 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007527 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007528 btrfs_put_ordered_extent(ordered);
7529 goto again;
7530 }
7531
Josef Bacikfbf19082009-10-01 17:10:23 -04007532 /*
7533 * XXX - page_mkwrite gets called every time the page is dirtied, even
7534 * if it was already dirty, so for space accounting reasons we need to
7535 * clear any delalloc bits for the range we are fixing to save. There
7536 * is probably a better way to do this, but for now keep consistent with
7537 * prepare_pages in the normal write path.
7538 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007539 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007540 EXTENT_DIRTY | EXTENT_DELALLOC |
7541 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007542 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007543
Josef Bacik2ac55d42010-02-03 19:33:23 +00007544 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7545 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007546 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007547 unlock_extent_cached(io_tree, page_start, page_end,
7548 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007549 ret = VM_FAULT_SIGBUS;
7550 goto out_unlock;
7551 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007552 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007553
7554 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007555 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007556 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007557 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007558 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007559
Chris Masone6dcd2d2008-07-17 12:53:50 -04007560 if (zero_start != PAGE_CACHE_SIZE) {
7561 kaddr = kmap(page);
7562 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7563 flush_dcache_page(page);
7564 kunmap(page);
7565 }
Chris Mason247e7432008-07-17 12:53:51 -04007566 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007567 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007568 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007569
Chris Mason257c62e2009-10-13 13:21:08 -04007570 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7571 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007572 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007573
Josef Bacik2ac55d42010-02-03 19:33:23 +00007574 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007575
7576out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007577 if (!ret) {
7578 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007579 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007580 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007581 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007582out:
Josef Bacikec39e182012-01-12 19:10:12 -05007583 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007584out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007585 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007586 return ret;
7587}
7588
Josef Bacika41ad392011-01-31 15:30:16 -05007589static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007590{
7591 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007592 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007593 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007594 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007595 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007596 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007597 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007598
Chris Mason4a096752008-07-21 10:29:44 -04007599 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007600 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007601
Josef Bacikfcb80c22011-05-03 10:40:22 -04007602 /*
7603 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7604 * 3 things going on here
7605 *
7606 * 1) We need to reserve space for our orphan item and the space to
7607 * delete our orphan item. Lord knows we don't want to have a dangling
7608 * orphan item because we didn't reserve space to remove it.
7609 *
7610 * 2) We need to reserve space to update our inode.
7611 *
7612 * 3) We need to have something to cache all the space that is going to
7613 * be free'd up by the truncate operation, but also have some slack
7614 * space reserved in case it uses space during the truncate (thank you
7615 * very much snapshotting).
7616 *
7617 * And we need these to all be seperate. The fact is we can use alot of
7618 * space doing the truncate, and we have no earthly idea how much space
7619 * we will use, so we need the truncate reservation to be seperate so it
7620 * doesn't end up using space reserved for updating the inode or
7621 * removing the orphan item. We also need to be able to stop the
7622 * transaction and start a new one, which means we need to be able to
7623 * update the inode several times, and we have no idea of knowing how
7624 * many times that will be, so we can't just reserve 1 item for the
7625 * entirety of the opration, so that has to be done seperately as well.
7626 * Then there is the orphan item, which does indeed need to be held on
7627 * to for the whole operation, and we need nobody to touch this reserved
7628 * space except the orphan code.
7629 *
7630 * So that leaves us with
7631 *
7632 * 1) root->orphan_block_rsv - for the orphan deletion.
7633 * 2) rsv - for the truncate reservation, which we will steal from the
7634 * transaction reservation.
7635 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7636 * updating the inode.
7637 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007638 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007639 if (!rsv)
7640 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007641 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007642 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007643
Josef Bacik907cbce2011-08-08 13:46:15 -04007644 /*
Josef Bacik07127182011-08-19 10:29:59 -04007645 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007646 * 1 for updating the inode.
7647 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007648 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007649 if (IS_ERR(trans)) {
7650 err = PTR_ERR(trans);
7651 goto out;
7652 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007653
Josef Bacik907cbce2011-08-08 13:46:15 -04007654 /* Migrate the slack space for the truncate to our reserve */
7655 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7656 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007657 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007658
Chris Mason5a3f23d2009-03-31 13:27:11 -04007659 /*
7660 * setattr is responsible for setting the ordered_data_close flag,
7661 * but that is only tested during the last file release. That
7662 * could happen well after the next commit, leaving a great big
7663 * window where new writes may get lost if someone chooses to write
7664 * to this file after truncating to zero
7665 *
7666 * The inode doesn't have any dirty data here, and so if we commit
7667 * this is a noop. If someone immediately starts writing to the inode
7668 * it is very likely we'll catch some of their writes in this
7669 * transaction, and the commit will find this file on the ordered
7670 * data list with good things to send down.
7671 *
7672 * This is a best effort solution, there is still a window where
7673 * using truncate to replace the contents of the file will
7674 * end up with a zero length file after a crash.
7675 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007676 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7677 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007678 btrfs_add_ordered_operation(trans, root, inode);
7679
Josef Bacik5dc562c2012-08-17 13:14:17 -04007680 /*
7681 * So if we truncate and then write and fsync we normally would just
7682 * write the extents that changed, which is a problem if we need to
7683 * first truncate that entire inode. So set this flag so we write out
7684 * all of the extents in the inode to the sync log so we're completely
7685 * safe.
7686 */
7687 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007688 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007689
Yan, Zheng80825102009-11-12 09:35:36 +00007690 while (1) {
7691 ret = btrfs_truncate_inode_items(trans, root, inode,
7692 inode->i_size,
7693 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007694 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007695 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007696 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007697 }
Chris Mason39279cc2007-06-12 06:35:45 -04007698
Josef Bacikfcb80c22011-05-03 10:40:22 -04007699 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007700 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007701 if (ret) {
7702 err = ret;
7703 break;
7704 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007705
Yan, Zheng80825102009-11-12 09:35:36 +00007706 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007707 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007708
7709 trans = btrfs_start_transaction(root, 2);
7710 if (IS_ERR(trans)) {
7711 ret = err = PTR_ERR(trans);
7712 trans = NULL;
7713 break;
7714 }
7715
7716 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7717 rsv, min_size);
7718 BUG_ON(ret); /* shouldn't happen */
7719 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007720 }
7721
7722 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007723 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007724 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007725 if (ret)
7726 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007727 }
7728
Chris Mason917c16b2011-11-08 14:49:59 -05007729 if (trans) {
7730 trans->block_rsv = &root->fs_info->trans_block_rsv;
7731 ret = btrfs_update_inode(trans, root, inode);
7732 if (ret && !err)
7733 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007734
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007735 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007736 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007737 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007738
7739out:
7740 btrfs_free_block_rsv(root, rsv);
7741
Josef Bacik3893e332011-01-31 16:03:11 -05007742 if (ret && !err)
7743 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007744
Josef Bacik3893e332011-01-31 16:03:11 -05007745 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007746}
7747
Sven Wegener3b963622008-06-09 21:57:42 -04007748/*
Chris Masond352ac62008-09-29 15:18:18 -04007749 * create a new subvolume directory/inode (helper for the ioctl).
7750 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007751int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007752 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007753{
Chris Mason39279cc2007-06-12 06:35:45 -04007754 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007755 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007756 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007757
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007758 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7759 new_dirid, new_dirid,
7760 S_IFDIR | (~current_umask() & S_IRWXUGO),
7761 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007762 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007763 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007764 inode->i_op = &btrfs_dir_inode_operations;
7765 inode->i_fop = &btrfs_dir_file_operations;
7766
Miklos Szeredibfe86842011-10-28 14:13:29 +02007767 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007768 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007769
Yan, Zheng76dda932009-09-21 16:00:26 -04007770 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007771
Yan, Zheng76dda932009-09-21 16:00:26 -04007772 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007773 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007774}
7775
Chris Mason39279cc2007-06-12 06:35:45 -04007776struct inode *btrfs_alloc_inode(struct super_block *sb)
7777{
7778 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007779 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007780
7781 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7782 if (!ei)
7783 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007784
7785 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007786 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007787 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007788 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007789 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007790 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007791 ei->disk_i_size = 0;
7792 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007793 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007794 ei->index_cnt = (u64)-1;
7795 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007796 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007797
Josef Bacik9e0baf62011-07-15 15:16:44 +00007798 spin_lock_init(&ei->lock);
7799 ei->outstanding_extents = 0;
7800 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007801
Josef Bacik72ac3c02012-05-23 14:13:11 -04007802 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007803 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007804
Miao Xie16cdcec2011-04-22 18:12:22 +08007805 ei->delayed_node = NULL;
7806
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007807 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007808 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007809 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7810 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007811 ei->io_tree.track_uptodate = 1;
7812 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007813 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007814 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007815 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007816 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007817 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007818 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007819 RB_CLEAR_NODE(&ei->rb_node);
7820
7821 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007822}
7823
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007824static void btrfs_i_callback(struct rcu_head *head)
7825{
7826 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007827 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7828}
7829
Chris Mason39279cc2007-06-12 06:35:45 -04007830void btrfs_destroy_inode(struct inode *inode)
7831{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007832 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007833 struct btrfs_root *root = BTRFS_I(inode)->root;
7834
Al Virob3d9b7a2012-06-09 13:51:19 -04007835 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007836 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007837 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7838 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007839 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7840 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007841
Chris Mason5a3f23d2009-03-31 13:27:11 -04007842 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007843 * This can happen where we create an inode, but somebody else also
7844 * created the same inode and we need to destroy the one we already
7845 * created.
7846 */
7847 if (!root)
7848 goto free;
7849
7850 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007851 * Make sure we're properly removed from the ordered operation
7852 * lists.
7853 */
7854 smp_mb();
7855 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
Miao Xie199c2a92013-05-15 07:48:23 +00007856 spin_lock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007857 list_del_init(&BTRFS_I(inode)->ordered_operations);
Miao Xie199c2a92013-05-15 07:48:23 +00007858 spin_unlock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007859 }
7860
Josef Bacik8a35d952012-05-23 14:26:42 -04007861 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7862 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007863 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
7864 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007865 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007866 }
Josef Bacik7b128762008-07-24 12:17:14 -04007867
Chris Masond3977122009-01-05 21:25:51 -05007868 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007869 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7870 if (!ordered)
7871 break;
7872 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007873 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
7874 (unsigned long long)ordered->file_offset,
7875 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007876 btrfs_remove_ordered_extent(inode, ordered);
7877 btrfs_put_ordered_extent(ordered);
7878 btrfs_put_ordered_extent(ordered);
7879 }
7880 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007881 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007882 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007883free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007884 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007885}
7886
Al Viro45321ac2010-06-07 13:43:19 -04007887int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007888{
7889 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007890
Naohiro Aota6379ef92013-06-06 09:56:34 +00007891 if (root == NULL)
7892 return 1;
7893
Liu Bofa6ac872013-02-20 14:10:23 +00007894 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007895 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007896 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007897 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007898 else
Al Viro45321ac2010-06-07 13:43:19 -04007899 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007900}
7901
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007902static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007903{
7904 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7905
7906 inode_init_once(&ei->vfs_inode);
7907}
7908
7909void btrfs_destroy_cachep(void)
7910{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007911 /*
7912 * Make sure all delayed rcu free inodes are flushed before we
7913 * destroy cache.
7914 */
7915 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007916 if (btrfs_inode_cachep)
7917 kmem_cache_destroy(btrfs_inode_cachep);
7918 if (btrfs_trans_handle_cachep)
7919 kmem_cache_destroy(btrfs_trans_handle_cachep);
7920 if (btrfs_transaction_cachep)
7921 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007922 if (btrfs_path_cachep)
7923 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007924 if (btrfs_free_space_cachep)
7925 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007926 if (btrfs_delalloc_work_cachep)
7927 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007928}
7929
7930int btrfs_init_cachep(void)
7931{
David Sterba837e1972012-09-07 03:00:48 -06007932 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007933 sizeof(struct btrfs_inode), 0,
7934 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007935 if (!btrfs_inode_cachep)
7936 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007937
David Sterba837e1972012-09-07 03:00:48 -06007938 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007939 sizeof(struct btrfs_trans_handle), 0,
7940 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007941 if (!btrfs_trans_handle_cachep)
7942 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007943
David Sterba837e1972012-09-07 03:00:48 -06007944 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007945 sizeof(struct btrfs_transaction), 0,
7946 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007947 if (!btrfs_transaction_cachep)
7948 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007949
David Sterba837e1972012-09-07 03:00:48 -06007950 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007951 sizeof(struct btrfs_path), 0,
7952 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007953 if (!btrfs_path_cachep)
7954 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007955
David Sterba837e1972012-09-07 03:00:48 -06007956 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007957 sizeof(struct btrfs_free_space), 0,
7958 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7959 if (!btrfs_free_space_cachep)
7960 goto fail;
7961
Miao Xie8ccf6f192012-10-25 09:28:04 +00007962 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7963 sizeof(struct btrfs_delalloc_work), 0,
7964 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7965 NULL);
7966 if (!btrfs_delalloc_work_cachep)
7967 goto fail;
7968
Chris Mason39279cc2007-06-12 06:35:45 -04007969 return 0;
7970fail:
7971 btrfs_destroy_cachep();
7972 return -ENOMEM;
7973}
7974
7975static int btrfs_getattr(struct vfsmount *mnt,
7976 struct dentry *dentry, struct kstat *stat)
7977{
Miao Xiedf0af1a2013-01-29 10:11:59 +00007978 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04007979 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007980 u32 blocksize = inode->i_sb->s_blocksize;
7981
Chris Mason39279cc2007-06-12 06:35:45 -04007982 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007983 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007984 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00007985
7986 spin_lock(&BTRFS_I(inode)->lock);
7987 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
7988 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05007989 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00007990 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007991 return 0;
7992}
7993
Chris Masond3977122009-01-05 21:25:51 -05007994static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7995 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007996{
7997 struct btrfs_trans_handle *trans;
7998 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007999 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04008000 struct inode *new_inode = new_dentry->d_inode;
8001 struct inode *old_inode = old_dentry->d_inode;
8002 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008003 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008004 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04008005 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08008006 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008007
Li Zefan33345d012011-04-20 10:31:50 +08008008 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04008009 return -EPERM;
8010
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008011 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08008012 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05008013 return -EXDEV;
8014
Li Zefan33345d012011-04-20 10:31:50 +08008015 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8016 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008017 return -ENOTEMPTY;
8018
Chris Mason39279cc2007-06-12 06:35:45 -04008019 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008020 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04008021 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05008022
8023
8024 /* check for collisions, even if the name isn't there */
8025 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8026 new_dentry->d_name.name,
8027 new_dentry->d_name.len);
8028
8029 if (ret) {
8030 if (ret == -EEXIST) {
8031 /* we shouldn't get
8032 * eexist without a new_inode */
8033 if (!new_inode) {
8034 WARN_ON(1);
8035 return ret;
8036 }
8037 } else {
8038 /* maybe -EOVERFLOW */
8039 return ret;
8040 }
8041 }
8042 ret = 0;
8043
Chris Mason5a3f23d2009-03-31 13:27:11 -04008044 /*
8045 * we're using rename to replace one file with another.
8046 * and the replacement file is large. Start IO on it now so
8047 * we don't add too much work to the end of the transaction
8048 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008049 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008050 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8051 filemap_flush(old_inode->i_mapping);
8052
Yan, Zheng76dda932009-09-21 16:00:26 -04008053 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008054 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008055 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008056 /*
8057 * We want to reserve the absolute worst case amount of items. So if
8058 * both inodes are subvols and we need to unlink them then that would
8059 * require 4 item modifications, but if they are both normal inodes it
8060 * would require 5 item modifications, so we'll assume their normal
8061 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8062 * should cover the worst case number of items we'll modify.
8063 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008064 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008065 if (IS_ERR(trans)) {
8066 ret = PTR_ERR(trans);
8067 goto out_notrans;
8068 }
Chris Mason5f39d392007-10-15 16:14:19 -04008069
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008070 if (dest != root)
8071 btrfs_record_root_in_trans(trans, dest);
8072
Yan, Zhenga5719522009-09-24 09:17:31 -04008073 ret = btrfs_set_inode_index(new_dir, &index);
8074 if (ret)
8075 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008076
Li Zefan33345d012011-04-20 10:31:50 +08008077 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008078 /* force full log commit if subvolume involved. */
8079 root->fs_info->last_trans_log_full_commit = trans->transid;
8080 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008081 ret = btrfs_insert_inode_ref(trans, dest,
8082 new_dentry->d_name.name,
8083 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008084 old_ino,
8085 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008086 if (ret)
8087 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008088 /*
8089 * this is an ugly little race, but the rename is required
8090 * to make sure that if we crash, the inode is either at the
8091 * old name or the new one. pinning the log transaction lets
8092 * us make sure we don't allow a log commit to come in after
8093 * we unlink the name but before we add the new name back in.
8094 */
8095 btrfs_pin_log_trans(root);
8096 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008097 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008098 * make sure the inode gets flushed if it is replacing
8099 * something.
8100 */
Li Zefan33345d012011-04-20 10:31:50 +08008101 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008102 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008103
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008104 inode_inc_iversion(old_dir);
8105 inode_inc_iversion(new_dir);
8106 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008107 old_dir->i_ctime = old_dir->i_mtime = ctime;
8108 new_dir->i_ctime = new_dir->i_mtime = ctime;
8109 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008110
Chris Mason12fcfd22009-03-24 10:24:20 -04008111 if (old_dentry->d_parent != new_dentry->d_parent)
8112 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8113
Li Zefan33345d012011-04-20 10:31:50 +08008114 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008115 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8116 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8117 old_dentry->d_name.name,
8118 old_dentry->d_name.len);
8119 } else {
Al Viro92986792011-03-04 17:14:37 +00008120 ret = __btrfs_unlink_inode(trans, root, old_dir,
8121 old_dentry->d_inode,
8122 old_dentry->d_name.name,
8123 old_dentry->d_name.len);
8124 if (!ret)
8125 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008126 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008127 if (ret) {
8128 btrfs_abort_transaction(trans, root, ret);
8129 goto out_fail;
8130 }
Chris Mason39279cc2007-06-12 06:35:45 -04008131
8132 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008133 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008134 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008135 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008136 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8137 root_objectid = BTRFS_I(new_inode)->location.objectid;
8138 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8139 root_objectid,
8140 new_dentry->d_name.name,
8141 new_dentry->d_name.len);
8142 BUG_ON(new_inode->i_nlink == 0);
8143 } else {
8144 ret = btrfs_unlink_inode(trans, dest, new_dir,
8145 new_dentry->d_inode,
8146 new_dentry->d_name.name,
8147 new_dentry->d_name.len);
8148 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008149 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008150 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008151 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008152 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008153 if (ret) {
8154 btrfs_abort_transaction(trans, root, ret);
8155 goto out_fail;
8156 }
Chris Mason39279cc2007-06-12 06:35:45 -04008157 }
Josef Bacikaec74772008-07-24 12:12:38 -04008158
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008159 ret = btrfs_add_link(trans, new_dir, old_inode,
8160 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008161 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008162 if (ret) {
8163 btrfs_abort_transaction(trans, root, ret);
8164 goto out_fail;
8165 }
Chris Mason39279cc2007-06-12 06:35:45 -04008166
Li Zefan33345d012011-04-20 10:31:50 +08008167 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008168 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008169 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008170 btrfs_end_log_trans(root);
8171 }
Chris Mason39279cc2007-06-12 06:35:45 -04008172out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008173 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008174out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008175 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008176 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008177
Chris Mason39279cc2007-06-12 06:35:45 -04008178 return ret;
8179}
8180
Miao Xie8ccf6f192012-10-25 09:28:04 +00008181static void btrfs_run_delalloc_work(struct btrfs_work *work)
8182{
8183 struct btrfs_delalloc_work *delalloc_work;
8184
8185 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8186 work);
8187 if (delalloc_work->wait)
8188 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8189 else
8190 filemap_flush(delalloc_work->inode->i_mapping);
8191
8192 if (delalloc_work->delay_iput)
8193 btrfs_add_delayed_iput(delalloc_work->inode);
8194 else
8195 iput(delalloc_work->inode);
8196 complete(&delalloc_work->completion);
8197}
8198
8199struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8200 int wait, int delay_iput)
8201{
8202 struct btrfs_delalloc_work *work;
8203
8204 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8205 if (!work)
8206 return NULL;
8207
8208 init_completion(&work->completion);
8209 INIT_LIST_HEAD(&work->list);
8210 work->inode = inode;
8211 work->wait = wait;
8212 work->delay_iput = delay_iput;
8213 work->work.func = btrfs_run_delalloc_work;
8214
8215 return work;
8216}
8217
8218void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8219{
8220 wait_for_completion(&work->completion);
8221 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8222}
8223
Chris Masond352ac62008-09-29 15:18:18 -04008224/*
8225 * some fairly slow code that needs optimization. This walks the list
8226 * of all the inodes with pending delalloc and forces them to disk.
8227 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00008228static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008229{
Chris Masonea8c2812008-08-04 23:17:27 -04008230 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008231 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008232 struct btrfs_delalloc_work *work, *next;
8233 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008234 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008235 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008236
Miao Xie8ccf6f192012-10-25 09:28:04 +00008237 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008238 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008239
Miao Xieeb73c1b2013-05-15 07:48:22 +00008240 spin_lock(&root->delalloc_lock);
8241 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008242 while (!list_empty(&splice)) {
8243 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008244 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008245
Miao Xieeb73c1b2013-05-15 07:48:22 +00008246 list_move_tail(&binode->delalloc_inodes,
8247 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008248 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008249 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008250 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008251 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008252 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008253 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008254
8255 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8256 if (unlikely(!work)) {
8257 ret = -ENOMEM;
8258 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008259 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008260 list_add_tail(&work->list, &works);
8261 btrfs_queue_worker(&root->fs_info->flush_workers,
8262 &work->work);
8263
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008264 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008265 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008266 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008267 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008268
Miao Xie1eafa6c2013-01-22 10:49:00 +00008269 list_for_each_entry_safe(work, next, &works, list) {
8270 list_del_init(&work->list);
8271 btrfs_wait_and_free_delalloc_work(work);
8272 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008273 return 0;
8274out:
8275 list_for_each_entry_safe(work, next, &works, list) {
8276 list_del_init(&work->list);
8277 btrfs_wait_and_free_delalloc_work(work);
8278 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008279
Miao Xieeb73c1b2013-05-15 07:48:22 +00008280 if (!list_empty_careful(&splice)) {
8281 spin_lock(&root->delalloc_lock);
8282 list_splice_tail(&splice, &root->delalloc_inodes);
8283 spin_unlock(&root->delalloc_lock);
8284 }
8285 return ret;
8286}
8287
8288int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8289{
8290 int ret;
8291
8292 if (root->fs_info->sb->s_flags & MS_RDONLY)
8293 return -EROFS;
8294
8295 ret = __start_delalloc_inodes(root, delay_iput);
8296 /*
8297 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008298 * we have to make sure the IO is actually started and that
8299 * ordered extents get created before we return
8300 */
8301 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008302 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008303 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008304 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008305 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8306 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008307 }
8308 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008309 return ret;
8310}
8311
8312int btrfs_start_all_delalloc_inodes(struct btrfs_fs_info *fs_info,
8313 int delay_iput)
8314{
8315 struct btrfs_root *root;
8316 struct list_head splice;
8317 int ret;
8318
8319 if (fs_info->sb->s_flags & MS_RDONLY)
8320 return -EROFS;
8321
8322 INIT_LIST_HEAD(&splice);
8323
8324 spin_lock(&fs_info->delalloc_root_lock);
8325 list_splice_init(&fs_info->delalloc_roots, &splice);
8326 while (!list_empty(&splice)) {
8327 root = list_first_entry(&splice, struct btrfs_root,
8328 delalloc_root);
8329 root = btrfs_grab_fs_root(root);
8330 BUG_ON(!root);
8331 list_move_tail(&root->delalloc_root,
8332 &fs_info->delalloc_roots);
8333 spin_unlock(&fs_info->delalloc_root_lock);
8334
8335 ret = __start_delalloc_inodes(root, delay_iput);
8336 btrfs_put_fs_root(root);
8337 if (ret)
8338 goto out;
8339
8340 spin_lock(&fs_info->delalloc_root_lock);
8341 }
8342 spin_unlock(&fs_info->delalloc_root_lock);
8343
8344 atomic_inc(&fs_info->async_submit_draining);
8345 while (atomic_read(&fs_info->nr_async_submits) ||
8346 atomic_read(&fs_info->async_delalloc_pages)) {
8347 wait_event(fs_info->async_submit_wait,
8348 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8349 atomic_read(&fs_info->async_delalloc_pages) == 0));
8350 }
8351 atomic_dec(&fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008352 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008353out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008354 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008355 spin_lock(&fs_info->delalloc_root_lock);
8356 list_splice_tail(&splice, &fs_info->delalloc_roots);
8357 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008358 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008359 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008360}
8361
Chris Mason39279cc2007-06-12 06:35:45 -04008362static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8363 const char *symname)
8364{
8365 struct btrfs_trans_handle *trans;
8366 struct btrfs_root *root = BTRFS_I(dir)->root;
8367 struct btrfs_path *path;
8368 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008369 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008370 int err;
8371 int drop_inode = 0;
8372 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008373 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008374 int name_len;
8375 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008376 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008377 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008378 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008379
8380 name_len = strlen(symname) + 1;
8381 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8382 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008383
Josef Bacik9ed74f22009-09-11 16:12:44 -04008384 /*
8385 * 2 items for inode item and ref
8386 * 2 items for dir items
8387 * 1 item for xattr if selinux is on
8388 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008389 trans = btrfs_start_transaction(root, 5);
8390 if (IS_ERR(trans))
8391 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008392
Li Zefan581bb052011-04-20 10:06:11 +08008393 err = btrfs_find_free_ino(root, &objectid);
8394 if (err)
8395 goto out_unlock;
8396
Josef Bacikaec74772008-07-24 12:12:38 -04008397 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008398 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04008399 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008400 if (IS_ERR(inode)) {
8401 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008402 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008403 }
Chris Mason39279cc2007-06-12 06:35:45 -04008404
Eric Paris2a7dba32011-02-01 11:05:39 -05008405 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008406 if (err) {
8407 drop_inode = 1;
8408 goto out_unlock;
8409 }
8410
Casey Schauflerad19db72011-12-15 10:09:07 -05008411 /*
8412 * If the active LSM wants to access the inode during
8413 * d_instantiate it needs these. Smack checks to see
8414 * if the filesystem supports xattrs by looking at the
8415 * ops vector.
8416 */
8417 inode->i_fop = &btrfs_file_operations;
8418 inode->i_op = &btrfs_file_inode_operations;
8419
Josef Bacika1b075d2010-11-19 20:36:11 +00008420 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008421 if (err)
8422 drop_inode = 1;
8423 else {
8424 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008425 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008426 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008427 }
Chris Mason39279cc2007-06-12 06:35:45 -04008428 if (drop_inode)
8429 goto out_unlock;
8430
8431 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008432 if (!path) {
8433 err = -ENOMEM;
8434 drop_inode = 1;
8435 goto out_unlock;
8436 }
Li Zefan33345d012011-04-20 10:31:50 +08008437 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008438 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008439 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8440 datasize = btrfs_file_extent_calc_inline_size(name_len);
8441 err = btrfs_insert_empty_item(trans, root, path, &key,
8442 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008443 if (err) {
8444 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008445 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008446 goto out_unlock;
8447 }
Chris Mason5f39d392007-10-15 16:14:19 -04008448 leaf = path->nodes[0];
8449 ei = btrfs_item_ptr(leaf, path->slots[0],
8450 struct btrfs_file_extent_item);
8451 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8452 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008453 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008454 btrfs_set_file_extent_encryption(leaf, ei, 0);
8455 btrfs_set_file_extent_compression(leaf, ei, 0);
8456 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8457 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8458
Chris Mason39279cc2007-06-12 06:35:45 -04008459 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008460 write_extent_buffer(leaf, symname, ptr, name_len);
8461 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008462 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008463
Chris Mason39279cc2007-06-12 06:35:45 -04008464 inode->i_op = &btrfs_symlink_inode_operations;
8465 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008466 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008467 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008468 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008469 err = btrfs_update_inode(trans, root, inode);
8470 if (err)
8471 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008472
8473out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008474 if (!err)
8475 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008476 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008477 if (drop_inode) {
8478 inode_dec_link_count(inode);
8479 iput(inode);
8480 }
Liu Bob53d3f52012-11-14 14:34:34 +00008481 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008482 return err;
8483}
Chris Mason16432982008-04-10 10:23:21 -04008484
Josef Bacik0af3d002010-06-21 14:48:16 -04008485static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8486 u64 start, u64 num_bytes, u64 min_size,
8487 loff_t actual_len, u64 *alloc_hint,
8488 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008489{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008490 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8491 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008492 struct btrfs_root *root = BTRFS_I(inode)->root;
8493 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008494 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008495 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008496 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008497 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008498 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008499
Josef Bacik0af3d002010-06-21 14:48:16 -04008500 if (trans)
8501 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008502 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008503 if (own_trans) {
8504 trans = btrfs_start_transaction(root, 3);
8505 if (IS_ERR(trans)) {
8506 ret = PTR_ERR(trans);
8507 break;
8508 }
Yan Zhengd899e052008-10-30 14:25:28 -04008509 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008510
Chris Mason154ea282013-03-05 11:11:26 -05008511 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8512 cur_bytes = max(cur_bytes, min_size);
8513 ret = btrfs_reserve_extent(trans, root, cur_bytes,
Zach Brown24542bf2012-11-16 00:04:43 +00008514 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008515 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008516 if (own_trans)
8517 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008518 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008519 }
8520
Yan Zhengd899e052008-10-30 14:25:28 -04008521 ret = insert_reserved_file_extent(trans, inode,
8522 cur_offset, ins.objectid,
8523 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008524 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008525 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008526 if (ret) {
8527 btrfs_abort_transaction(trans, root, ret);
8528 if (own_trans)
8529 btrfs_end_transaction(trans, root);
8530 break;
8531 }
Chris Masona1ed8352009-09-11 12:27:37 -04008532 btrfs_drop_extent_cache(inode, cur_offset,
8533 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008534
Josef Bacik5dc562c2012-08-17 13:14:17 -04008535 em = alloc_extent_map();
8536 if (!em) {
8537 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8538 &BTRFS_I(inode)->runtime_flags);
8539 goto next;
8540 }
8541
8542 em->start = cur_offset;
8543 em->orig_start = cur_offset;
8544 em->len = ins.offset;
8545 em->block_start = ins.objectid;
8546 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008547 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008548 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008549 em->bdev = root->fs_info->fs_devices->latest_bdev;
8550 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8551 em->generation = trans->transid;
8552
8553 while (1) {
8554 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008555 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008556 write_unlock(&em_tree->lock);
8557 if (ret != -EEXIST)
8558 break;
8559 btrfs_drop_extent_cache(inode, cur_offset,
8560 cur_offset + ins.offset - 1,
8561 0);
8562 }
8563 free_extent_map(em);
8564next:
Yan Zhengd899e052008-10-30 14:25:28 -04008565 num_bytes -= ins.offset;
8566 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008567 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008568
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008569 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008570 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008571 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008572 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008573 (actual_len > inode->i_size) &&
8574 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008575 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008576 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008577 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008578 i_size = cur_offset;
8579 i_size_write(inode, i_size);
8580 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008581 }
8582
Yan Zhengd899e052008-10-30 14:25:28 -04008583 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008584
8585 if (ret) {
8586 btrfs_abort_transaction(trans, root, ret);
8587 if (own_trans)
8588 btrfs_end_transaction(trans, root);
8589 break;
8590 }
Yan Zhengd899e052008-10-30 14:25:28 -04008591
Josef Bacik0af3d002010-06-21 14:48:16 -04008592 if (own_trans)
8593 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008594 }
Yan Zhengd899e052008-10-30 14:25:28 -04008595 return ret;
8596}
8597
Josef Bacik0af3d002010-06-21 14:48:16 -04008598int btrfs_prealloc_file_range(struct inode *inode, int mode,
8599 u64 start, u64 num_bytes, u64 min_size,
8600 loff_t actual_len, u64 *alloc_hint)
8601{
8602 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8603 min_size, actual_len, alloc_hint,
8604 NULL);
8605}
8606
8607int btrfs_prealloc_file_range_trans(struct inode *inode,
8608 struct btrfs_trans_handle *trans, int mode,
8609 u64 start, u64 num_bytes, u64 min_size,
8610 loff_t actual_len, u64 *alloc_hint)
8611{
8612 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8613 min_size, actual_len, alloc_hint, trans);
8614}
8615
Chris Masone6dcd2d2008-07-17 12:53:50 -04008616static int btrfs_set_page_dirty(struct page *page)
8617{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008618 return __set_page_dirty_nobuffers(page);
8619}
8620
Al Viro10556cb2011-06-20 19:28:19 -04008621static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008622{
Li Zefanb83cc962010-12-20 16:04:08 +08008623 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008624 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008625
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008626 if (mask & MAY_WRITE &&
8627 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8628 if (btrfs_root_readonly(root))
8629 return -EROFS;
8630 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8631 return -EACCES;
8632 }
Al Viro2830ba72011-06-20 19:16:29 -04008633 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008634}
Chris Mason39279cc2007-06-12 06:35:45 -04008635
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008636static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008637 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008638 .lookup = btrfs_lookup,
8639 .create = btrfs_create,
8640 .unlink = btrfs_unlink,
8641 .link = btrfs_link,
8642 .mkdir = btrfs_mkdir,
8643 .rmdir = btrfs_rmdir,
8644 .rename = btrfs_rename,
8645 .symlink = btrfs_symlink,
8646 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008647 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008648 .setxattr = btrfs_setxattr,
8649 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008650 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008651 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008652 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008653 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008654};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008655static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008656 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008657 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008658 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008659};
Yan, Zheng76dda932009-09-21 16:00:26 -04008660
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008661static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008662 .llseek = generic_file_llseek,
8663 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008664 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008665 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008666#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008667 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008668#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008669 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008670 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008671};
8672
Chris Masond1310b22008-01-24 16:13:08 -05008673static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008674 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008675 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008676 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008677 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008678 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008679 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008680 .set_bit_hook = btrfs_set_bit_hook,
8681 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008682 .merge_extent_hook = btrfs_merge_extent_hook,
8683 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008684};
8685
Chris Mason35054392009-01-21 13:11:13 -05008686/*
8687 * btrfs doesn't support the bmap operation because swapfiles
8688 * use bmap to make a mapping of extents in the file. They assume
8689 * these extents won't change over the life of the file and they
8690 * use the bmap result to do IO directly to the drive.
8691 *
8692 * the btrfs bmap call would return logical addresses that aren't
8693 * suitable for IO and they also will change frequently as COW
8694 * operations happen. So, swapfile + btrfs == corruption.
8695 *
8696 * For now we're avoiding this by dropping bmap.
8697 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008698static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008699 .readpage = btrfs_readpage,
8700 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008701 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008702 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008703 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008704 .invalidatepage = btrfs_invalidatepage,
8705 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008706 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008707 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008708};
8709
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008710static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008711 .readpage = btrfs_readpage,
8712 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008713 .invalidatepage = btrfs_invalidatepage,
8714 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008715};
8716
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008717static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008718 .getattr = btrfs_getattr,
8719 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008720 .setxattr = btrfs_setxattr,
8721 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008722 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008723 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008724 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008725 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008726 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008727 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008728};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008729static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008730 .getattr = btrfs_getattr,
8731 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008732 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008733 .setxattr = btrfs_setxattr,
8734 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008735 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008736 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008737 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008738 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008739};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008740static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008741 .readlink = generic_readlink,
8742 .follow_link = page_follow_link_light,
8743 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008744 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008745 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008746 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008747 .setxattr = btrfs_setxattr,
8748 .getxattr = btrfs_getxattr,
8749 .listxattr = btrfs_listxattr,
8750 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008751 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008752 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008753};
Yan, Zheng76dda932009-09-21 16:00:26 -04008754
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008755const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008756 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008757 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008758};