blob: b7f398c36cb7127f9f7f78018458e8db8b63f6fe [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>
Chris Mason4b4e25f2008-11-20 10:22:27 -050042#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040043#include "ctree.h"
44#include "disk-io.h"
45#include "transaction.h"
46#include "btrfs_inode.h"
47#include "ioctl.h"
48#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040049#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040050#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040051#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020052#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040053#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050054#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050055#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080056#include "inode-map.h"
Chris Mason39279cc2007-06-12 06:35:45 -040057
58struct btrfs_iget_args {
59 u64 ino;
60 struct btrfs_root *root;
61};
62
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070063static const struct inode_operations btrfs_dir_inode_operations;
64static const struct inode_operations btrfs_symlink_inode_operations;
65static const struct inode_operations btrfs_dir_ro_inode_operations;
66static const struct inode_operations btrfs_special_inode_operations;
67static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070068static const struct address_space_operations btrfs_aops;
69static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070070static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050071static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040072
73static struct kmem_cache *btrfs_inode_cachep;
74struct kmem_cache *btrfs_trans_handle_cachep;
75struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040076struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050077struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040078
79#define S_SHIFT 12
80static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
81 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
82 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
83 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
84 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
85 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
86 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
87 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
88};
89
Josef Bacika41ad392011-01-31 15:30:16 -050090static int btrfs_setsize(struct inode *inode, loff_t newsize);
91static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040092static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050093static noinline int cow_file_range(struct inode *inode,
94 struct page *locked_page,
95 u64 start, u64 end, int *page_started,
96 unsigned long *nr_written, int unlock);
Chris Mason21151332011-11-10 20:39:08 -050097static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
98 struct btrfs_root *root, struct inode *inode);
Josef Bacik7b128762008-07-24 12:17:14 -040099
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000100static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500101 struct inode *inode, struct inode *dir,
102 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500103{
104 int err;
105
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000106 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500107 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500108 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500109 return err;
110}
111
Chris Masond352ac62008-09-29 15:18:18 -0400112/*
Chris Masonc8b97812008-10-29 14:49:59 -0400113 * this does all the hard work for inserting an inline extent into
114 * the btree. The caller should have done a btrfs_drop_extents so that
115 * no overlapping inline items exist in the btree
116 */
Chris Masond3977122009-01-05 21:25:51 -0500117static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400118 struct btrfs_root *root, struct inode *inode,
119 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000120 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400121 struct page **compressed_pages)
122{
123 struct btrfs_key key;
124 struct btrfs_path *path;
125 struct extent_buffer *leaf;
126 struct page *page = NULL;
127 char *kaddr;
128 unsigned long ptr;
129 struct btrfs_file_extent_item *ei;
130 int err = 0;
131 int ret;
132 size_t cur_size = size;
133 size_t datasize;
134 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400135
Li Zefanfe3f5662011-03-28 08:30:38 +0000136 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400137 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400138
Chris Masond3977122009-01-05 21:25:51 -0500139 path = btrfs_alloc_path();
140 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400141 return -ENOMEM;
142
Chris Masonb9473432009-03-13 11:00:37 -0400143 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400144
Li Zefan33345d012011-04-20 10:31:50 +0800145 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400146 key.offset = start;
147 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400148 datasize = btrfs_file_extent_calc_inline_size(cur_size);
149
150 inode_add_bytes(inode, size);
151 ret = btrfs_insert_empty_item(trans, root, path, &key,
152 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400153 if (ret) {
154 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400155 goto fail;
156 }
157 leaf = path->nodes[0];
158 ei = btrfs_item_ptr(leaf, path->slots[0],
159 struct btrfs_file_extent_item);
160 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
161 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
162 btrfs_set_file_extent_encryption(leaf, ei, 0);
163 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
164 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
165 ptr = btrfs_file_extent_inline_start(ei);
166
Li Zefan261507a02010-12-17 14:21:50 +0800167 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400168 struct page *cpage;
169 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500170 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400171 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500172 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400173 PAGE_CACHE_SIZE);
174
Cong Wang7ac687d2011-11-25 23:14:28 +0800175 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400176 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800177 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400178
179 i++;
180 ptr += cur_size;
181 compressed_size -= cur_size;
182 }
183 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800184 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400185 } else {
186 page = find_get_page(inode->i_mapping,
187 start >> PAGE_CACHE_SHIFT);
188 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800189 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400190 offset = start & (PAGE_CACHE_SIZE - 1);
191 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800192 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400193 page_cache_release(page);
194 }
195 btrfs_mark_buffer_dirty(leaf);
196 btrfs_free_path(path);
197
Yan, Zhengc21677542009-11-12 09:34:21 +0000198 /*
199 * we're an inline extent, so nobody can
200 * extend the file past i_size without locking
201 * a page we already have locked.
202 *
203 * We must do any isize and inode updates
204 * before we unlock the pages. Otherwise we
205 * could end up racing with unlink.
206 */
Chris Masonc8b97812008-10-29 14:49:59 -0400207 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100208 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc21677542009-11-12 09:34:21 +0000209
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100210 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400211fail:
212 btrfs_free_path(path);
213 return err;
214}
215
216
217/*
218 * conditionally insert an inline extent into the file. This
219 * does the checks required to make sure the data is small enough
220 * to fit as an inline extent.
221 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400222static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400223 struct btrfs_root *root,
224 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000225 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400226 struct page **compressed_pages)
227{
228 u64 isize = i_size_read(inode);
229 u64 actual_end = min(end + 1, isize);
230 u64 inline_len = actual_end - start;
231 u64 aligned_end = (end + root->sectorsize - 1) &
232 ~((u64)root->sectorsize - 1);
233 u64 hint_byte;
234 u64 data_len = inline_len;
235 int ret;
236
237 if (compressed_size)
238 data_len = compressed_size;
239
240 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400241 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400242 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
243 (!compressed_size &&
244 (actual_end & (root->sectorsize - 1)) == 0) ||
245 end + 1 < isize ||
246 data_len > root->fs_info->max_inline) {
247 return 1;
248 }
249
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000250 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
Chris Masona1ed8352009-09-11 12:27:37 -0400251 &hint_byte, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100252 if (ret)
253 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400254
255 if (isize > actual_end)
256 inline_len = min_t(u64, isize, actual_end);
257 ret = insert_inline_extent(trans, root, inode, start,
258 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000259 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400260 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100261 btrfs_abort_transaction(trans, root, ret);
262 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400263 } else if (ret == -ENOSPC) {
264 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100265 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400266
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400267 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400268 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400269 return 0;
270}
271
Chris Mason771ed682008-11-06 22:02:51 -0500272struct async_extent {
273 u64 start;
274 u64 ram_size;
275 u64 compressed_size;
276 struct page **pages;
277 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800278 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500279 struct list_head list;
280};
281
282struct async_cow {
283 struct inode *inode;
284 struct btrfs_root *root;
285 struct page *locked_page;
286 u64 start;
287 u64 end;
288 struct list_head extents;
289 struct btrfs_work work;
290};
291
292static noinline int add_async_extent(struct async_cow *cow,
293 u64 start, u64 ram_size,
294 u64 compressed_size,
295 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800296 unsigned long nr_pages,
297 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500298{
299 struct async_extent *async_extent;
300
301 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100302 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500303 async_extent->start = start;
304 async_extent->ram_size = ram_size;
305 async_extent->compressed_size = compressed_size;
306 async_extent->pages = pages;
307 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800308 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500309 list_add_tail(&async_extent->list, &cow->extents);
310 return 0;
311}
312
Chris Masonc8b97812008-10-29 14:49:59 -0400313/*
Chris Mason771ed682008-11-06 22:02:51 -0500314 * we create compressed extents in two phases. The first
315 * phase compresses a range of pages that have already been
316 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400317 *
Chris Mason771ed682008-11-06 22:02:51 -0500318 * This is done inside an ordered work queue, and the compression
319 * is spread across many cpus. The actual IO submission is step
320 * two, and the ordered work queue takes care of making sure that
321 * happens in the same order things were put onto the queue by
322 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400323 *
Chris Mason771ed682008-11-06 22:02:51 -0500324 * If this code finds it can't get good compression, it puts an
325 * entry onto the work queue to write the uncompressed bytes. This
326 * makes sure that both compressed inodes and uncompressed inodes
327 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400328 */
Chris Mason771ed682008-11-06 22:02:51 -0500329static noinline int compress_file_range(struct inode *inode,
330 struct page *locked_page,
331 u64 start, u64 end,
332 struct async_cow *async_cow,
333 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400334{
335 struct btrfs_root *root = BTRFS_I(inode)->root;
336 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400337 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400338 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400339 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500340 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400341 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400342 struct page **pages = NULL;
343 unsigned long nr_pages;
344 unsigned long nr_pages_ret = 0;
345 unsigned long total_compressed = 0;
346 unsigned long total_in = 0;
347 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500348 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400349 int i;
350 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800351 int compress_type = root->fs_info->compress_type;
Chris Masonb888db22007-08-27 16:49:44 -0400352
Liu Bo4cb13e52012-03-29 09:57:45 -0400353 /* if this is a small write inside eof, kick off a defrag */
354 if ((end - start + 1) < 16 * 1024 &&
355 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400356 btrfs_add_inode_defrag(NULL, inode);
357
Chris Mason42dc7ba2008-12-15 11:44:56 -0500358 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400359again:
360 will_compress = 0;
361 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
362 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
363
Chris Masonf03d9302009-02-04 09:31:06 -0500364 /*
365 * we don't want to send crud past the end of i_size through
366 * compression, that's just a waste of CPU time. So, if the
367 * end of the file is before the start of our current
368 * requested range of bytes, we bail out to the uncompressed
369 * cleanup code that can deal with all of this.
370 *
371 * It isn't really the fastest way to fix things, but this is a
372 * very uncommon corner.
373 */
374 if (actual_end <= start)
375 goto cleanup_and_bail_uncompressed;
376
Chris Masonc8b97812008-10-29 14:49:59 -0400377 total_compressed = actual_end - start;
378
379 /* we want to make sure that amount of ram required to uncompress
380 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500381 * of a compressed extent to 128k. This is a crucial number
382 * because it also controls how easily we can spread reads across
383 * cpus for decompression.
384 *
385 * We also want to make sure the amount of IO required to do
386 * a random read is reasonably small, so we limit the size of
387 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400388 */
389 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400390 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500391 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400392 total_in = 0;
393 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400394
Chris Mason771ed682008-11-06 22:02:51 -0500395 /*
396 * we do compression for mount -o compress and when the
397 * inode has not been flagged as nocompress. This flag can
398 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400399 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200400 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500401 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000402 (BTRFS_I(inode)->force_compress) ||
403 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400404 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400405 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800406 if (!pages) {
407 /* just bail out to the uncompressed code */
408 goto cont;
409 }
Chris Mason179e29e2007-11-01 11:28:41 -0400410
Li Zefan261507a02010-12-17 14:21:50 +0800411 if (BTRFS_I(inode)->force_compress)
412 compress_type = BTRFS_I(inode)->force_compress;
413
414 ret = btrfs_compress_pages(compress_type,
415 inode->i_mapping, start,
416 total_compressed, pages,
417 nr_pages, &nr_pages_ret,
418 &total_in,
419 &total_compressed,
420 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400421
422 if (!ret) {
423 unsigned long offset = total_compressed &
424 (PAGE_CACHE_SIZE - 1);
425 struct page *page = pages[nr_pages_ret - 1];
426 char *kaddr;
427
428 /* zero the tail end of the last page, we might be
429 * sending it down to disk
430 */
431 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800432 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400433 memset(kaddr + offset, 0,
434 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800435 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400436 }
437 will_compress = 1;
438 }
439 }
Li Zefan560f7d72011-09-08 10:22:01 +0800440cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400441 if (start == 0) {
Josef Bacik7a7eaa402011-04-13 12:54:33 -0400442 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100443 if (IS_ERR(trans)) {
444 ret = PTR_ERR(trans);
445 trans = NULL;
446 goto cleanup_and_out;
447 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400448 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500449
Chris Masonc8b97812008-10-29 14:49:59 -0400450 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500451 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400452 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500453 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400454 */
455 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000456 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400457 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500458 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400459 ret = cow_file_range_inline(trans, root, inode,
460 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000461 total_compressed,
462 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400463 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100464 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500465 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100466 * inline extent creation worked or returned error,
467 * we don't need to create any more async work items.
468 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500469 */
Chris Masonc8b97812008-10-29 14:49:59 -0400470 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400471 &BTRFS_I(inode)->io_tree,
472 start, end, NULL,
473 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400474 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400475 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc21677542009-11-12 09:34:21 +0000476
477 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400478 goto free_pages_out;
479 }
Yan, Zhengc21677542009-11-12 09:34:21 +0000480 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400481 }
482
483 if (will_compress) {
484 /*
485 * we aren't doing an inline extent round the compressed size
486 * up to a block size boundary so the allocator does sane
487 * things
488 */
489 total_compressed = (total_compressed + blocksize - 1) &
490 ~(blocksize - 1);
491
492 /*
493 * one last check to make sure the compression is really a
494 * win, compare the page count read with the blocks on disk
495 */
496 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
497 ~(PAGE_CACHE_SIZE - 1);
498 if (total_compressed >= total_in) {
499 will_compress = 0;
500 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400501 num_bytes = total_in;
502 }
503 }
504 if (!will_compress && pages) {
505 /*
506 * the compression code ran but failed to make things smaller,
507 * free any pages it allocated and our page pointer array
508 */
509 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400510 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400511 page_cache_release(pages[i]);
512 }
513 kfree(pages);
514 pages = NULL;
515 total_compressed = 0;
516 nr_pages_ret = 0;
517
518 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500519 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
520 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500521 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500522 }
Chris Masonc8b97812008-10-29 14:49:59 -0400523 }
Chris Mason771ed682008-11-06 22:02:51 -0500524 if (will_compress) {
525 *num_added += 1;
526
527 /* the async work queues will take care of doing actual
528 * allocation on disk for these compressed pages,
529 * and will submit them to the elevator.
530 */
531 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800532 total_compressed, pages, nr_pages_ret,
533 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500534
Yan, Zheng24ae6362010-12-06 07:02:36 +0000535 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500536 start += num_bytes;
537 pages = NULL;
538 cond_resched();
539 goto again;
540 }
541 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500542cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500543 /*
544 * No compression, but we still need to write the pages in
545 * the file we've been given so far. redirty the locked
546 * page if it corresponds to our extent and set things up
547 * for the async work queue to run cow_file_range to do
548 * the normal delalloc dance
549 */
550 if (page_offset(locked_page) >= start &&
551 page_offset(locked_page) <= end) {
552 __set_page_dirty_nobuffers(locked_page);
553 /* unlocked later on in the async handlers */
554 }
Li Zefan261507a02010-12-17 14:21:50 +0800555 add_async_extent(async_cow, start, end - start + 1,
556 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500557 *num_added += 1;
558 }
559
560out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100561 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500562
563free_pages_out:
564 for (i = 0; i < nr_pages_ret; i++) {
565 WARN_ON(pages[i]->mapping);
566 page_cache_release(pages[i]);
567 }
Chris Masond3977122009-01-05 21:25:51 -0500568 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500569
570 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100571
572cleanup_and_out:
573 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
574 start, end, NULL,
575 EXTENT_CLEAR_UNLOCK_PAGE |
576 EXTENT_CLEAR_DIRTY |
577 EXTENT_CLEAR_DELALLOC |
578 EXTENT_SET_WRITEBACK |
579 EXTENT_END_WRITEBACK);
580 if (!trans || IS_ERR(trans))
581 btrfs_error(root->fs_info, ret, "Failed to join transaction");
582 else
583 btrfs_abort_transaction(trans, root, ret);
584 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500585}
586
587/*
588 * phase two of compressed writeback. This is the ordered portion
589 * of the code, which only gets called in the order the work was
590 * queued. We walk all the async extents created by compress_file_range
591 * and send them down to the disk.
592 */
593static noinline int submit_compressed_extents(struct inode *inode,
594 struct async_cow *async_cow)
595{
596 struct async_extent *async_extent;
597 u64 alloc_hint = 0;
598 struct btrfs_trans_handle *trans;
599 struct btrfs_key ins;
600 struct extent_map *em;
601 struct btrfs_root *root = BTRFS_I(inode)->root;
602 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
603 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500604 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500605
606 if (list_empty(&async_cow->extents))
607 return 0;
608
Chris Mason771ed682008-11-06 22:02:51 -0500609
Chris Masond3977122009-01-05 21:25:51 -0500610 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500611 async_extent = list_entry(async_cow->extents.next,
612 struct async_extent, list);
613 list_del(&async_extent->list);
614
615 io_tree = &BTRFS_I(inode)->io_tree;
616
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500617retry:
Chris Mason771ed682008-11-06 22:02:51 -0500618 /* did the compression code fall back to uncompressed IO? */
619 if (!async_extent->pages) {
620 int page_started = 0;
621 unsigned long nr_written = 0;
622
623 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000624 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100625 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500626
627 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500628 ret = cow_file_range(inode, async_cow->locked_page,
629 async_extent->start,
630 async_extent->start +
631 async_extent->ram_size - 1,
632 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500633
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100634 /* JDM XXX */
635
Chris Mason771ed682008-11-06 22:02:51 -0500636 /*
637 * if page_started, cow_file_range inserted an
638 * inline extent and took care of all the unlocking
639 * and IO for us. Otherwise, we need to submit
640 * all those pages down to the drive.
641 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500642 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500643 extent_write_locked_range(io_tree,
644 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500645 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500646 async_extent->ram_size - 1,
647 btrfs_get_extent,
648 WB_SYNC_ALL);
649 kfree(async_extent);
650 cond_resched();
651 continue;
652 }
653
654 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100655 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500656
Josef Bacik7a7eaa402011-04-13 12:54:33 -0400657 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100658 if (IS_ERR(trans)) {
659 ret = PTR_ERR(trans);
660 } else {
661 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
662 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500663 async_extent->compressed_size,
664 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500665 0, alloc_hint, &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100666 if (ret)
667 btrfs_abort_transaction(trans, root, ret);
668 btrfs_end_transaction(trans, root);
669 }
Yan, Zhengc21677542009-11-12 09:34:21 +0000670
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500671 if (ret) {
672 int i;
673 for (i = 0; i < async_extent->nr_pages; i++) {
674 WARN_ON(async_extent->pages[i]->mapping);
675 page_cache_release(async_extent->pages[i]);
676 }
677 kfree(async_extent->pages);
678 async_extent->nr_pages = 0;
679 async_extent->pages = NULL;
680 unlock_extent(io_tree, async_extent->start,
681 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100682 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100683 if (ret == -ENOSPC)
684 goto retry;
685 goto out_free; /* JDM: Requeue? */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500686 }
687
Yan, Zhengc21677542009-11-12 09:34:21 +0000688 /*
689 * here we're doing allocation and writeback of the
690 * compressed pages
691 */
692 btrfs_drop_extent_cache(inode, async_extent->start,
693 async_extent->start +
694 async_extent->ram_size - 1, 0);
695
David Sterba172ddd62011-04-21 00:48:27 +0200696 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100697 BUG_ON(!em); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500698 em->start = async_extent->start;
699 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500700 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500701
702 em->block_start = ins.objectid;
703 em->block_len = ins.offset;
704 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800705 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500706 set_bit(EXTENT_FLAG_PINNED, &em->flags);
707 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
708
Chris Masond3977122009-01-05 21:25:51 -0500709 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400710 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500711 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400712 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500713 if (ret != -EEXIST) {
714 free_extent_map(em);
715 break;
716 }
717 btrfs_drop_extent_cache(inode, async_extent->start,
718 async_extent->start +
719 async_extent->ram_size - 1, 0);
720 }
721
Li Zefan261507a02010-12-17 14:21:50 +0800722 ret = btrfs_add_ordered_extent_compress(inode,
723 async_extent->start,
724 ins.objectid,
725 async_extent->ram_size,
726 ins.offset,
727 BTRFS_ORDERED_COMPRESSED,
728 async_extent->compress_type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100729 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500730
Chris Mason771ed682008-11-06 22:02:51 -0500731 /*
732 * clear dirty, set writeback and unlock the pages.
733 */
734 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400735 &BTRFS_I(inode)->io_tree,
736 async_extent->start,
737 async_extent->start +
738 async_extent->ram_size - 1,
739 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
740 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400741 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400742 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500743
744 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500745 async_extent->start,
746 async_extent->ram_size,
747 ins.objectid,
748 ins.offset, async_extent->pages,
749 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500750
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100751 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500752 alloc_hint = ins.objectid + ins.offset;
753 kfree(async_extent);
754 cond_resched();
755 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100756 ret = 0;
757out:
758 return ret;
759out_free:
760 kfree(async_extent);
761 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500762}
763
Josef Bacik4b46fce2010-05-23 11:00:55 -0400764static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
765 u64 num_bytes)
766{
767 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
768 struct extent_map *em;
769 u64 alloc_hint = 0;
770
771 read_lock(&em_tree->lock);
772 em = search_extent_mapping(em_tree, start, num_bytes);
773 if (em) {
774 /*
775 * if block start isn't an actual block number then find the
776 * first block in this inode and use that as a hint. If that
777 * block is also bogus then just don't worry about it.
778 */
779 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
780 free_extent_map(em);
781 em = search_extent_mapping(em_tree, 0, 0);
782 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
783 alloc_hint = em->block_start;
784 if (em)
785 free_extent_map(em);
786 } else {
787 alloc_hint = em->block_start;
788 free_extent_map(em);
789 }
790 }
791 read_unlock(&em_tree->lock);
792
793 return alloc_hint;
794}
795
Chris Mason771ed682008-11-06 22:02:51 -0500796/*
797 * when extent_io.c finds a delayed allocation range in the file,
798 * the call backs end up in this code. The basic idea is to
799 * allocate extents on disk for the range, and create ordered data structs
800 * in ram to track those extents.
801 *
802 * locked_page is the page that writepage had locked already. We use
803 * it to make sure we don't do extra locks or unlocks.
804 *
805 * *page_started is set to one if we unlock locked_page and do everything
806 * required to start IO on it. It may be clean and already done with
807 * IO when we return.
808 */
809static noinline int cow_file_range(struct inode *inode,
810 struct page *locked_page,
811 u64 start, u64 end, int *page_started,
812 unsigned long *nr_written,
813 int unlock)
814{
815 struct btrfs_root *root = BTRFS_I(inode)->root;
816 struct btrfs_trans_handle *trans;
817 u64 alloc_hint = 0;
818 u64 num_bytes;
819 unsigned long ram_size;
820 u64 disk_num_bytes;
821 u64 cur_alloc_size;
822 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500823 struct btrfs_key ins;
824 struct extent_map *em;
825 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
826 int ret = 0;
827
Chris Mason2cf85722011-07-26 15:35:09 -0400828 BUG_ON(btrfs_is_free_space_inode(root, inode));
Josef Bacik7a7eaa402011-04-13 12:54:33 -0400829 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100830 if (IS_ERR(trans)) {
831 extent_clear_unlock_delalloc(inode,
832 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400833 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100834 EXTENT_CLEAR_UNLOCK_PAGE |
835 EXTENT_CLEAR_UNLOCK |
836 EXTENT_CLEAR_DELALLOC |
837 EXTENT_CLEAR_DIRTY |
838 EXTENT_SET_WRITEBACK |
839 EXTENT_END_WRITEBACK);
840 return PTR_ERR(trans);
841 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400842 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500843
Chris Mason771ed682008-11-06 22:02:51 -0500844 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
845 num_bytes = max(blocksize, num_bytes);
846 disk_num_bytes = num_bytes;
847 ret = 0;
848
Chris Mason4cb53002011-05-24 15:35:30 -0400849 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400850 if (num_bytes < 64 * 1024 &&
851 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400852 btrfs_add_inode_defrag(trans, inode);
853
Chris Mason771ed682008-11-06 22:02:51 -0500854 if (start == 0) {
855 /* lets try to make an inline extent */
856 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000857 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500858 if (ret == 0) {
859 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400860 &BTRFS_I(inode)->io_tree,
861 start, end, NULL,
862 EXTENT_CLEAR_UNLOCK_PAGE |
863 EXTENT_CLEAR_UNLOCK |
864 EXTENT_CLEAR_DELALLOC |
865 EXTENT_CLEAR_DIRTY |
866 EXTENT_SET_WRITEBACK |
867 EXTENT_END_WRITEBACK);
Yan, Zhengc21677542009-11-12 09:34:21 +0000868
Chris Mason771ed682008-11-06 22:02:51 -0500869 *nr_written = *nr_written +
870 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
871 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500872 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100873 } else if (ret < 0) {
874 btrfs_abort_transaction(trans, root, ret);
875 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500876 }
877 }
Chris Masonc8b97812008-10-29 14:49:59 -0400878
879 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200880 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400881
Josef Bacik4b46fce2010-05-23 11:00:55 -0400882 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400883 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400884
Chris Masond3977122009-01-05 21:25:51 -0500885 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400886 unsigned long op;
887
Josef Bacik287a0ab2010-03-19 18:07:23 +0000888 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400889 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500890 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500891 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100892 if (ret < 0) {
893 btrfs_abort_transaction(trans, root, ret);
894 goto out_unlock;
895 }
Chris Masond3977122009-01-05 21:25:51 -0500896
David Sterba172ddd62011-04-21 00:48:27 +0200897 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100898 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400899 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500900 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500901 ram_size = ins.offset;
902 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400903
Chris Masone6dcd2d2008-07-17 12:53:50 -0400904 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400905 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400906 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400907 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400908
Chris Masond3977122009-01-05 21:25:51 -0500909 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400910 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400911 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400912 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400913 if (ret != -EEXIST) {
914 free_extent_map(em);
915 break;
916 }
917 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400918 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400919 }
920
Chris Mason98d20f62008-04-14 09:46:10 -0400921 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400922 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500923 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100924 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -0400925
Yan Zheng17d217f2008-12-12 10:03:38 -0500926 if (root->root_key.objectid ==
927 BTRFS_DATA_RELOC_TREE_OBJECTID) {
928 ret = btrfs_reloc_clone_csums(inode, start,
929 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100930 if (ret) {
931 btrfs_abort_transaction(trans, root, ret);
932 goto out_unlock;
933 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500934 }
935
Chris Masond3977122009-01-05 21:25:51 -0500936 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400937 break;
Chris Masond3977122009-01-05 21:25:51 -0500938
Chris Masonc8b97812008-10-29 14:49:59 -0400939 /* we're not doing compressed IO, don't unlock the first
940 * page (which the caller expects to stay locked), don't
941 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400942 *
943 * Do set the Private2 bit so we know this page was properly
944 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400945 */
Chris Masona791e352009-10-08 11:27:10 -0400946 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
947 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
948 EXTENT_SET_PRIVATE2;
949
Chris Masonc8b97812008-10-29 14:49:59 -0400950 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
951 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400952 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400953 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500954 num_bytes -= cur_alloc_size;
955 alloc_hint = ins.objectid + ins.offset;
956 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400957 }
Chris Mason771ed682008-11-06 22:02:51 -0500958 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100959out:
Chris Masonb888db22007-08-27 16:49:44 -0400960 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400961
Chris Masonbe20aa92007-12-17 20:14:01 -0500962 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100963out_unlock:
964 extent_clear_unlock_delalloc(inode,
965 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400966 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100967 EXTENT_CLEAR_UNLOCK_PAGE |
968 EXTENT_CLEAR_UNLOCK |
969 EXTENT_CLEAR_DELALLOC |
970 EXTENT_CLEAR_DIRTY |
971 EXTENT_SET_WRITEBACK |
972 EXTENT_END_WRITEBACK);
973
974 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500975}
Chris Masonc8b97812008-10-29 14:49:59 -0400976
Chris Mason771ed682008-11-06 22:02:51 -0500977/*
978 * work queue call back to started compression on a file and pages
979 */
980static noinline void async_cow_start(struct btrfs_work *work)
981{
982 struct async_cow *async_cow;
983 int num_added = 0;
984 async_cow = container_of(work, struct async_cow, work);
985
986 compress_file_range(async_cow->inode, async_cow->locked_page,
987 async_cow->start, async_cow->end, async_cow,
988 &num_added);
989 if (num_added == 0)
990 async_cow->inode = NULL;
991}
992
993/*
994 * work queue call back to submit previously compressed pages
995 */
996static noinline void async_cow_submit(struct btrfs_work *work)
997{
998 struct async_cow *async_cow;
999 struct btrfs_root *root;
1000 unsigned long nr_pages;
1001
1002 async_cow = container_of(work, struct async_cow, work);
1003
1004 root = async_cow->root;
1005 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1006 PAGE_CACHE_SHIFT;
1007
1008 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
1009
1010 if (atomic_read(&root->fs_info->async_delalloc_pages) <
1011 5 * 1042 * 1024 &&
1012 waitqueue_active(&root->fs_info->async_submit_wait))
1013 wake_up(&root->fs_info->async_submit_wait);
1014
Chris Masond3977122009-01-05 21:25:51 -05001015 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001016 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001017}
Chris Masonc8b97812008-10-29 14:49:59 -04001018
Chris Mason771ed682008-11-06 22:02:51 -05001019static noinline void async_cow_free(struct btrfs_work *work)
1020{
1021 struct async_cow *async_cow;
1022 async_cow = container_of(work, struct async_cow, work);
1023 kfree(async_cow);
1024}
1025
1026static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1027 u64 start, u64 end, int *page_started,
1028 unsigned long *nr_written)
1029{
1030 struct async_cow *async_cow;
1031 struct btrfs_root *root = BTRFS_I(inode)->root;
1032 unsigned long nr_pages;
1033 u64 cur_end;
1034 int limit = 10 * 1024 * 1042;
1035
Chris Masona3429ab2009-10-08 12:30:20 -04001036 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1037 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001038 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001039 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001040 BUG_ON(!async_cow); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001041 async_cow->inode = inode;
1042 async_cow->root = root;
1043 async_cow->locked_page = locked_page;
1044 async_cow->start = start;
1045
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001046 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001047 cur_end = end;
1048 else
1049 cur_end = min(end, start + 512 * 1024 - 1);
1050
1051 async_cow->end = cur_end;
1052 INIT_LIST_HEAD(&async_cow->extents);
1053
1054 async_cow->work.func = async_cow_start;
1055 async_cow->work.ordered_func = async_cow_submit;
1056 async_cow->work.ordered_free = async_cow_free;
1057 async_cow->work.flags = 0;
1058
Chris Mason771ed682008-11-06 22:02:51 -05001059 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1060 PAGE_CACHE_SHIFT;
1061 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1062
1063 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1064 &async_cow->work);
1065
1066 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1067 wait_event(root->fs_info->async_submit_wait,
1068 (atomic_read(&root->fs_info->async_delalloc_pages) <
1069 limit));
1070 }
1071
Chris Masond3977122009-01-05 21:25:51 -05001072 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001073 atomic_read(&root->fs_info->async_delalloc_pages)) {
1074 wait_event(root->fs_info->async_submit_wait,
1075 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1076 0));
1077 }
1078
1079 *nr_written += nr_pages;
1080 start = cur_end + 1;
1081 }
1082 *page_started = 1;
1083 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001084}
1085
Chris Masond3977122009-01-05 21:25:51 -05001086static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001087 u64 bytenr, u64 num_bytes)
1088{
1089 int ret;
1090 struct btrfs_ordered_sum *sums;
1091 LIST_HEAD(list);
1092
Yan Zheng07d400a2009-01-06 11:42:00 -05001093 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001094 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001095 if (ret == 0 && list_empty(&list))
1096 return 0;
1097
1098 while (!list_empty(&list)) {
1099 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1100 list_del(&sums->list);
1101 kfree(sums);
1102 }
1103 return 1;
1104}
1105
Chris Masond352ac62008-09-29 15:18:18 -04001106/*
1107 * when nowcow writeback call back. This checks for snapshots or COW copies
1108 * of the extents that exist in the file, and COWs the file as required.
1109 *
1110 * If no cow copies or snapshots exist, we write directly to the existing
1111 * blocks on disk
1112 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001113static noinline int run_delalloc_nocow(struct inode *inode,
1114 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001115 u64 start, u64 end, int *page_started, int force,
1116 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001117{
Chris Masonbe20aa92007-12-17 20:14:01 -05001118 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001119 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001120 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001121 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001122 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001123 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001124 u64 cow_start;
1125 u64 cur_offset;
1126 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001127 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001128 u64 disk_bytenr;
1129 u64 num_bytes;
1130 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001131 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001132 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001133 int nocow;
1134 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001135 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001136 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001137
1138 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001139 if (!path) {
1140 extent_clear_unlock_delalloc(inode,
1141 &BTRFS_I(inode)->io_tree,
1142 start, end, locked_page,
1143 EXTENT_CLEAR_UNLOCK_PAGE |
1144 EXTENT_CLEAR_UNLOCK |
1145 EXTENT_CLEAR_DELALLOC |
1146 EXTENT_CLEAR_DIRTY |
1147 EXTENT_SET_WRITEBACK |
1148 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001149 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001150 }
Li Zefan82d59022011-04-20 10:33:24 +08001151
Chris Mason2cf85722011-07-26 15:35:09 -04001152 nolock = btrfs_is_free_space_inode(root, inode);
Li Zefan82d59022011-04-20 10:33:24 +08001153
1154 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001155 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001156 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001157 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001158
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001159 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001160 extent_clear_unlock_delalloc(inode,
1161 &BTRFS_I(inode)->io_tree,
1162 start, end, locked_page,
1163 EXTENT_CLEAR_UNLOCK_PAGE |
1164 EXTENT_CLEAR_UNLOCK |
1165 EXTENT_CLEAR_DELALLOC |
1166 EXTENT_CLEAR_DIRTY |
1167 EXTENT_SET_WRITEBACK |
1168 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001169 btrfs_free_path(path);
1170 return PTR_ERR(trans);
1171 }
1172
Josef Bacik74b21072011-04-13 12:02:53 -04001173 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001174
Yan Zheng80ff3852008-10-30 14:20:02 -04001175 cow_start = (u64)-1;
1176 cur_offset = start;
1177 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001178 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001179 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001180 if (ret < 0) {
1181 btrfs_abort_transaction(trans, root, ret);
1182 goto error;
1183 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001184 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1185 leaf = path->nodes[0];
1186 btrfs_item_key_to_cpu(leaf, &found_key,
1187 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001188 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001189 found_key.type == BTRFS_EXTENT_DATA_KEY)
1190 path->slots[0]--;
1191 }
1192 check_prev = 0;
1193next_slot:
1194 leaf = path->nodes[0];
1195 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1196 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001197 if (ret < 0) {
1198 btrfs_abort_transaction(trans, root, ret);
1199 goto error;
1200 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001201 if (ret > 0)
1202 break;
1203 leaf = path->nodes[0];
1204 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001205
Yan Zheng80ff3852008-10-30 14:20:02 -04001206 nocow = 0;
1207 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001208 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001209 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001210
Li Zefan33345d012011-04-20 10:31:50 +08001211 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001212 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1213 found_key.offset > end)
1214 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001215
Yan Zheng80ff3852008-10-30 14:20:02 -04001216 if (found_key.offset > cur_offset) {
1217 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001218 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001219 goto out_check;
1220 }
Chris Masonc31f8832008-01-08 15:46:31 -05001221
Yan Zheng80ff3852008-10-30 14:20:02 -04001222 fi = btrfs_item_ptr(leaf, path->slots[0],
1223 struct btrfs_file_extent_item);
1224 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001225
Yan Zhengd899e052008-10-30 14:25:28 -04001226 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1227 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001228 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001229 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001230 extent_end = found_key.offset +
1231 btrfs_file_extent_num_bytes(leaf, fi);
1232 if (extent_end <= start) {
1233 path->slots[0]++;
1234 goto next_slot;
1235 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001236 if (disk_bytenr == 0)
1237 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001238 if (btrfs_file_extent_compression(leaf, fi) ||
1239 btrfs_file_extent_encryption(leaf, fi) ||
1240 btrfs_file_extent_other_encoding(leaf, fi))
1241 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001242 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1243 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001244 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001245 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001246 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001247 found_key.offset -
1248 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001249 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001250 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001251 disk_bytenr += cur_offset - found_key.offset;
1252 num_bytes = min(end + 1, extent_end) - cur_offset;
1253 /*
1254 * force cow if csum exists in the range.
1255 * this ensure that csum for a given extent are
1256 * either valid or do not exist.
1257 */
1258 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1259 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001260 nocow = 1;
1261 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1262 extent_end = found_key.offset +
1263 btrfs_file_extent_inline_len(leaf, fi);
1264 extent_end = ALIGN(extent_end, root->sectorsize);
1265 } else {
1266 BUG_ON(1);
1267 }
1268out_check:
1269 if (extent_end <= start) {
1270 path->slots[0]++;
1271 goto next_slot;
1272 }
1273 if (!nocow) {
1274 if (cow_start == (u64)-1)
1275 cow_start = cur_offset;
1276 cur_offset = extent_end;
1277 if (cur_offset > end)
1278 break;
1279 path->slots[0]++;
1280 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001281 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001282
David Sterbab3b4aa72011-04-21 01:20:15 +02001283 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001284 if (cow_start != (u64)-1) {
1285 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001286 found_key.offset - 1, page_started,
1287 nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001288 if (ret) {
1289 btrfs_abort_transaction(trans, root, ret);
1290 goto error;
1291 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001292 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001293 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001294
Yan Zhengd899e052008-10-30 14:25:28 -04001295 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1296 struct extent_map *em;
1297 struct extent_map_tree *em_tree;
1298 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001299 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001300 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001301 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001302 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001303 em->len = num_bytes;
1304 em->block_len = num_bytes;
1305 em->block_start = disk_bytenr;
1306 em->bdev = root->fs_info->fs_devices->latest_bdev;
1307 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1308 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001309 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001310 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001311 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001312 if (ret != -EEXIST) {
1313 free_extent_map(em);
1314 break;
1315 }
1316 btrfs_drop_extent_cache(inode, em->start,
1317 em->start + em->len - 1, 0);
1318 }
1319 type = BTRFS_ORDERED_PREALLOC;
1320 } else {
1321 type = BTRFS_ORDERED_NOCOW;
1322 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001323
1324 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001325 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001326 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001327
Yan, Zhengefa56462010-05-16 10:49:59 -04001328 if (root->root_key.objectid ==
1329 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1330 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1331 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001332 if (ret) {
1333 btrfs_abort_transaction(trans, root, ret);
1334 goto error;
1335 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001336 }
1337
Yan Zhengd899e052008-10-30 14:25:28 -04001338 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001339 cur_offset, cur_offset + num_bytes - 1,
1340 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1341 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1342 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001343 cur_offset = extent_end;
1344 if (cur_offset > end)
1345 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001346 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001347 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001348
Josef Bacik17ca04a2012-05-31 15:58:55 -04001349 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001350 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001351 cur_offset = end;
1352 }
1353
Yan Zheng80ff3852008-10-30 14:20:02 -04001354 if (cow_start != (u64)-1) {
1355 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001356 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001357 if (ret) {
1358 btrfs_abort_transaction(trans, root, ret);
1359 goto error;
1360 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001361 }
1362
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001363error:
Josef Bacik0cb59c92010-07-02 12:14:14 -04001364 if (nolock) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001365 err = btrfs_end_transaction_nolock(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001366 } else {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001367 err = btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001368 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001369 if (!ret)
1370 ret = err;
1371
Josef Bacik17ca04a2012-05-31 15:58:55 -04001372 if (ret && cur_offset < end)
1373 extent_clear_unlock_delalloc(inode,
1374 &BTRFS_I(inode)->io_tree,
1375 cur_offset, end, locked_page,
1376 EXTENT_CLEAR_UNLOCK_PAGE |
1377 EXTENT_CLEAR_UNLOCK |
1378 EXTENT_CLEAR_DELALLOC |
1379 EXTENT_CLEAR_DIRTY |
1380 EXTENT_SET_WRITEBACK |
1381 EXTENT_END_WRITEBACK);
1382
Yan Zheng7ea394f2008-08-05 13:05:02 -04001383 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001384 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001385}
1386
Chris Masond352ac62008-09-29 15:18:18 -04001387/*
1388 * extent_io.c call back to do delayed allocation processing
1389 */
Chris Masonc8b97812008-10-29 14:49:59 -04001390static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001391 u64 start, u64 end, int *page_started,
1392 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001393{
Chris Masonbe20aa92007-12-17 20:14:01 -05001394 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001395 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001396
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001397 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001398 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001399 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001400 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001401 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001402 page_started, 0, nr_written);
Chris Mason1e701a32010-03-11 09:42:04 -05001403 else if (!btrfs_test_opt(root, COMPRESS) &&
Liu Bo75e7cb72011-03-22 10:12:20 +00001404 !(BTRFS_I(inode)->force_compress) &&
1405 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))
Chris Mason7f366cf2009-03-12 20:12:45 -04001406 ret = cow_file_range(inode, locked_page, start, end,
1407 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001408 else
Chris Mason771ed682008-11-06 22:02:51 -05001409 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001410 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001411 return ret;
1412}
1413
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001414static void btrfs_split_extent_hook(struct inode *inode,
1415 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001416{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001417 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001418 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001419 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001420
Josef Bacik9e0baf62011-07-15 15:16:44 +00001421 spin_lock(&BTRFS_I(inode)->lock);
1422 BTRFS_I(inode)->outstanding_extents++;
1423 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001424}
1425
1426/*
1427 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1428 * extents so we can keep track of new extents that are just merged onto old
1429 * extents, such as when we are doing sequential writes, so we can properly
1430 * account for the metadata space we'll need.
1431 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001432static void btrfs_merge_extent_hook(struct inode *inode,
1433 struct extent_state *new,
1434 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001435{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001436 /* not delalloc, ignore it */
1437 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001438 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001439
Josef Bacik9e0baf62011-07-15 15:16:44 +00001440 spin_lock(&BTRFS_I(inode)->lock);
1441 BTRFS_I(inode)->outstanding_extents--;
1442 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001443}
1444
Chris Masond352ac62008-09-29 15:18:18 -04001445/*
1446 * extent_io.c set_bit_hook, used to track delayed allocation
1447 * bytes in this file, and to maintain the list of inodes that
1448 * have pending delalloc work to be done.
1449 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001450static void btrfs_set_bit_hook(struct inode *inode,
1451 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001452{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001453
Chris Mason75eff682008-12-15 15:54:40 -05001454 /*
1455 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001456 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001457 * bit, which is only set or cleared with irqs on
1458 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001459 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001460 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001461 u64 len = state->end + 1 - state->start;
Chris Mason2cf85722011-07-26 15:35:09 -04001462 bool do_list = !btrfs_is_free_space_inode(root, inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001463
Josef Bacik9e0baf62011-07-15 15:16:44 +00001464 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001465 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001466 } else {
1467 spin_lock(&BTRFS_I(inode)->lock);
1468 BTRFS_I(inode)->outstanding_extents++;
1469 spin_unlock(&BTRFS_I(inode)->lock);
1470 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001471
Chris Mason75eff682008-12-15 15:54:40 -05001472 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001473 BTRFS_I(inode)->delalloc_bytes += len;
1474 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001475 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001476 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1477 &root->fs_info->delalloc_inodes);
1478 }
Chris Mason75eff682008-12-15 15:54:40 -05001479 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001480 }
Chris Mason291d6732008-01-29 15:55:23 -05001481}
1482
Chris Masond352ac62008-09-29 15:18:18 -04001483/*
1484 * extent_io.c clear_bit_hook, see set_bit_hook for why
1485 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001486static void btrfs_clear_bit_hook(struct inode *inode,
1487 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001488{
Chris Mason75eff682008-12-15 15:54:40 -05001489 /*
1490 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001491 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001492 * bit, which is only set or cleared with irqs on
1493 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001494 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001495 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001496 u64 len = state->end + 1 - state->start;
Chris Mason2cf85722011-07-26 15:35:09 -04001497 bool do_list = !btrfs_is_free_space_inode(root, inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001498
Josef Bacik9e0baf62011-07-15 15:16:44 +00001499 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001500 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001501 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1502 spin_lock(&BTRFS_I(inode)->lock);
1503 BTRFS_I(inode)->outstanding_extents--;
1504 spin_unlock(&BTRFS_I(inode)->lock);
1505 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001506
1507 if (*bits & EXTENT_DO_ACCOUNTING)
1508 btrfs_delalloc_release_metadata(inode, len);
1509
Josef Bacik0cb59c92010-07-02 12:14:14 -04001510 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1511 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001512 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001513
Chris Mason75eff682008-12-15 15:54:40 -05001514 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001515 root->fs_info->delalloc_bytes -= len;
1516 BTRFS_I(inode)->delalloc_bytes -= len;
1517
Josef Bacik0cb59c92010-07-02 12:14:14 -04001518 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001519 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1520 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1521 }
Chris Mason75eff682008-12-15 15:54:40 -05001522 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001523 }
Chris Mason291d6732008-01-29 15:55:23 -05001524}
1525
Chris Masond352ac62008-09-29 15:18:18 -04001526/*
1527 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1528 * we don't create bios that span stripes or chunks
1529 */
Chris Mason239b14b2008-03-24 15:02:07 -04001530int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001531 size_t size, struct bio *bio,
1532 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001533{
1534 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1535 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001536 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001537 u64 length = 0;
1538 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001539 int ret;
1540
Chris Mason771ed682008-11-06 22:02:51 -05001541 if (bio_flags & EXTENT_BIO_COMPRESSED)
1542 return 0;
1543
Chris Masonf2d8d742008-04-21 10:03:05 -04001544 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001545 map_tree = &root->fs_info->mapping_tree;
1546 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001547 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001548 &map_length, NULL, 0);
Jeff Mahoney3444a972011-10-03 23:23:13 -04001549 /* Will always return 0 or 1 with map_multi == NULL */
1550 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001551 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001552 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001553 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001554}
1555
Chris Masond352ac62008-09-29 15:18:18 -04001556/*
1557 * in order to insert checksums into the metadata in large chunks,
1558 * we wait until bio submission time. All the pages in the bio are
1559 * checksummed and sums are attached onto the ordered extent record.
1560 *
1561 * At IO completion time the cums attached on the ordered extent record
1562 * are inserted into the btree
1563 */
Chris Masond3977122009-01-05 21:25:51 -05001564static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1565 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001566 unsigned long bio_flags,
1567 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001568{
Chris Mason065631f2008-02-20 12:07:25 -05001569 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001570 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001571
Chris Masond20f7042008-12-08 16:58:54 -05001572 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001573 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001574 return 0;
1575}
Chris Masone0156402008-04-16 11:15:20 -04001576
Chris Mason4a69a412008-11-06 22:03:00 -05001577/*
1578 * in order to insert checksums into the metadata in large chunks,
1579 * we wait until bio submission time. All the pages in the bio are
1580 * checksummed and sums are attached onto the ordered extent record.
1581 *
1582 * At IO completion time the cums attached on the ordered extent record
1583 * are inserted into the btree
1584 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001585static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001586 int mirror_num, unsigned long bio_flags,
1587 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001588{
1589 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001590 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001591}
1592
Chris Masond352ac62008-09-29 15:18:18 -04001593/*
Chris Masoncad321a2008-12-17 14:51:42 -05001594 * extent_io.c submission hook. This does the right thing for csum calculation
1595 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001596 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001597static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001598 int mirror_num, unsigned long bio_flags,
1599 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001600{
1601 struct btrfs_root *root = BTRFS_I(inode)->root;
1602 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001603 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001604 int metadata = 0;
Chris Mason44b8bd72008-04-16 11:14:51 -04001605
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001606 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001607
Chris Mason2cf85722011-07-26 15:35:09 -04001608 if (btrfs_is_free_space_inode(root, inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001609 metadata = 2;
1610
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001611 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001612 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1613 if (ret)
1614 return ret;
1615
Chris Masond20f7042008-12-08 16:58:54 -05001616 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001617 return btrfs_submit_compressed_read(inode, bio,
1618 mirror_num, bio_flags);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001619 } else if (!skip_sum) {
1620 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1621 if (ret)
1622 return ret;
1623 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001624 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001625 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001626 /* csum items have already been cloned */
1627 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1628 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001629 /* we're doing a write, do the async checksumming */
1630 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001631 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001632 bio_flags, bio_offset,
1633 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001634 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001635 }
1636
Chris Mason0b86a832008-03-24 15:01:56 -04001637mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001638 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001639}
Chris Mason6885f302008-02-20 16:11:05 -05001640
Chris Masond352ac62008-09-29 15:18:18 -04001641/*
1642 * given a list of ordered sums record them in the inode. This happens
1643 * at IO completion time based on sums calculated at bio submission time.
1644 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001645static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001646 struct inode *inode, u64 file_offset,
1647 struct list_head *list)
1648{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001649 struct btrfs_ordered_sum *sum;
1650
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001651 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001652 btrfs_csum_file_blocks(trans,
1653 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001654 }
1655 return 0;
1656}
1657
Josef Bacik2ac55d42010-02-03 19:33:23 +00001658int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1659 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001660{
Chris Masond3977122009-01-05 21:25:51 -05001661 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001662 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001663 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001664 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001665}
1666
Chris Masond352ac62008-09-29 15:18:18 -04001667/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001668struct btrfs_writepage_fixup {
1669 struct page *page;
1670 struct btrfs_work work;
1671};
1672
Christoph Hellwigb2950862008-12-02 09:54:17 -05001673static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001674{
1675 struct btrfs_writepage_fixup *fixup;
1676 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001677 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001678 struct page *page;
1679 struct inode *inode;
1680 u64 page_start;
1681 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001682 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001683
1684 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1685 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001686again:
Chris Mason247e7432008-07-17 12:53:51 -04001687 lock_page(page);
1688 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1689 ClearPageChecked(page);
1690 goto out_page;
1691 }
1692
1693 inode = page->mapping->host;
1694 page_start = page_offset(page);
1695 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1696
Josef Bacik2ac55d42010-02-03 19:33:23 +00001697 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001698 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001699
1700 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001701 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001702 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001703
1704 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1705 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001706 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1707 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001708 unlock_page(page);
1709 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001710 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001711 goto again;
1712 }
Chris Mason247e7432008-07-17 12:53:51 -04001713
Jeff Mahoney87826df2012-02-15 16:23:57 +01001714 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1715 if (ret) {
1716 mapping_set_error(page->mapping, ret);
1717 end_extent_writepage(page, ret, page_start, page_end);
1718 ClearPageChecked(page);
1719 goto out;
1720 }
1721
Josef Bacik2ac55d42010-02-03 19:33:23 +00001722 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001723 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001724 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001725out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001726 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1727 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001728out_page:
1729 unlock_page(page);
1730 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001731 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001732}
1733
1734/*
1735 * There are a few paths in the higher layers of the kernel that directly
1736 * set the page dirty bit without asking the filesystem if it is a
1737 * good idea. This causes problems because we want to make sure COW
1738 * properly happens and the data=ordered rules are followed.
1739 *
Chris Masonc8b97812008-10-29 14:49:59 -04001740 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001741 * hasn't been properly setup for IO. We kick off an async process
1742 * to fix it up. The async helper will wait for ordered extents, set
1743 * the delalloc bit and make it safe to write the page.
1744 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001745static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001746{
1747 struct inode *inode = page->mapping->host;
1748 struct btrfs_writepage_fixup *fixup;
1749 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001750
Chris Mason8b62b722009-09-02 16:53:46 -04001751 /* this page is properly in the ordered list */
1752 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001753 return 0;
1754
1755 if (PageChecked(page))
1756 return -EAGAIN;
1757
1758 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1759 if (!fixup)
1760 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001761
Chris Mason247e7432008-07-17 12:53:51 -04001762 SetPageChecked(page);
1763 page_cache_get(page);
1764 fixup->work.func = btrfs_writepage_fixup_worker;
1765 fixup->page = page;
1766 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001767 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001768}
1769
Yan Zhengd899e052008-10-30 14:25:28 -04001770static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1771 struct inode *inode, u64 file_pos,
1772 u64 disk_bytenr, u64 disk_num_bytes,
1773 u64 num_bytes, u64 ram_bytes,
1774 u8 compression, u8 encryption,
1775 u16 other_encoding, int extent_type)
1776{
1777 struct btrfs_root *root = BTRFS_I(inode)->root;
1778 struct btrfs_file_extent_item *fi;
1779 struct btrfs_path *path;
1780 struct extent_buffer *leaf;
1781 struct btrfs_key ins;
1782 u64 hint;
1783 int ret;
1784
1785 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001786 if (!path)
1787 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001788
Chris Masonb9473432009-03-13 11:00:37 -04001789 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001790
1791 /*
1792 * we may be replacing one extent in the tree with another.
1793 * The new extent is pinned in the extent map, and we don't want
1794 * to drop it from the cache until it is completely in the btree.
1795 *
1796 * So, tell btrfs_drop_extents to leave this extent in the cache.
1797 * the caller is expected to unpin it and allow it to be merged
1798 * with the others.
1799 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001800 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1801 &hint, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001802 if (ret)
1803 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001804
Li Zefan33345d012011-04-20 10:31:50 +08001805 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001806 ins.offset = file_pos;
1807 ins.type = BTRFS_EXTENT_DATA_KEY;
1808 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001809 if (ret)
1810 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001811 leaf = path->nodes[0];
1812 fi = btrfs_item_ptr(leaf, path->slots[0],
1813 struct btrfs_file_extent_item);
1814 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1815 btrfs_set_file_extent_type(leaf, fi, extent_type);
1816 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1817 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1818 btrfs_set_file_extent_offset(leaf, fi, 0);
1819 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1820 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1821 btrfs_set_file_extent_compression(leaf, fi, compression);
1822 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1823 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001824
1825 btrfs_unlock_up_safe(path, 1);
1826 btrfs_set_lock_blocking(leaf);
1827
Yan Zhengd899e052008-10-30 14:25:28 -04001828 btrfs_mark_buffer_dirty(leaf);
1829
1830 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001831
1832 ins.objectid = disk_bytenr;
1833 ins.offset = disk_num_bytes;
1834 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001835 ret = btrfs_alloc_reserved_file_extent(trans, root,
1836 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001837 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001838out:
Yan Zhengd899e052008-10-30 14:25:28 -04001839 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001840
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001841 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001842}
1843
Chris Mason5d13a982009-03-13 11:41:46 -04001844/*
1845 * helper function for btrfs_finish_ordered_io, this
1846 * just reads in some of the csum leaves to prime them into ram
1847 * before we start the transaction. It limits the amount of btree
1848 * reads required while inside the transaction.
1849 */
Chris Masond352ac62008-09-29 15:18:18 -04001850/* as ordered data IO finishes, this gets called so we can finish
1851 * an ordered extent if the range of bytes in the file it covers are
1852 * fully written.
1853 */
Josef Bacik5fd02042012-05-02 14:00:54 -04001854static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001855{
Josef Bacik5fd02042012-05-02 14:00:54 -04001856 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001857 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001858 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001859 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001860 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001861 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001862 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001863 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001864
Chris Mason2cf85722011-07-26 15:35:09 -04001865 nolock = btrfs_is_free_space_inode(root, inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001866
Josef Bacik5fd02042012-05-02 14:00:54 -04001867 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
1868 ret = -EIO;
1869 goto out;
1870 }
1871
Yan, Zhengc21677542009-11-12 09:34:21 +00001872 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001873 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Yan, Zhengc21677542009-11-12 09:34:21 +00001874 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1875 if (!ret) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001876 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001877 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001878 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001879 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001880 if (IS_ERR(trans))
1881 return PTR_ERR(trans);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001882 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason21151332011-11-10 20:39:08 -05001883 ret = btrfs_update_inode_fallback(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001884 if (ret) /* -ENOMEM or corruption */
1885 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc21677542009-11-12 09:34:21 +00001886 }
1887 goto out;
1888 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001889
Josef Bacik2ac55d42010-02-03 19:33:23 +00001890 lock_extent_bits(io_tree, ordered_extent->file_offset,
1891 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001892 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001893
Josef Bacik0cb59c92010-07-02 12:14:14 -04001894 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001895 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001896 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001897 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001898 if (IS_ERR(trans)) {
1899 ret = PTR_ERR(trans);
1900 trans = NULL;
1901 goto out_unlock;
1902 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001903 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc21677542009-11-12 09:34:21 +00001904
Chris Masonc8b97812008-10-29 14:49:59 -04001905 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001906 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001907 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001908 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001909 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001910 ordered_extent->file_offset,
1911 ordered_extent->file_offset +
1912 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001913 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001914 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001915 ret = insert_reserved_file_extent(trans, inode,
1916 ordered_extent->file_offset,
1917 ordered_extent->start,
1918 ordered_extent->disk_len,
1919 ordered_extent->len,
1920 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001921 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001922 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001923 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1924 ordered_extent->file_offset,
1925 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001926 }
Josef Bacik5fd02042012-05-02 14:00:54 -04001927
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001928 if (ret < 0) {
1929 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04001930 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001931 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001932
Chris Masone6dcd2d2008-07-17 12:53:50 -04001933 add_pending_csums(trans, inode, ordered_extent->file_offset,
1934 &ordered_extent->list);
1935
Josef Bacik1ef30be2011-04-05 19:25:36 -04001936 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
Miao Xiea39f7522011-09-11 10:52:25 -04001937 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Chris Mason21151332011-11-10 20:39:08 -05001938 ret = btrfs_update_inode_fallback(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001939 if (ret) { /* -ENOMEM or corruption */
1940 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04001941 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001942 }
Josef Bacik1ef30be2011-04-05 19:25:36 -04001943 }
1944 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04001945out_unlock:
1946 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1947 ordered_extent->file_offset +
1948 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc21677542009-11-12 09:34:21 +00001949out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001950 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001951 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001952 if (trans) {
1953 if (nolock)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001954 btrfs_end_transaction_nolock(trans, root);
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001955 else
Josef Bacik0cb59c92010-07-02 12:14:14 -04001956 btrfs_end_transaction(trans, root);
1957 }
1958
Josef Bacik5fd02042012-05-02 14:00:54 -04001959 if (ret)
1960 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
1961 ordered_extent->file_offset +
1962 ordered_extent->len - 1, NULL, GFP_NOFS);
1963
1964 /*
1965 * This needs to be dont to make sure anybody waiting knows we are done
1966 * upating everything for this ordered extent.
1967 */
1968 btrfs_remove_ordered_extent(inode, ordered_extent);
1969
Chris Masone6dcd2d2008-07-17 12:53:50 -04001970 /* once for us */
1971 btrfs_put_ordered_extent(ordered_extent);
1972 /* once for the tree */
1973 btrfs_put_ordered_extent(ordered_extent);
1974
Josef Bacik5fd02042012-05-02 14:00:54 -04001975 return ret;
1976}
1977
1978static void finish_ordered_fn(struct btrfs_work *work)
1979{
1980 struct btrfs_ordered_extent *ordered_extent;
1981 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
1982 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001983}
1984
Christoph Hellwigb2950862008-12-02 09:54:17 -05001985static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001986 struct extent_state *state, int uptodate)
1987{
Josef Bacik5fd02042012-05-02 14:00:54 -04001988 struct inode *inode = page->mapping->host;
1989 struct btrfs_root *root = BTRFS_I(inode)->root;
1990 struct btrfs_ordered_extent *ordered_extent = NULL;
1991 struct btrfs_workers *workers;
1992
liubo1abe9b82011-03-24 11:18:59 +00001993 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
1994
Chris Mason8b62b722009-09-02 16:53:46 -04001995 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04001996 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1997 end - start + 1, uptodate))
1998 return 0;
1999
2000 ordered_extent->work.func = finish_ordered_fn;
2001 ordered_extent->work.flags = 0;
2002
2003 if (btrfs_is_free_space_inode(root, inode))
2004 workers = &root->fs_info->endio_freespace_worker;
2005 else
2006 workers = &root->fs_info->endio_write_workers;
2007 btrfs_queue_worker(workers, &ordered_extent->work);
2008
2009 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002010}
2011
Chris Masond352ac62008-09-29 15:18:18 -04002012/*
Chris Masond352ac62008-09-29 15:18:18 -04002013 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002014 * if there's a match, we allow the bio to finish. If not, the code in
2015 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002016 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002017static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002018 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002019{
Chris Mason35ebb932007-10-30 16:56:53 -04002020 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04002021 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002022 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002023 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002024 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002025 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002026 struct btrfs_root *root = BTRFS_I(inode)->root;
2027 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05002028
Chris Masond20f7042008-12-08 16:58:54 -05002029 if (PageChecked(page)) {
2030 ClearPageChecked(page);
2031 goto good;
2032 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002033
2034 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002035 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002036
Yan Zheng17d217f2008-12-12 10:03:38 -05002037 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002038 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002039 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2040 GFP_NOFS);
2041 return 0;
2042 }
2043
Yanc2e639f2008-02-04 08:57:25 -05002044 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002045 private = state->private;
2046 ret = 0;
2047 } else {
2048 ret = get_state_private(io_tree, start, &private);
2049 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002050 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002051 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002052 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002053
Chris Masonff79f812007-10-15 16:22:25 -04002054 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2055 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002056 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002057 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002058
Cong Wang7ac687d2011-11-25 23:14:28 +08002059 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002060good:
Chris Mason07157aa2007-08-30 08:50:51 -04002061 return 0;
2062
2063zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002064 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002065 "private %llu\n",
2066 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002067 (unsigned long long)start, csum,
2068 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002069 memset(kaddr + offset, 1, end - start + 1);
2070 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002071 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002072 if (private == 0)
2073 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002074 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002075}
Chris Masonb888db22007-08-27 16:49:44 -04002076
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002077struct delayed_iput {
2078 struct list_head list;
2079 struct inode *inode;
2080};
2081
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002082/* JDM: If this is fs-wide, why can't we add a pointer to
2083 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002084void btrfs_add_delayed_iput(struct inode *inode)
2085{
2086 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2087 struct delayed_iput *delayed;
2088
2089 if (atomic_add_unless(&inode->i_count, -1, 1))
2090 return;
2091
2092 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2093 delayed->inode = inode;
2094
2095 spin_lock(&fs_info->delayed_iput_lock);
2096 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2097 spin_unlock(&fs_info->delayed_iput_lock);
2098}
2099
2100void btrfs_run_delayed_iputs(struct btrfs_root *root)
2101{
2102 LIST_HEAD(list);
2103 struct btrfs_fs_info *fs_info = root->fs_info;
2104 struct delayed_iput *delayed;
2105 int empty;
2106
2107 spin_lock(&fs_info->delayed_iput_lock);
2108 empty = list_empty(&fs_info->delayed_iputs);
2109 spin_unlock(&fs_info->delayed_iput_lock);
2110 if (empty)
2111 return;
2112
2113 down_read(&root->fs_info->cleanup_work_sem);
2114 spin_lock(&fs_info->delayed_iput_lock);
2115 list_splice_init(&fs_info->delayed_iputs, &list);
2116 spin_unlock(&fs_info->delayed_iput_lock);
2117
2118 while (!list_empty(&list)) {
2119 delayed = list_entry(list.next, struct delayed_iput, list);
2120 list_del(&delayed->list);
2121 iput(delayed->inode);
2122 kfree(delayed);
2123 }
2124 up_read(&root->fs_info->cleanup_work_sem);
2125}
2126
Yan, Zhengd68fc572010-05-16 10:49:58 -04002127enum btrfs_orphan_cleanup_state {
2128 ORPHAN_CLEANUP_STARTED = 1,
2129 ORPHAN_CLEANUP_DONE = 2,
2130};
2131
2132/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002133 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002134 * files in the subvolume, it removes orphan item and frees block_rsv
2135 * structure.
2136 */
2137void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2138 struct btrfs_root *root)
2139{
Josef Bacik90290e12011-12-02 15:44:12 -05002140 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002141 int ret;
2142
Josef Bacik8a35d952012-05-23 14:26:42 -04002143 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002144 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2145 return;
2146
Josef Bacik90290e12011-12-02 15:44:12 -05002147 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002148 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002149 spin_unlock(&root->orphan_lock);
2150 return;
2151 }
2152
2153 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2154 spin_unlock(&root->orphan_lock);
2155 return;
2156 }
2157
2158 block_rsv = root->orphan_block_rsv;
2159 root->orphan_block_rsv = NULL;
2160 spin_unlock(&root->orphan_lock);
2161
Yan, Zhengd68fc572010-05-16 10:49:58 -04002162 if (root->orphan_item_inserted &&
2163 btrfs_root_refs(&root->root_item) > 0) {
2164 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2165 root->root_key.objectid);
2166 BUG_ON(ret);
2167 root->orphan_item_inserted = 0;
2168 }
2169
Josef Bacik90290e12011-12-02 15:44:12 -05002170 if (block_rsv) {
2171 WARN_ON(block_rsv->size > 0);
2172 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002173 }
2174}
2175
2176/*
Josef Bacik7b128762008-07-24 12:17:14 -04002177 * This creates an orphan entry for the given inode in case something goes
2178 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002179 *
2180 * NOTE: caller of this function should reserve 5 units of metadata for
2181 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002182 */
2183int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2184{
2185 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002186 struct btrfs_block_rsv *block_rsv = NULL;
2187 int reserve = 0;
2188 int insert = 0;
2189 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002190
Yan, Zhengd68fc572010-05-16 10:49:58 -04002191 if (!root->orphan_block_rsv) {
2192 block_rsv = btrfs_alloc_block_rsv(root);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002193 if (!block_rsv)
2194 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002195 }
2196
Yan, Zhengd68fc572010-05-16 10:49:58 -04002197 spin_lock(&root->orphan_lock);
2198 if (!root->orphan_block_rsv) {
2199 root->orphan_block_rsv = block_rsv;
2200 } else if (block_rsv) {
2201 btrfs_free_block_rsv(root, block_rsv);
2202 block_rsv = NULL;
2203 }
Josef Bacik7b128762008-07-24 12:17:14 -04002204
Josef Bacik8a35d952012-05-23 14:26:42 -04002205 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2206 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002207#if 0
2208 /*
2209 * For proper ENOSPC handling, we should do orphan
2210 * cleanup when mounting. But this introduces backward
2211 * compatibility issue.
2212 */
2213 if (!xchg(&root->orphan_item_inserted, 1))
2214 insert = 2;
2215 else
2216 insert = 1;
2217#endif
2218 insert = 1;
Josef Bacik8a35d952012-05-23 14:26:42 -04002219 atomic_dec(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002220 }
Josef Bacik7b128762008-07-24 12:17:14 -04002221
Josef Bacik72ac3c02012-05-23 14:13:11 -04002222 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2223 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002224 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002225 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002226
Yan, Zhengd68fc572010-05-16 10:49:58 -04002227 /* grab metadata reservation from transaction handle */
2228 if (reserve) {
2229 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002230 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002231 }
2232
2233 /* insert an orphan item to track this unlinked/truncated file */
2234 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002235 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002236 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04002237 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2238 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002239 btrfs_abort_transaction(trans, root, ret);
2240 return ret;
2241 }
2242 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002243 }
2244
2245 /* insert an orphan item to track subvolume contains orphan files */
2246 if (insert >= 2) {
2247 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2248 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002249 if (ret && ret != -EEXIST) {
2250 btrfs_abort_transaction(trans, root, ret);
2251 return ret;
2252 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002253 }
2254 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002255}
2256
2257/*
2258 * We have done the truncate/delete so we can go ahead and remove the orphan
2259 * item for this particular inode.
2260 */
2261int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2262{
2263 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002264 int delete_item = 0;
2265 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002266 int ret = 0;
2267
Yan, Zhengd68fc572010-05-16 10:49:58 -04002268 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002269 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2270 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002271 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002272
Josef Bacik72ac3c02012-05-23 14:13:11 -04002273 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2274 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002275 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002276 spin_unlock(&root->orphan_lock);
2277
2278 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002279 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002280 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04002281 }
2282
Josef Bacik8a35d952012-05-23 14:26:42 -04002283 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002284 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04002285 atomic_dec(&root->orphan_inodes);
2286 }
Josef Bacik7b128762008-07-24 12:17:14 -04002287
Yan, Zhengd68fc572010-05-16 10:49:58 -04002288 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002289}
2290
2291/*
2292 * this cleans up any orphans that may be left on the list from the last use
2293 * of this root.
2294 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002295int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002296{
2297 struct btrfs_path *path;
2298 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002299 struct btrfs_key key, found_key;
2300 struct btrfs_trans_handle *trans;
2301 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002302 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002303 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2304
Yan, Zhengd68fc572010-05-16 10:49:58 -04002305 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002306 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002307
2308 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002309 if (!path) {
2310 ret = -ENOMEM;
2311 goto out;
2312 }
Josef Bacik7b128762008-07-24 12:17:14 -04002313 path->reada = -1;
2314
2315 key.objectid = BTRFS_ORPHAN_OBJECTID;
2316 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2317 key.offset = (u64)-1;
2318
Josef Bacik7b128762008-07-24 12:17:14 -04002319 while (1) {
2320 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002321 if (ret < 0)
2322 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002323
2324 /*
2325 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002326 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002327 * find the key and see if we have stuff that matches
2328 */
2329 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002330 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002331 if (path->slots[0] == 0)
2332 break;
2333 path->slots[0]--;
2334 }
2335
2336 /* pull out the item */
2337 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002338 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2339
2340 /* make sure the item matches what we want */
2341 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2342 break;
2343 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2344 break;
2345
2346 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002347 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002348
2349 /*
2350 * this is where we are basically btrfs_lookup, without the
2351 * crossing root thing. we store the inode number in the
2352 * offset of the orphan item.
2353 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002354
2355 if (found_key.offset == last_objectid) {
2356 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2357 "stopping orphan cleanup\n");
2358 ret = -EINVAL;
2359 goto out;
2360 }
2361
2362 last_objectid = found_key.offset;
2363
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002364 found_key.objectid = found_key.offset;
2365 found_key.type = BTRFS_INODE_ITEM_KEY;
2366 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002367 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04002368 ret = PTR_RET(inode);
2369 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002370 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002371
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002372 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2373 struct btrfs_root *dead_root;
2374 struct btrfs_fs_info *fs_info = root->fs_info;
2375 int is_dead_root = 0;
2376
2377 /*
2378 * this is an orphan in the tree root. Currently these
2379 * could come from 2 sources:
2380 * a) a snapshot deletion in progress
2381 * b) a free space cache inode
2382 * We need to distinguish those two, as the snapshot
2383 * orphan must not get deleted.
2384 * find_dead_roots already ran before us, so if this
2385 * is a snapshot deletion, we should find the root
2386 * in the dead_roots list
2387 */
2388 spin_lock(&fs_info->trans_lock);
2389 list_for_each_entry(dead_root, &fs_info->dead_roots,
2390 root_list) {
2391 if (dead_root->root_key.objectid ==
2392 found_key.objectid) {
2393 is_dead_root = 1;
2394 break;
2395 }
2396 }
2397 spin_unlock(&fs_info->trans_lock);
2398 if (is_dead_root) {
2399 /* prevent this orphan from being found again */
2400 key.offset = found_key.objectid - 1;
2401 continue;
2402 }
2403 }
Josef Bacika8c9e572011-09-21 16:55:59 -04002404 /*
2405 * Inode is already gone but the orphan item is still there,
2406 * kill the orphan item.
2407 */
2408 if (ret == -ESTALE) {
2409 trans = btrfs_start_transaction(root, 1);
2410 if (IS_ERR(trans)) {
2411 ret = PTR_ERR(trans);
2412 goto out;
2413 }
Josef Bacik8a35d952012-05-23 14:26:42 -04002414 printk(KERN_ERR "auto deleting %Lu\n",
2415 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04002416 ret = btrfs_del_orphan_item(trans, root,
2417 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002418 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04002419 btrfs_end_transaction(trans, root);
2420 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002421 }
Josef Bacik7b128762008-07-24 12:17:14 -04002422
Josef Bacik7b128762008-07-24 12:17:14 -04002423 /*
2424 * add this inode to the orphan list so btrfs_orphan_del does
2425 * the proper thing when we hit it
2426 */
Josef Bacik8a35d952012-05-23 14:26:42 -04002427 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2428 &BTRFS_I(inode)->runtime_flags);
Josef Bacik7b128762008-07-24 12:17:14 -04002429
Josef Bacik7b128762008-07-24 12:17:14 -04002430 /* if we have links, this was a truncate, lets do that */
2431 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002432 if (!S_ISREG(inode->i_mode)) {
2433 WARN_ON(1);
2434 iput(inode);
2435 continue;
2436 }
Josef Bacik7b128762008-07-24 12:17:14 -04002437 nr_truncate++;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002438 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002439 } else {
2440 nr_unlink++;
2441 }
2442
2443 /* this will do delete_inode and everything for us */
2444 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002445 if (ret)
2446 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002447 }
Miao Xie3254c872011-11-10 20:45:05 -05002448 /* release the path since we're done with it */
2449 btrfs_release_path(path);
2450
Yan, Zhengd68fc572010-05-16 10:49:58 -04002451 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2452
2453 if (root->orphan_block_rsv)
2454 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2455 (u64)-1);
2456
2457 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa402011-04-13 12:54:33 -04002458 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002459 if (!IS_ERR(trans))
2460 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002461 }
Josef Bacik7b128762008-07-24 12:17:14 -04002462
2463 if (nr_unlink)
2464 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2465 if (nr_truncate)
2466 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002467
2468out:
2469 if (ret)
2470 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2471 btrfs_free_path(path);
2472 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002473}
2474
Chris Masond352ac62008-09-29 15:18:18 -04002475/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002476 * very simple check to peek ahead in the leaf looking for xattrs. If we
2477 * don't find any xattrs, we know there can't be any acls.
2478 *
2479 * slot is the slot the inode is in, objectid is the objectid of the inode
2480 */
2481static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2482 int slot, u64 objectid)
2483{
2484 u32 nritems = btrfs_header_nritems(leaf);
2485 struct btrfs_key found_key;
2486 int scanned = 0;
2487
2488 slot++;
2489 while (slot < nritems) {
2490 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2491
2492 /* we found a different objectid, there must not be acls */
2493 if (found_key.objectid != objectid)
2494 return 0;
2495
2496 /* we found an xattr, assume we've got an acl */
2497 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2498 return 1;
2499
2500 /*
2501 * we found a key greater than an xattr key, there can't
2502 * be any acls later on
2503 */
2504 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2505 return 0;
2506
2507 slot++;
2508 scanned++;
2509
2510 /*
2511 * it goes inode, inode backrefs, xattrs, extents,
2512 * so if there are a ton of hard links to an inode there can
2513 * be a lot of backrefs. Don't waste time searching too hard,
2514 * this is just an optimization
2515 */
2516 if (scanned >= 8)
2517 break;
2518 }
2519 /* we hit the end of the leaf before we found an xattr or
2520 * something larger than an xattr. We have to assume the inode
2521 * has acls
2522 */
2523 return 1;
2524}
2525
2526/*
Chris Masond352ac62008-09-29 15:18:18 -04002527 * read an inode from the btree into the in-memory inode
2528 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002529static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002530{
2531 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002532 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002533 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002534 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002535 struct btrfs_root *root = BTRFS_I(inode)->root;
2536 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002537 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002538 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002539 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00002540 bool filled = false;
2541
2542 ret = btrfs_fill_inode(inode, &rdev);
2543 if (!ret)
2544 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04002545
2546 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07002547 if (!path)
2548 goto make_bad;
2549
Josef Bacikd90c7322011-05-17 09:50:54 -04002550 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002551 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002552
Chris Mason39279cc2007-06-12 06:35:45 -04002553 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002554 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002555 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002556
Chris Mason5f39d392007-10-15 16:14:19 -04002557 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00002558
2559 if (filled)
2560 goto cache_acl;
2561
Chris Mason5f39d392007-10-15 16:14:19 -04002562 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2563 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002564 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002565 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002566 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2567 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002568 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002569
2570 tspec = btrfs_inode_atime(inode_item);
2571 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2572 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2573
2574 tspec = btrfs_inode_mtime(inode_item);
2575 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2576 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2577
2578 tspec = btrfs_inode_ctime(inode_item);
2579 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2580 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2581
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002582 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002583 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002584 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002585 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002586 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002587 rdev = btrfs_inode_rdev(leaf, inode_item);
2588
Josef Bacikaec74772008-07-24 12:12:38 -04002589 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002590 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00002591cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04002592 /*
2593 * try to precache a NULL acl entry for files that don't have
2594 * any xattrs or acls
2595 */
Li Zefan33345d012011-04-20 10:31:50 +08002596 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2597 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002598 if (!maybe_acls)
2599 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002600
Chris Mason39279cc2007-06-12 06:35:45 -04002601 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002602
Chris Mason39279cc2007-06-12 06:35:45 -04002603 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002604 case S_IFREG:
2605 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002606 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002607 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002608 inode->i_fop = &btrfs_file_operations;
2609 inode->i_op = &btrfs_file_inode_operations;
2610 break;
2611 case S_IFDIR:
2612 inode->i_fop = &btrfs_dir_file_operations;
2613 if (root == root->fs_info->tree_root)
2614 inode->i_op = &btrfs_dir_ro_inode_operations;
2615 else
2616 inode->i_op = &btrfs_dir_inode_operations;
2617 break;
2618 case S_IFLNK:
2619 inode->i_op = &btrfs_symlink_inode_operations;
2620 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002621 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002622 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002623 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002624 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002625 init_special_inode(inode, inode->i_mode, rdev);
2626 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002627 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002628
2629 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002630 return;
2631
2632make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002633 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002634 make_bad_inode(inode);
2635}
2636
Chris Masond352ac62008-09-29 15:18:18 -04002637/*
2638 * given a leaf and an inode, copy the inode fields into the leaf
2639 */
Chris Masone02119d2008-09-05 16:13:11 -04002640static void fill_inode_item(struct btrfs_trans_handle *trans,
2641 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002642 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002643 struct inode *inode)
2644{
Chris Mason5f39d392007-10-15 16:14:19 -04002645 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2646 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002647 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002648 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2649 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2650
2651 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2652 inode->i_atime.tv_sec);
2653 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2654 inode->i_atime.tv_nsec);
2655
2656 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2657 inode->i_mtime.tv_sec);
2658 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2659 inode->i_mtime.tv_nsec);
2660
2661 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2662 inode->i_ctime.tv_sec);
2663 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2664 inode->i_ctime.tv_nsec);
2665
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002666 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002667 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002668 btrfs_set_inode_sequence(leaf, item, inode->i_version);
Chris Masone02119d2008-09-05 16:13:11 -04002669 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002670 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002671 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Josef Bacikd82a6f12011-05-11 15:26:06 -04002672 btrfs_set_inode_block_group(leaf, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002673}
2674
Chris Masond352ac62008-09-29 15:18:18 -04002675/*
2676 * copy everything in the in-memory inode into the btree.
2677 */
Chris Mason21151332011-11-10 20:39:08 -05002678static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05002679 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002680{
2681 struct btrfs_inode_item *inode_item;
2682 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002683 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002684 int ret;
2685
2686 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002687 if (!path)
2688 return -ENOMEM;
2689
Chris Masonb9473432009-03-13 11:00:37 -04002690 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002691 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2692 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002693 if (ret) {
2694 if (ret > 0)
2695 ret = -ENOENT;
2696 goto failed;
2697 }
2698
Chris Masonb4ce94d2009-02-04 09:25:08 -05002699 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002700 leaf = path->nodes[0];
2701 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002702 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002703
Chris Masone02119d2008-09-05 16:13:11 -04002704 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002705 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002706 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002707 ret = 0;
2708failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002709 btrfs_free_path(path);
2710 return ret;
2711}
2712
Chris Masond352ac62008-09-29 15:18:18 -04002713/*
Chris Mason21151332011-11-10 20:39:08 -05002714 * copy everything in the in-memory inode into the btree.
2715 */
2716noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2717 struct btrfs_root *root, struct inode *inode)
2718{
2719 int ret;
2720
2721 /*
2722 * If the inode is a free space inode, we can deadlock during commit
2723 * if we put it into the delayed code.
2724 *
2725 * The data relocation inode should also be directly updated
2726 * without delay
2727 */
2728 if (!btrfs_is_free_space_inode(root, inode)
2729 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
2730 ret = btrfs_delayed_update_inode(trans, root, inode);
2731 if (!ret)
2732 btrfs_set_inode_last_trans(trans, inode);
2733 return ret;
2734 }
2735
2736 return btrfs_update_inode_item(trans, root, inode);
2737}
2738
2739static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2740 struct btrfs_root *root, struct inode *inode)
2741{
2742 int ret;
2743
2744 ret = btrfs_update_inode(trans, root, inode);
2745 if (ret == -ENOSPC)
2746 return btrfs_update_inode_item(trans, root, inode);
2747 return ret;
2748}
2749
2750/*
Chris Masond352ac62008-09-29 15:18:18 -04002751 * unlink helper that gets used here in inode.c and in the tree logging
2752 * recovery code. It remove a link in a directory with a given name, and
2753 * also drops the back refs in the inode to the directory
2754 */
Al Viro92986792011-03-04 17:14:37 +00002755static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2756 struct btrfs_root *root,
2757 struct inode *dir, struct inode *inode,
2758 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002759{
2760 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002761 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002762 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002763 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002764 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002765 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002766 u64 ino = btrfs_ino(inode);
2767 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002768
2769 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002770 if (!path) {
2771 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002772 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002773 }
2774
Chris Masonb9473432009-03-13 11:00:37 -04002775 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002776 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002777 name, name_len, -1);
2778 if (IS_ERR(di)) {
2779 ret = PTR_ERR(di);
2780 goto err;
2781 }
2782 if (!di) {
2783 ret = -ENOENT;
2784 goto err;
2785 }
Chris Mason5f39d392007-10-15 16:14:19 -04002786 leaf = path->nodes[0];
2787 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002788 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002789 if (ret)
2790 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02002791 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002792
Li Zefan33345d012011-04-20 10:31:50 +08002793 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2794 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002795 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002796 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002797 "inode %llu parent %llu\n", name_len, name,
2798 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002799 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002800 goto err;
2801 }
2802
Miao Xie16cdcec2011-04-22 18:12:22 +08002803 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002804 if (ret) {
2805 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002806 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002807 }
Chris Mason39279cc2007-06-12 06:35:45 -04002808
Chris Masone02119d2008-09-05 16:13:11 -04002809 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002810 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002811 if (ret != 0 && ret != -ENOENT) {
2812 btrfs_abort_transaction(trans, root, ret);
2813 goto err;
2814 }
Chris Masone02119d2008-09-05 16:13:11 -04002815
2816 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2817 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002818 if (ret == -ENOENT)
2819 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002820err:
2821 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002822 if (ret)
2823 goto out;
2824
2825 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002826 inode_inc_iversion(inode);
2827 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002828 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2829 btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002830out:
Chris Mason39279cc2007-06-12 06:35:45 -04002831 return ret;
2832}
2833
Al Viro92986792011-03-04 17:14:37 +00002834int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2835 struct btrfs_root *root,
2836 struct inode *dir, struct inode *inode,
2837 const char *name, int name_len)
2838{
2839 int ret;
2840 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2841 if (!ret) {
2842 btrfs_drop_nlink(inode);
2843 ret = btrfs_update_inode(trans, root, inode);
2844 }
2845 return ret;
2846}
2847
2848
Yan, Zhenga22285a2010-05-16 10:48:46 -04002849/* helper to check if there is any shared block in the path */
2850static int check_path_shared(struct btrfs_root *root,
2851 struct btrfs_path *path)
2852{
2853 struct extent_buffer *eb;
2854 int level;
Dan Carpenter0e4dcbe2010-06-01 08:23:11 +00002855 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002856
2857 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002858 int ret;
2859
Yan, Zhenga22285a2010-05-16 10:48:46 -04002860 if (!path->nodes[level])
2861 break;
2862 eb = path->nodes[level];
2863 if (!btrfs_block_can_be_shared(root, eb))
2864 continue;
2865 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2866 &refs, NULL);
2867 if (refs > 1)
2868 return 1;
2869 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002870 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002871}
2872
2873/*
2874 * helper to start transaction for unlink and rmdir.
2875 *
2876 * unlink and rmdir are special in btrfs, they do not always free space.
2877 * so in enospc case, we should make sure they will free space before
2878 * allowing them to use the global metadata reservation.
2879 */
2880static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2881 struct dentry *dentry)
2882{
2883 struct btrfs_trans_handle *trans;
2884 struct btrfs_root *root = BTRFS_I(dir)->root;
2885 struct btrfs_path *path;
2886 struct btrfs_inode_ref *ref;
2887 struct btrfs_dir_item *di;
2888 struct inode *inode = dentry->d_inode;
2889 u64 index;
2890 int check_link = 1;
2891 int err = -ENOSPC;
2892 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002893 u64 ino = btrfs_ino(inode);
2894 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002895
Josef Bacike70bea52011-10-11 14:18:24 -04002896 /*
2897 * 1 for the possible orphan item
2898 * 1 for the dir item
2899 * 1 for the dir index
2900 * 1 for the inode ref
2901 * 1 for the inode ref in the tree log
2902 * 2 for the dir entries in the log
2903 * 1 for the inode
2904 */
2905 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002906 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2907 return trans;
2908
Li Zefan33345d012011-04-20 10:31:50 +08002909 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04002910 return ERR_PTR(-ENOSPC);
2911
2912 /* check if there is someone else holds reference */
2913 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2914 return ERR_PTR(-ENOSPC);
2915
2916 if (atomic_read(&inode->i_count) > 2)
2917 return ERR_PTR(-ENOSPC);
2918
2919 if (xchg(&root->fs_info->enospc_unlink, 1))
2920 return ERR_PTR(-ENOSPC);
2921
2922 path = btrfs_alloc_path();
2923 if (!path) {
2924 root->fs_info->enospc_unlink = 0;
2925 return ERR_PTR(-ENOMEM);
2926 }
2927
Josef Bacik3880a1b2011-10-14 14:46:51 -04002928 /* 1 for the orphan item */
2929 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002930 if (IS_ERR(trans)) {
2931 btrfs_free_path(path);
2932 root->fs_info->enospc_unlink = 0;
2933 return trans;
2934 }
2935
2936 path->skip_locking = 1;
2937 path->search_commit_root = 1;
2938
2939 ret = btrfs_lookup_inode(trans, root, path,
2940 &BTRFS_I(dir)->location, 0);
2941 if (ret < 0) {
2942 err = ret;
2943 goto out;
2944 }
2945 if (ret == 0) {
2946 if (check_path_shared(root, path))
2947 goto out;
2948 } else {
2949 check_link = 0;
2950 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002951 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002952
2953 ret = btrfs_lookup_inode(trans, root, path,
2954 &BTRFS_I(inode)->location, 0);
2955 if (ret < 0) {
2956 err = ret;
2957 goto out;
2958 }
2959 if (ret == 0) {
2960 if (check_path_shared(root, path))
2961 goto out;
2962 } else {
2963 check_link = 0;
2964 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002965 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002966
2967 if (ret == 0 && S_ISREG(inode->i_mode)) {
2968 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08002969 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002970 if (ret < 0) {
2971 err = ret;
2972 goto out;
2973 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002974 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002975 if (check_path_shared(root, path))
2976 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02002977 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002978 }
2979
2980 if (!check_link) {
2981 err = 0;
2982 goto out;
2983 }
2984
Li Zefan33345d012011-04-20 10:31:50 +08002985 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002986 dentry->d_name.name, dentry->d_name.len, 0);
2987 if (IS_ERR(di)) {
2988 err = PTR_ERR(di);
2989 goto out;
2990 }
2991 if (di) {
2992 if (check_path_shared(root, path))
2993 goto out;
2994 } else {
2995 err = 0;
2996 goto out;
2997 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002998 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002999
3000 ref = btrfs_lookup_inode_ref(trans, root, path,
3001 dentry->d_name.name, dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08003002 ino, dir_ino, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003003 if (IS_ERR(ref)) {
3004 err = PTR_ERR(ref);
3005 goto out;
3006 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003007 BUG_ON(!ref); /* Logic error */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003008 if (check_path_shared(root, path))
3009 goto out;
3010 index = btrfs_inode_ref_index(path->nodes[0], ref);
David Sterbab3b4aa72011-04-21 01:20:15 +02003011 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003012
Miao Xie16cdcec2011-04-22 18:12:22 +08003013 /*
3014 * This is a commit root search, if we can lookup inode item and other
3015 * relative items in the commit root, it means the transaction of
3016 * dir/file creation has been committed, and the dir index item that we
3017 * delay to insert has also been inserted into the commit root. So
3018 * we needn't worry about the delayed insertion of the dir index item
3019 * here.
3020 */
Li Zefan33345d012011-04-20 10:31:50 +08003021 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003022 dentry->d_name.name, dentry->d_name.len, 0);
3023 if (IS_ERR(di)) {
3024 err = PTR_ERR(di);
3025 goto out;
3026 }
3027 BUG_ON(ret == -ENOENT);
3028 if (check_path_shared(root, path))
3029 goto out;
3030
3031 err = 0;
3032out:
3033 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003034 /* Migrate the orphan reservation over */
3035 if (!err)
3036 err = btrfs_block_rsv_migrate(trans->block_rsv,
3037 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003038 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003039
Yan, Zhenga22285a2010-05-16 10:48:46 -04003040 if (err) {
3041 btrfs_end_transaction(trans, root);
3042 root->fs_info->enospc_unlink = 0;
3043 return ERR_PTR(err);
3044 }
3045
3046 trans->block_rsv = &root->fs_info->global_block_rsv;
3047 return trans;
3048}
3049
3050static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3051 struct btrfs_root *root)
3052{
3053 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003054 btrfs_block_rsv_release(root, trans->block_rsv,
3055 trans->bytes_reserved);
3056 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003057 BUG_ON(!root->fs_info->enospc_unlink);
3058 root->fs_info->enospc_unlink = 0;
3059 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003060 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003061}
3062
Chris Mason39279cc2007-06-12 06:35:45 -04003063static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3064{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003065 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003066 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003067 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003068 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05003069 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003070
Yan, Zhenga22285a2010-05-16 10:48:46 -04003071 trans = __unlink_start_trans(dir, dentry);
3072 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003073 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003074
Chris Mason12fcfd22009-03-24 10:24:20 -04003075 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3076
Chris Masone02119d2008-09-05 16:13:11 -04003077 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3078 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003079 if (ret)
3080 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003081
Yan, Zhenga22285a2010-05-16 10:48:46 -04003082 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003083 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003084 if (ret)
3085 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003086 }
Josef Bacik7b128762008-07-24 12:17:14 -04003087
Tsutomu Itohb5324022011-07-19 07:27:20 +00003088out:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003089 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003090 __unlink_end_trans(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003091 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003092 return ret;
3093}
3094
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003095int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3096 struct btrfs_root *root,
3097 struct inode *dir, u64 objectid,
3098 const char *name, int name_len)
3099{
3100 struct btrfs_path *path;
3101 struct extent_buffer *leaf;
3102 struct btrfs_dir_item *di;
3103 struct btrfs_key key;
3104 u64 index;
3105 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003106 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003107
3108 path = btrfs_alloc_path();
3109 if (!path)
3110 return -ENOMEM;
3111
Li Zefan33345d012011-04-20 10:31:50 +08003112 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003113 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003114 if (IS_ERR_OR_NULL(di)) {
3115 if (!di)
3116 ret = -ENOENT;
3117 else
3118 ret = PTR_ERR(di);
3119 goto out;
3120 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003121
3122 leaf = path->nodes[0];
3123 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3124 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3125 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003126 if (ret) {
3127 btrfs_abort_transaction(trans, root, ret);
3128 goto out;
3129 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003130 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003131
3132 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3133 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003134 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003135 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003136 if (ret != -ENOENT) {
3137 btrfs_abort_transaction(trans, root, ret);
3138 goto out;
3139 }
Li Zefan33345d012011-04-20 10:31:50 +08003140 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003141 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003142 if (IS_ERR_OR_NULL(di)) {
3143 if (!di)
3144 ret = -ENOENT;
3145 else
3146 ret = PTR_ERR(di);
3147 btrfs_abort_transaction(trans, root, ret);
3148 goto out;
3149 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003150
3151 leaf = path->nodes[0];
3152 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003153 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003154 index = key.offset;
3155 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003156 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003157
Miao Xie16cdcec2011-04-22 18:12:22 +08003158 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003159 if (ret) {
3160 btrfs_abort_transaction(trans, root, ret);
3161 goto out;
3162 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003163
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003164 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003165 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003166 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3167 ret = btrfs_update_inode(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003168 if (ret)
3169 btrfs_abort_transaction(trans, root, ret);
3170out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003171 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003172 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003173}
3174
Chris Mason39279cc2007-06-12 06:35:45 -04003175static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3176{
3177 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003178 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003179 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003180 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05003181 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003182
Chris Mason3394e162008-11-17 20:42:26 -05003183 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Li Zefan33345d012011-04-20 10:31:50 +08003184 btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04003185 return -ENOTEMPTY;
3186
Yan, Zhenga22285a2010-05-16 10:48:46 -04003187 trans = __unlink_start_trans(dir, dentry);
3188 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003189 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003190
Li Zefan33345d012011-04-20 10:31:50 +08003191 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003192 err = btrfs_unlink_subvol(trans, root, dir,
3193 BTRFS_I(inode)->location.objectid,
3194 dentry->d_name.name,
3195 dentry->d_name.len);
3196 goto out;
3197 }
3198
Josef Bacik7b128762008-07-24 12:17:14 -04003199 err = btrfs_orphan_add(trans, inode);
3200 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003201 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003202
Chris Mason39279cc2007-06-12 06:35:45 -04003203 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003204 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3205 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003206 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003207 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003208out:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003209 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003210 __unlink_end_trans(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003211 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05003212
Chris Mason39279cc2007-06-12 06:35:45 -04003213 return err;
3214}
3215
Chris Mason323ac952008-10-01 19:05:46 -04003216/*
Chris Mason39279cc2007-06-12 06:35:45 -04003217 * this can truncate away extent items, csum items and directory items.
3218 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003219 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003220 *
3221 * csum items that cross the new i_size are truncated to the new size
3222 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003223 *
3224 * min_type is the minimum key type to truncate down to. If set to 0, this
3225 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003226 */
Yan, Zheng80825102009-11-12 09:35:36 +00003227int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3228 struct btrfs_root *root,
3229 struct inode *inode,
3230 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003231{
Chris Mason39279cc2007-06-12 06:35:45 -04003232 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003233 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003234 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003235 struct btrfs_key key;
3236 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003237 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003238 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003239 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003240 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003241 u64 mask = root->sectorsize - 1;
3242 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003243 int found_extent;
3244 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003245 int pending_del_nr = 0;
3246 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003247 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003248 int ret;
3249 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003250 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003251
3252 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003253
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003254 path = btrfs_alloc_path();
3255 if (!path)
3256 return -ENOMEM;
3257 path->reada = -1;
3258
Josef Bacik0af3d002010-06-21 14:48:16 -04003259 if (root->ref_cows || root == root->fs_info->tree_root)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003260 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003261
Miao Xie16cdcec2011-04-22 18:12:22 +08003262 /*
3263 * This function is also used to drop the items in the log tree before
3264 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3265 * it is used to drop the loged items. So we shouldn't kill the delayed
3266 * items.
3267 */
3268 if (min_type == 0 && root == BTRFS_I(inode)->root)
3269 btrfs_kill_delayed_inode_items(inode);
3270
Li Zefan33345d012011-04-20 10:31:50 +08003271 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003272 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003273 key.type = (u8)-1;
3274
Chris Mason85e21ba2008-01-29 15:11:36 -05003275search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003276 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003277 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003278 if (ret < 0) {
3279 err = ret;
3280 goto out;
3281 }
Chris Masond3977122009-01-05 21:25:51 -05003282
Chris Mason85e21ba2008-01-29 15:11:36 -05003283 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003284 /* there are no items in the tree for us to truncate, we're
3285 * done
3286 */
Yan, Zheng80825102009-11-12 09:35:36 +00003287 if (path->slots[0] == 0)
3288 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003289 path->slots[0]--;
3290 }
3291
Chris Masond3977122009-01-05 21:25:51 -05003292 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003293 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003294 leaf = path->nodes[0];
3295 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3296 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003297
Li Zefan33345d012011-04-20 10:31:50 +08003298 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003299 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003300
Chris Mason85e21ba2008-01-29 15:11:36 -05003301 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003302 break;
3303
Chris Mason5f39d392007-10-15 16:14:19 -04003304 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003305 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003306 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003307 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003308 extent_type = btrfs_file_extent_type(leaf, fi);
3309 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003310 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003311 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003312 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003313 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003314 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003315 }
Yan008630c2007-11-07 13:31:09 -05003316 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003317 }
Yan, Zheng80825102009-11-12 09:35:36 +00003318 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003319 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003320 } else {
3321 if (item_end < new_size)
3322 break;
3323 if (found_key.offset >= new_size)
3324 del_item = 1;
3325 else
3326 del_item = 0;
3327 }
Chris Mason39279cc2007-06-12 06:35:45 -04003328 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003329 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003330 if (found_type != BTRFS_EXTENT_DATA_KEY)
3331 goto delete;
3332
3333 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003334 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003335 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003336 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003337 u64 orig_num_bytes =
3338 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003339 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003340 found_key.offset + root->sectorsize - 1;
Yanb1632b102008-01-30 11:54:04 -05003341 extent_num_bytes = extent_num_bytes &
3342 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003343 btrfs_set_file_extent_num_bytes(leaf, fi,
3344 extent_num_bytes);
3345 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003346 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003347 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003348 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003349 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003350 } else {
Chris Masondb945352007-10-15 16:15:53 -04003351 extent_num_bytes =
3352 btrfs_file_extent_disk_num_bytes(leaf,
3353 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003354 extent_offset = found_key.offset -
3355 btrfs_file_extent_offset(leaf, fi);
3356
Chris Mason39279cc2007-06-12 06:35:45 -04003357 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003358 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003359 if (extent_start != 0) {
3360 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003361 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003362 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003363 }
3364 }
Chris Mason90692182008-02-08 13:49:28 -05003365 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003366 /*
3367 * we can't truncate inline items that have had
3368 * special encodings
3369 */
3370 if (!del_item &&
3371 btrfs_file_extent_compression(leaf, fi) == 0 &&
3372 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3373 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003374 u32 size = new_size - found_key.offset;
3375
3376 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003377 inode_sub_bytes(inode, item_end + 1 -
3378 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003379 }
3380 size =
3381 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003382 btrfs_truncate_item(trans, root, path,
3383 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003384 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003385 inode_sub_bytes(inode, item_end + 1 -
3386 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003387 }
Chris Mason39279cc2007-06-12 06:35:45 -04003388 }
Chris Mason179e29e2007-11-01 11:28:41 -04003389delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003390 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003391 if (!pending_del_nr) {
3392 /* no pending yet, add ourselves */
3393 pending_del_slot = path->slots[0];
3394 pending_del_nr = 1;
3395 } else if (pending_del_nr &&
3396 path->slots[0] + 1 == pending_del_slot) {
3397 /* hop on the pending chunk */
3398 pending_del_nr++;
3399 pending_del_slot = path->slots[0];
3400 } else {
Chris Masond3977122009-01-05 21:25:51 -05003401 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003402 }
Chris Mason39279cc2007-06-12 06:35:45 -04003403 } else {
3404 break;
3405 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003406 if (found_extent && (root->ref_cows ||
3407 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003408 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003409 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003410 extent_num_bytes, 0,
3411 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003412 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003413 BUG_ON(ret);
3414 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003415
Yan, Zheng80825102009-11-12 09:35:36 +00003416 if (found_type == BTRFS_INODE_ITEM_KEY)
3417 break;
3418
3419 if (path->slots[0] == 0 ||
3420 path->slots[0] != pending_del_slot) {
Li Zefan82d59022011-04-20 10:33:24 +08003421 if (root->ref_cows &&
3422 BTRFS_I(inode)->location.objectid !=
3423 BTRFS_FREE_INO_OBJECTID) {
Yan, Zheng80825102009-11-12 09:35:36 +00003424 err = -EAGAIN;
3425 goto out;
3426 }
3427 if (pending_del_nr) {
3428 ret = btrfs_del_items(trans, root, path,
3429 pending_del_slot,
3430 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003431 if (ret) {
3432 btrfs_abort_transaction(trans,
3433 root, ret);
3434 goto error;
3435 }
Yan, Zheng80825102009-11-12 09:35:36 +00003436 pending_del_nr = 0;
3437 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003438 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003439 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003440 } else {
3441 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003442 }
Chris Mason39279cc2007-06-12 06:35:45 -04003443 }
Yan, Zheng80825102009-11-12 09:35:36 +00003444out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003445 if (pending_del_nr) {
3446 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3447 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003448 if (ret)
3449 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003450 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003451error:
Chris Mason39279cc2007-06-12 06:35:45 -04003452 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003453 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003454}
3455
Chris Masona52d9a82007-08-27 16:49:44 -04003456/*
3457 * taken from block_truncate_page, but does cow as it zeros out
3458 * any bytes left in the last page in the file.
3459 */
3460static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3461{
3462 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003463 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003464 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3465 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003466 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003467 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003468 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003469 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3470 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3471 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003472 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04003473 int ret = 0;
3474 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003475 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003476
3477 if ((offset & (blocksize - 1)) == 0)
3478 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003479 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003480 if (ret)
3481 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003482
3483 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003484again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003485 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003486 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003487 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003488 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003489 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003490
3491 page_start = page_offset(page);
3492 page_end = page_start + PAGE_CACHE_SIZE - 1;
3493
Chris Masona52d9a82007-08-27 16:49:44 -04003494 if (!PageUptodate(page)) {
3495 ret = btrfs_readpage(NULL, page);
3496 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003497 if (page->mapping != mapping) {
3498 unlock_page(page);
3499 page_cache_release(page);
3500 goto again;
3501 }
Chris Masona52d9a82007-08-27 16:49:44 -04003502 if (!PageUptodate(page)) {
3503 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003504 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003505 }
3506 }
Chris Mason211c17f2008-05-15 09:13:45 -04003507 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003508
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003509 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003510 set_page_extent_mapped(page);
3511
3512 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3513 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003514 unlock_extent_cached(io_tree, page_start, page_end,
3515 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003516 unlock_page(page);
3517 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003518 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003519 btrfs_put_ordered_extent(ordered);
3520 goto again;
3521 }
3522
Josef Bacik2ac55d42010-02-03 19:33:23 +00003523 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003524 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003525 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003526
Josef Bacik2ac55d42010-02-03 19:33:23 +00003527 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3528 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003529 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003530 unlock_extent_cached(io_tree, page_start, page_end,
3531 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003532 goto out_unlock;
3533 }
3534
Chris Masone6dcd2d2008-07-17 12:53:50 -04003535 ret = 0;
3536 if (offset != PAGE_CACHE_SIZE) {
3537 kaddr = kmap(page);
3538 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3539 flush_dcache_page(page);
3540 kunmap(page);
3541 }
Chris Mason247e7432008-07-17 12:53:51 -04003542 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003543 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003544 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3545 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003546
Chris Mason89642222008-07-24 09:41:53 -04003547out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003548 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003549 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003550 unlock_page(page);
3551 page_cache_release(page);
3552out:
3553 return ret;
3554}
3555
Josef Bacik695a0d02011-03-04 15:46:53 -05003556/*
3557 * This function puts in dummy file extents for the area we're creating a hole
3558 * for. So if we are truncating this file to a larger size we need to insert
3559 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3560 * the range between oldsize and size
3561 */
Josef Bacika41ad392011-01-31 15:30:16 -05003562int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003563{
3564 struct btrfs_trans_handle *trans;
3565 struct btrfs_root *root = BTRFS_I(inode)->root;
3566 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003567 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003568 struct extent_state *cached_state = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003569 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003570 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003571 u64 block_end = (size + mask) & ~mask;
3572 u64 last_byte;
3573 u64 cur_offset;
3574 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003575 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003576
3577 if (size <= hole_start)
3578 return 0;
3579
Yan Zheng9036c102008-10-30 14:19:41 -04003580 while (1) {
3581 struct btrfs_ordered_extent *ordered;
3582 btrfs_wait_ordered_range(inode, hole_start,
3583 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003584 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003585 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04003586 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3587 if (!ordered)
3588 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003589 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3590 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003591 btrfs_put_ordered_extent(ordered);
3592 }
3593
Yan Zheng9036c102008-10-30 14:19:41 -04003594 cur_offset = hole_start;
3595 while (1) {
3596 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3597 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003598 if (IS_ERR(em)) {
3599 err = PTR_ERR(em);
3600 break;
3601 }
Yan Zheng9036c102008-10-30 14:19:41 -04003602 last_byte = min(extent_map_end(em), block_end);
3603 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003604 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003605 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003606 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003607
Miao Xie36423202011-12-14 20:12:02 -05003608 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003609 if (IS_ERR(trans)) {
3610 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003611 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003612 }
Yan, Zheng80825102009-11-12 09:35:36 +00003613
3614 err = btrfs_drop_extents(trans, inode, cur_offset,
3615 cur_offset + hole_size,
3616 &hint_byte, 1);
Miao Xie5b397372011-09-11 10:52:24 -04003617 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003618 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003619 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003620 break;
Miao Xie5b397372011-09-11 10:52:24 -04003621 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003622
Yan Zheng9036c102008-10-30 14:19:41 -04003623 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003624 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003625 0, hole_size, 0, hole_size,
3626 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04003627 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003628 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003629 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003630 break;
Miao Xie5b397372011-09-11 10:52:24 -04003631 }
Yan, Zheng80825102009-11-12 09:35:36 +00003632
Yan Zheng9036c102008-10-30 14:19:41 -04003633 btrfs_drop_extent_cache(inode, hole_start,
3634 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003635
Miao Xie36423202011-12-14 20:12:02 -05003636 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003637 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003638 }
3639 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003640 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003641 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003642 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003643 break;
3644 }
3645
Yan, Zhenga22285a2010-05-16 10:48:46 -04003646 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003647 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3648 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003649 return err;
3650}
3651
Josef Bacika41ad392011-01-31 15:30:16 -05003652static int btrfs_setsize(struct inode *inode, loff_t newsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003653{
Miao Xief4a2f4c2011-12-14 20:12:01 -05003654 struct btrfs_root *root = BTRFS_I(inode)->root;
3655 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05003656 loff_t oldsize = i_size_read(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003657 int ret;
3658
Josef Bacika41ad392011-01-31 15:30:16 -05003659 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003660 return 0;
3661
Josef Bacika41ad392011-01-31 15:30:16 -05003662 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05003663 truncate_pagecache(inode, oldsize, newsize);
3664 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05003665 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00003666 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003667
Miao Xief4a2f4c2011-12-14 20:12:01 -05003668 trans = btrfs_start_transaction(root, 1);
3669 if (IS_ERR(trans))
3670 return PTR_ERR(trans);
3671
3672 i_size_write(inode, newsize);
3673 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3674 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003675 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05003676 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003677
Josef Bacika41ad392011-01-31 15:30:16 -05003678 /*
3679 * We're truncating a file that used to have good data down to
3680 * zero. Make sure it gets into the ordered flush list so that
3681 * any new writes get down to disk quickly.
3682 */
3683 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04003684 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
3685 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00003686
Josef Bacika41ad392011-01-31 15:30:16 -05003687 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3688 truncate_setsize(inode, newsize);
3689 ret = btrfs_truncate(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003690 }
3691
Josef Bacika41ad392011-01-31 15:30:16 -05003692 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003693}
3694
Chris Mason39279cc2007-06-12 06:35:45 -04003695static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3696{
3697 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003698 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003699 int err;
3700
Li Zefanb83cc962010-12-20 16:04:08 +08003701 if (btrfs_root_readonly(root))
3702 return -EROFS;
3703
Chris Mason39279cc2007-06-12 06:35:45 -04003704 err = inode_change_ok(inode, attr);
3705 if (err)
3706 return err;
3707
Chris Mason5a3f23d2009-03-31 13:27:11 -04003708 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Josef Bacika41ad392011-01-31 15:30:16 -05003709 err = btrfs_setsize(inode, attr->ia_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003710 if (err)
3711 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003712 }
Yan Zheng9036c102008-10-30 14:19:41 -04003713
Christoph Hellwig10257742010-06-04 11:30:02 +02003714 if (attr->ia_valid) {
3715 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003716 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05003717 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003718
Josef Bacik22c44fe2011-11-30 10:45:38 -05003719 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02003720 err = btrfs_acl_chmod(inode);
3721 }
3722
Chris Mason39279cc2007-06-12 06:35:45 -04003723 return err;
3724}
Chris Mason61295eb2008-01-14 16:24:38 -05003725
Al Virobd555972010-06-07 11:35:40 -04003726void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003727{
3728 struct btrfs_trans_handle *trans;
3729 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04003730 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04003731 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003732 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003733 int ret;
3734
liubo1abe9b82011-03-24 11:18:59 +00003735 trace_btrfs_inode_evict(inode);
3736
Chris Mason39279cc2007-06-12 06:35:45 -04003737 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003738 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Chris Mason2cf85722011-07-26 15:35:09 -04003739 btrfs_is_free_space_inode(root, inode)))
Al Virobd555972010-06-07 11:35:40 -04003740 goto no_delete;
3741
Chris Mason39279cc2007-06-12 06:35:45 -04003742 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003743 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003744 goto no_delete;
3745 }
Al Virobd555972010-06-07 11:35:40 -04003746 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003747 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003748
Yan, Zhengc71bf092009-11-12 09:34:40 +00003749 if (root->fs_info->log_root_recovering) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003750 BUG_ON(!test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3751 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00003752 goto no_delete;
3753 }
3754
Yan, Zheng76dda932009-09-21 16:00:26 -04003755 if (inode->i_nlink > 0) {
3756 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3757 goto no_delete;
3758 }
3759
Josef Bacik4289a662011-08-05 13:22:24 -04003760 rsv = btrfs_alloc_block_rsv(root);
3761 if (!rsv) {
3762 btrfs_orphan_del(NULL, inode);
3763 goto no_delete;
3764 }
Josef Bacik4a338542011-08-29 11:01:31 -04003765 rsv->size = min_size;
Josef Bacik726c35f2011-09-26 15:46:06 -04003766 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04003767
Chris Masondbe674a2008-07-17 12:54:05 -04003768 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003769
Josef Bacik4289a662011-08-05 13:22:24 -04003770 /*
3771 * This is a bit simpler than btrfs_truncate since
3772 *
3773 * 1) We've already reserved our space for our orphan item in the
3774 * unlink.
3775 * 2) We're going to delete the inode item, so we don't need to update
3776 * it at all.
3777 *
3778 * So we just need to reserve some slack space in case we add bytes when
3779 * doing the truncate.
3780 */
Yan, Zheng80825102009-11-12 09:35:36 +00003781 while (1) {
Miao Xieaa38a712011-11-18 17:43:00 +08003782 ret = btrfs_block_rsv_refill_noflush(root, rsv, min_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003783
Josef Bacik726c35f2011-09-26 15:46:06 -04003784 /*
3785 * Try and steal from the global reserve since we will
3786 * likely not use this space anyway, we want to try as
3787 * hard as possible to get this to work.
3788 */
3789 if (ret)
3790 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
3791
Yan, Zhengd68fc572010-05-16 10:49:58 -04003792 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04003793 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04003794 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04003795 btrfs_orphan_del(NULL, inode);
3796 btrfs_free_block_rsv(root, rsv);
3797 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003798 }
3799
Josef Bacik4289a662011-08-05 13:22:24 -04003800 trans = btrfs_start_transaction(root, 0);
3801 if (IS_ERR(trans)) {
3802 btrfs_orphan_del(NULL, inode);
3803 btrfs_free_block_rsv(root, rsv);
3804 goto no_delete;
3805 }
3806
3807 trans->block_rsv = rsv;
3808
Yan, Zhengd68fc572010-05-16 10:49:58 -04003809 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003810 if (ret != -EAGAIN)
3811 break;
3812
3813 nr = trans->blocks_used;
3814 btrfs_end_transaction(trans, root);
3815 trans = NULL;
3816 btrfs_btree_balance_dirty(root, nr);
Josef Bacik7b128762008-07-24 12:17:14 -04003817 }
3818
Josef Bacik4289a662011-08-05 13:22:24 -04003819 btrfs_free_block_rsv(root, rsv);
3820
Yan, Zheng80825102009-11-12 09:35:36 +00003821 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04003822 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003823 ret = btrfs_orphan_del(trans, inode);
3824 BUG_ON(ret);
3825 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003826
Josef Bacik4289a662011-08-05 13:22:24 -04003827 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08003828 if (!(root == root->fs_info->tree_root ||
3829 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08003830 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08003831
Chris Masond3c2fdc2007-09-17 10:58:06 -04003832 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003833 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003834 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003835no_delete:
Al Virobd555972010-06-07 11:35:40 -04003836 end_writeback(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003837 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003838}
3839
3840/*
3841 * this returns the key found in the dir entry in the location pointer.
3842 * If no dir entries were found, location->objectid is 0.
3843 */
3844static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3845 struct btrfs_key *location)
3846{
3847 const char *name = dentry->d_name.name;
3848 int namelen = dentry->d_name.len;
3849 struct btrfs_dir_item *di;
3850 struct btrfs_path *path;
3851 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003852 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003853
3854 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07003855 if (!path)
3856 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05003857
Li Zefan33345d012011-04-20 10:31:50 +08003858 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04003859 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003860 if (IS_ERR(di))
3861 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003862
David Sterbac7040052011-04-19 18:00:01 +02003863 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05003864 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003865
Chris Mason5f39d392007-10-15 16:14:19 -04003866 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003867out:
Chris Mason39279cc2007-06-12 06:35:45 -04003868 btrfs_free_path(path);
3869 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003870out_err:
3871 location->objectid = 0;
3872 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003873}
3874
3875/*
3876 * when we hit a tree root in a directory, the btrfs part of the inode
3877 * needs to be changed to reflect the root directory of the tree root. This
3878 * is kind of like crossing a mount point.
3879 */
3880static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003881 struct inode *dir,
3882 struct dentry *dentry,
3883 struct btrfs_key *location,
3884 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003885{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003886 struct btrfs_path *path;
3887 struct btrfs_root *new_root;
3888 struct btrfs_root_ref *ref;
3889 struct extent_buffer *leaf;
3890 int ret;
3891 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003892
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003893 path = btrfs_alloc_path();
3894 if (!path) {
3895 err = -ENOMEM;
3896 goto out;
3897 }
Chris Mason39279cc2007-06-12 06:35:45 -04003898
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003899 err = -ENOENT;
3900 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3901 BTRFS_I(dir)->root->root_key.objectid,
3902 location->objectid);
3903 if (ret) {
3904 if (ret < 0)
3905 err = ret;
3906 goto out;
3907 }
Chris Mason39279cc2007-06-12 06:35:45 -04003908
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003909 leaf = path->nodes[0];
3910 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08003911 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003912 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3913 goto out;
3914
3915 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3916 (unsigned long)(ref + 1),
3917 dentry->d_name.len);
3918 if (ret)
3919 goto out;
3920
David Sterbab3b4aa72011-04-21 01:20:15 +02003921 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003922
3923 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3924 if (IS_ERR(new_root)) {
3925 err = PTR_ERR(new_root);
3926 goto out;
3927 }
3928
3929 if (btrfs_root_refs(&new_root->root_item) == 0) {
3930 err = -ENOENT;
3931 goto out;
3932 }
3933
3934 *sub_root = new_root;
3935 location->objectid = btrfs_root_dirid(&new_root->root_item);
3936 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003937 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003938 err = 0;
3939out:
3940 btrfs_free_path(path);
3941 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003942}
3943
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003944static void inode_tree_add(struct inode *inode)
3945{
3946 struct btrfs_root *root = BTRFS_I(inode)->root;
3947 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003948 struct rb_node **p;
3949 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08003950 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003951again:
3952 p = &root->inode_tree.rb_node;
3953 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003954
Al Viro1d3382c2010-10-23 15:19:20 -04003955 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003956 return;
3957
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003958 spin_lock(&root->inode_lock);
3959 while (*p) {
3960 parent = *p;
3961 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3962
Li Zefan33345d012011-04-20 10:31:50 +08003963 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003964 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08003965 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003966 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003967 else {
3968 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04003969 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003970 rb_erase(parent, &root->inode_tree);
3971 RB_CLEAR_NODE(parent);
3972 spin_unlock(&root->inode_lock);
3973 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003974 }
3975 }
3976 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3977 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3978 spin_unlock(&root->inode_lock);
3979}
3980
3981static void inode_tree_del(struct inode *inode)
3982{
3983 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003984 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003985
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003986 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003987 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003988 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003989 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003990 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003991 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003992 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003993
Josef Bacik0af3d002010-06-21 14:48:16 -04003994 /*
3995 * Free space cache has inodes in the tree root, but the tree root has a
3996 * root_refs of 0, so this could end up dropping the tree root as a
3997 * snapshot, so we need the extra !root->fs_info->tree_root check to
3998 * make sure we don't drop it.
3999 */
4000 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4001 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004002 synchronize_srcu(&root->fs_info->subvol_srcu);
4003 spin_lock(&root->inode_lock);
4004 empty = RB_EMPTY_ROOT(&root->inode_tree);
4005 spin_unlock(&root->inode_lock);
4006 if (empty)
4007 btrfs_add_dead_root(root);
4008 }
4009}
4010
Jeff Mahoney143bede2012-03-01 14:56:26 +01004011void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004012{
4013 struct rb_node *node;
4014 struct rb_node *prev;
4015 struct btrfs_inode *entry;
4016 struct inode *inode;
4017 u64 objectid = 0;
4018
4019 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4020
4021 spin_lock(&root->inode_lock);
4022again:
4023 node = root->inode_tree.rb_node;
4024 prev = NULL;
4025 while (node) {
4026 prev = node;
4027 entry = rb_entry(node, struct btrfs_inode, rb_node);
4028
Li Zefan33345d012011-04-20 10:31:50 +08004029 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004030 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004031 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004032 node = node->rb_right;
4033 else
4034 break;
4035 }
4036 if (!node) {
4037 while (prev) {
4038 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004039 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004040 node = prev;
4041 break;
4042 }
4043 prev = rb_next(prev);
4044 }
4045 }
4046 while (node) {
4047 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004048 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004049 inode = igrab(&entry->vfs_inode);
4050 if (inode) {
4051 spin_unlock(&root->inode_lock);
4052 if (atomic_read(&inode->i_count) > 1)
4053 d_prune_aliases(inode);
4054 /*
Al Viro45321ac2010-06-07 13:43:19 -04004055 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004056 * the inode cache when its usage count
4057 * hits zero.
4058 */
4059 iput(inode);
4060 cond_resched();
4061 spin_lock(&root->inode_lock);
4062 goto again;
4063 }
4064
4065 if (cond_resched_lock(&root->inode_lock))
4066 goto again;
4067
4068 node = rb_next(node);
4069 }
4070 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004071}
4072
Chris Masone02119d2008-09-05 16:13:11 -04004073static int btrfs_init_locked_inode(struct inode *inode, void *p)
4074{
4075 struct btrfs_iget_args *args = p;
4076 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004077 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05004078 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004079 return 0;
4080}
4081
4082static int btrfs_find_actor(struct inode *inode, void *opaque)
4083{
4084 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004085 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004086 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004087}
4088
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004089static struct inode *btrfs_iget_locked(struct super_block *s,
4090 u64 objectid,
4091 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004092{
4093 struct inode *inode;
4094 struct btrfs_iget_args args;
4095 args.ino = objectid;
4096 args.root = root;
4097
4098 inode = iget5_locked(s, objectid, btrfs_find_actor,
4099 btrfs_init_locked_inode,
4100 (void *)&args);
4101 return inode;
4102}
4103
Balaji Rao1a54ef82008-07-21 02:01:04 +05304104/* Get an inode object given its location and corresponding root.
4105 * Returns in *is_new if the inode was read from disk
4106 */
4107struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004108 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304109{
4110 struct inode *inode;
4111
4112 inode = btrfs_iget_locked(s, location->objectid, root);
4113 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004114 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304115
4116 if (inode->i_state & I_NEW) {
4117 BTRFS_I(inode)->root = root;
4118 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4119 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004120 if (!is_bad_inode(inode)) {
4121 inode_tree_add(inode);
4122 unlock_new_inode(inode);
4123 if (new)
4124 *new = 1;
4125 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004126 unlock_new_inode(inode);
4127 iput(inode);
4128 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004129 }
4130 }
4131
Balaji Rao1a54ef82008-07-21 02:01:04 +05304132 return inode;
4133}
4134
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004135static struct inode *new_simple_dir(struct super_block *s,
4136 struct btrfs_key *key,
4137 struct btrfs_root *root)
4138{
4139 struct inode *inode = new_inode(s);
4140
4141 if (!inode)
4142 return ERR_PTR(-ENOMEM);
4143
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004144 BTRFS_I(inode)->root = root;
4145 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004146 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004147
4148 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004149 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004150 inode->i_fop = &simple_dir_operations;
4151 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4152 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4153
4154 return inode;
4155}
4156
Chris Mason3de45862008-11-17 21:02:50 -05004157struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004158{
Chris Masond3977122009-01-05 21:25:51 -05004159 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004160 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004161 struct btrfs_root *sub_root = root;
4162 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004163 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004164 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004165
4166 if (dentry->d_name.len > BTRFS_NAME_LEN)
4167 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004168
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004169 if (unlikely(d_need_lookup(dentry))) {
4170 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4171 kfree(dentry->d_fsdata);
4172 dentry->d_fsdata = NULL;
Josef Bacika66e7cc2011-09-18 10:34:03 -04004173 /* This thing is hashed, drop it for now */
4174 d_drop(dentry);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004175 } else {
4176 ret = btrfs_inode_by_name(dir, dentry, &location);
4177 }
Chris Mason5f39d392007-10-15 16:14:19 -04004178
Chris Mason39279cc2007-06-12 06:35:45 -04004179 if (ret < 0)
4180 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004181
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004182 if (location.objectid == 0)
4183 return NULL;
4184
4185 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004186 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004187 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004188 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004189
4190 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4191
Yan, Zheng76dda932009-09-21 16:00:26 -04004192 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004193 ret = fixup_tree_root_location(root, dir, dentry,
4194 &location, &sub_root);
4195 if (ret < 0) {
4196 if (ret != -ENOENT)
4197 inode = ERR_PTR(ret);
4198 else
4199 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4200 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004201 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004202 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004203 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4204
Julia Lawall34d19ba2011-01-24 19:55:19 +00004205 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004206 down_read(&root->fs_info->cleanup_work_sem);
4207 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004208 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004209 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004210 if (ret)
4211 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004212 }
4213
Chris Mason3de45862008-11-17 21:02:50 -05004214 return inode;
4215}
4216
Nick Pigginfe15ce42011-01-07 17:49:23 +11004217static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004218{
4219 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004220 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004221
Li Zefan848cce02012-02-21 17:04:28 +08004222 if (!inode && !IS_ROOT(dentry))
4223 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004224
Li Zefan848cce02012-02-21 17:04:28 +08004225 if (inode) {
4226 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004227 if (btrfs_root_refs(&root->root_item) == 0)
4228 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004229
4230 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4231 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004232 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004233 return 0;
4234}
4235
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004236static void btrfs_dentry_release(struct dentry *dentry)
4237{
4238 if (dentry->d_fsdata)
4239 kfree(dentry->d_fsdata);
4240}
4241
Chris Mason3de45862008-11-17 21:02:50 -05004242static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4243 struct nameidata *nd)
4244{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004245 struct dentry *ret;
4246
4247 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4248 if (unlikely(d_need_lookup(dentry))) {
4249 spin_lock(&dentry->d_lock);
4250 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4251 spin_unlock(&dentry->d_lock);
4252 }
4253 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004254}
4255
Miao Xie16cdcec2011-04-22 18:12:22 +08004256unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004257 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4258};
4259
David Woodhousecbdf5a22008-08-06 19:42:33 +01004260static int btrfs_real_readdir(struct file *filp, void *dirent,
4261 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004262{
Chris Mason6da6aba2007-12-18 16:15:09 -05004263 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004264 struct btrfs_root *root = BTRFS_I(inode)->root;
4265 struct btrfs_item *item;
4266 struct btrfs_dir_item *di;
4267 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004268 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004269 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004270 struct list_head ins_list;
4271 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004272 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004273 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004274 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004275 unsigned char d_type;
4276 int over = 0;
4277 u32 di_cur;
4278 u32 di_total;
4279 u32 di_len;
4280 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004281 char tmp_name[32];
4282 char *name_ptr;
4283 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004284 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004285
4286 /* FIXME, use a real flag for deciding about the key type */
4287 if (root->fs_info->tree_root == root)
4288 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004289
Chris Mason39544012007-12-12 14:38:19 -05004290 /* special case for "." */
4291 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004292 over = filldir(dirent, ".", 1,
4293 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004294 if (over)
4295 return 0;
4296 filp->f_pos = 1;
4297 }
Chris Mason39544012007-12-12 14:38:19 -05004298 /* special case for .., just use the back ref */
4299 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004300 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004301 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004302 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004303 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004304 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004305 filp->f_pos = 2;
4306 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004307 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004308 if (!path)
4309 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004310
Josef Bacik026fd312011-05-13 10:32:11 -04004311 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004312
Miao Xie16cdcec2011-04-22 18:12:22 +08004313 if (key_type == BTRFS_DIR_INDEX_KEY) {
4314 INIT_LIST_HEAD(&ins_list);
4315 INIT_LIST_HEAD(&del_list);
4316 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4317 }
4318
Chris Mason39279cc2007-06-12 06:35:45 -04004319 btrfs_set_key_type(&key, key_type);
4320 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004321 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004322
Chris Mason39279cc2007-06-12 06:35:45 -04004323 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4324 if (ret < 0)
4325 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004326
4327 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004328 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004329 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004330 if (slot >= btrfs_header_nritems(leaf)) {
4331 ret = btrfs_next_leaf(root, path);
4332 if (ret < 0)
4333 goto err;
4334 else if (ret > 0)
4335 break;
4336 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004337 }
Chris Mason3de45862008-11-17 21:02:50 -05004338
Chris Mason5f39d392007-10-15 16:14:19 -04004339 item = btrfs_item_nr(leaf, slot);
4340 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4341
4342 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004343 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004344 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004345 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004346 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004347 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004348 if (key_type == BTRFS_DIR_INDEX_KEY &&
4349 btrfs_should_delete_dir_index(&del_list,
4350 found_key.offset))
4351 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004352
4353 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004354 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004355
Chris Mason39279cc2007-06-12 06:35:45 -04004356 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4357 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004358 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004359
4360 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004361 struct btrfs_key location;
4362
Josef Bacik22a94d42011-03-16 16:47:17 -04004363 if (verify_dir_item(root, leaf, di))
4364 break;
4365
Chris Mason5f39d392007-10-15 16:14:19 -04004366 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004367 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004368 name_ptr = tmp_name;
4369 } else {
4370 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004371 if (!name_ptr) {
4372 ret = -ENOMEM;
4373 goto err;
4374 }
Chris Mason5f39d392007-10-15 16:14:19 -04004375 }
4376 read_extent_buffer(leaf, name_ptr,
4377 (unsigned long)(di + 1), name_len);
4378
4379 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4380 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004381
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004382
Chris Mason3de45862008-11-17 21:02:50 -05004383 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01004384 * skip it.
4385 *
4386 * In contrast to old kernels, we insert the snapshot's
4387 * dir item and dir index after it has been created, so
4388 * we won't find a reference to our own snapshot. We
4389 * still keep the following code for backward
4390 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05004391 */
4392 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4393 location.objectid == root->root_key.objectid) {
4394 over = 0;
4395 goto skip;
4396 }
Chris Mason5f39d392007-10-15 16:14:19 -04004397 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004398 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004399 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004400
Chris Mason3de45862008-11-17 21:02:50 -05004401skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004402 if (name_ptr != tmp_name)
4403 kfree(name_ptr);
4404
Chris Mason39279cc2007-06-12 06:35:45 -04004405 if (over)
4406 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004407 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004408 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004409 di_cur += di_len;
4410 di = (struct btrfs_dir_item *)((char *)di + di_len);
4411 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004412next:
4413 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004414 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004415
Miao Xie16cdcec2011-04-22 18:12:22 +08004416 if (key_type == BTRFS_DIR_INDEX_KEY) {
4417 if (is_curr)
4418 filp->f_pos++;
4419 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4420 &ins_list);
4421 if (ret)
4422 goto nopos;
4423 }
4424
David Woodhouse49593bf2008-08-17 17:08:36 +01004425 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004426 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004427 /*
4428 * 32-bit glibc will use getdents64, but then strtol -
4429 * so the last number we can serve is this.
4430 */
4431 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004432 else
4433 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004434nopos:
4435 ret = 0;
4436err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004437 if (key_type == BTRFS_DIR_INDEX_KEY)
4438 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004439 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004440 return ret;
4441}
4442
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004443int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004444{
4445 struct btrfs_root *root = BTRFS_I(inode)->root;
4446 struct btrfs_trans_handle *trans;
4447 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004448 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004449
Josef Bacik72ac3c02012-05-23 14:13:11 -04004450 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04004451 return 0;
4452
Chris Mason2cf85722011-07-26 15:35:09 -04004453 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(root, inode))
Li Zefan82d59022011-04-20 10:33:24 +08004454 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004455
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004456 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004457 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04004458 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004459 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04004460 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004461 if (IS_ERR(trans))
4462 return PTR_ERR(trans);
Josef Bacik0af3d002010-06-21 14:48:16 -04004463 if (nolock)
4464 ret = btrfs_end_transaction_nolock(trans, root);
4465 else
4466 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004467 }
4468 return ret;
4469}
4470
4471/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004472 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004473 * inode changes. But, it is most likely to find the inode in cache.
4474 * FIXME, needs more benchmarking...there are no reasons other than performance
4475 * to keep or drop this code.
4476 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05004477int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004478{
4479 struct btrfs_root *root = BTRFS_I(inode)->root;
4480 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004481 int ret;
4482
Josef Bacik72ac3c02012-05-23 14:13:11 -04004483 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05004484 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004485
Josef Bacik7a7eaa402011-04-13 12:54:33 -04004486 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004487 if (IS_ERR(trans))
4488 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004489
4490 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004491 if (ret && ret == -ENOSPC) {
4492 /* whoops, lets try again with the full transaction */
4493 btrfs_end_transaction(trans, root);
4494 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004495 if (IS_ERR(trans))
4496 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004497
Chris Mason94b60442010-05-26 11:02:00 -04004498 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004499 }
Chris Mason39279cc2007-06-12 06:35:45 -04004500 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004501 if (BTRFS_I(inode)->delayed_node)
4502 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004503
4504 return ret;
4505}
4506
4507/*
4508 * This is a copy of file_update_time. We need this so we can return error on
4509 * ENOSPC for updating the inode in the case of file write and mmap writes.
4510 */
4511int btrfs_update_time(struct file *file)
4512{
4513 struct inode *inode = file->f_path.dentry->d_inode;
4514 struct timespec now;
4515 int ret;
4516 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
4517
4518 /* First try to exhaust all avenues to not sync */
4519 if (IS_NOCMTIME(inode))
4520 return 0;
4521
4522 now = current_fs_time(inode->i_sb);
4523 if (!timespec_equal(&inode->i_mtime, &now))
4524 sync_it = S_MTIME;
4525
4526 if (!timespec_equal(&inode->i_ctime, &now))
4527 sync_it |= S_CTIME;
4528
4529 if (IS_I_VERSION(inode))
4530 sync_it |= S_VERSION;
4531
4532 if (!sync_it)
4533 return 0;
4534
4535 /* Finally allowed to write? Takes lock. */
4536 if (mnt_want_write_file(file))
4537 return 0;
4538
4539 /* Only change inode inside the lock region */
4540 if (sync_it & S_VERSION)
4541 inode_inc_iversion(inode);
4542 if (sync_it & S_CTIME)
4543 inode->i_ctime = now;
4544 if (sync_it & S_MTIME)
4545 inode->i_mtime = now;
4546 ret = btrfs_dirty_inode(inode);
4547 if (!ret)
4548 mark_inode_dirty_sync(inode);
4549 mnt_drop_write(file->f_path.mnt);
4550 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004551}
4552
Chris Masond352ac62008-09-29 15:18:18 -04004553/*
4554 * find the highest existing sequence number in a directory
4555 * and then set the in-memory index_cnt variable to reflect
4556 * free sequence numbers
4557 */
Josef Bacikaec74772008-07-24 12:12:38 -04004558static int btrfs_set_inode_index_count(struct inode *inode)
4559{
4560 struct btrfs_root *root = BTRFS_I(inode)->root;
4561 struct btrfs_key key, found_key;
4562 struct btrfs_path *path;
4563 struct extent_buffer *leaf;
4564 int ret;
4565
Li Zefan33345d012011-04-20 10:31:50 +08004566 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004567 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4568 key.offset = (u64)-1;
4569
4570 path = btrfs_alloc_path();
4571 if (!path)
4572 return -ENOMEM;
4573
4574 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4575 if (ret < 0)
4576 goto out;
4577 /* FIXME: we should be able to handle this */
4578 if (ret == 0)
4579 goto out;
4580 ret = 0;
4581
4582 /*
4583 * MAGIC NUMBER EXPLANATION:
4584 * since we search a directory based on f_pos we have to start at 2
4585 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4586 * else has to start at 2
4587 */
4588 if (path->slots[0] == 0) {
4589 BTRFS_I(inode)->index_cnt = 2;
4590 goto out;
4591 }
4592
4593 path->slots[0]--;
4594
4595 leaf = path->nodes[0];
4596 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4597
Li Zefan33345d012011-04-20 10:31:50 +08004598 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004599 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4600 BTRFS_I(inode)->index_cnt = 2;
4601 goto out;
4602 }
4603
4604 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4605out:
4606 btrfs_free_path(path);
4607 return ret;
4608}
4609
Chris Masond352ac62008-09-29 15:18:18 -04004610/*
4611 * helper to find a free sequence number in a given directory. This current
4612 * code is very simple, later versions will do smarter things in the btree
4613 */
Chris Mason3de45862008-11-17 21:02:50 -05004614int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004615{
4616 int ret = 0;
4617
4618 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004619 ret = btrfs_inode_delayed_dir_index_count(dir);
4620 if (ret) {
4621 ret = btrfs_set_inode_index_count(dir);
4622 if (ret)
4623 return ret;
4624 }
Josef Bacikaec74772008-07-24 12:12:38 -04004625 }
4626
Chris Mason00e4e6b2008-08-05 11:18:09 -04004627 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004628 BTRFS_I(dir)->index_cnt++;
4629
4630 return ret;
4631}
4632
Chris Mason39279cc2007-06-12 06:35:45 -04004633static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4634 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004635 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004636 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04004637 u64 ref_objectid, u64 objectid,
4638 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004639{
4640 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004641 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004642 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004643 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004644 struct btrfs_inode_ref *ref;
4645 struct btrfs_key key[2];
4646 u32 sizes[2];
4647 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004648 int ret;
4649 int owner;
4650
Chris Mason5f39d392007-10-15 16:14:19 -04004651 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004652 if (!path)
4653 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04004654
Chris Mason39279cc2007-06-12 06:35:45 -04004655 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004656 if (!inode) {
4657 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004658 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004659 }
Chris Mason39279cc2007-06-12 06:35:45 -04004660
Li Zefan581bb052011-04-20 10:06:11 +08004661 /*
4662 * we have to initialize this early, so we can reclaim the inode
4663 * number if we fail afterwards in this function.
4664 */
4665 inode->i_ino = objectid;
4666
Josef Bacikaec74772008-07-24 12:12:38 -04004667 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004668 trace_btrfs_inode_request(dir);
4669
Chris Mason3de45862008-11-17 21:02:50 -05004670 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004671 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004672 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004673 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004674 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004675 }
Josef Bacikaec74772008-07-24 12:12:38 -04004676 }
4677 /*
4678 * index_cnt is ignored for everything but a dir,
4679 * btrfs_get_inode_index_count has an explanation for the magic
4680 * number
4681 */
4682 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004683 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004684 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004685 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik6a632092009-02-20 11:00:09 -05004686 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004687
Al Viro569254b2011-07-24 17:08:40 -04004688 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04004689 owner = 0;
4690 else
4691 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004692
4693 key[0].objectid = objectid;
4694 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4695 key[0].offset = 0;
4696
4697 key[1].objectid = objectid;
4698 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4699 key[1].offset = ref_objectid;
4700
4701 sizes[0] = sizeof(struct btrfs_inode_item);
4702 sizes[1] = name_len + sizeof(*ref);
4703
Chris Masonb9473432009-03-13 11:00:37 -04004704 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004705 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4706 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004707 goto fail;
4708
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004709 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004710 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004711 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004712 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4713 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004714 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004715
4716 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4717 struct btrfs_inode_ref);
4718 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004719 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004720 ptr = (unsigned long)(ref + 1);
4721 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4722
Chris Mason5f39d392007-10-15 16:14:19 -04004723 btrfs_mark_buffer_dirty(path->nodes[0]);
4724 btrfs_free_path(path);
4725
Chris Mason39279cc2007-06-12 06:35:45 -04004726 location = &BTRFS_I(inode)->location;
4727 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004728 location->offset = 0;
4729 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4730
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004731 btrfs_inherit_iflags(inode, dir);
4732
Al Viro569254b2011-07-24 17:08:40 -04004733 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04004734 if (btrfs_test_opt(root, NODATASUM))
4735 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo75e7cb72011-03-22 10:12:20 +00004736 if (btrfs_test_opt(root, NODATACOW) ||
4737 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004738 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4739 }
4740
Chris Mason39279cc2007-06-12 06:35:45 -04004741 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004742 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004743
4744 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04004745 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00004746
Chris Mason39279cc2007-06-12 06:35:45 -04004747 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004748fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004749 if (dir)
4750 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004751 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004752 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004753 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004754}
4755
4756static inline u8 btrfs_inode_type(struct inode *inode)
4757{
4758 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4759}
4760
Chris Masond352ac62008-09-29 15:18:18 -04004761/*
4762 * utility function to add 'inode' into 'parent_inode' with
4763 * a give name and a given sequence number.
4764 * if 'add_backref' is true, also insert a backref from the
4765 * inode to the parent directory.
4766 */
Chris Masone02119d2008-09-05 16:13:11 -04004767int btrfs_add_link(struct btrfs_trans_handle *trans,
4768 struct inode *parent_inode, struct inode *inode,
4769 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004770{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004771 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004772 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004773 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004774 u64 ino = btrfs_ino(inode);
4775 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004776
Li Zefan33345d012011-04-20 10:31:50 +08004777 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004778 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4779 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004780 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004781 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4782 key.offset = 0;
4783 }
Chris Mason39279cc2007-06-12 06:35:45 -04004784
Li Zefan33345d012011-04-20 10:31:50 +08004785 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004786 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4787 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004788 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004789 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004790 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4791 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004792 }
4793
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004794 /* Nothing to clean up yet */
4795 if (ret)
4796 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004797
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004798 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4799 parent_inode, &key,
4800 btrfs_inode_type(inode), index);
4801 if (ret == -EEXIST)
4802 goto fail_dir_item;
4803 else if (ret) {
4804 btrfs_abort_transaction(trans, root, ret);
4805 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004806 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004807
4808 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4809 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004810 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004811 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4812 ret = btrfs_update_inode(trans, root, parent_inode);
4813 if (ret)
4814 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004815 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05004816
4817fail_dir_item:
4818 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4819 u64 local_index;
4820 int err;
4821 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4822 key.objectid, root->root_key.objectid,
4823 parent_ino, &local_index, name, name_len);
4824
4825 } else if (add_backref) {
4826 u64 local_index;
4827 int err;
4828
4829 err = btrfs_del_inode_ref(trans, root, name, name_len,
4830 ino, parent_ino, &local_index);
4831 }
4832 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004833}
4834
4835static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004836 struct inode *dir, struct dentry *dentry,
4837 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004838{
Josef Bacika1b075d2010-11-19 20:36:11 +00004839 int err = btrfs_add_link(trans, dir, inode,
4840 dentry->d_name.name, dentry->d_name.len,
4841 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004842 if (err > 0)
4843 err = -EEXIST;
4844 return err;
4845}
4846
Josef Bacik618e21d2007-07-11 10:18:17 -04004847static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04004848 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04004849{
4850 struct btrfs_trans_handle *trans;
4851 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004852 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004853 int err;
4854 int drop_inode = 0;
4855 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004856 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004857 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004858
4859 if (!new_valid_dev(rdev))
4860 return -EINVAL;
4861
Josef Bacik9ed74f22009-09-11 16:12:44 -04004862 /*
4863 * 2 for inode item and ref
4864 * 2 for dir items
4865 * 1 for xattr if selinux is on
4866 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004867 trans = btrfs_start_transaction(root, 5);
4868 if (IS_ERR(trans))
4869 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004870
Li Zefan581bb052011-04-20 10:06:11 +08004871 err = btrfs_find_free_ino(root, &objectid);
4872 if (err)
4873 goto out_unlock;
4874
Josef Bacikaec74772008-07-24 12:12:38 -04004875 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004876 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004877 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004878 if (IS_ERR(inode)) {
4879 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004880 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004881 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004882
Eric Paris2a7dba32011-02-01 11:05:39 -05004883 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004884 if (err) {
4885 drop_inode = 1;
4886 goto out_unlock;
4887 }
4888
Casey Schauflerad19db72011-12-15 10:09:07 -05004889 /*
4890 * If the active LSM wants to access the inode during
4891 * d_instantiate it needs these. Smack checks to see
4892 * if the filesystem supports xattrs by looking at the
4893 * ops vector.
4894 */
4895
4896 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00004897 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004898 if (err)
4899 drop_inode = 1;
4900 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04004901 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004902 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05004903 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004904 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004905out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004906 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004907 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004908 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004909 if (drop_inode) {
4910 inode_dec_link_count(inode);
4911 iput(inode);
4912 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004913 return err;
4914}
4915
Chris Mason39279cc2007-06-12 06:35:45 -04004916static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viro4acdaf22011-07-26 01:42:34 -04004917 umode_t mode, struct nameidata *nd)
Chris Mason39279cc2007-06-12 06:35:45 -04004918{
4919 struct btrfs_trans_handle *trans;
4920 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004921 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004922 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004923 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004924 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004925 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004926 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004927
Josef Bacik9ed74f22009-09-11 16:12:44 -04004928 /*
4929 * 2 for inode item and ref
4930 * 2 for dir items
4931 * 1 for xattr if selinux is on
4932 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004933 trans = btrfs_start_transaction(root, 5);
4934 if (IS_ERR(trans))
4935 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004936
Li Zefan581bb052011-04-20 10:06:11 +08004937 err = btrfs_find_free_ino(root, &objectid);
4938 if (err)
4939 goto out_unlock;
4940
Josef Bacikaec74772008-07-24 12:12:38 -04004941 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004942 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004943 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004944 if (IS_ERR(inode)) {
4945 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004946 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004947 }
Chris Mason39279cc2007-06-12 06:35:45 -04004948
Eric Paris2a7dba32011-02-01 11:05:39 -05004949 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004950 if (err) {
4951 drop_inode = 1;
4952 goto out_unlock;
4953 }
4954
Casey Schauflerad19db72011-12-15 10:09:07 -05004955 /*
4956 * If the active LSM wants to access the inode during
4957 * d_instantiate it needs these. Smack checks to see
4958 * if the filesystem supports xattrs by looking at the
4959 * ops vector.
4960 */
4961 inode->i_fop = &btrfs_file_operations;
4962 inode->i_op = &btrfs_file_inode_operations;
4963
Josef Bacika1b075d2010-11-19 20:36:11 +00004964 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004965 if (err)
4966 drop_inode = 1;
4967 else {
4968 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004969 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05004970 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro08c422c2011-12-23 07:58:13 -05004971 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004972 }
Chris Mason39279cc2007-06-12 06:35:45 -04004973out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004974 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004975 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004976 if (drop_inode) {
4977 inode_dec_link_count(inode);
4978 iput(inode);
4979 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04004980 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004981 return err;
4982}
4983
4984static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4985 struct dentry *dentry)
4986{
4987 struct btrfs_trans_handle *trans;
4988 struct btrfs_root *root = BTRFS_I(dir)->root;
4989 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004990 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004991 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004992 int err;
4993 int drop_inode = 0;
4994
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004995 /* do not allow sys_link's with other subvols of the same device */
4996 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00004997 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004998
Al Viroc055e992011-03-04 17:15:18 +00004999 if (inode->i_nlink == ~0U)
5000 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005001
Chris Mason3de45862008-11-17 21:02:50 -05005002 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005003 if (err)
5004 goto fail;
5005
Yan, Zhenga22285a2010-05-16 10:48:46 -04005006 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005007 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005008 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005009 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005010 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005011 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005012 if (IS_ERR(trans)) {
5013 err = PTR_ERR(trans);
5014 goto fail;
5015 }
Chris Mason5f39d392007-10-15 16:14:19 -04005016
Miao Xie31534952011-04-13 13:19:21 +08005017 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005018 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005019 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6e2010-10-23 11:11:40 -04005020 ihold(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005021
Josef Bacika1b075d2010-11-19 20:36:11 +00005022 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005023
Yan, Zhenga5719522009-09-24 09:17:31 -04005024 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005025 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005026 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005027 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005028 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005029 if (err)
5030 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005031 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005032 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005033 }
Chris Mason39279cc2007-06-12 06:35:45 -04005034
Chris Masond3c2fdc2007-09-17 10:58:06 -04005035 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005036 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005037fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005038 if (drop_inode) {
5039 inode_dec_link_count(inode);
5040 iput(inode);
5041 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04005042 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005043 return err;
5044}
5045
Al Viro18bb1db2011-07-26 01:41:39 -04005046static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005047{
Chris Masonb9d86662008-05-02 16:13:49 -04005048 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005049 struct btrfs_trans_handle *trans;
5050 struct btrfs_root *root = BTRFS_I(dir)->root;
5051 int err = 0;
5052 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005053 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005054 u64 index = 0;
Chris Masond3c2fdc2007-09-17 10:58:06 -04005055 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005056
Josef Bacik9ed74f22009-09-11 16:12:44 -04005057 /*
5058 * 2 items for inode and ref
5059 * 2 items for dir items
5060 * 1 for xattr if selinux is on
5061 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005062 trans = btrfs_start_transaction(root, 5);
5063 if (IS_ERR(trans))
5064 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005065
Li Zefan581bb052011-04-20 10:06:11 +08005066 err = btrfs_find_free_ino(root, &objectid);
5067 if (err)
5068 goto out_fail;
5069
Josef Bacikaec74772008-07-24 12:12:38 -04005070 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005071 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005072 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005073 if (IS_ERR(inode)) {
5074 err = PTR_ERR(inode);
5075 goto out_fail;
5076 }
Chris Mason5f39d392007-10-15 16:14:19 -04005077
Chris Mason39279cc2007-06-12 06:35:45 -04005078 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005079
Eric Paris2a7dba32011-02-01 11:05:39 -05005080 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005081 if (err)
5082 goto out_fail;
5083
Chris Mason39279cc2007-06-12 06:35:45 -04005084 inode->i_op = &btrfs_dir_inode_operations;
5085 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005086
Chris Masondbe674a2008-07-17 12:54:05 -04005087 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005088 err = btrfs_update_inode(trans, root, inode);
5089 if (err)
5090 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005091
Josef Bacika1b075d2010-11-19 20:36:11 +00005092 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5093 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005094 if (err)
5095 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005096
Chris Mason39279cc2007-06-12 06:35:45 -04005097 d_instantiate(dentry, inode);
5098 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005099
5100out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04005101 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005102 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005103 if (drop_on_err)
5104 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04005105 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005106 return err;
5107}
5108
Chris Masond352ac62008-09-29 15:18:18 -04005109/* helper for btfs_get_extent. Given an existing extent in the tree,
5110 * and an extent that you want to insert, deal with overlap and insert
5111 * the new extent into the tree.
5112 */
Chris Mason3b951512008-04-17 11:29:12 -04005113static int merge_extent_mapping(struct extent_map_tree *em_tree,
5114 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005115 struct extent_map *em,
5116 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005117{
5118 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005119
Chris Masone6dcd2d2008-07-17 12:53:50 -04005120 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5121 start_diff = map_start - em->start;
5122 em->start = map_start;
5123 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005124 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5125 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005126 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005127 em->block_len -= start_diff;
5128 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005129 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005130}
5131
Chris Masonc8b97812008-10-29 14:49:59 -04005132static noinline int uncompress_inline(struct btrfs_path *path,
5133 struct inode *inode, struct page *page,
5134 size_t pg_offset, u64 extent_offset,
5135 struct btrfs_file_extent_item *item)
5136{
5137 int ret;
5138 struct extent_buffer *leaf = path->nodes[0];
5139 char *tmp;
5140 size_t max_size;
5141 unsigned long inline_size;
5142 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005143 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005144
5145 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005146 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005147 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5148 inline_size = btrfs_file_extent_inline_item_len(leaf,
5149 btrfs_item_nr(leaf, path->slots[0]));
5150 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005151 if (!tmp)
5152 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005153 ptr = btrfs_file_extent_inline_start(item);
5154
5155 read_extent_buffer(leaf, tmp, ptr, inline_size);
5156
Chris Mason5b050f02008-11-11 09:34:41 -05005157 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005158 ret = btrfs_decompress(compress_type, tmp, page,
5159 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005160 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005161 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005162 unsigned long copy_size = min_t(u64,
5163 PAGE_CACHE_SIZE - pg_offset,
5164 max_size - extent_offset);
5165 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005166 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005167 }
5168 kfree(tmp);
5169 return 0;
5170}
5171
Chris Masond352ac62008-09-29 15:18:18 -04005172/*
5173 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005174 * the ugly parts come from merging extents from the disk with the in-ram
5175 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005176 * where the in-ram extents might be locked pending data=ordered completion.
5177 *
5178 * This also copies inline extents directly into the page.
5179 */
Chris Masond3977122009-01-05 21:25:51 -05005180
Chris Masona52d9a82007-08-27 16:49:44 -04005181struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005182 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005183 int create)
5184{
5185 int ret;
5186 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005187 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005188 u64 extent_start = 0;
5189 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005190 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005191 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005192 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005193 struct btrfs_root *root = BTRFS_I(inode)->root;
5194 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005195 struct extent_buffer *leaf;
5196 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005197 struct extent_map *em = NULL;
5198 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005199 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005200 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005201 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005202
Chris Masona52d9a82007-08-27 16:49:44 -04005203again:
Chris Mason890871b2009-09-02 16:24:52 -04005204 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005205 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005206 if (em)
5207 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005208 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005209
Chris Masona52d9a82007-08-27 16:49:44 -04005210 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005211 if (em->start > start || em->start + em->len <= start)
5212 free_extent_map(em);
5213 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005214 free_extent_map(em);
5215 else
5216 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005217 }
David Sterba172ddd62011-04-21 00:48:27 +02005218 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005219 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005220 err = -ENOMEM;
5221 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005222 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005223 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005224 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005225 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005226 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005227 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005228
5229 if (!path) {
5230 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005231 if (!path) {
5232 err = -ENOMEM;
5233 goto out;
5234 }
5235 /*
5236 * Chances are we'll be called again, so go ahead and do
5237 * readahead
5238 */
5239 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005240 }
5241
Chris Mason179e29e2007-11-01 11:28:41 -04005242 ret = btrfs_lookup_file_extent(trans, root, path,
5243 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005244 if (ret < 0) {
5245 err = ret;
5246 goto out;
5247 }
5248
5249 if (ret != 0) {
5250 if (path->slots[0] == 0)
5251 goto not_found;
5252 path->slots[0]--;
5253 }
5254
Chris Mason5f39d392007-10-15 16:14:19 -04005255 leaf = path->nodes[0];
5256 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005257 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005258 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005259 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5260 found_type = btrfs_key_type(&found_key);
5261 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005262 found_type != BTRFS_EXTENT_DATA_KEY) {
5263 goto not_found;
5264 }
5265
Chris Mason5f39d392007-10-15 16:14:19 -04005266 found_type = btrfs_file_extent_type(leaf, item);
5267 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005268 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005269 if (found_type == BTRFS_FILE_EXTENT_REG ||
5270 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005271 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005272 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005273 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5274 size_t size;
5275 size = btrfs_file_extent_inline_len(leaf, item);
5276 extent_end = (extent_start + size + root->sectorsize - 1) &
5277 ~((u64)root->sectorsize - 1);
5278 }
5279
5280 if (start >= extent_end) {
5281 path->slots[0]++;
5282 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5283 ret = btrfs_next_leaf(root, path);
5284 if (ret < 0) {
5285 err = ret;
5286 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005287 }
Yan Zheng9036c102008-10-30 14:19:41 -04005288 if (ret > 0)
5289 goto not_found;
5290 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005291 }
Yan Zheng9036c102008-10-30 14:19:41 -04005292 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5293 if (found_key.objectid != objectid ||
5294 found_key.type != BTRFS_EXTENT_DATA_KEY)
5295 goto not_found;
5296 if (start + len <= found_key.offset)
5297 goto not_found;
5298 em->start = start;
5299 em->len = found_key.offset - start;
5300 goto not_found_em;
5301 }
5302
Yan Zhengd899e052008-10-30 14:25:28 -04005303 if (found_type == BTRFS_FILE_EXTENT_REG ||
5304 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005305 em->start = extent_start;
5306 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005307 em->orig_start = extent_start -
5308 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005309 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5310 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005311 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005312 goto insert;
5313 }
Li Zefan261507a02010-12-17 14:21:50 +08005314 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005315 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005316 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005317 em->block_start = bytenr;
5318 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5319 item);
5320 } else {
5321 bytenr += btrfs_file_extent_offset(leaf, item);
5322 em->block_start = bytenr;
5323 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005324 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5325 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005326 }
Chris Masona52d9a82007-08-27 16:49:44 -04005327 goto insert;
5328 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005329 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005330 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005331 size_t size;
5332 size_t extent_offset;
5333 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005334
Chris Masona52d9a82007-08-27 16:49:44 -04005335 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005336 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005337 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005338 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005339 goto out;
5340 }
5341
Yan Zheng9036c102008-10-30 14:19:41 -04005342 size = btrfs_file_extent_inline_len(leaf, item);
5343 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005344 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005345 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005346 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005347 em->len = (copy_size + root->sectorsize - 1) &
5348 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005349 em->orig_start = EXTENT_MAP_INLINE;
Li Zefan261507a02010-12-17 14:21:50 +08005350 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005351 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005352 em->compress_type = compress_type;
5353 }
Yan689f9342007-10-29 11:41:07 -04005354 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005355 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005356 if (btrfs_file_extent_compression(leaf, item) !=
5357 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005358 ret = uncompress_inline(path, inode, page,
5359 pg_offset,
5360 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005361 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04005362 } else {
5363 map = kmap(page);
5364 read_extent_buffer(leaf, map + pg_offset, ptr,
5365 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005366 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5367 memset(map + pg_offset + copy_size, 0,
5368 PAGE_CACHE_SIZE - pg_offset -
5369 copy_size);
5370 }
Chris Masonc8b97812008-10-29 14:49:59 -04005371 kunmap(page);
5372 }
Chris Mason179e29e2007-11-01 11:28:41 -04005373 flush_dcache_page(page);
5374 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01005375 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04005376 if (!trans) {
5377 kunmap(page);
5378 free_extent_map(em);
5379 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04005380
David Sterbab3b4aa72011-04-21 01:20:15 +02005381 btrfs_release_path(path);
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005382 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04005383
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005384 if (IS_ERR(trans))
5385 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005386 goto again;
5387 }
Chris Masonc8b97812008-10-29 14:49:59 -04005388 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005389 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005390 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005391 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005392 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005393 }
Chris Masond1310b22008-01-24 16:13:08 -05005394 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005395 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005396 goto insert;
5397 } else {
Chris Masond3977122009-01-05 21:25:51 -05005398 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005399 WARN_ON(1);
5400 }
5401not_found:
5402 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005403 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005404not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005405 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005406 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005407insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02005408 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05005409 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005410 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5411 "[%llu %llu]\n", (unsigned long long)em->start,
5412 (unsigned long long)em->len,
5413 (unsigned long long)start,
5414 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005415 err = -EIO;
5416 goto out;
5417 }
Chris Masond1310b22008-01-24 16:13:08 -05005418
5419 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005420 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005421 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005422 /* it is possible that someone inserted the extent into the tree
5423 * while we had the lock dropped. It is also possible that
5424 * an overlapping map exists in the tree
5425 */
Chris Masona52d9a82007-08-27 16:49:44 -04005426 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005427 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005428
5429 ret = 0;
5430
Chris Mason3b951512008-04-17 11:29:12 -04005431 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005432 if (existing && (existing->start > start ||
5433 existing->start + existing->len <= start)) {
5434 free_extent_map(existing);
5435 existing = NULL;
5436 }
Chris Mason3b951512008-04-17 11:29:12 -04005437 if (!existing) {
5438 existing = lookup_extent_mapping(em_tree, em->start,
5439 em->len);
5440 if (existing) {
5441 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005442 em, start,
5443 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005444 free_extent_map(existing);
5445 if (err) {
5446 free_extent_map(em);
5447 em = NULL;
5448 }
5449 } else {
5450 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005451 free_extent_map(em);
5452 em = NULL;
5453 }
5454 } else {
5455 free_extent_map(em);
5456 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005457 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005458 }
Chris Masona52d9a82007-08-27 16:49:44 -04005459 }
Chris Mason890871b2009-09-02 16:24:52 -04005460 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005461out:
liubo1abe9b82011-03-24 11:18:59 +00005462
5463 trace_btrfs_get_extent(root, em);
5464
Chris Masonf4219502008-07-22 11:18:09 -04005465 if (path)
5466 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005467 if (trans) {
5468 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005469 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005470 err = ret;
5471 }
Chris Masona52d9a82007-08-27 16:49:44 -04005472 if (err) {
5473 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005474 return ERR_PTR(err);
5475 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005476 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04005477 return em;
5478}
5479
Chris Masonec29ed52011-02-23 16:23:20 -05005480struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5481 size_t pg_offset, u64 start, u64 len,
5482 int create)
5483{
5484 struct extent_map *em;
5485 struct extent_map *hole_em = NULL;
5486 u64 range_start = start;
5487 u64 end;
5488 u64 found;
5489 u64 found_end;
5490 int err = 0;
5491
5492 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5493 if (IS_ERR(em))
5494 return em;
5495 if (em) {
5496 /*
5497 * if our em maps to a hole, there might
5498 * actually be delalloc bytes behind it
5499 */
5500 if (em->block_start != EXTENT_MAP_HOLE)
5501 return em;
5502 else
5503 hole_em = em;
5504 }
5505
5506 /* check to see if we've wrapped (len == -1 or similar) */
5507 end = start + len;
5508 if (end < start)
5509 end = (u64)-1;
5510 else
5511 end -= 1;
5512
5513 em = NULL;
5514
5515 /* ok, we didn't find anything, lets look for delalloc */
5516 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5517 end, len, EXTENT_DELALLOC, 1);
5518 found_end = range_start + found;
5519 if (found_end < range_start)
5520 found_end = (u64)-1;
5521
5522 /*
5523 * we didn't find anything useful, return
5524 * the original results from get_extent()
5525 */
5526 if (range_start > end || found_end <= start) {
5527 em = hole_em;
5528 hole_em = NULL;
5529 goto out;
5530 }
5531
5532 /* adjust the range_start to make sure it doesn't
5533 * go backwards from the start they passed in
5534 */
5535 range_start = max(start,range_start);
5536 found = found_end - range_start;
5537
5538 if (found > 0) {
5539 u64 hole_start = start;
5540 u64 hole_len = len;
5541
David Sterba172ddd62011-04-21 00:48:27 +02005542 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05005543 if (!em) {
5544 err = -ENOMEM;
5545 goto out;
5546 }
5547 /*
5548 * when btrfs_get_extent can't find anything it
5549 * returns one huge hole
5550 *
5551 * make sure what it found really fits our range, and
5552 * adjust to make sure it is based on the start from
5553 * the caller
5554 */
5555 if (hole_em) {
5556 u64 calc_end = extent_map_end(hole_em);
5557
5558 if (calc_end <= start || (hole_em->start > end)) {
5559 free_extent_map(hole_em);
5560 hole_em = NULL;
5561 } else {
5562 hole_start = max(hole_em->start, start);
5563 hole_len = calc_end - hole_start;
5564 }
5565 }
5566 em->bdev = NULL;
5567 if (hole_em && range_start > hole_start) {
5568 /* our hole starts before our delalloc, so we
5569 * have to return just the parts of the hole
5570 * that go until the delalloc starts
5571 */
5572 em->len = min(hole_len,
5573 range_start - hole_start);
5574 em->start = hole_start;
5575 em->orig_start = hole_start;
5576 /*
5577 * don't adjust block start at all,
5578 * it is fixed at EXTENT_MAP_HOLE
5579 */
5580 em->block_start = hole_em->block_start;
5581 em->block_len = hole_len;
5582 } else {
5583 em->start = range_start;
5584 em->len = found;
5585 em->orig_start = range_start;
5586 em->block_start = EXTENT_MAP_DELALLOC;
5587 em->block_len = found;
5588 }
5589 } else if (hole_em) {
5590 return hole_em;
5591 }
5592out:
5593
5594 free_extent_map(hole_em);
5595 if (err) {
5596 free_extent_map(em);
5597 return ERR_PTR(err);
5598 }
5599 return em;
5600}
5601
Josef Bacik4b46fce2010-05-23 11:00:55 -04005602static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
Josef Bacik16d299a2011-04-06 14:53:07 -04005603 struct extent_map *em,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005604 u64 start, u64 len)
5605{
5606 struct btrfs_root *root = BTRFS_I(inode)->root;
5607 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005608 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5609 struct btrfs_key ins;
5610 u64 alloc_hint;
5611 int ret;
Josef Bacik16d299a2011-04-06 14:53:07 -04005612 bool insert = false;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005613
Josef Bacik16d299a2011-04-06 14:53:07 -04005614 /*
5615 * Ok if the extent map we looked up is a hole and is for the exact
5616 * range we want, there is no reason to allocate a new one, however if
5617 * it is not right then we need to free this one and drop the cache for
5618 * our range.
5619 */
5620 if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
5621 em->len != len) {
5622 free_extent_map(em);
5623 em = NULL;
5624 insert = true;
5625 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5626 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005627
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005628 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005629 if (IS_ERR(trans))
5630 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005631
Chris Mason4cb53002011-05-24 15:35:30 -04005632 if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024)
5633 btrfs_add_inode_defrag(trans, inode);
5634
Josef Bacik4b46fce2010-05-23 11:00:55 -04005635 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5636
5637 alloc_hint = get_extent_allocation_hint(inode, start, len);
5638 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005639 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005640 if (ret) {
5641 em = ERR_PTR(ret);
5642 goto out;
5643 }
5644
Josef Bacik4b46fce2010-05-23 11:00:55 -04005645 if (!em) {
David Sterba172ddd62011-04-21 00:48:27 +02005646 em = alloc_extent_map();
Josef Bacik16d299a2011-04-06 14:53:07 -04005647 if (!em) {
5648 em = ERR_PTR(-ENOMEM);
5649 goto out;
5650 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005651 }
5652
5653 em->start = start;
5654 em->orig_start = em->start;
5655 em->len = ins.offset;
5656
5657 em->block_start = ins.objectid;
5658 em->block_len = ins.offset;
5659 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik16d299a2011-04-06 14:53:07 -04005660
5661 /*
5662 * We need to do this because if we're using the original em we searched
5663 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5664 */
5665 em->flags = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005666 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5667
Josef Bacik16d299a2011-04-06 14:53:07 -04005668 while (insert) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005669 write_lock(&em_tree->lock);
5670 ret = add_extent_mapping(em_tree, em);
5671 write_unlock(&em_tree->lock);
5672 if (ret != -EEXIST)
5673 break;
5674 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5675 }
5676
5677 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5678 ins.offset, ins.offset, 0);
5679 if (ret) {
5680 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5681 em = ERR_PTR(ret);
5682 }
5683out:
5684 btrfs_end_transaction(trans, root);
5685 return em;
5686}
5687
Chris Mason46bfbb52010-05-26 11:04:10 -04005688/*
5689 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5690 * block must be cow'd
5691 */
5692static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5693 struct inode *inode, u64 offset, u64 len)
5694{
5695 struct btrfs_path *path;
5696 int ret;
5697 struct extent_buffer *leaf;
5698 struct btrfs_root *root = BTRFS_I(inode)->root;
5699 struct btrfs_file_extent_item *fi;
5700 struct btrfs_key key;
5701 u64 disk_bytenr;
5702 u64 backref_offset;
5703 u64 extent_end;
5704 u64 num_bytes;
5705 int slot;
5706 int found_type;
5707
5708 path = btrfs_alloc_path();
5709 if (!path)
5710 return -ENOMEM;
5711
Li Zefan33345d012011-04-20 10:31:50 +08005712 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005713 offset, 0);
5714 if (ret < 0)
5715 goto out;
5716
5717 slot = path->slots[0];
5718 if (ret == 1) {
5719 if (slot == 0) {
5720 /* can't find the item, must cow */
5721 ret = 0;
5722 goto out;
5723 }
5724 slot--;
5725 }
5726 ret = 0;
5727 leaf = path->nodes[0];
5728 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005729 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005730 key.type != BTRFS_EXTENT_DATA_KEY) {
5731 /* not our file or wrong item type, must cow */
5732 goto out;
5733 }
5734
5735 if (key.offset > offset) {
5736 /* Wrong offset, must cow */
5737 goto out;
5738 }
5739
5740 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5741 found_type = btrfs_file_extent_type(leaf, fi);
5742 if (found_type != BTRFS_FILE_EXTENT_REG &&
5743 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5744 /* not a regular extent, must cow */
5745 goto out;
5746 }
5747 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5748 backref_offset = btrfs_file_extent_offset(leaf, fi);
5749
5750 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5751 if (extent_end < offset + len) {
5752 /* extent doesn't include our full range, must cow */
5753 goto out;
5754 }
5755
5756 if (btrfs_extent_readonly(root, disk_bytenr))
5757 goto out;
5758
5759 /*
5760 * look for other files referencing this extent, if we
5761 * find any we must cow
5762 */
Li Zefan33345d012011-04-20 10:31:50 +08005763 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005764 key.offset - backref_offset, disk_bytenr))
5765 goto out;
5766
5767 /*
5768 * adjust disk_bytenr and num_bytes to cover just the bytes
5769 * in this extent we are about to write. If there
5770 * are any csums in that range we have to cow in order
5771 * to keep the csums correct
5772 */
5773 disk_bytenr += backref_offset;
5774 disk_bytenr += offset - key.offset;
5775 num_bytes = min(offset + len, extent_end) - offset;
5776 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5777 goto out;
5778 /*
5779 * all of the above have passed, it is safe to overwrite this extent
5780 * without cow
5781 */
5782 ret = 1;
5783out:
5784 btrfs_free_path(path);
5785 return ret;
5786}
5787
Josef Bacik4b46fce2010-05-23 11:00:55 -04005788static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5789 struct buffer_head *bh_result, int create)
5790{
5791 struct extent_map *em;
5792 struct btrfs_root *root = BTRFS_I(inode)->root;
5793 u64 start = iblock << inode->i_blkbits;
5794 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005795 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005796
5797 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5798 if (IS_ERR(em))
5799 return PTR_ERR(em);
5800
5801 /*
5802 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5803 * io. INLINE is special, and we could probably kludge it in here, but
5804 * it's still buffered so for safety lets just fall back to the generic
5805 * buffered path.
5806 *
5807 * For COMPRESSED we _have_ to read the entire extent in so we can
5808 * decompress it, so there will be buffering required no matter what we
5809 * do, so go ahead and fallback to buffered.
5810 *
5811 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5812 * to buffered IO. Don't blame me, this is the price we pay for using
5813 * the generic code.
5814 */
5815 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5816 em->block_start == EXTENT_MAP_INLINE) {
5817 free_extent_map(em);
5818 return -ENOTBLK;
5819 }
5820
5821 /* Just a good old fashioned hole, return */
5822 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5823 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5824 free_extent_map(em);
5825 /* DIO will do one hole at a time, so just unlock a sector */
5826 unlock_extent(&BTRFS_I(inode)->io_tree, start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005827 start + root->sectorsize - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005828 return 0;
5829 }
5830
5831 /*
5832 * We don't allocate a new extent in the following cases
5833 *
5834 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5835 * existing extent.
5836 * 2) The extent is marked as PREALLOC. We're good to go here and can
5837 * just use the extent.
5838 *
5839 */
Chris Mason46bfbb52010-05-26 11:04:10 -04005840 if (!create) {
5841 len = em->len - (start - em->start);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005842 goto map;
Chris Mason46bfbb52010-05-26 11:04:10 -04005843 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005844
5845 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5846 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5847 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005848 int type;
5849 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04005850 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005851
5852 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5853 type = BTRFS_ORDERED_PREALLOC;
5854 else
5855 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04005856 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04005857 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04005858
5859 /*
5860 * we're not going to log anything, but we do need
5861 * to make sure the current transaction stays open
5862 * while we look for nocow cross refs
5863 */
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005864 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005865 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04005866 goto must_cow;
5867
5868 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5869 ret = btrfs_add_ordered_extent_dio(inode, start,
5870 block_start, len, len, type);
5871 btrfs_end_transaction(trans, root);
5872 if (ret) {
5873 free_extent_map(em);
5874 return ret;
5875 }
5876 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005877 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005878 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005879 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005880must_cow:
5881 /*
5882 * this will cow the extent, reset the len in case we changed
5883 * it above
5884 */
5885 len = bh_result->b_size;
Josef Bacik16d299a2011-04-06 14:53:07 -04005886 em = btrfs_new_extent_direct(inode, em, start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04005887 if (IS_ERR(em))
5888 return PTR_ERR(em);
5889 len = min(len, em->len - (start - em->start));
5890unlock:
Chris Mason4845e442010-05-25 20:56:50 -04005891 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5892 EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5893 0, NULL, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005894map:
5895 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5896 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04005897 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005898 bh_result->b_bdev = em->bdev;
5899 set_buffer_mapped(bh_result);
5900 if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5901 set_buffer_new(bh_result);
5902
5903 free_extent_map(em);
5904
5905 return 0;
5906}
5907
5908struct btrfs_dio_private {
5909 struct inode *inode;
5910 u64 logical_offset;
5911 u64 disk_bytenr;
5912 u64 bytes;
5913 u32 *csums;
5914 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00005915
5916 /* number of bios pending for this dio */
5917 atomic_t pending_bios;
5918
5919 /* IO errors */
5920 int errors;
5921
5922 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005923};
5924
5925static void btrfs_endio_direct_read(struct bio *bio, int err)
5926{
Miao Xiee65e1532010-11-22 03:04:43 +00005927 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005928 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5929 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005930 struct inode *inode = dip->inode;
5931 struct btrfs_root *root = BTRFS_I(inode)->root;
5932 u64 start;
5933 u32 *private = dip->csums;
5934
5935 start = dip->logical_offset;
5936 do {
5937 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5938 struct page *page = bvec->bv_page;
5939 char *kaddr;
5940 u32 csum = ~(u32)0;
5941 unsigned long flags;
5942
5943 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08005944 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005945 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5946 csum, bvec->bv_len);
5947 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08005948 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005949 local_irq_restore(flags);
5950
5951 flush_dcache_page(bvec->bv_page);
5952 if (csum != *private) {
Li Zefan33345d012011-04-20 10:31:50 +08005953 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04005954 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08005955 (unsigned long long)btrfs_ino(inode),
5956 (unsigned long long)start,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005957 csum, *private);
5958 err = -EIO;
5959 }
5960 }
5961
5962 start += bvec->bv_len;
5963 private++;
5964 bvec++;
5965 } while (bvec <= bvec_end);
5966
5967 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005968 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005969 bio->bi_private = dip->private;
5970
5971 kfree(dip->csums);
5972 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04005973
5974 /* If we had a csum failure make sure to clear the uptodate flag */
5975 if (err)
5976 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005977 dio_end_io(bio, err);
5978}
5979
5980static void btrfs_endio_direct_write(struct bio *bio, int err)
5981{
5982 struct btrfs_dio_private *dip = bio->bi_private;
5983 struct inode *inode = dip->inode;
5984 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005985 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05005986 u64 ordered_offset = dip->logical_offset;
5987 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005988 int ret;
5989
5990 if (err)
5991 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05005992again:
5993 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
5994 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04005995 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005996 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05005997 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005998
Josef Bacik5fd02042012-05-02 14:00:54 -04005999 ordered->work.func = finish_ordered_fn;
6000 ordered->work.flags = 0;
6001 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6002 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006003out_test:
6004 /*
6005 * our bio might span multiple ordered extents. If we haven't
6006 * completed the accounting for the whole dio, go back and try again
6007 */
6008 if (ordered_offset < dip->logical_offset + dip->bytes) {
6009 ordered_bytes = dip->logical_offset + dip->bytes -
6010 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006011 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006012 goto again;
6013 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006014out_done:
6015 bio->bi_private = dip->private;
6016
Josef Bacik4b46fce2010-05-23 11:00:55 -04006017 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006018
6019 /* If we had an error make sure to clear the uptodate flag */
6020 if (err)
6021 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006022 dio_end_io(bio, err);
6023}
6024
Chris Masoneaf25d92010-05-25 09:48:28 -04006025static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6026 struct bio *bio, int mirror_num,
6027 unsigned long bio_flags, u64 offset)
6028{
6029 int ret;
6030 struct btrfs_root *root = BTRFS_I(inode)->root;
6031 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006032 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006033 return 0;
6034}
6035
Miao Xiee65e1532010-11-22 03:04:43 +00006036static void btrfs_end_dio_bio(struct bio *bio, int err)
6037{
6038 struct btrfs_dio_private *dip = bio->bi_private;
6039
6040 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006041 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006042 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006043 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006044 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006045 dip->errors = 1;
6046
6047 /*
6048 * before atomic variable goto zero, we must make sure
6049 * dip->errors is perceived to be set.
6050 */
6051 smp_mb__before_atomic_dec();
6052 }
6053
6054 /* if there are more bios still pending for this dio, just exit */
6055 if (!atomic_dec_and_test(&dip->pending_bios))
6056 goto out;
6057
6058 if (dip->errors)
6059 bio_io_error(dip->orig_bio);
6060 else {
6061 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6062 bio_endio(dip->orig_bio, 0);
6063 }
6064out:
6065 bio_put(bio);
6066}
6067
6068static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6069 u64 first_sector, gfp_t gfp_flags)
6070{
6071 int nr_vecs = bio_get_nr_vecs(bdev);
6072 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6073}
6074
6075static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6076 int rw, u64 file_offset, int skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006077 u32 *csums, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006078{
6079 int write = rw & REQ_WRITE;
6080 struct btrfs_root *root = BTRFS_I(inode)->root;
6081 int ret;
6082
6083 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006084
6085 if (!write) {
6086 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6087 if (ret)
6088 goto err;
6089 }
Miao Xiee65e1532010-11-22 03:04:43 +00006090
Josef Bacik1ae39932011-04-06 14:41:34 -04006091 if (skip_sum)
6092 goto map;
6093
6094 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006095 ret = btrfs_wq_submit_bio(root->fs_info,
6096 inode, rw, bio, 0, 0,
6097 file_offset,
6098 __btrfs_submit_bio_start_direct_io,
6099 __btrfs_submit_bio_done);
6100 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006101 } else if (write) {
6102 /*
6103 * If we aren't doing async submit, calculate the csum of the
6104 * bio now.
6105 */
6106 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6107 if (ret)
6108 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006109 } else if (!skip_sum) {
6110 ret = btrfs_lookup_bio_sums_dio(root, inode, bio,
Miao Xiee65e1532010-11-22 03:04:43 +00006111 file_offset, csums);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006112 if (ret)
6113 goto err;
6114 }
Miao Xiee65e1532010-11-22 03:04:43 +00006115
Josef Bacik1ae39932011-04-06 14:41:34 -04006116map:
6117 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006118err:
6119 bio_put(bio);
6120 return ret;
6121}
6122
6123static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6124 int skip_sum)
6125{
6126 struct inode *inode = dip->inode;
6127 struct btrfs_root *root = BTRFS_I(inode)->root;
6128 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6129 struct bio *bio;
6130 struct bio *orig_bio = dip->orig_bio;
6131 struct bio_vec *bvec = orig_bio->bi_io_vec;
6132 u64 start_sector = orig_bio->bi_sector;
6133 u64 file_offset = dip->logical_offset;
6134 u64 submit_len = 0;
6135 u64 map_length;
6136 int nr_pages = 0;
6137 u32 *csums = dip->csums;
6138 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006139 int async_submit = 0;
Josef Bacik98bc3142011-03-22 11:00:46 -04006140 int write = rw & REQ_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00006141
Miao Xiee65e1532010-11-22 03:04:43 +00006142 map_length = orig_bio->bi_size;
6143 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6144 &map_length, NULL, 0);
6145 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006146 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006147 return -EIO;
6148 }
6149
Josef Bacik02f57c72011-04-06 14:25:44 -04006150 if (map_length >= orig_bio->bi_size) {
6151 bio = orig_bio;
6152 goto submit;
6153 }
6154
Josef Bacik1ae39932011-04-06 14:41:34 -04006155 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04006156 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6157 if (!bio)
6158 return -ENOMEM;
6159 bio->bi_private = dip;
6160 bio->bi_end_io = btrfs_end_dio_bio;
6161 atomic_inc(&dip->pending_bios);
6162
Miao Xiee65e1532010-11-22 03:04:43 +00006163 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6164 if (unlikely(map_length < submit_len + bvec->bv_len ||
6165 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6166 bvec->bv_offset) < bvec->bv_len)) {
6167 /*
6168 * inc the count before we submit the bio so
6169 * we know the end IO handler won't happen before
6170 * we inc the count. Otherwise, the dip might get freed
6171 * before we're done setting it up
6172 */
6173 atomic_inc(&dip->pending_bios);
6174 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6175 file_offset, skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006176 csums, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006177 if (ret) {
6178 bio_put(bio);
6179 atomic_dec(&dip->pending_bios);
6180 goto out_err;
6181 }
6182
Josef Bacik98bc3142011-03-22 11:00:46 -04006183 /* Write's use the ordered csums */
6184 if (!write && !skip_sum)
Miao Xiee65e1532010-11-22 03:04:43 +00006185 csums = csums + nr_pages;
6186 start_sector += submit_len >> 9;
6187 file_offset += submit_len;
6188
6189 submit_len = 0;
6190 nr_pages = 0;
6191
6192 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6193 start_sector, GFP_NOFS);
6194 if (!bio)
6195 goto out_err;
6196 bio->bi_private = dip;
6197 bio->bi_end_io = btrfs_end_dio_bio;
6198
6199 map_length = orig_bio->bi_size;
6200 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6201 &map_length, NULL, 0);
6202 if (ret) {
6203 bio_put(bio);
6204 goto out_err;
6205 }
6206 } else {
6207 submit_len += bvec->bv_len;
6208 nr_pages ++;
6209 bvec++;
6210 }
6211 }
6212
Josef Bacik02f57c72011-04-06 14:25:44 -04006213submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006214 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006215 csums, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006216 if (!ret)
6217 return 0;
6218
6219 bio_put(bio);
6220out_err:
6221 dip->errors = 1;
6222 /*
6223 * before atomic variable goto zero, we must
6224 * make sure dip->errors is perceived to be set.
6225 */
6226 smp_mb__before_atomic_dec();
6227 if (atomic_dec_and_test(&dip->pending_bios))
6228 bio_io_error(dip->orig_bio);
6229
6230 /* bio_end_io() will handle error, so we needn't return it */
6231 return 0;
6232}
6233
Josef Bacik4b46fce2010-05-23 11:00:55 -04006234static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6235 loff_t file_offset)
6236{
6237 struct btrfs_root *root = BTRFS_I(inode)->root;
6238 struct btrfs_dio_private *dip;
6239 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006240 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006241 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006242 int ret = 0;
6243
6244 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6245
6246 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6247 if (!dip) {
6248 ret = -ENOMEM;
6249 goto free_ordered;
6250 }
6251 dip->csums = NULL;
6252
Josef Bacik98bc3142011-03-22 11:00:46 -04006253 /* Write's use the ordered csum stuff, so we don't need dip->csums */
6254 if (!write && !skip_sum) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006255 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
6256 if (!dip->csums) {
Daniel J Bluemanb4966b72011-03-09 16:46:42 +00006257 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006258 ret = -ENOMEM;
6259 goto free_ordered;
6260 }
6261 }
6262
6263 dip->private = bio->bi_private;
6264 dip->inode = inode;
6265 dip->logical_offset = file_offset;
6266
Josef Bacik4b46fce2010-05-23 11:00:55 -04006267 dip->bytes = 0;
6268 do {
6269 dip->bytes += bvec->bv_len;
6270 bvec++;
6271 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6272
Chris Mason46bfbb52010-05-26 11:04:10 -04006273 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006274 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006275 dip->errors = 0;
6276 dip->orig_bio = bio;
6277 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006278
6279 if (write)
6280 bio->bi_end_io = btrfs_endio_direct_write;
6281 else
6282 bio->bi_end_io = btrfs_endio_direct_read;
6283
Miao Xiee65e1532010-11-22 03:04:43 +00006284 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6285 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006286 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006287free_ordered:
6288 /*
6289 * If this is a write, we need to clean up the reserved space and kill
6290 * the ordered extent.
6291 */
6292 if (write) {
6293 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006294 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006295 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6296 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6297 btrfs_free_reserved_extent(root, ordered->start,
6298 ordered->disk_len);
6299 btrfs_put_ordered_extent(ordered);
6300 btrfs_put_ordered_extent(ordered);
6301 }
6302 bio_endio(bio, ret);
6303}
6304
Chris Mason5a5f79b2010-05-26 21:33:37 -04006305static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6306 const struct iovec *iov, loff_t offset,
6307 unsigned long nr_segs)
6308{
6309 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006310 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006311 size_t size;
6312 unsigned long addr;
6313 unsigned blocksize_mask = root->sectorsize - 1;
6314 ssize_t retval = -EINVAL;
6315 loff_t end = offset;
6316
6317 if (offset & blocksize_mask)
6318 goto out;
6319
6320 /* Check the memory alignment. Blocks cannot straddle pages */
6321 for (seg = 0; seg < nr_segs; seg++) {
6322 addr = (unsigned long)iov[seg].iov_base;
6323 size = iov[seg].iov_len;
6324 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006325 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006326 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006327
6328 /* If this is a write we don't need to check anymore */
6329 if (rw & WRITE)
6330 continue;
6331
6332 /*
6333 * Check to make sure we don't have duplicate iov_base's in this
6334 * iovec, if so return EINVAL, otherwise we'll get csum errors
6335 * when reading back.
6336 */
6337 for (i = seg + 1; i < nr_segs; i++) {
6338 if (iov[seg].iov_base == iov[i].iov_base)
6339 goto out;
6340 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006341 }
6342 retval = 0;
6343out:
6344 return retval;
6345}
Chris Mason16432982008-04-10 10:23:21 -04006346static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6347 const struct iovec *iov, loff_t offset,
6348 unsigned long nr_segs)
6349{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006350 struct file *file = iocb->ki_filp;
6351 struct inode *inode = file->f_mapping->host;
6352 struct btrfs_ordered_extent *ordered;
Chris Mason4845e442010-05-25 20:56:50 -04006353 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006354 u64 lockstart, lockend;
6355 ssize_t ret;
Chris Mason4845e442010-05-25 20:56:50 -04006356 int writing = rw & WRITE;
6357 int write_bits = 0;
Chris Mason3f7c5792010-05-26 10:59:53 -04006358 size_t count = iov_length(iov, nr_segs);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006359
Chris Mason5a5f79b2010-05-26 21:33:37 -04006360 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
6361 offset, nr_segs)) {
6362 return 0;
6363 }
6364
Josef Bacik4b46fce2010-05-23 11:00:55 -04006365 lockstart = offset;
Chris Mason3f7c5792010-05-26 10:59:53 -04006366 lockend = offset + count - 1;
6367
6368 if (writing) {
6369 ret = btrfs_delalloc_reserve_space(inode, count);
6370 if (ret)
6371 goto out;
6372 }
Chris Mason4845e442010-05-25 20:56:50 -04006373
Josef Bacik4b46fce2010-05-23 11:00:55 -04006374 while (1) {
Chris Mason4845e442010-05-25 20:56:50 -04006375 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006376 0, &cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006377 /*
6378 * We're concerned with the entire range that we're going to be
6379 * doing DIO to, so we need to make sure theres no ordered
6380 * extents in this range.
6381 */
6382 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6383 lockend - lockstart + 1);
6384 if (!ordered)
6385 break;
Chris Mason4845e442010-05-25 20:56:50 -04006386 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6387 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006388 btrfs_start_ordered_extent(inode, ordered, 1);
6389 btrfs_put_ordered_extent(ordered);
6390 cond_resched();
6391 }
6392
Chris Mason4845e442010-05-25 20:56:50 -04006393 /*
6394 * we don't use btrfs_set_extent_delalloc because we don't want
6395 * the dirty or uptodate bits
6396 */
6397 if (writing) {
6398 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
6399 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01006400 EXTENT_DELALLOC, NULL, &cached_state,
Chris Mason4845e442010-05-25 20:56:50 -04006401 GFP_NOFS);
6402 if (ret) {
6403 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6404 lockend, EXTENT_LOCKED | write_bits,
6405 1, 0, &cached_state, GFP_NOFS);
6406 goto out;
6407 }
6408 }
6409
6410 free_extent_state(cached_state);
6411 cached_state = NULL;
6412
Chris Mason5a5f79b2010-05-26 21:33:37 -04006413 ret = __blockdev_direct_IO(rw, iocb, inode,
6414 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6415 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6416 btrfs_submit_direct, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006417
6418 if (ret < 0 && ret != -EIOCBQUEUED) {
Chris Mason4845e442010-05-25 20:56:50 -04006419 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
6420 offset + iov_length(iov, nr_segs) - 1,
6421 EXTENT_LOCKED | write_bits, 1, 0,
6422 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006423 } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
6424 /*
6425 * We're falling back to buffered, unlock the section we didn't
6426 * do IO on.
6427 */
Chris Mason4845e442010-05-25 20:56:50 -04006428 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
6429 offset + iov_length(iov, nr_segs) - 1,
6430 EXTENT_LOCKED | write_bits, 1, 0,
6431 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006432 }
Chris Mason4845e442010-05-25 20:56:50 -04006433out:
6434 free_extent_state(cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006435 return ret;
Chris Mason16432982008-04-10 10:23:21 -04006436}
6437
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006438static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6439 __u64 start, __u64 len)
6440{
Chris Masonec29ed52011-02-23 16:23:20 -05006441 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006442}
6443
Chris Mason9ebefb182007-06-15 13:50:00 -04006444int btrfs_readpage(struct file *file, struct page *page)
6445{
Chris Masond1310b22008-01-24 16:13:08 -05006446 struct extent_io_tree *tree;
6447 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02006448 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006449}
Chris Mason1832a6d2007-12-21 16:27:21 -05006450
Chris Mason39279cc2007-06-12 06:35:45 -04006451static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6452{
Chris Masond1310b22008-01-24 16:13:08 -05006453 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006454
6455
6456 if (current->flags & PF_MEMALLOC) {
6457 redirty_page_for_writepage(wbc, page);
6458 unlock_page(page);
6459 return 0;
6460 }
Chris Masond1310b22008-01-24 16:13:08 -05006461 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006462 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6463}
Chris Mason39279cc2007-06-12 06:35:45 -04006464
Chris Masonf4219502008-07-22 11:18:09 -04006465int btrfs_writepages(struct address_space *mapping,
6466 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006467{
Chris Masond1310b22008-01-24 16:13:08 -05006468 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006469
Chris Masond1310b22008-01-24 16:13:08 -05006470 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006471 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6472}
6473
Chris Mason3ab2fb52007-11-08 10:59:22 -05006474static int
6475btrfs_readpages(struct file *file, struct address_space *mapping,
6476 struct list_head *pages, unsigned nr_pages)
6477{
Chris Masond1310b22008-01-24 16:13:08 -05006478 struct extent_io_tree *tree;
6479 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006480 return extent_readpages(tree, mapping, pages, nr_pages,
6481 btrfs_get_extent);
6482}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006483static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006484{
Chris Masond1310b22008-01-24 16:13:08 -05006485 struct extent_io_tree *tree;
6486 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006487 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006488
Chris Masond1310b22008-01-24 16:13:08 -05006489 tree = &BTRFS_I(page->mapping->host)->io_tree;
6490 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006491 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006492 if (ret == 1) {
6493 ClearPagePrivate(page);
6494 set_page_private(page, 0);
6495 page_cache_release(page);
6496 }
6497 return ret;
6498}
Chris Mason39279cc2007-06-12 06:35:45 -04006499
Chris Masone6dcd2d2008-07-17 12:53:50 -04006500static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6501{
Chris Mason98509cf2008-09-11 15:51:43 -04006502 if (PageWriteback(page) || PageDirty(page))
6503 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006504 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006505}
6506
Chris Masona52d9a82007-08-27 16:49:44 -04006507static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6508{
Josef Bacik5fd02042012-05-02 14:00:54 -04006509 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05006510 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006511 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006512 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006513 u64 page_start = page_offset(page);
6514 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006515
Chris Mason8b62b722009-09-02 16:53:46 -04006516 /*
6517 * we have the page locked, so new writeback can't start,
6518 * and the dirty bit won't be cleared while we are here.
6519 *
6520 * Wait for IO on this page so that we can safely clear
6521 * the PagePrivate2 bit and do ordered accounting
6522 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006523 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006524
Josef Bacik5fd02042012-05-02 14:00:54 -04006525 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006526 if (offset) {
6527 btrfs_releasepage(page, GFP_NOFS);
6528 return;
6529 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006530 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Josef Bacik5fd02042012-05-02 14:00:54 -04006531 ordered = btrfs_lookup_ordered_extent(inode,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006532 page_offset(page));
6533 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006534 /*
6535 * IO on this page will never be started, so we need
6536 * to account for any ordered extents now
6537 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006538 clear_extent_bit(tree, page_start, page_end,
6539 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04006540 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006541 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006542 /*
6543 * whoever cleared the private bit is responsible
6544 * for the finish_ordered_io
6545 */
Josef Bacik5fd02042012-05-02 14:00:54 -04006546 if (TestClearPagePrivate2(page) &&
6547 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
6548 PAGE_CACHE_SIZE, 1)) {
6549 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04006550 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006551 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006552 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006553 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006554 }
6555 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006556 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00006557 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006558 __btrfs_releasepage(page, GFP_NOFS);
6559
Chris Mason4a096752008-07-21 10:29:44 -04006560 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006561 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006562 ClearPagePrivate(page);
6563 set_page_private(page, 0);
6564 page_cache_release(page);
6565 }
Chris Mason39279cc2007-06-12 06:35:45 -04006566}
6567
Chris Mason9ebefb182007-06-15 13:50:00 -04006568/*
6569 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6570 * called from a page fault handler when a page is first dirtied. Hence we must
6571 * be careful to check for EOF conditions here. We set the page up correctly
6572 * for a written page which means we get ENOSPC checking when writing into
6573 * holes and correct delalloc and unwritten extent mapping on filesystems that
6574 * support these features.
6575 *
6576 * We are not allowed to take the i_mutex here so we have to play games to
6577 * protect against truncate races as the page could now be beyond EOF. Because
6578 * vmtruncate() writes the inode size before removing pages, once we have the
6579 * page lock we can determine safely if the page is beyond EOF. If it is not
6580 * beyond EOF, then the page is guaranteed safe against truncation until we
6581 * unlock the page.
6582 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006583int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006584{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006585 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006586 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006587 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006588 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6589 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006590 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006591 char *kaddr;
6592 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006593 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006594 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05006595 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006596 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006597 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006598
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006599 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006600 if (!ret) {
Josef Bacik22c44fe2011-11-30 10:45:38 -05006601 ret = btrfs_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05006602 reserved = 1;
6603 }
Nick Piggin56a76f82009-03-31 15:23:23 -07006604 if (ret) {
6605 if (ret == -ENOMEM)
6606 ret = VM_FAULT_OOM;
6607 else /* -ENOSPC, -EIO, etc */
6608 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05006609 if (reserved)
6610 goto out;
6611 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07006612 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006613
Nick Piggin56a76f82009-03-31 15:23:23 -07006614 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006615again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006616 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006617 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006618 page_start = page_offset(page);
6619 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006620
Chris Mason9ebefb182007-06-15 13:50:00 -04006621 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006622 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006623 /* page got truncated out from underneath us */
6624 goto out_unlock;
6625 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006626 wait_on_page_writeback(page);
6627
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006628 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006629 set_page_extent_mapped(page);
6630
Chris Masoneb84ae02008-07-17 13:53:27 -04006631 /*
6632 * we can't set the delalloc bits if there are pending ordered
6633 * extents. Drop our locks and wait for them to finish
6634 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006635 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6636 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006637 unlock_extent_cached(io_tree, page_start, page_end,
6638 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006639 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006640 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006641 btrfs_put_ordered_extent(ordered);
6642 goto again;
6643 }
6644
Josef Bacikfbf19082009-10-01 17:10:23 -04006645 /*
6646 * XXX - page_mkwrite gets called every time the page is dirtied, even
6647 * if it was already dirty, so for space accounting reasons we need to
6648 * clear any delalloc bits for the range we are fixing to save. There
6649 * is probably a better way to do this, but for now keep consistent with
6650 * prepare_pages in the normal write path.
6651 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006652 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006653 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006654 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006655
Josef Bacik2ac55d42010-02-03 19:33:23 +00006656 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6657 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006658 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006659 unlock_extent_cached(io_tree, page_start, page_end,
6660 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006661 ret = VM_FAULT_SIGBUS;
6662 goto out_unlock;
6663 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006664 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006665
6666 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006667 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006668 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006669 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006670 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006671
Chris Masone6dcd2d2008-07-17 12:53:50 -04006672 if (zero_start != PAGE_CACHE_SIZE) {
6673 kaddr = kmap(page);
6674 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6675 flush_dcache_page(page);
6676 kunmap(page);
6677 }
Chris Mason247e7432008-07-17 12:53:51 -04006678 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006679 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006680 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006681
Chris Mason257c62e2009-10-13 13:21:08 -04006682 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6683 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6684
Josef Bacik2ac55d42010-02-03 19:33:23 +00006685 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006686
6687out_unlock:
Chris Mason50a9b212009-09-11 12:33:12 -04006688 if (!ret)
6689 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04006690 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05006691out:
Josef Bacikec39e182012-01-12 19:10:12 -05006692 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006693out_noreserve:
Chris Mason9ebefb182007-06-15 13:50:00 -04006694 return ret;
6695}
6696
Josef Bacika41ad392011-01-31 15:30:16 -05006697static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006698{
6699 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006700 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04006701 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006702 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006703 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04006704 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04006705 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04006706 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04006707
Josef Bacik5d5e1032009-10-13 16:46:49 -04006708 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6709 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006710 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006711
Chris Mason4a096752008-07-21 10:29:44 -04006712 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006713 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006714
Josef Bacikfcb80c22011-05-03 10:40:22 -04006715 /*
6716 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6717 * 3 things going on here
6718 *
6719 * 1) We need to reserve space for our orphan item and the space to
6720 * delete our orphan item. Lord knows we don't want to have a dangling
6721 * orphan item because we didn't reserve space to remove it.
6722 *
6723 * 2) We need to reserve space to update our inode.
6724 *
6725 * 3) We need to have something to cache all the space that is going to
6726 * be free'd up by the truncate operation, but also have some slack
6727 * space reserved in case it uses space during the truncate (thank you
6728 * very much snapshotting).
6729 *
6730 * And we need these to all be seperate. The fact is we can use alot of
6731 * space doing the truncate, and we have no earthly idea how much space
6732 * we will use, so we need the truncate reservation to be seperate so it
6733 * doesn't end up using space reserved for updating the inode or
6734 * removing the orphan item. We also need to be able to stop the
6735 * transaction and start a new one, which means we need to be able to
6736 * update the inode several times, and we have no idea of knowing how
6737 * many times that will be, so we can't just reserve 1 item for the
6738 * entirety of the opration, so that has to be done seperately as well.
6739 * Then there is the orphan item, which does indeed need to be held on
6740 * to for the whole operation, and we need nobody to touch this reserved
6741 * space except the orphan code.
6742 *
6743 * So that leaves us with
6744 *
6745 * 1) root->orphan_block_rsv - for the orphan deletion.
6746 * 2) rsv - for the truncate reservation, which we will steal from the
6747 * transaction reservation.
6748 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6749 * updating the inode.
6750 */
6751 rsv = btrfs_alloc_block_rsv(root);
6752 if (!rsv)
6753 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04006754 rsv->size = min_size;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006755
Josef Bacik907cbce2011-08-08 13:46:15 -04006756 /*
Josef Bacik07127182011-08-19 10:29:59 -04006757 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04006758 * 1 for the orphan item we're going to add
6759 * 1 for the orphan item deletion
6760 * 1 for updating the inode.
6761 */
Josef Bacikfcb80c22011-05-03 10:40:22 -04006762 trans = btrfs_start_transaction(root, 4);
6763 if (IS_ERR(trans)) {
6764 err = PTR_ERR(trans);
6765 goto out;
6766 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05006767
Josef Bacik907cbce2011-08-08 13:46:15 -04006768 /* Migrate the slack space for the truncate to our reserve */
6769 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
6770 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006771 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05006772
6773 ret = btrfs_orphan_add(trans, inode);
6774 if (ret) {
6775 btrfs_end_transaction(trans, root);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006776 goto out;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006777 }
6778
Chris Mason5a3f23d2009-03-31 13:27:11 -04006779 /*
6780 * setattr is responsible for setting the ordered_data_close flag,
6781 * but that is only tested during the last file release. That
6782 * could happen well after the next commit, leaving a great big
6783 * window where new writes may get lost if someone chooses to write
6784 * to this file after truncating to zero
6785 *
6786 * The inode doesn't have any dirty data here, and so if we commit
6787 * this is a noop. If someone immediately starts writing to the inode
6788 * it is very likely we'll catch some of their writes in this
6789 * transaction, and the commit will find this file on the ordered
6790 * data list with good things to send down.
6791 *
6792 * This is a best effort solution, there is still a window where
6793 * using truncate to replace the contents of the file will
6794 * end up with a zero length file after a crash.
6795 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04006796 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
6797 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04006798 btrfs_add_ordered_operation(trans, root, inode);
6799
Yan, Zheng80825102009-11-12 09:35:36 +00006800 while (1) {
Josef Bacik36ba0222011-10-18 12:15:48 -04006801 ret = btrfs_block_rsv_refill(root, rsv, min_size);
Josef Bacik907cbce2011-08-08 13:46:15 -04006802 if (ret) {
6803 /*
6804 * This can only happen with the original transaction we
6805 * started above, every other time we shouldn't have a
6806 * transaction started yet.
6807 */
6808 if (ret == -EAGAIN)
6809 goto end_trans;
6810 err = ret;
6811 break;
6812 }
6813
Yan, Zhengd68fc572010-05-16 10:49:58 -04006814 if (!trans) {
Josef Bacik907cbce2011-08-08 13:46:15 -04006815 /* Just need the 1 for updating the inode */
6816 trans = btrfs_start_transaction(root, 1);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006817 if (IS_ERR(trans)) {
Josef Bacik7041ee92011-12-09 13:26:22 -05006818 ret = err = PTR_ERR(trans);
6819 trans = NULL;
6820 break;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006821 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006822 }
6823
Josef Bacik907cbce2011-08-08 13:46:15 -04006824 trans->block_rsv = rsv;
6825
Yan, Zheng80825102009-11-12 09:35:36 +00006826 ret = btrfs_truncate_inode_items(trans, root, inode,
6827 inode->i_size,
6828 BTRFS_EXTENT_DATA_KEY);
Josef Bacik3893e332011-01-31 16:03:11 -05006829 if (ret != -EAGAIN) {
6830 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006831 break;
Josef Bacik3893e332011-01-31 16:03:11 -05006832 }
Chris Mason39279cc2007-06-12 06:35:45 -04006833
Josef Bacikfcb80c22011-05-03 10:40:22 -04006834 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006835 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006836 if (ret) {
6837 err = ret;
6838 break;
6839 }
Josef Bacik907cbce2011-08-08 13:46:15 -04006840end_trans:
Yan, Zheng80825102009-11-12 09:35:36 +00006841 nr = trans->blocks_used;
6842 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006843 trans = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00006844 btrfs_btree_balance_dirty(root, nr);
Yan, Zheng80825102009-11-12 09:35:36 +00006845 }
6846
6847 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04006848 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006849 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006850 if (ret)
6851 err = ret;
Josef Bacikded5db92011-03-04 14:09:46 -05006852 } else if (ret && inode->i_nlink > 0) {
6853 /*
6854 * Failed to do the truncate, remove us from the in memory
6855 * orphan list.
6856 */
6857 ret = btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00006858 }
6859
Chris Mason917c16b2011-11-08 14:49:59 -05006860 if (trans) {
6861 trans->block_rsv = &root->fs_info->trans_block_rsv;
6862 ret = btrfs_update_inode(trans, root, inode);
6863 if (ret && !err)
6864 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04006865
Chris Mason917c16b2011-11-08 14:49:59 -05006866 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006867 ret = btrfs_end_transaction(trans, root);
Chris Mason917c16b2011-11-08 14:49:59 -05006868 btrfs_btree_balance_dirty(root, nr);
6869 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04006870
6871out:
6872 btrfs_free_block_rsv(root, rsv);
6873
Josef Bacik3893e332011-01-31 16:03:11 -05006874 if (ret && !err)
6875 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05006876
Josef Bacik3893e332011-01-31 16:03:11 -05006877 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04006878}
6879
Sven Wegener3b963622008-06-09 21:57:42 -04006880/*
Chris Masond352ac62008-09-29 15:18:18 -04006881 * create a new subvolume directory/inode (helper for the ioctl).
6882 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05006883int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006884 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04006885{
Chris Mason39279cc2007-06-12 06:35:45 -04006886 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04006887 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006888 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006889
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01006890 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
6891 new_dirid, new_dirid,
6892 S_IFDIR | (~current_umask() & S_IRWXUGO),
6893 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04006894 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04006895 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006896 inode->i_op = &btrfs_dir_inode_operations;
6897 inode->i_fop = &btrfs_dir_file_operations;
6898
Miklos Szeredibfe86842011-10-28 14:13:29 +02006899 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04006900 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04006901
Yan, Zheng76dda932009-09-21 16:00:26 -04006902 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006903
Yan, Zheng76dda932009-09-21 16:00:26 -04006904 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07006905 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04006906}
6907
Chris Mason39279cc2007-06-12 06:35:45 -04006908struct inode *btrfs_alloc_inode(struct super_block *sb)
6909{
6910 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006911 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006912
6913 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6914 if (!ei)
6915 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006916
6917 ei->root = NULL;
6918 ei->space_info = NULL;
6919 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04006920 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04006921 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04006922 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006923 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006924 ei->disk_i_size = 0;
6925 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04006926 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006927 ei->index_cnt = (u64)-1;
6928 ei->last_unlink_trans = 0;
6929
Josef Bacik9e0baf62011-07-15 15:16:44 +00006930 spin_lock_init(&ei->lock);
6931 ei->outstanding_extents = 0;
6932 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006933
Josef Bacik72ac3c02012-05-23 14:13:11 -04006934 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08006935 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006936
Miao Xie16cdcec2011-04-22 18:12:22 +08006937 ei->delayed_node = NULL;
6938
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006939 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02006940 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02006941 extent_io_tree_init(&ei->io_tree, &inode->i_data);
6942 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04006943 ei->io_tree.track_uptodate = 1;
6944 ei->io_failure_tree.track_uptodate = 1;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006945 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05006946 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006947 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006948 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006949 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006950 RB_CLEAR_NODE(&ei->rb_node);
6951
6952 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006953}
6954
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11006955static void btrfs_i_callback(struct rcu_head *head)
6956{
6957 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11006958 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6959}
6960
Chris Mason39279cc2007-06-12 06:35:45 -04006961void btrfs_destroy_inode(struct inode *inode)
6962{
Chris Masone6dcd2d2008-07-17 12:53:50 -04006963 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006964 struct btrfs_root *root = BTRFS_I(inode)->root;
6965
Chris Mason39279cc2007-06-12 06:35:45 -04006966 WARN_ON(!list_empty(&inode->i_dentry));
6967 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00006968 WARN_ON(BTRFS_I(inode)->outstanding_extents);
6969 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04006970 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
6971 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04006972
Chris Mason5a3f23d2009-03-31 13:27:11 -04006973 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05006974 * This can happen where we create an inode, but somebody else also
6975 * created the same inode and we need to destroy the one we already
6976 * created.
6977 */
6978 if (!root)
6979 goto free;
6980
6981 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006982 * Make sure we're properly removed from the ordered operation
6983 * lists.
6984 */
6985 smp_mb();
6986 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6987 spin_lock(&root->fs_info->ordered_extent_lock);
6988 list_del_init(&BTRFS_I(inode)->ordered_operations);
6989 spin_unlock(&root->fs_info->ordered_extent_lock);
6990 }
6991
Josef Bacik8a35d952012-05-23 14:26:42 -04006992 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
6993 &BTRFS_I(inode)->runtime_flags)) {
Li Zefan33345d012011-04-20 10:31:50 +08006994 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
6995 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04006996 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04006997 }
Josef Bacik7b128762008-07-24 12:17:14 -04006998
Chris Masond3977122009-01-05 21:25:51 -05006999 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007000 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7001 if (!ordered)
7002 break;
7003 else {
Chris Masond3977122009-01-05 21:25:51 -05007004 printk(KERN_ERR "btrfs found ordered "
7005 "extent %llu %llu on inode cleanup\n",
7006 (unsigned long long)ordered->file_offset,
7007 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007008 btrfs_remove_ordered_extent(inode, ordered);
7009 btrfs_put_ordered_extent(ordered);
7010 btrfs_put_ordered_extent(ordered);
7011 }
7012 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007013 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007014 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007015free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007016 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007017 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007018}
7019
Al Viro45321ac2010-06-07 13:43:19 -04007020int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007021{
7022 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007023
Josef Bacik0af3d002010-06-21 14:48:16 -04007024 if (btrfs_root_refs(&root->root_item) == 0 &&
Chris Mason2cf85722011-07-26 15:35:09 -04007025 !btrfs_is_free_space_inode(root, inode))
Al Viro45321ac2010-06-07 13:43:19 -04007026 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007027 else
Al Viro45321ac2010-06-07 13:43:19 -04007028 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007029}
7030
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007031static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007032{
7033 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7034
7035 inode_init_once(&ei->vfs_inode);
7036}
7037
7038void btrfs_destroy_cachep(void)
7039{
7040 if (btrfs_inode_cachep)
7041 kmem_cache_destroy(btrfs_inode_cachep);
7042 if (btrfs_trans_handle_cachep)
7043 kmem_cache_destroy(btrfs_trans_handle_cachep);
7044 if (btrfs_transaction_cachep)
7045 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007046 if (btrfs_path_cachep)
7047 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007048 if (btrfs_free_space_cachep)
7049 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007050}
7051
7052int btrfs_init_cachep(void)
7053{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007054 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
7055 sizeof(struct btrfs_inode), 0,
7056 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007057 if (!btrfs_inode_cachep)
7058 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007059
7060 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
7061 sizeof(struct btrfs_trans_handle), 0,
7062 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007063 if (!btrfs_trans_handle_cachep)
7064 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007065
7066 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
7067 sizeof(struct btrfs_transaction), 0,
7068 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007069 if (!btrfs_transaction_cachep)
7070 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007071
7072 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
7073 sizeof(struct btrfs_path), 0,
7074 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007075 if (!btrfs_path_cachep)
7076 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007077
Josef Bacikdc89e982011-01-28 17:05:48 -05007078 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
7079 sizeof(struct btrfs_free_space), 0,
7080 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7081 if (!btrfs_free_space_cachep)
7082 goto fail;
7083
Chris Mason39279cc2007-06-12 06:35:45 -04007084 return 0;
7085fail:
7086 btrfs_destroy_cachep();
7087 return -ENOMEM;
7088}
7089
7090static int btrfs_getattr(struct vfsmount *mnt,
7091 struct dentry *dentry, struct kstat *stat)
7092{
7093 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007094 u32 blocksize = inode->i_sb->s_blocksize;
7095
Chris Mason39279cc2007-06-12 06:35:45 -04007096 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007097 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007098 stat->blksize = PAGE_CACHE_SIZE;
David Sterbafadc0d82011-11-20 07:33:38 -05007099 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
7100 ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007101 return 0;
7102}
7103
Liu Bo75e7cb72011-03-22 10:12:20 +00007104/*
7105 * If a file is moved, it will inherit the cow and compression flags of the new
7106 * directory.
7107 */
7108static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7109{
7110 struct btrfs_inode *b_dir = BTRFS_I(dir);
7111 struct btrfs_inode *b_inode = BTRFS_I(inode);
7112
7113 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7114 b_inode->flags |= BTRFS_INODE_NODATACOW;
7115 else
7116 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7117
7118 if (b_dir->flags & BTRFS_INODE_COMPRESS)
7119 b_inode->flags |= BTRFS_INODE_COMPRESS;
7120 else
7121 b_inode->flags &= ~BTRFS_INODE_COMPRESS;
7122}
7123
Chris Masond3977122009-01-05 21:25:51 -05007124static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7125 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007126{
7127 struct btrfs_trans_handle *trans;
7128 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007129 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007130 struct inode *new_inode = new_dentry->d_inode;
7131 struct inode *old_inode = old_dentry->d_inode;
7132 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007133 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007134 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007135 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007136 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007137
Li Zefan33345d012011-04-20 10:31:50 +08007138 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007139 return -EPERM;
7140
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007141 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007142 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007143 return -EXDEV;
7144
Li Zefan33345d012011-04-20 10:31:50 +08007145 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7146 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007147 return -ENOTEMPTY;
7148
Chris Mason39279cc2007-06-12 06:35:45 -04007149 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007150 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007151 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007152 /*
7153 * we're using rename to replace one file with another.
7154 * and the replacement file is large. Start IO on it now so
7155 * we don't add too much work to the end of the transaction
7156 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007157 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007158 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7159 filemap_flush(old_inode->i_mapping);
7160
Yan, Zheng76dda932009-09-21 16:00:26 -04007161 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007162 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007163 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007164 /*
7165 * We want to reserve the absolute worst case amount of items. So if
7166 * both inodes are subvols and we need to unlink them then that would
7167 * require 4 item modifications, but if they are both normal inodes it
7168 * would require 5 item modifications, so we'll assume their normal
7169 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7170 * should cover the worst case number of items we'll modify.
7171 */
7172 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007173 if (IS_ERR(trans)) {
7174 ret = PTR_ERR(trans);
7175 goto out_notrans;
7176 }
Chris Mason5f39d392007-10-15 16:14:19 -04007177
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007178 if (dest != root)
7179 btrfs_record_root_in_trans(trans, dest);
7180
Yan, Zhenga5719522009-09-24 09:17:31 -04007181 ret = btrfs_set_inode_index(new_dir, &index);
7182 if (ret)
7183 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007184
Li Zefan33345d012011-04-20 10:31:50 +08007185 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007186 /* force full log commit if subvolume involved. */
7187 root->fs_info->last_trans_log_full_commit = trans->transid;
7188 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007189 ret = btrfs_insert_inode_ref(trans, dest,
7190 new_dentry->d_name.name,
7191 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007192 old_ino,
7193 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007194 if (ret)
7195 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007196 /*
7197 * this is an ugly little race, but the rename is required
7198 * to make sure that if we crash, the inode is either at the
7199 * old name or the new one. pinning the log transaction lets
7200 * us make sure we don't allow a log commit to come in after
7201 * we unlink the name but before we add the new name back in.
7202 */
7203 btrfs_pin_log_trans(root);
7204 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007205 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007206 * make sure the inode gets flushed if it is replacing
7207 * something.
7208 */
Li Zefan33345d012011-04-20 10:31:50 +08007209 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007210 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007211
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007212 inode_inc_iversion(old_dir);
7213 inode_inc_iversion(new_dir);
7214 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007215 old_dir->i_ctime = old_dir->i_mtime = ctime;
7216 new_dir->i_ctime = new_dir->i_mtime = ctime;
7217 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007218
Chris Mason12fcfd22009-03-24 10:24:20 -04007219 if (old_dentry->d_parent != new_dentry->d_parent)
7220 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7221
Li Zefan33345d012011-04-20 10:31:50 +08007222 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007223 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7224 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7225 old_dentry->d_name.name,
7226 old_dentry->d_name.len);
7227 } else {
Al Viro92986792011-03-04 17:14:37 +00007228 ret = __btrfs_unlink_inode(trans, root, old_dir,
7229 old_dentry->d_inode,
7230 old_dentry->d_name.name,
7231 old_dentry->d_name.len);
7232 if (!ret)
7233 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007234 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007235 if (ret) {
7236 btrfs_abort_transaction(trans, root, ret);
7237 goto out_fail;
7238 }
Chris Mason39279cc2007-06-12 06:35:45 -04007239
7240 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007241 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007242 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007243 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007244 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7245 root_objectid = BTRFS_I(new_inode)->location.objectid;
7246 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7247 root_objectid,
7248 new_dentry->d_name.name,
7249 new_dentry->d_name.len);
7250 BUG_ON(new_inode->i_nlink == 0);
7251 } else {
7252 ret = btrfs_unlink_inode(trans, dest, new_dir,
7253 new_dentry->d_inode,
7254 new_dentry->d_name.name,
7255 new_dentry->d_name.len);
7256 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007257 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007258 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007259 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007260 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007261 if (ret) {
7262 btrfs_abort_transaction(trans, root, ret);
7263 goto out_fail;
7264 }
Chris Mason39279cc2007-06-12 06:35:45 -04007265 }
Josef Bacikaec74772008-07-24 12:12:38 -04007266
Liu Bo75e7cb72011-03-22 10:12:20 +00007267 fixup_inode_flags(new_dir, old_inode);
7268
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007269 ret = btrfs_add_link(trans, new_dir, old_inode,
7270 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007271 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007272 if (ret) {
7273 btrfs_abort_transaction(trans, root, ret);
7274 goto out_fail;
7275 }
Chris Mason39279cc2007-06-12 06:35:45 -04007276
Li Zefan33345d012011-04-20 10:31:50 +08007277 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04007278 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00007279 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007280 btrfs_end_log_trans(root);
7281 }
Chris Mason39279cc2007-06-12 06:35:45 -04007282out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007283 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007284out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007285 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007286 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007287
Chris Mason39279cc2007-06-12 06:35:45 -04007288 return ret;
7289}
7290
Chris Masond352ac62008-09-29 15:18:18 -04007291/*
7292 * some fairly slow code that needs optimization. This walks the list
7293 * of all the inodes with pending delalloc and forces them to disk.
7294 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007295int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007296{
7297 struct list_head *head = &root->fs_info->delalloc_inodes;
7298 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007299 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04007300
Yan Zhengc146afa2008-11-12 14:34:12 -05007301 if (root->fs_info->sb->s_flags & MS_RDONLY)
7302 return -EROFS;
7303
Chris Mason75eff682008-12-15 15:54:40 -05007304 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05007305 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04007306 binode = list_entry(head->next, struct btrfs_inode,
7307 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007308 inode = igrab(&binode->vfs_inode);
7309 if (!inode)
7310 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007311 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007312 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007313 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007314 if (delay_iput)
7315 btrfs_add_delayed_iput(inode);
7316 else
7317 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007318 }
7319 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007320 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007321 }
Chris Mason75eff682008-12-15 15:54:40 -05007322 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007323
7324 /* the filemap_flush will queue IO into the worker threads, but
7325 * we have to make sure the IO is actually started and that
7326 * ordered extents get created before we return
7327 */
7328 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007329 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007330 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007331 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007332 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7333 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007334 }
7335 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04007336 return 0;
7337}
7338
Chris Mason39279cc2007-06-12 06:35:45 -04007339static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7340 const char *symname)
7341{
7342 struct btrfs_trans_handle *trans;
7343 struct btrfs_root *root = BTRFS_I(dir)->root;
7344 struct btrfs_path *path;
7345 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007346 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007347 int err;
7348 int drop_inode = 0;
7349 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007350 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007351 int name_len;
7352 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007353 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007354 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007355 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05007356 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007357
7358 name_len = strlen(symname) + 1;
7359 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7360 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007361
Josef Bacik9ed74f22009-09-11 16:12:44 -04007362 /*
7363 * 2 items for inode item and ref
7364 * 2 items for dir items
7365 * 1 item for xattr if selinux is on
7366 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007367 trans = btrfs_start_transaction(root, 5);
7368 if (IS_ERR(trans))
7369 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007370
Li Zefan581bb052011-04-20 10:06:11 +08007371 err = btrfs_find_free_ino(root, &objectid);
7372 if (err)
7373 goto out_unlock;
7374
Josef Bacikaec74772008-07-24 12:12:38 -04007375 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007376 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007377 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007378 if (IS_ERR(inode)) {
7379 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007380 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007381 }
Chris Mason39279cc2007-06-12 06:35:45 -04007382
Eric Paris2a7dba32011-02-01 11:05:39 -05007383 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007384 if (err) {
7385 drop_inode = 1;
7386 goto out_unlock;
7387 }
7388
Casey Schauflerad19db72011-12-15 10:09:07 -05007389 /*
7390 * If the active LSM wants to access the inode during
7391 * d_instantiate it needs these. Smack checks to see
7392 * if the filesystem supports xattrs by looking at the
7393 * ops vector.
7394 */
7395 inode->i_fop = &btrfs_file_operations;
7396 inode->i_op = &btrfs_file_inode_operations;
7397
Josef Bacika1b075d2010-11-19 20:36:11 +00007398 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007399 if (err)
7400 drop_inode = 1;
7401 else {
7402 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007403 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05007404 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007405 }
Chris Mason39279cc2007-06-12 06:35:45 -04007406 if (drop_inode)
7407 goto out_unlock;
7408
7409 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007410 if (!path) {
7411 err = -ENOMEM;
7412 drop_inode = 1;
7413 goto out_unlock;
7414 }
Li Zefan33345d012011-04-20 10:31:50 +08007415 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007416 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007417 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7418 datasize = btrfs_file_extent_calc_inline_size(name_len);
7419 err = btrfs_insert_empty_item(trans, root, path, &key,
7420 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007421 if (err) {
7422 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00007423 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04007424 goto out_unlock;
7425 }
Chris Mason5f39d392007-10-15 16:14:19 -04007426 leaf = path->nodes[0];
7427 ei = btrfs_item_ptr(leaf, path->slots[0],
7428 struct btrfs_file_extent_item);
7429 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7430 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007431 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007432 btrfs_set_file_extent_encryption(leaf, ei, 0);
7433 btrfs_set_file_extent_compression(leaf, ei, 0);
7434 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7435 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7436
Chris Mason39279cc2007-06-12 06:35:45 -04007437 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007438 write_extent_buffer(leaf, symname, ptr, name_len);
7439 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007440 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007441
Chris Mason39279cc2007-06-12 06:35:45 -04007442 inode->i_op = &btrfs_symlink_inode_operations;
7443 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007444 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007445 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007446 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007447 err = btrfs_update_inode(trans, root, inode);
7448 if (err)
7449 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007450
7451out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05007452 if (!err)
7453 d_instantiate(dentry, inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04007454 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007455 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007456 if (drop_inode) {
7457 inode_dec_link_count(inode);
7458 iput(inode);
7459 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04007460 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04007461 return err;
7462}
Chris Mason16432982008-04-10 10:23:21 -04007463
Josef Bacik0af3d002010-06-21 14:48:16 -04007464static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7465 u64 start, u64 num_bytes, u64 min_size,
7466 loff_t actual_len, u64 *alloc_hint,
7467 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007468{
Yan Zhengd899e052008-10-30 14:25:28 -04007469 struct btrfs_root *root = BTRFS_I(inode)->root;
7470 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007471 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007472 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007473 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007474 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007475
Josef Bacik0af3d002010-06-21 14:48:16 -04007476 if (trans)
7477 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007478 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007479 if (own_trans) {
7480 trans = btrfs_start_transaction(root, 3);
7481 if (IS_ERR(trans)) {
7482 ret = PTR_ERR(trans);
7483 break;
7484 }
Yan Zhengd899e052008-10-30 14:25:28 -04007485 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007486
Yan, Zhengefa56462010-05-16 10:49:59 -04007487 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05007488 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007489 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007490 if (own_trans)
7491 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007492 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007493 }
7494
Yan Zhengd899e052008-10-30 14:25:28 -04007495 ret = insert_reserved_file_extent(trans, inode,
7496 cur_offset, ins.objectid,
7497 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007498 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007499 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007500 if (ret) {
7501 btrfs_abort_transaction(trans, root, ret);
7502 if (own_trans)
7503 btrfs_end_transaction(trans, root);
7504 break;
7505 }
Chris Masona1ed8352009-09-11 12:27:37 -04007506 btrfs_drop_extent_cache(inode, cur_offset,
7507 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007508
Yan Zhengd899e052008-10-30 14:25:28 -04007509 num_bytes -= ins.offset;
7510 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007511 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007512
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007513 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04007514 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007515 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007516 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007517 (actual_len > inode->i_size) &&
7518 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007519 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007520 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007521 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007522 i_size = cur_offset;
7523 i_size_write(inode, i_size);
7524 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007525 }
7526
Yan Zhengd899e052008-10-30 14:25:28 -04007527 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007528
7529 if (ret) {
7530 btrfs_abort_transaction(trans, root, ret);
7531 if (own_trans)
7532 btrfs_end_transaction(trans, root);
7533 break;
7534 }
Yan Zhengd899e052008-10-30 14:25:28 -04007535
Josef Bacik0af3d002010-06-21 14:48:16 -04007536 if (own_trans)
7537 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007538 }
Yan Zhengd899e052008-10-30 14:25:28 -04007539 return ret;
7540}
7541
Josef Bacik0af3d002010-06-21 14:48:16 -04007542int btrfs_prealloc_file_range(struct inode *inode, int mode,
7543 u64 start, u64 num_bytes, u64 min_size,
7544 loff_t actual_len, u64 *alloc_hint)
7545{
7546 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7547 min_size, actual_len, alloc_hint,
7548 NULL);
7549}
7550
7551int btrfs_prealloc_file_range_trans(struct inode *inode,
7552 struct btrfs_trans_handle *trans, int mode,
7553 u64 start, u64 num_bytes, u64 min_size,
7554 loff_t actual_len, u64 *alloc_hint)
7555{
7556 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7557 min_size, actual_len, alloc_hint, trans);
7558}
7559
Chris Masone6dcd2d2008-07-17 12:53:50 -04007560static int btrfs_set_page_dirty(struct page *page)
7561{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007562 return __set_page_dirty_nobuffers(page);
7563}
7564
Al Viro10556cb2011-06-20 19:28:19 -04007565static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007566{
Li Zefanb83cc962010-12-20 16:04:08 +08007567 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007568 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08007569
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007570 if (mask & MAY_WRITE &&
7571 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7572 if (btrfs_root_readonly(root))
7573 return -EROFS;
7574 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7575 return -EACCES;
7576 }
Al Viro2830ba72011-06-20 19:16:29 -04007577 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05007578}
Chris Mason39279cc2007-06-12 06:35:45 -04007579
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007580static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007581 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007582 .lookup = btrfs_lookup,
7583 .create = btrfs_create,
7584 .unlink = btrfs_unlink,
7585 .link = btrfs_link,
7586 .mkdir = btrfs_mkdir,
7587 .rmdir = btrfs_rmdir,
7588 .rename = btrfs_rename,
7589 .symlink = btrfs_symlink,
7590 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007591 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007592 .setxattr = btrfs_setxattr,
7593 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007594 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007595 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007596 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007597 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007598};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007599static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007600 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007601 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007602 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007603};
Yan, Zheng76dda932009-09-21 16:00:26 -04007604
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007605static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007606 .llseek = generic_file_llseek,
7607 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007608 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007609 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007610#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007611 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007612#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007613 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007614 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007615};
7616
Chris Masond1310b22008-01-24 16:13:08 -05007617static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007618 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007619 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007620 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007621 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007622 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007623 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007624 .set_bit_hook = btrfs_set_bit_hook,
7625 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007626 .merge_extent_hook = btrfs_merge_extent_hook,
7627 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007628};
7629
Chris Mason35054392009-01-21 13:11:13 -05007630/*
7631 * btrfs doesn't support the bmap operation because swapfiles
7632 * use bmap to make a mapping of extents in the file. They assume
7633 * these extents won't change over the life of the file and they
7634 * use the bmap result to do IO directly to the drive.
7635 *
7636 * the btrfs bmap call would return logical addresses that aren't
7637 * suitable for IO and they also will change frequently as COW
7638 * operations happen. So, swapfile + btrfs == corruption.
7639 *
7640 * For now we're avoiding this by dropping bmap.
7641 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007642static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007643 .readpage = btrfs_readpage,
7644 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04007645 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007646 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04007647 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007648 .invalidatepage = btrfs_invalidatepage,
7649 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007650 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007651 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007652};
7653
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007654static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007655 .readpage = btrfs_readpage,
7656 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007657 .invalidatepage = btrfs_invalidatepage,
7658 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007659};
7660
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007661static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007662 .getattr = btrfs_getattr,
7663 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007664 .setxattr = btrfs_setxattr,
7665 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007666 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007667 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007668 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007669 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007670 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007671};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007672static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007673 .getattr = btrfs_getattr,
7674 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007675 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007676 .setxattr = btrfs_setxattr,
7677 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007678 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007679 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007680 .get_acl = btrfs_get_acl,
Josef Bacik618e21d2007-07-11 10:18:17 -04007681};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007682static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007683 .readlink = generic_readlink,
7684 .follow_link = page_follow_link_light,
7685 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007686 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05007687 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007688 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007689 .setxattr = btrfs_setxattr,
7690 .getxattr = btrfs_getxattr,
7691 .listxattr = btrfs_listxattr,
7692 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007693 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007694};
Yan, Zheng76dda932009-09-21 16:00:26 -04007695
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007696const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007697 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04007698 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04007699};