blob: 6f4e41dca970a78be403d061bfe6eb5123c3978b [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>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050043#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040044#include "ctree.h"
45#include "disk-io.h"
46#include "transaction.h"
47#include "btrfs_inode.h"
48#include "ioctl.h"
49#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040050#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040051#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040052#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020053#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040054#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050055#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050056#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080057#include "inode-map.h"
Chris Mason39279cc2007-06-12 06:35:45 -040058
59struct btrfs_iget_args {
60 u64 ino;
61 struct btrfs_root *root;
62};
63
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070064static const struct inode_operations btrfs_dir_inode_operations;
65static const struct inode_operations btrfs_symlink_inode_operations;
66static const struct inode_operations btrfs_dir_ro_inode_operations;
67static const struct inode_operations btrfs_special_inode_operations;
68static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070069static const struct address_space_operations btrfs_aops;
70static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070071static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050072static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040073
74static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000075static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040076struct kmem_cache *btrfs_trans_handle_cachep;
77struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040078struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050079struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040080
81#define S_SHIFT 12
82static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
83 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
84 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
85 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
86 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
87 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
88 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
89 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
90};
91
Josef Bacika41ad392011-01-31 15:30:16 -050092static int btrfs_setsize(struct inode *inode, loff_t newsize);
93static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040094static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050095static noinline int cow_file_range(struct inode *inode,
96 struct page *locked_page,
97 u64 start, u64 end, int *page_started,
98 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -040099static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
100 u64 len, u64 orig_start,
101 u64 block_start, u64 block_len,
102 u64 orig_block_len, int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400103
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000104static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500105 struct inode *inode, struct inode *dir,
106 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500107{
108 int err;
109
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000110 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500111 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500112 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500113 return err;
114}
115
Chris Masond352ac62008-09-29 15:18:18 -0400116/*
Chris Masonc8b97812008-10-29 14:49:59 -0400117 * this does all the hard work for inserting an inline extent into
118 * the btree. The caller should have done a btrfs_drop_extents so that
119 * no overlapping inline items exist in the btree
120 */
Chris Masond3977122009-01-05 21:25:51 -0500121static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400122 struct btrfs_root *root, struct inode *inode,
123 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000124 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400125 struct page **compressed_pages)
126{
127 struct btrfs_key key;
128 struct btrfs_path *path;
129 struct extent_buffer *leaf;
130 struct page *page = NULL;
131 char *kaddr;
132 unsigned long ptr;
133 struct btrfs_file_extent_item *ei;
134 int err = 0;
135 int ret;
136 size_t cur_size = size;
137 size_t datasize;
138 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400139
Li Zefanfe3f5662011-03-28 08:30:38 +0000140 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400141 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400142
Chris Masond3977122009-01-05 21:25:51 -0500143 path = btrfs_alloc_path();
144 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400145 return -ENOMEM;
146
Chris Masonb9473432009-03-13 11:00:37 -0400147 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400148
Li Zefan33345d012011-04-20 10:31:50 +0800149 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400150 key.offset = start;
151 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400152 datasize = btrfs_file_extent_calc_inline_size(cur_size);
153
154 inode_add_bytes(inode, size);
155 ret = btrfs_insert_empty_item(trans, root, path, &key,
156 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400157 if (ret) {
158 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400159 goto fail;
160 }
161 leaf = path->nodes[0];
162 ei = btrfs_item_ptr(leaf, path->slots[0],
163 struct btrfs_file_extent_item);
164 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
165 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
166 btrfs_set_file_extent_encryption(leaf, ei, 0);
167 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
168 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
169 ptr = btrfs_file_extent_inline_start(ei);
170
Li Zefan261507a02010-12-17 14:21:50 +0800171 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400172 struct page *cpage;
173 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500174 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400175 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500176 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400177 PAGE_CACHE_SIZE);
178
Cong Wang7ac687d2011-11-25 23:14:28 +0800179 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400180 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800181 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400182
183 i++;
184 ptr += cur_size;
185 compressed_size -= cur_size;
186 }
187 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800188 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400189 } else {
190 page = find_get_page(inode->i_mapping,
191 start >> PAGE_CACHE_SHIFT);
192 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800193 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400194 offset = start & (PAGE_CACHE_SIZE - 1);
195 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800196 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400197 page_cache_release(page);
198 }
199 btrfs_mark_buffer_dirty(leaf);
200 btrfs_free_path(path);
201
Yan, Zhengc21677542009-11-12 09:34:21 +0000202 /*
203 * we're an inline extent, so nobody can
204 * extend the file past i_size without locking
205 * a page we already have locked.
206 *
207 * We must do any isize and inode updates
208 * before we unlock the pages. Otherwise we
209 * could end up racing with unlink.
210 */
Chris Masonc8b97812008-10-29 14:49:59 -0400211 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100212 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc21677542009-11-12 09:34:21 +0000213
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100214 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400215fail:
216 btrfs_free_path(path);
217 return err;
218}
219
220
221/*
222 * conditionally insert an inline extent into the file. This
223 * does the checks required to make sure the data is small enough
224 * to fit as an inline extent.
225 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400226static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400227 struct btrfs_root *root,
228 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000229 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400230 struct page **compressed_pages)
231{
232 u64 isize = i_size_read(inode);
233 u64 actual_end = min(end + 1, isize);
234 u64 inline_len = actual_end - start;
235 u64 aligned_end = (end + root->sectorsize - 1) &
236 ~((u64)root->sectorsize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400237 u64 data_len = inline_len;
238 int ret;
239
240 if (compressed_size)
241 data_len = compressed_size;
242
243 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400244 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400245 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
246 (!compressed_size &&
247 (actual_end & (root->sectorsize - 1)) == 0) ||
248 end + 1 < isize ||
249 data_len > root->fs_info->max_inline) {
250 return 1;
251 }
252
Josef Bacik26714852012-08-29 12:24:27 -0400253 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100254 if (ret)
255 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400256
257 if (isize > actual_end)
258 inline_len = min_t(u64, isize, actual_end);
259 ret = insert_inline_extent(trans, root, inode, start,
260 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000261 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400262 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100263 btrfs_abort_transaction(trans, root, ret);
264 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400265 } else if (ret == -ENOSPC) {
266 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100267 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400268
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400269 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400270 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400271 return 0;
272}
273
Chris Mason771ed682008-11-06 22:02:51 -0500274struct async_extent {
275 u64 start;
276 u64 ram_size;
277 u64 compressed_size;
278 struct page **pages;
279 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800280 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500281 struct list_head list;
282};
283
284struct async_cow {
285 struct inode *inode;
286 struct btrfs_root *root;
287 struct page *locked_page;
288 u64 start;
289 u64 end;
290 struct list_head extents;
291 struct btrfs_work work;
292};
293
294static noinline int add_async_extent(struct async_cow *cow,
295 u64 start, u64 ram_size,
296 u64 compressed_size,
297 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800298 unsigned long nr_pages,
299 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500300{
301 struct async_extent *async_extent;
302
303 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100304 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500305 async_extent->start = start;
306 async_extent->ram_size = ram_size;
307 async_extent->compressed_size = compressed_size;
308 async_extent->pages = pages;
309 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800310 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500311 list_add_tail(&async_extent->list, &cow->extents);
312 return 0;
313}
314
Chris Masonc8b97812008-10-29 14:49:59 -0400315/*
Chris Mason771ed682008-11-06 22:02:51 -0500316 * we create compressed extents in two phases. The first
317 * phase compresses a range of pages that have already been
318 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400319 *
Chris Mason771ed682008-11-06 22:02:51 -0500320 * This is done inside an ordered work queue, and the compression
321 * is spread across many cpus. The actual IO submission is step
322 * two, and the ordered work queue takes care of making sure that
323 * happens in the same order things were put onto the queue by
324 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400325 *
Chris Mason771ed682008-11-06 22:02:51 -0500326 * If this code finds it can't get good compression, it puts an
327 * entry onto the work queue to write the uncompressed bytes. This
328 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300329 * are written in the same order that the flusher thread sent them
330 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400331 */
Chris Mason771ed682008-11-06 22:02:51 -0500332static noinline int compress_file_range(struct inode *inode,
333 struct page *locked_page,
334 u64 start, u64 end,
335 struct async_cow *async_cow,
336 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400337{
338 struct btrfs_root *root = BTRFS_I(inode)->root;
339 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400340 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400341 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400342 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500343 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400344 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400345 struct page **pages = NULL;
346 unsigned long nr_pages;
347 unsigned long nr_pages_ret = 0;
348 unsigned long total_compressed = 0;
349 unsigned long total_in = 0;
350 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500351 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400352 int i;
353 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800354 int compress_type = root->fs_info->compress_type;
Chris Masonb888db22007-08-27 16:49:44 -0400355
Liu Bo4cb13e52012-03-29 09:57:45 -0400356 /* if this is a small write inside eof, kick off a defrag */
357 if ((end - start + 1) < 16 * 1024 &&
358 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400359 btrfs_add_inode_defrag(NULL, inode);
360
Chris Mason42dc7ba2008-12-15 11:44:56 -0500361 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400362again:
363 will_compress = 0;
364 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
365 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
366
Chris Masonf03d9302009-02-04 09:31:06 -0500367 /*
368 * we don't want to send crud past the end of i_size through
369 * compression, that's just a waste of CPU time. So, if the
370 * end of the file is before the start of our current
371 * requested range of bytes, we bail out to the uncompressed
372 * cleanup code that can deal with all of this.
373 *
374 * It isn't really the fastest way to fix things, but this is a
375 * very uncommon corner.
376 */
377 if (actual_end <= start)
378 goto cleanup_and_bail_uncompressed;
379
Chris Masonc8b97812008-10-29 14:49:59 -0400380 total_compressed = actual_end - start;
381
382 /* we want to make sure that amount of ram required to uncompress
383 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500384 * of a compressed extent to 128k. This is a crucial number
385 * because it also controls how easily we can spread reads across
386 * cpus for decompression.
387 *
388 * We also want to make sure the amount of IO required to do
389 * a random read is reasonably small, so we limit the size of
390 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400391 */
392 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400393 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500394 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400395 total_in = 0;
396 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400397
Chris Mason771ed682008-11-06 22:02:51 -0500398 /*
399 * we do compression for mount -o compress and when the
400 * inode has not been flagged as nocompress. This flag can
401 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400402 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200403 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500404 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000405 (BTRFS_I(inode)->force_compress) ||
406 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400407 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400408 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800409 if (!pages) {
410 /* just bail out to the uncompressed code */
411 goto cont;
412 }
Chris Mason179e29e2007-11-01 11:28:41 -0400413
Li Zefan261507a02010-12-17 14:21:50 +0800414 if (BTRFS_I(inode)->force_compress)
415 compress_type = BTRFS_I(inode)->force_compress;
416
417 ret = btrfs_compress_pages(compress_type,
418 inode->i_mapping, start,
419 total_compressed, pages,
420 nr_pages, &nr_pages_ret,
421 &total_in,
422 &total_compressed,
423 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400424
425 if (!ret) {
426 unsigned long offset = total_compressed &
427 (PAGE_CACHE_SIZE - 1);
428 struct page *page = pages[nr_pages_ret - 1];
429 char *kaddr;
430
431 /* zero the tail end of the last page, we might be
432 * sending it down to disk
433 */
434 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800435 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400436 memset(kaddr + offset, 0,
437 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800438 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400439 }
440 will_compress = 1;
441 }
442 }
Li Zefan560f7d72011-09-08 10:22:01 +0800443cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400444 if (start == 0) {
Josef Bacik7a7eaa402011-04-13 12:54:33 -0400445 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100446 if (IS_ERR(trans)) {
447 ret = PTR_ERR(trans);
448 trans = NULL;
449 goto cleanup_and_out;
450 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400451 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500452
Chris Masonc8b97812008-10-29 14:49:59 -0400453 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500454 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400455 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500456 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400457 */
458 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000459 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400460 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500461 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400462 ret = cow_file_range_inline(trans, root, inode,
463 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000464 total_compressed,
465 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400466 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100467 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500468 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100469 * inline extent creation worked or returned error,
470 * we don't need to create any more async work items.
471 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500472 */
Chris Masonc8b97812008-10-29 14:49:59 -0400473 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400474 &BTRFS_I(inode)->io_tree,
475 start, end, NULL,
476 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400477 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400478 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc21677542009-11-12 09:34:21 +0000479
480 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400481 goto free_pages_out;
482 }
Yan, Zhengc21677542009-11-12 09:34:21 +0000483 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400484 }
485
486 if (will_compress) {
487 /*
488 * we aren't doing an inline extent round the compressed size
489 * up to a block size boundary so the allocator does sane
490 * things
491 */
492 total_compressed = (total_compressed + blocksize - 1) &
493 ~(blocksize - 1);
494
495 /*
496 * one last check to make sure the compression is really a
497 * win, compare the page count read with the blocks on disk
498 */
499 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
500 ~(PAGE_CACHE_SIZE - 1);
501 if (total_compressed >= total_in) {
502 will_compress = 0;
503 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400504 num_bytes = total_in;
505 }
506 }
507 if (!will_compress && pages) {
508 /*
509 * the compression code ran but failed to make things smaller,
510 * free any pages it allocated and our page pointer array
511 */
512 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400513 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400514 page_cache_release(pages[i]);
515 }
516 kfree(pages);
517 pages = NULL;
518 total_compressed = 0;
519 nr_pages_ret = 0;
520
521 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500522 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
523 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500524 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500525 }
Chris Masonc8b97812008-10-29 14:49:59 -0400526 }
Chris Mason771ed682008-11-06 22:02:51 -0500527 if (will_compress) {
528 *num_added += 1;
529
530 /* the async work queues will take care of doing actual
531 * allocation on disk for these compressed pages,
532 * and will submit them to the elevator.
533 */
534 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800535 total_compressed, pages, nr_pages_ret,
536 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500537
Yan, Zheng24ae6362010-12-06 07:02:36 +0000538 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500539 start += num_bytes;
540 pages = NULL;
541 cond_resched();
542 goto again;
543 }
544 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500545cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500546 /*
547 * No compression, but we still need to write the pages in
548 * the file we've been given so far. redirty the locked
549 * page if it corresponds to our extent and set things up
550 * for the async work queue to run cow_file_range to do
551 * the normal delalloc dance
552 */
553 if (page_offset(locked_page) >= start &&
554 page_offset(locked_page) <= end) {
555 __set_page_dirty_nobuffers(locked_page);
556 /* unlocked later on in the async handlers */
557 }
Li Zefan261507a02010-12-17 14:21:50 +0800558 add_async_extent(async_cow, start, end - start + 1,
559 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500560 *num_added += 1;
561 }
562
563out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100564 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500565
566free_pages_out:
567 for (i = 0; i < nr_pages_ret; i++) {
568 WARN_ON(pages[i]->mapping);
569 page_cache_release(pages[i]);
570 }
Chris Masond3977122009-01-05 21:25:51 -0500571 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500572
573 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100574
575cleanup_and_out:
576 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
577 start, end, NULL,
578 EXTENT_CLEAR_UNLOCK_PAGE |
579 EXTENT_CLEAR_DIRTY |
580 EXTENT_CLEAR_DELALLOC |
581 EXTENT_SET_WRITEBACK |
582 EXTENT_END_WRITEBACK);
583 if (!trans || IS_ERR(trans))
584 btrfs_error(root->fs_info, ret, "Failed to join transaction");
585 else
586 btrfs_abort_transaction(trans, root, ret);
587 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500588}
589
590/*
591 * phase two of compressed writeback. This is the ordered portion
592 * of the code, which only gets called in the order the work was
593 * queued. We walk all the async extents created by compress_file_range
594 * and send them down to the disk.
595 */
596static noinline int submit_compressed_extents(struct inode *inode,
597 struct async_cow *async_cow)
598{
599 struct async_extent *async_extent;
600 u64 alloc_hint = 0;
601 struct btrfs_trans_handle *trans;
602 struct btrfs_key ins;
603 struct extent_map *em;
604 struct btrfs_root *root = BTRFS_I(inode)->root;
605 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
606 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500607 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500608
609 if (list_empty(&async_cow->extents))
610 return 0;
611
Chris Mason771ed682008-11-06 22:02:51 -0500612
Chris Masond3977122009-01-05 21:25:51 -0500613 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500614 async_extent = list_entry(async_cow->extents.next,
615 struct async_extent, list);
616 list_del(&async_extent->list);
617
618 io_tree = &BTRFS_I(inode)->io_tree;
619
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500620retry:
Chris Mason771ed682008-11-06 22:02:51 -0500621 /* did the compression code fall back to uncompressed IO? */
622 if (!async_extent->pages) {
623 int page_started = 0;
624 unsigned long nr_written = 0;
625
626 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000627 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100628 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500629
630 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500631 ret = cow_file_range(inode, async_cow->locked_page,
632 async_extent->start,
633 async_extent->start +
634 async_extent->ram_size - 1,
635 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500636
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100637 /* JDM XXX */
638
Chris Mason771ed682008-11-06 22:02:51 -0500639 /*
640 * if page_started, cow_file_range inserted an
641 * inline extent and took care of all the unlocking
642 * and IO for us. Otherwise, we need to submit
643 * all those pages down to the drive.
644 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500645 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500646 extent_write_locked_range(io_tree,
647 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500648 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500649 async_extent->ram_size - 1,
650 btrfs_get_extent,
651 WB_SYNC_ALL);
652 kfree(async_extent);
653 cond_resched();
654 continue;
655 }
656
657 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100658 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500659
Josef Bacik7a7eaa402011-04-13 12:54:33 -0400660 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100661 if (IS_ERR(trans)) {
662 ret = PTR_ERR(trans);
663 } else {
664 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
665 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500666 async_extent->compressed_size,
667 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500668 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600669 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100670 btrfs_abort_transaction(trans, root, ret);
671 btrfs_end_transaction(trans, root);
672 }
Yan, Zhengc21677542009-11-12 09:34:21 +0000673
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500674 if (ret) {
675 int i;
676 for (i = 0; i < async_extent->nr_pages; i++) {
677 WARN_ON(async_extent->pages[i]->mapping);
678 page_cache_release(async_extent->pages[i]);
679 }
680 kfree(async_extent->pages);
681 async_extent->nr_pages = 0;
682 async_extent->pages = NULL;
683 unlock_extent(io_tree, async_extent->start,
684 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100685 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100686 if (ret == -ENOSPC)
687 goto retry;
688 goto out_free; /* JDM: Requeue? */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500689 }
690
Yan, Zhengc21677542009-11-12 09:34:21 +0000691 /*
692 * here we're doing allocation and writeback of the
693 * compressed pages
694 */
695 btrfs_drop_extent_cache(inode, async_extent->start,
696 async_extent->start +
697 async_extent->ram_size - 1, 0);
698
David Sterba172ddd62011-04-21 00:48:27 +0200699 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100700 BUG_ON(!em); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500701 em->start = async_extent->start;
702 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500703 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500704
705 em->block_start = ins.objectid;
706 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500707 em->orig_block_len = ins.offset;
Chris Mason771ed682008-11-06 22:02:51 -0500708 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800709 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500710 set_bit(EXTENT_FLAG_PINNED, &em->flags);
711 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400712 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500713
Chris Masond3977122009-01-05 21:25:51 -0500714 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400715 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500716 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400717 if (!ret)
718 list_move(&em->list,
719 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400720 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500721 if (ret != -EEXIST) {
722 free_extent_map(em);
723 break;
724 }
725 btrfs_drop_extent_cache(inode, async_extent->start,
726 async_extent->start +
727 async_extent->ram_size - 1, 0);
728 }
729
Li Zefan261507a02010-12-17 14:21:50 +0800730 ret = btrfs_add_ordered_extent_compress(inode,
731 async_extent->start,
732 ins.objectid,
733 async_extent->ram_size,
734 ins.offset,
735 BTRFS_ORDERED_COMPRESSED,
736 async_extent->compress_type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100737 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500738
Chris Mason771ed682008-11-06 22:02:51 -0500739 /*
740 * clear dirty, set writeback and unlock the pages.
741 */
742 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400743 &BTRFS_I(inode)->io_tree,
744 async_extent->start,
745 async_extent->start +
746 async_extent->ram_size - 1,
747 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
748 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400749 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400750 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500751
752 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500753 async_extent->start,
754 async_extent->ram_size,
755 ins.objectid,
756 ins.offset, async_extent->pages,
757 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500758
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100759 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500760 alloc_hint = ins.objectid + ins.offset;
761 kfree(async_extent);
762 cond_resched();
763 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100764 ret = 0;
765out:
766 return ret;
767out_free:
768 kfree(async_extent);
769 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500770}
771
Josef Bacik4b46fce2010-05-23 11:00:55 -0400772static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
773 u64 num_bytes)
774{
775 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
776 struct extent_map *em;
777 u64 alloc_hint = 0;
778
779 read_lock(&em_tree->lock);
780 em = search_extent_mapping(em_tree, start, num_bytes);
781 if (em) {
782 /*
783 * if block start isn't an actual block number then find the
784 * first block in this inode and use that as a hint. If that
785 * block is also bogus then just don't worry about it.
786 */
787 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
788 free_extent_map(em);
789 em = search_extent_mapping(em_tree, 0, 0);
790 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
791 alloc_hint = em->block_start;
792 if (em)
793 free_extent_map(em);
794 } else {
795 alloc_hint = em->block_start;
796 free_extent_map(em);
797 }
798 }
799 read_unlock(&em_tree->lock);
800
801 return alloc_hint;
802}
803
Chris Mason771ed682008-11-06 22:02:51 -0500804/*
805 * when extent_io.c finds a delayed allocation range in the file,
806 * the call backs end up in this code. The basic idea is to
807 * allocate extents on disk for the range, and create ordered data structs
808 * in ram to track those extents.
809 *
810 * locked_page is the page that writepage had locked already. We use
811 * it to make sure we don't do extra locks or unlocks.
812 *
813 * *page_started is set to one if we unlock locked_page and do everything
814 * required to start IO on it. It may be clean and already done with
815 * IO when we return.
816 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000817static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
818 struct inode *inode,
819 struct btrfs_root *root,
820 struct page *locked_page,
821 u64 start, u64 end, int *page_started,
822 unsigned long *nr_written,
823 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500824{
Chris Mason771ed682008-11-06 22:02:51 -0500825 u64 alloc_hint = 0;
826 u64 num_bytes;
827 unsigned long ram_size;
828 u64 disk_num_bytes;
829 u64 cur_alloc_size;
830 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500831 struct btrfs_key ins;
832 struct extent_map *em;
833 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
834 int ret = 0;
835
Liu Bo83eea1f2012-07-10 05:28:39 -0600836 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500837
Chris Mason771ed682008-11-06 22:02:51 -0500838 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
839 num_bytes = max(blocksize, num_bytes);
840 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500841
Chris Mason4cb53002011-05-24 15:35:30 -0400842 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400843 if (num_bytes < 64 * 1024 &&
844 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400845 btrfs_add_inode_defrag(trans, inode);
846
Chris Mason771ed682008-11-06 22:02:51 -0500847 if (start == 0) {
848 /* lets try to make an inline extent */
849 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000850 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500851 if (ret == 0) {
852 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400853 &BTRFS_I(inode)->io_tree,
854 start, end, NULL,
855 EXTENT_CLEAR_UNLOCK_PAGE |
856 EXTENT_CLEAR_UNLOCK |
857 EXTENT_CLEAR_DELALLOC |
858 EXTENT_CLEAR_DIRTY |
859 EXTENT_SET_WRITEBACK |
860 EXTENT_END_WRITEBACK);
Yan, Zhengc21677542009-11-12 09:34:21 +0000861
Chris Mason771ed682008-11-06 22:02:51 -0500862 *nr_written = *nr_written +
863 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
864 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500865 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100866 } else if (ret < 0) {
867 btrfs_abort_transaction(trans, root, ret);
868 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500869 }
870 }
Chris Masonc8b97812008-10-29 14:49:59 -0400871
872 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200873 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400874
Josef Bacik4b46fce2010-05-23 11:00:55 -0400875 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400876 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400877
Chris Masond3977122009-01-05 21:25:51 -0500878 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400879 unsigned long op;
880
Josef Bacik287a0ab2010-03-19 18:07:23 +0000881 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400882 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500883 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500884 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100885 if (ret < 0) {
886 btrfs_abort_transaction(trans, root, ret);
887 goto out_unlock;
888 }
Chris Masond3977122009-01-05 21:25:51 -0500889
David Sterba172ddd62011-04-21 00:48:27 +0200890 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100891 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400892 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500893 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500894 ram_size = ins.offset;
895 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400896
Chris Masone6dcd2d2008-07-17 12:53:50 -0400897 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400898 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500899 em->orig_block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400900 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400901 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400902 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400903
Chris Masond3977122009-01-05 21:25:51 -0500904 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400905 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400906 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400907 if (!ret)
908 list_move(&em->list,
909 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400910 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400911 if (ret != -EEXIST) {
912 free_extent_map(em);
913 break;
914 }
915 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400916 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400917 }
918
Chris Mason98d20f62008-04-14 09:46:10 -0400919 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400920 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500921 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100922 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -0400923
Yan Zheng17d217f2008-12-12 10:03:38 -0500924 if (root->root_key.objectid ==
925 BTRFS_DATA_RELOC_TREE_OBJECTID) {
926 ret = btrfs_reloc_clone_csums(inode, start,
927 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100928 if (ret) {
929 btrfs_abort_transaction(trans, root, ret);
930 goto out_unlock;
931 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500932 }
933
Chris Masond3977122009-01-05 21:25:51 -0500934 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400935 break;
Chris Masond3977122009-01-05 21:25:51 -0500936
Chris Masonc8b97812008-10-29 14:49:59 -0400937 /* we're not doing compressed IO, don't unlock the first
938 * page (which the caller expects to stay locked), don't
939 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400940 *
941 * Do set the Private2 bit so we know this page was properly
942 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400943 */
Chris Masona791e352009-10-08 11:27:10 -0400944 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
945 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
946 EXTENT_SET_PRIVATE2;
947
Chris Masonc8b97812008-10-29 14:49:59 -0400948 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
949 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400950 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400951 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500952 num_bytes -= cur_alloc_size;
953 alloc_hint = ins.objectid + ins.offset;
954 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400955 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100956out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500957 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000958
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100959out_unlock:
960 extent_clear_unlock_delalloc(inode,
961 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400962 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100963 EXTENT_CLEAR_UNLOCK_PAGE |
964 EXTENT_CLEAR_UNLOCK |
965 EXTENT_CLEAR_DELALLOC |
966 EXTENT_CLEAR_DIRTY |
967 EXTENT_SET_WRITEBACK |
968 EXTENT_END_WRITEBACK);
969
970 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500971}
Chris Masonc8b97812008-10-29 14:49:59 -0400972
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000973static noinline int cow_file_range(struct inode *inode,
974 struct page *locked_page,
975 u64 start, u64 end, int *page_started,
976 unsigned long *nr_written,
977 int unlock)
978{
979 struct btrfs_trans_handle *trans;
980 struct btrfs_root *root = BTRFS_I(inode)->root;
981 int ret;
982
983 trans = btrfs_join_transaction(root);
984 if (IS_ERR(trans)) {
985 extent_clear_unlock_delalloc(inode,
986 &BTRFS_I(inode)->io_tree,
987 start, end, locked_page,
988 EXTENT_CLEAR_UNLOCK_PAGE |
989 EXTENT_CLEAR_UNLOCK |
990 EXTENT_CLEAR_DELALLOC |
991 EXTENT_CLEAR_DIRTY |
992 EXTENT_SET_WRITEBACK |
993 EXTENT_END_WRITEBACK);
994 return PTR_ERR(trans);
995 }
996 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
997
998 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
999 page_started, nr_written, unlock);
1000
1001 btrfs_end_transaction(trans, root);
1002
1003 return ret;
1004}
1005
Chris Mason771ed682008-11-06 22:02:51 -05001006/*
1007 * work queue call back to started compression on a file and pages
1008 */
1009static noinline void async_cow_start(struct btrfs_work *work)
1010{
1011 struct async_cow *async_cow;
1012 int num_added = 0;
1013 async_cow = container_of(work, struct async_cow, work);
1014
1015 compress_file_range(async_cow->inode, async_cow->locked_page,
1016 async_cow->start, async_cow->end, async_cow,
1017 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001018 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001019 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001020 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001021 }
Chris Mason771ed682008-11-06 22:02:51 -05001022}
1023
1024/*
1025 * work queue call back to submit previously compressed pages
1026 */
1027static noinline void async_cow_submit(struct btrfs_work *work)
1028{
1029 struct async_cow *async_cow;
1030 struct btrfs_root *root;
1031 unsigned long nr_pages;
1032
1033 async_cow = container_of(work, struct async_cow, work);
1034
1035 root = async_cow->root;
1036 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1037 PAGE_CACHE_SHIFT;
1038
Josef Bacik66657b32012-08-01 15:36:24 -04001039 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001040 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001041 waitqueue_active(&root->fs_info->async_submit_wait))
1042 wake_up(&root->fs_info->async_submit_wait);
1043
Chris Masond3977122009-01-05 21:25:51 -05001044 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001045 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001046}
Chris Masonc8b97812008-10-29 14:49:59 -04001047
Chris Mason771ed682008-11-06 22:02:51 -05001048static noinline void async_cow_free(struct btrfs_work *work)
1049{
1050 struct async_cow *async_cow;
1051 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001052 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001053 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001054 kfree(async_cow);
1055}
1056
1057static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1058 u64 start, u64 end, int *page_started,
1059 unsigned long *nr_written)
1060{
1061 struct async_cow *async_cow;
1062 struct btrfs_root *root = BTRFS_I(inode)->root;
1063 unsigned long nr_pages;
1064 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001065 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001066
Chris Masona3429ab2009-10-08 12:30:20 -04001067 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1068 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001069 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001070 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001071 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001072 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001073 async_cow->root = root;
1074 async_cow->locked_page = locked_page;
1075 async_cow->start = start;
1076
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001077 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001078 cur_end = end;
1079 else
1080 cur_end = min(end, start + 512 * 1024 - 1);
1081
1082 async_cow->end = cur_end;
1083 INIT_LIST_HEAD(&async_cow->extents);
1084
1085 async_cow->work.func = async_cow_start;
1086 async_cow->work.ordered_func = async_cow_submit;
1087 async_cow->work.ordered_free = async_cow_free;
1088 async_cow->work.flags = 0;
1089
Chris Mason771ed682008-11-06 22:02:51 -05001090 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1091 PAGE_CACHE_SHIFT;
1092 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1093
1094 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1095 &async_cow->work);
1096
1097 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1098 wait_event(root->fs_info->async_submit_wait,
1099 (atomic_read(&root->fs_info->async_delalloc_pages) <
1100 limit));
1101 }
1102
Chris Masond3977122009-01-05 21:25:51 -05001103 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001104 atomic_read(&root->fs_info->async_delalloc_pages)) {
1105 wait_event(root->fs_info->async_submit_wait,
1106 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1107 0));
1108 }
1109
1110 *nr_written += nr_pages;
1111 start = cur_end + 1;
1112 }
1113 *page_started = 1;
1114 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001115}
1116
Chris Masond3977122009-01-05 21:25:51 -05001117static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001118 u64 bytenr, u64 num_bytes)
1119{
1120 int ret;
1121 struct btrfs_ordered_sum *sums;
1122 LIST_HEAD(list);
1123
Yan Zheng07d400a2009-01-06 11:42:00 -05001124 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001125 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001126 if (ret == 0 && list_empty(&list))
1127 return 0;
1128
1129 while (!list_empty(&list)) {
1130 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1131 list_del(&sums->list);
1132 kfree(sums);
1133 }
1134 return 1;
1135}
1136
Chris Masond352ac62008-09-29 15:18:18 -04001137/*
1138 * when nowcow writeback call back. This checks for snapshots or COW copies
1139 * of the extents that exist in the file, and COWs the file as required.
1140 *
1141 * If no cow copies or snapshots exist, we write directly to the existing
1142 * blocks on disk
1143 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001144static noinline int run_delalloc_nocow(struct inode *inode,
1145 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001146 u64 start, u64 end, int *page_started, int force,
1147 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001148{
Chris Masonbe20aa92007-12-17 20:14:01 -05001149 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001150 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001151 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001152 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001153 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001154 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001155 u64 cow_start;
1156 u64 cur_offset;
1157 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001158 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001159 u64 disk_bytenr;
1160 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001161 u64 disk_num_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001162 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001163 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001164 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001165 int nocow;
1166 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001167 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001168 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001169
1170 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001171 if (!path) {
1172 extent_clear_unlock_delalloc(inode,
1173 &BTRFS_I(inode)->io_tree,
1174 start, end, locked_page,
1175 EXTENT_CLEAR_UNLOCK_PAGE |
1176 EXTENT_CLEAR_UNLOCK |
1177 EXTENT_CLEAR_DELALLOC |
1178 EXTENT_CLEAR_DIRTY |
1179 EXTENT_SET_WRITEBACK |
1180 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001181 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001182 }
Li Zefan82d59022011-04-20 10:33:24 +08001183
Liu Bo83eea1f2012-07-10 05:28:39 -06001184 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001185
1186 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001187 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001188 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001189 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001190
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001191 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001192 extent_clear_unlock_delalloc(inode,
1193 &BTRFS_I(inode)->io_tree,
1194 start, end, locked_page,
1195 EXTENT_CLEAR_UNLOCK_PAGE |
1196 EXTENT_CLEAR_UNLOCK |
1197 EXTENT_CLEAR_DELALLOC |
1198 EXTENT_CLEAR_DIRTY |
1199 EXTENT_SET_WRITEBACK |
1200 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001201 btrfs_free_path(path);
1202 return PTR_ERR(trans);
1203 }
1204
Josef Bacik74b21072011-04-13 12:02:53 -04001205 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001206
Yan Zheng80ff3852008-10-30 14:20:02 -04001207 cow_start = (u64)-1;
1208 cur_offset = start;
1209 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001210 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001211 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001212 if (ret < 0) {
1213 btrfs_abort_transaction(trans, root, ret);
1214 goto error;
1215 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001216 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1217 leaf = path->nodes[0];
1218 btrfs_item_key_to_cpu(leaf, &found_key,
1219 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001220 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001221 found_key.type == BTRFS_EXTENT_DATA_KEY)
1222 path->slots[0]--;
1223 }
1224 check_prev = 0;
1225next_slot:
1226 leaf = path->nodes[0];
1227 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1228 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001229 if (ret < 0) {
1230 btrfs_abort_transaction(trans, root, ret);
1231 goto error;
1232 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001233 if (ret > 0)
1234 break;
1235 leaf = path->nodes[0];
1236 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001237
Yan Zheng80ff3852008-10-30 14:20:02 -04001238 nocow = 0;
1239 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001240 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001241 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001242
Li Zefan33345d012011-04-20 10:31:50 +08001243 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001244 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1245 found_key.offset > end)
1246 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001247
Yan Zheng80ff3852008-10-30 14:20:02 -04001248 if (found_key.offset > cur_offset) {
1249 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001250 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001251 goto out_check;
1252 }
Chris Masonc31f8832008-01-08 15:46:31 -05001253
Yan Zheng80ff3852008-10-30 14:20:02 -04001254 fi = btrfs_item_ptr(leaf, path->slots[0],
1255 struct btrfs_file_extent_item);
1256 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001257
Yan Zhengd899e052008-10-30 14:25:28 -04001258 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1259 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001260 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001261 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 extent_end = found_key.offset +
1263 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001264 disk_num_bytes =
1265 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 if (extent_end <= start) {
1267 path->slots[0]++;
1268 goto next_slot;
1269 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001270 if (disk_bytenr == 0)
1271 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001272 if (btrfs_file_extent_compression(leaf, fi) ||
1273 btrfs_file_extent_encryption(leaf, fi) ||
1274 btrfs_file_extent_other_encoding(leaf, fi))
1275 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001276 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1277 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001278 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001279 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001280 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001281 found_key.offset -
1282 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001283 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001284 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001285 disk_bytenr += cur_offset - found_key.offset;
1286 num_bytes = min(end + 1, extent_end) - cur_offset;
1287 /*
1288 * force cow if csum exists in the range.
1289 * this ensure that csum for a given extent are
1290 * either valid or do not exist.
1291 */
1292 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1293 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001294 nocow = 1;
1295 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1296 extent_end = found_key.offset +
1297 btrfs_file_extent_inline_len(leaf, fi);
1298 extent_end = ALIGN(extent_end, root->sectorsize);
1299 } else {
1300 BUG_ON(1);
1301 }
1302out_check:
1303 if (extent_end <= start) {
1304 path->slots[0]++;
1305 goto next_slot;
1306 }
1307 if (!nocow) {
1308 if (cow_start == (u64)-1)
1309 cow_start = cur_offset;
1310 cur_offset = extent_end;
1311 if (cur_offset > end)
1312 break;
1313 path->slots[0]++;
1314 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001315 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001316
David Sterbab3b4aa72011-04-21 01:20:15 +02001317 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001318 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001319 ret = __cow_file_range(trans, inode, root, locked_page,
1320 cow_start, found_key.offset - 1,
1321 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001322 if (ret) {
1323 btrfs_abort_transaction(trans, root, ret);
1324 goto error;
1325 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001326 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001327 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001328
Yan Zhengd899e052008-10-30 14:25:28 -04001329 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1330 struct extent_map *em;
1331 struct extent_map_tree *em_tree;
1332 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001333 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001334 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001335 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001336 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001337 em->len = num_bytes;
1338 em->block_len = num_bytes;
1339 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001340 em->orig_block_len = disk_num_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001341 em->bdev = root->fs_info->fs_devices->latest_bdev;
1342 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001343 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001344 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001345 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001346 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001347 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04001348 if (!ret)
1349 list_move(&em->list,
1350 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -04001351 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001352 if (ret != -EEXIST) {
1353 free_extent_map(em);
1354 break;
1355 }
1356 btrfs_drop_extent_cache(inode, em->start,
1357 em->start + em->len - 1, 0);
1358 }
1359 type = BTRFS_ORDERED_PREALLOC;
1360 } else {
1361 type = BTRFS_ORDERED_NOCOW;
1362 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001363
1364 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001365 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001366 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001367
Yan, Zhengefa56462010-05-16 10:49:59 -04001368 if (root->root_key.objectid ==
1369 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1370 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1371 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001372 if (ret) {
1373 btrfs_abort_transaction(trans, root, ret);
1374 goto error;
1375 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001376 }
1377
Yan Zhengd899e052008-10-30 14:25:28 -04001378 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001379 cur_offset, cur_offset + num_bytes - 1,
1380 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1381 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1382 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001383 cur_offset = extent_end;
1384 if (cur_offset > end)
1385 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001386 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001387 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001388
Josef Bacik17ca04a2012-05-31 15:58:55 -04001389 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001390 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001391 cur_offset = end;
1392 }
1393
Yan Zheng80ff3852008-10-30 14:20:02 -04001394 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001395 ret = __cow_file_range(trans, inode, root, locked_page,
1396 cow_start, end,
1397 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001398 if (ret) {
1399 btrfs_abort_transaction(trans, root, ret);
1400 goto error;
1401 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001402 }
1403
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001404error:
Miao Xiea698d0752012-09-20 01:51:59 -06001405 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001406 if (!ret)
1407 ret = err;
1408
Josef Bacik17ca04a2012-05-31 15:58:55 -04001409 if (ret && cur_offset < end)
1410 extent_clear_unlock_delalloc(inode,
1411 &BTRFS_I(inode)->io_tree,
1412 cur_offset, end, locked_page,
1413 EXTENT_CLEAR_UNLOCK_PAGE |
1414 EXTENT_CLEAR_UNLOCK |
1415 EXTENT_CLEAR_DELALLOC |
1416 EXTENT_CLEAR_DIRTY |
1417 EXTENT_SET_WRITEBACK |
1418 EXTENT_END_WRITEBACK);
1419
Yan Zheng7ea394f2008-08-05 13:05:02 -04001420 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001421 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001422}
1423
Chris Masond352ac62008-09-29 15:18:18 -04001424/*
1425 * extent_io.c call back to do delayed allocation processing
1426 */
Chris Masonc8b97812008-10-29 14:49:59 -04001427static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001428 u64 start, u64 end, int *page_started,
1429 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001430{
Chris Masonbe20aa92007-12-17 20:14:01 -05001431 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001432 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001433
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001434 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001435 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001436 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001437 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001438 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001439 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001440 } else if (!btrfs_test_opt(root, COMPRESS) &&
1441 !(BTRFS_I(inode)->force_compress) &&
1442 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001443 ret = cow_file_range(inode, locked_page, start, end,
1444 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001445 } else {
1446 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1447 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001448 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001449 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001450 }
Chris Masonb888db22007-08-27 16:49:44 -04001451 return ret;
1452}
1453
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001454static void btrfs_split_extent_hook(struct inode *inode,
1455 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001456{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001457 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001458 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001459 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001460
Josef Bacik9e0baf62011-07-15 15:16:44 +00001461 spin_lock(&BTRFS_I(inode)->lock);
1462 BTRFS_I(inode)->outstanding_extents++;
1463 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001464}
1465
1466/*
1467 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1468 * extents so we can keep track of new extents that are just merged onto old
1469 * extents, such as when we are doing sequential writes, so we can properly
1470 * account for the metadata space we'll need.
1471 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001472static void btrfs_merge_extent_hook(struct inode *inode,
1473 struct extent_state *new,
1474 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001475{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001476 /* not delalloc, ignore it */
1477 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001478 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001479
Josef Bacik9e0baf62011-07-15 15:16:44 +00001480 spin_lock(&BTRFS_I(inode)->lock);
1481 BTRFS_I(inode)->outstanding_extents--;
1482 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001483}
1484
Chris Masond352ac62008-09-29 15:18:18 -04001485/*
1486 * extent_io.c set_bit_hook, used to track delayed allocation
1487 * bytes in this file, and to maintain the list of inodes that
1488 * have pending delalloc work to be done.
1489 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001490static void btrfs_set_bit_hook(struct inode *inode,
1491 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001492{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001493
Chris Mason75eff682008-12-15 15:54:40 -05001494 /*
1495 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001496 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001497 * bit, which is only set or cleared with irqs on
1498 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001499 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001500 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001501 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001502 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001503
Josef Bacik9e0baf62011-07-15 15:16:44 +00001504 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001505 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001506 } else {
1507 spin_lock(&BTRFS_I(inode)->lock);
1508 BTRFS_I(inode)->outstanding_extents++;
1509 spin_unlock(&BTRFS_I(inode)->lock);
1510 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001511
Chris Mason75eff682008-12-15 15:54:40 -05001512 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001513 BTRFS_I(inode)->delalloc_bytes += len;
1514 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001515 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001516 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1517 &root->fs_info->delalloc_inodes);
1518 }
Chris Mason75eff682008-12-15 15:54:40 -05001519 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001520 }
Chris Mason291d6732008-01-29 15:55:23 -05001521}
1522
Chris Masond352ac62008-09-29 15:18:18 -04001523/*
1524 * extent_io.c clear_bit_hook, see set_bit_hook for why
1525 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001526static void btrfs_clear_bit_hook(struct inode *inode,
1527 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001528{
Chris Mason75eff682008-12-15 15:54:40 -05001529 /*
1530 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001531 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001532 * bit, which is only set or cleared with irqs on
1533 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001534 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001535 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001536 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001537 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001538
Josef Bacik9e0baf62011-07-15 15:16:44 +00001539 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001540 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001541 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1542 spin_lock(&BTRFS_I(inode)->lock);
1543 BTRFS_I(inode)->outstanding_extents--;
1544 spin_unlock(&BTRFS_I(inode)->lock);
1545 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001546
1547 if (*bits & EXTENT_DO_ACCOUNTING)
1548 btrfs_delalloc_release_metadata(inode, len);
1549
Josef Bacik0cb59c92010-07-02 12:14:14 -04001550 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1551 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001552 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001553
Chris Mason75eff682008-12-15 15:54:40 -05001554 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001555 root->fs_info->delalloc_bytes -= len;
1556 BTRFS_I(inode)->delalloc_bytes -= len;
1557
Josef Bacik0cb59c92010-07-02 12:14:14 -04001558 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001559 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1560 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1561 }
Chris Mason75eff682008-12-15 15:54:40 -05001562 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001563 }
Chris Mason291d6732008-01-29 15:55:23 -05001564}
1565
Chris Masond352ac62008-09-29 15:18:18 -04001566/*
1567 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1568 * we don't create bios that span stripes or chunks
1569 */
David Woodhouse64a16702009-07-15 23:29:37 +01001570int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001571 size_t size, struct bio *bio,
1572 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001573{
1574 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001575 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001576 u64 length = 0;
1577 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001578 int ret;
1579
Chris Mason771ed682008-11-06 22:02:51 -05001580 if (bio_flags & EXTENT_BIO_COMPRESSED)
1581 return 0;
1582
Chris Masonf2d8d742008-04-21 10:03:05 -04001583 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001584 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001585 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001586 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001587 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001588 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001589 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001590 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001591 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001592}
1593
Chris Masond352ac62008-09-29 15:18:18 -04001594/*
1595 * in order to insert checksums into the metadata in large chunks,
1596 * we wait until bio submission time. All the pages in the bio are
1597 * checksummed and sums are attached onto the ordered extent record.
1598 *
1599 * At IO completion time the cums attached on the ordered extent record
1600 * are inserted into the btree
1601 */
Chris Masond3977122009-01-05 21:25:51 -05001602static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1603 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001604 unsigned long bio_flags,
1605 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001606{
Chris Mason065631f2008-02-20 12:07:25 -05001607 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001608 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001609
Chris Masond20f7042008-12-08 16:58:54 -05001610 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001611 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001612 return 0;
1613}
Chris Masone0156402008-04-16 11:15:20 -04001614
Chris Mason4a69a412008-11-06 22:03:00 -05001615/*
1616 * in order to insert checksums into the metadata in large chunks,
1617 * we wait until bio submission time. All the pages in the bio are
1618 * checksummed and sums are attached onto the ordered extent record.
1619 *
1620 * At IO completion time the cums attached on the ordered extent record
1621 * are inserted into the btree
1622 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001623static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001624 int mirror_num, unsigned long bio_flags,
1625 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001626{
1627 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001628 int ret;
1629
1630 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1631 if (ret)
1632 bio_endio(bio, ret);
1633 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001634}
1635
Chris Masond352ac62008-09-29 15:18:18 -04001636/*
Chris Masoncad321a2008-12-17 14:51:42 -05001637 * extent_io.c submission hook. This does the right thing for csum calculation
1638 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001639 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001640static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001641 int mirror_num, unsigned long bio_flags,
1642 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001643{
1644 struct btrfs_root *root = BTRFS_I(inode)->root;
1645 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001646 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001647 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001648 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001649
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001650 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001651
Liu Bo83eea1f2012-07-10 05:28:39 -06001652 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001653 metadata = 2;
1654
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001655 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001656 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1657 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001658 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001659
Chris Masond20f7042008-12-08 16:58:54 -05001660 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001661 ret = btrfs_submit_compressed_read(inode, bio,
1662 mirror_num,
1663 bio_flags);
1664 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001665 } else if (!skip_sum) {
1666 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1667 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001668 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001669 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001670 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001671 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001672 /* csum items have already been cloned */
1673 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1674 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001675 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001676 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001677 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001678 bio_flags, bio_offset,
1679 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001680 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001681 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001682 } else if (!skip_sum) {
1683 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1684 if (ret)
1685 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001686 }
1687
Chris Mason0b86a832008-03-24 15:01:56 -04001688mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001689 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1690
1691out:
1692 if (ret < 0)
1693 bio_endio(bio, ret);
1694 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001695}
Chris Mason6885f302008-02-20 16:11:05 -05001696
Chris Masond352ac62008-09-29 15:18:18 -04001697/*
1698 * given a list of ordered sums record them in the inode. This happens
1699 * at IO completion time based on sums calculated at bio submission time.
1700 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001701static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001702 struct inode *inode, u64 file_offset,
1703 struct list_head *list)
1704{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001705 struct btrfs_ordered_sum *sum;
1706
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001707 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001708 btrfs_csum_file_blocks(trans,
1709 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001710 }
1711 return 0;
1712}
1713
Josef Bacik2ac55d42010-02-03 19:33:23 +00001714int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1715 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001716{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001717 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001718 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001719 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001720}
1721
Chris Masond352ac62008-09-29 15:18:18 -04001722/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001723struct btrfs_writepage_fixup {
1724 struct page *page;
1725 struct btrfs_work work;
1726};
1727
Christoph Hellwigb2950862008-12-02 09:54:17 -05001728static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001729{
1730 struct btrfs_writepage_fixup *fixup;
1731 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001732 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001733 struct page *page;
1734 struct inode *inode;
1735 u64 page_start;
1736 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001737 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001738
1739 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1740 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001741again:
Chris Mason247e7432008-07-17 12:53:51 -04001742 lock_page(page);
1743 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1744 ClearPageChecked(page);
1745 goto out_page;
1746 }
1747
1748 inode = page->mapping->host;
1749 page_start = page_offset(page);
1750 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1751
Josef Bacik2ac55d42010-02-03 19:33:23 +00001752 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001753 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001754
1755 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001756 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001757 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001758
1759 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1760 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001761 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1762 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001763 unlock_page(page);
1764 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001765 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001766 goto again;
1767 }
Chris Mason247e7432008-07-17 12:53:51 -04001768
Jeff Mahoney87826df2012-02-15 16:23:57 +01001769 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1770 if (ret) {
1771 mapping_set_error(page->mapping, ret);
1772 end_extent_writepage(page, ret, page_start, page_end);
1773 ClearPageChecked(page);
1774 goto out;
1775 }
1776
Josef Bacik2ac55d42010-02-03 19:33:23 +00001777 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001778 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001779 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001780out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001781 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1782 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001783out_page:
1784 unlock_page(page);
1785 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001786 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001787}
1788
1789/*
1790 * There are a few paths in the higher layers of the kernel that directly
1791 * set the page dirty bit without asking the filesystem if it is a
1792 * good idea. This causes problems because we want to make sure COW
1793 * properly happens and the data=ordered rules are followed.
1794 *
Chris Masonc8b97812008-10-29 14:49:59 -04001795 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001796 * hasn't been properly setup for IO. We kick off an async process
1797 * to fix it up. The async helper will wait for ordered extents, set
1798 * the delalloc bit and make it safe to write the page.
1799 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001800static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001801{
1802 struct inode *inode = page->mapping->host;
1803 struct btrfs_writepage_fixup *fixup;
1804 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001805
Chris Mason8b62b722009-09-02 16:53:46 -04001806 /* this page is properly in the ordered list */
1807 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001808 return 0;
1809
1810 if (PageChecked(page))
1811 return -EAGAIN;
1812
1813 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1814 if (!fixup)
1815 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001816
Chris Mason247e7432008-07-17 12:53:51 -04001817 SetPageChecked(page);
1818 page_cache_get(page);
1819 fixup->work.func = btrfs_writepage_fixup_worker;
1820 fixup->page = page;
1821 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001822 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001823}
1824
Yan Zhengd899e052008-10-30 14:25:28 -04001825static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1826 struct inode *inode, u64 file_pos,
1827 u64 disk_bytenr, u64 disk_num_bytes,
1828 u64 num_bytes, u64 ram_bytes,
1829 u8 compression, u8 encryption,
1830 u16 other_encoding, int extent_type)
1831{
1832 struct btrfs_root *root = BTRFS_I(inode)->root;
1833 struct btrfs_file_extent_item *fi;
1834 struct btrfs_path *path;
1835 struct extent_buffer *leaf;
1836 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001837 int ret;
1838
1839 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001840 if (!path)
1841 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001842
Chris Masonb9473432009-03-13 11:00:37 -04001843 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001844
1845 /*
1846 * we may be replacing one extent in the tree with another.
1847 * The new extent is pinned in the extent map, and we don't want
1848 * to drop it from the cache until it is completely in the btree.
1849 *
1850 * So, tell btrfs_drop_extents to leave this extent in the cache.
1851 * the caller is expected to unpin it and allow it to be merged
1852 * with the others.
1853 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001854 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001855 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001856 if (ret)
1857 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001858
Li Zefan33345d012011-04-20 10:31:50 +08001859 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001860 ins.offset = file_pos;
1861 ins.type = BTRFS_EXTENT_DATA_KEY;
1862 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001863 if (ret)
1864 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001865 leaf = path->nodes[0];
1866 fi = btrfs_item_ptr(leaf, path->slots[0],
1867 struct btrfs_file_extent_item);
1868 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1869 btrfs_set_file_extent_type(leaf, fi, extent_type);
1870 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1871 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1872 btrfs_set_file_extent_offset(leaf, fi, 0);
1873 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1874 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1875 btrfs_set_file_extent_compression(leaf, fi, compression);
1876 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1877 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001878
Yan Zhengd899e052008-10-30 14:25:28 -04001879 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001880 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001881
1882 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001883
1884 ins.objectid = disk_bytenr;
1885 ins.offset = disk_num_bytes;
1886 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001887 ret = btrfs_alloc_reserved_file_extent(trans, root,
1888 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001889 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001890out:
Yan Zhengd899e052008-10-30 14:25:28 -04001891 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001892
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001893 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001894}
1895
Chris Mason5d13a982009-03-13 11:41:46 -04001896/*
1897 * helper function for btrfs_finish_ordered_io, this
1898 * just reads in some of the csum leaves to prime them into ram
1899 * before we start the transaction. It limits the amount of btree
1900 * reads required while inside the transaction.
1901 */
Chris Masond352ac62008-09-29 15:18:18 -04001902/* as ordered data IO finishes, this gets called so we can finish
1903 * an ordered extent if the range of bytes in the file it covers are
1904 * fully written.
1905 */
Josef Bacik5fd02042012-05-02 14:00:54 -04001906static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001907{
Josef Bacik5fd02042012-05-02 14:00:54 -04001908 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001909 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001910 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001911 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001912 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001913 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001914 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001915 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001916
Liu Bo83eea1f2012-07-10 05:28:39 -06001917 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001918
Josef Bacik5fd02042012-05-02 14:00:54 -04001919 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
1920 ret = -EIO;
1921 goto out;
1922 }
1923
Yan, Zhengc21677542009-11-12 09:34:21 +00001924 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001925 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05001926 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1927 if (nolock)
1928 trans = btrfs_join_transaction_nolock(root);
1929 else
1930 trans = btrfs_join_transaction(root);
1931 if (IS_ERR(trans)) {
1932 ret = PTR_ERR(trans);
1933 trans = NULL;
1934 goto out;
Yan, Zhengc21677542009-11-12 09:34:21 +00001935 }
Josef Bacik6c760c02012-11-09 10:53:21 -05001936 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1937 ret = btrfs_update_inode_fallback(trans, root, inode);
1938 if (ret) /* -ENOMEM or corruption */
1939 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc21677542009-11-12 09:34:21 +00001940 goto out;
1941 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001942
Josef Bacik2ac55d42010-02-03 19:33:23 +00001943 lock_extent_bits(io_tree, ordered_extent->file_offset,
1944 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001945 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001946
Josef Bacik0cb59c92010-07-02 12:14:14 -04001947 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001948 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001949 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001950 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001951 if (IS_ERR(trans)) {
1952 ret = PTR_ERR(trans);
1953 trans = NULL;
1954 goto out_unlock;
1955 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001956 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc21677542009-11-12 09:34:21 +00001957
Chris Masonc8b97812008-10-29 14:49:59 -04001958 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001959 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001960 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001961 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001962 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001963 ordered_extent->file_offset,
1964 ordered_extent->file_offset +
1965 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001966 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001967 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001968 ret = insert_reserved_file_extent(trans, inode,
1969 ordered_extent->file_offset,
1970 ordered_extent->start,
1971 ordered_extent->disk_len,
1972 ordered_extent->len,
1973 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001974 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001975 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04001976 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04001977 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1978 ordered_extent->file_offset, ordered_extent->len,
1979 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001980 if (ret < 0) {
1981 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04001982 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001983 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001984
Chris Masone6dcd2d2008-07-17 12:53:50 -04001985 add_pending_csums(trans, inode, ordered_extent->file_offset,
1986 &ordered_extent->list);
1987
Josef Bacik6c760c02012-11-09 10:53:21 -05001988 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1989 ret = btrfs_update_inode_fallback(trans, root, inode);
1990 if (ret) { /* -ENOMEM or corruption */
1991 btrfs_abort_transaction(trans, root, ret);
1992 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04001993 }
1994 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04001995out_unlock:
1996 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1997 ordered_extent->file_offset +
1998 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc21677542009-11-12 09:34:21 +00001999out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002000 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002001 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002002 if (trans)
2003 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002004
Josef Bacik5fd02042012-05-02 14:00:54 -04002005 if (ret)
2006 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2007 ordered_extent->file_offset +
2008 ordered_extent->len - 1, NULL, GFP_NOFS);
2009
2010 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002011 * This needs to be done to make sure anybody waiting knows we are done
2012 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002013 */
2014 btrfs_remove_ordered_extent(inode, ordered_extent);
2015
Chris Masone6dcd2d2008-07-17 12:53:50 -04002016 /* once for us */
2017 btrfs_put_ordered_extent(ordered_extent);
2018 /* once for the tree */
2019 btrfs_put_ordered_extent(ordered_extent);
2020
Josef Bacik5fd02042012-05-02 14:00:54 -04002021 return ret;
2022}
2023
2024static void finish_ordered_fn(struct btrfs_work *work)
2025{
2026 struct btrfs_ordered_extent *ordered_extent;
2027 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2028 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002029}
2030
Christoph Hellwigb2950862008-12-02 09:54:17 -05002031static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002032 struct extent_state *state, int uptodate)
2033{
Josef Bacik5fd02042012-05-02 14:00:54 -04002034 struct inode *inode = page->mapping->host;
2035 struct btrfs_root *root = BTRFS_I(inode)->root;
2036 struct btrfs_ordered_extent *ordered_extent = NULL;
2037 struct btrfs_workers *workers;
2038
liubo1abe9b82011-03-24 11:18:59 +00002039 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2040
Chris Mason8b62b722009-09-02 16:53:46 -04002041 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002042 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2043 end - start + 1, uptodate))
2044 return 0;
2045
2046 ordered_extent->work.func = finish_ordered_fn;
2047 ordered_extent->work.flags = 0;
2048
Liu Bo83eea1f2012-07-10 05:28:39 -06002049 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002050 workers = &root->fs_info->endio_freespace_worker;
2051 else
2052 workers = &root->fs_info->endio_write_workers;
2053 btrfs_queue_worker(workers, &ordered_extent->work);
2054
2055 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002056}
2057
Chris Masond352ac62008-09-29 15:18:18 -04002058/*
Chris Masond352ac62008-09-29 15:18:18 -04002059 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002060 * if there's a match, we allow the bio to finish. If not, the code in
2061 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002062 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002063static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002064 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002065{
Chris Mason35ebb932007-10-30 16:56:53 -04002066 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04002067 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002068 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002069 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002070 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002071 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002072 struct btrfs_root *root = BTRFS_I(inode)->root;
2073 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05002074
Chris Masond20f7042008-12-08 16:58:54 -05002075 if (PageChecked(page)) {
2076 ClearPageChecked(page);
2077 goto good;
2078 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002079
2080 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002081 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002082
Yan Zheng17d217f2008-12-12 10:03:38 -05002083 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002084 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002085 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2086 GFP_NOFS);
2087 return 0;
2088 }
2089
Yanc2e639f2008-02-04 08:57:25 -05002090 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002091 private = state->private;
2092 ret = 0;
2093 } else {
2094 ret = get_state_private(io_tree, start, &private);
2095 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002096 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002097 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002098 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002099
Chris Masonff79f812007-10-15 16:22:25 -04002100 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2101 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002102 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002103 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002104
Cong Wang7ac687d2011-11-25 23:14:28 +08002105 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002106good:
Chris Mason07157aa2007-08-30 08:50:51 -04002107 return 0;
2108
2109zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002110 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002111 "private %llu\n",
2112 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002113 (unsigned long long)start, csum,
2114 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002115 memset(kaddr + offset, 1, end - start + 1);
2116 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002117 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002118 if (private == 0)
2119 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002120 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002121}
Chris Masonb888db22007-08-27 16:49:44 -04002122
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002123struct delayed_iput {
2124 struct list_head list;
2125 struct inode *inode;
2126};
2127
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002128/* JDM: If this is fs-wide, why can't we add a pointer to
2129 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002130void btrfs_add_delayed_iput(struct inode *inode)
2131{
2132 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2133 struct delayed_iput *delayed;
2134
2135 if (atomic_add_unless(&inode->i_count, -1, 1))
2136 return;
2137
2138 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2139 delayed->inode = inode;
2140
2141 spin_lock(&fs_info->delayed_iput_lock);
2142 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2143 spin_unlock(&fs_info->delayed_iput_lock);
2144}
2145
2146void btrfs_run_delayed_iputs(struct btrfs_root *root)
2147{
2148 LIST_HEAD(list);
2149 struct btrfs_fs_info *fs_info = root->fs_info;
2150 struct delayed_iput *delayed;
2151 int empty;
2152
2153 spin_lock(&fs_info->delayed_iput_lock);
2154 empty = list_empty(&fs_info->delayed_iputs);
2155 spin_unlock(&fs_info->delayed_iput_lock);
2156 if (empty)
2157 return;
2158
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002159 spin_lock(&fs_info->delayed_iput_lock);
2160 list_splice_init(&fs_info->delayed_iputs, &list);
2161 spin_unlock(&fs_info->delayed_iput_lock);
2162
2163 while (!list_empty(&list)) {
2164 delayed = list_entry(list.next, struct delayed_iput, list);
2165 list_del(&delayed->list);
2166 iput(delayed->inode);
2167 kfree(delayed);
2168 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002169}
2170
Yan, Zhengd68fc572010-05-16 10:49:58 -04002171enum btrfs_orphan_cleanup_state {
2172 ORPHAN_CLEANUP_STARTED = 1,
2173 ORPHAN_CLEANUP_DONE = 2,
2174};
2175
2176/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002177 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002178 * files in the subvolume, it removes orphan item and frees block_rsv
2179 * structure.
2180 */
2181void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2182 struct btrfs_root *root)
2183{
Josef Bacik90290e12011-12-02 15:44:12 -05002184 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002185 int ret;
2186
Josef Bacik8a35d952012-05-23 14:26:42 -04002187 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002188 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2189 return;
2190
Josef Bacik90290e12011-12-02 15:44:12 -05002191 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002192 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002193 spin_unlock(&root->orphan_lock);
2194 return;
2195 }
2196
2197 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2198 spin_unlock(&root->orphan_lock);
2199 return;
2200 }
2201
2202 block_rsv = root->orphan_block_rsv;
2203 root->orphan_block_rsv = NULL;
2204 spin_unlock(&root->orphan_lock);
2205
Yan, Zhengd68fc572010-05-16 10:49:58 -04002206 if (root->orphan_item_inserted &&
2207 btrfs_root_refs(&root->root_item) > 0) {
2208 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2209 root->root_key.objectid);
2210 BUG_ON(ret);
2211 root->orphan_item_inserted = 0;
2212 }
2213
Josef Bacik90290e12011-12-02 15:44:12 -05002214 if (block_rsv) {
2215 WARN_ON(block_rsv->size > 0);
2216 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002217 }
2218}
2219
2220/*
Josef Bacik7b128762008-07-24 12:17:14 -04002221 * This creates an orphan entry for the given inode in case something goes
2222 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002223 *
2224 * NOTE: caller of this function should reserve 5 units of metadata for
2225 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002226 */
2227int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2228{
2229 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002230 struct btrfs_block_rsv *block_rsv = NULL;
2231 int reserve = 0;
2232 int insert = 0;
2233 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002234
Yan, Zhengd68fc572010-05-16 10:49:58 -04002235 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002236 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002237 if (!block_rsv)
2238 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002239 }
2240
Yan, Zhengd68fc572010-05-16 10:49:58 -04002241 spin_lock(&root->orphan_lock);
2242 if (!root->orphan_block_rsv) {
2243 root->orphan_block_rsv = block_rsv;
2244 } else if (block_rsv) {
2245 btrfs_free_block_rsv(root, block_rsv);
2246 block_rsv = NULL;
2247 }
Josef Bacik7b128762008-07-24 12:17:14 -04002248
Josef Bacik8a35d952012-05-23 14:26:42 -04002249 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2250 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002251#if 0
2252 /*
2253 * For proper ENOSPC handling, we should do orphan
2254 * cleanup when mounting. But this introduces backward
2255 * compatibility issue.
2256 */
2257 if (!xchg(&root->orphan_item_inserted, 1))
2258 insert = 2;
2259 else
2260 insert = 1;
2261#endif
2262 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002263 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002264 }
Josef Bacik7b128762008-07-24 12:17:14 -04002265
Josef Bacik72ac3c02012-05-23 14:13:11 -04002266 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2267 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002268 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002269 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002270
Yan, Zhengd68fc572010-05-16 10:49:58 -04002271 /* grab metadata reservation from transaction handle */
2272 if (reserve) {
2273 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002274 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002275 }
2276
2277 /* insert an orphan item to track this unlinked/truncated file */
2278 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002279 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002280 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04002281 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2282 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002283 btrfs_abort_transaction(trans, root, ret);
2284 return ret;
2285 }
2286 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002287 }
2288
2289 /* insert an orphan item to track subvolume contains orphan files */
2290 if (insert >= 2) {
2291 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2292 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002293 if (ret && ret != -EEXIST) {
2294 btrfs_abort_transaction(trans, root, ret);
2295 return ret;
2296 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002297 }
2298 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002299}
2300
2301/*
2302 * We have done the truncate/delete so we can go ahead and remove the orphan
2303 * item for this particular inode.
2304 */
2305int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2306{
2307 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002308 int delete_item = 0;
2309 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002310 int ret = 0;
2311
Yan, Zhengd68fc572010-05-16 10:49:58 -04002312 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002313 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2314 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002315 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002316
Josef Bacik72ac3c02012-05-23 14:13:11 -04002317 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2318 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002319 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002320 spin_unlock(&root->orphan_lock);
2321
2322 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002323 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002324 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04002325 }
2326
Josef Bacik8a35d952012-05-23 14:26:42 -04002327 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002328 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04002329 atomic_dec(&root->orphan_inodes);
2330 }
Josef Bacik7b128762008-07-24 12:17:14 -04002331
Yan, Zhengd68fc572010-05-16 10:49:58 -04002332 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002333}
2334
2335/*
2336 * this cleans up any orphans that may be left on the list from the last use
2337 * of this root.
2338 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002339int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002340{
2341 struct btrfs_path *path;
2342 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002343 struct btrfs_key key, found_key;
2344 struct btrfs_trans_handle *trans;
2345 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002346 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002347 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2348
Yan, Zhengd68fc572010-05-16 10:49:58 -04002349 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002350 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002351
2352 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002353 if (!path) {
2354 ret = -ENOMEM;
2355 goto out;
2356 }
Josef Bacik7b128762008-07-24 12:17:14 -04002357 path->reada = -1;
2358
2359 key.objectid = BTRFS_ORPHAN_OBJECTID;
2360 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2361 key.offset = (u64)-1;
2362
Josef Bacik7b128762008-07-24 12:17:14 -04002363 while (1) {
2364 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002365 if (ret < 0)
2366 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002367
2368 /*
2369 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002370 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002371 * find the key and see if we have stuff that matches
2372 */
2373 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002374 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002375 if (path->slots[0] == 0)
2376 break;
2377 path->slots[0]--;
2378 }
2379
2380 /* pull out the item */
2381 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002382 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2383
2384 /* make sure the item matches what we want */
2385 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2386 break;
2387 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2388 break;
2389
2390 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002391 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002392
2393 /*
2394 * this is where we are basically btrfs_lookup, without the
2395 * crossing root thing. we store the inode number in the
2396 * offset of the orphan item.
2397 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002398
2399 if (found_key.offset == last_objectid) {
2400 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2401 "stopping orphan cleanup\n");
2402 ret = -EINVAL;
2403 goto out;
2404 }
2405
2406 last_objectid = found_key.offset;
2407
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002408 found_key.objectid = found_key.offset;
2409 found_key.type = BTRFS_INODE_ITEM_KEY;
2410 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002411 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04002412 ret = PTR_RET(inode);
2413 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002414 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002415
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002416 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2417 struct btrfs_root *dead_root;
2418 struct btrfs_fs_info *fs_info = root->fs_info;
2419 int is_dead_root = 0;
2420
2421 /*
2422 * this is an orphan in the tree root. Currently these
2423 * could come from 2 sources:
2424 * a) a snapshot deletion in progress
2425 * b) a free space cache inode
2426 * We need to distinguish those two, as the snapshot
2427 * orphan must not get deleted.
2428 * find_dead_roots already ran before us, so if this
2429 * is a snapshot deletion, we should find the root
2430 * in the dead_roots list
2431 */
2432 spin_lock(&fs_info->trans_lock);
2433 list_for_each_entry(dead_root, &fs_info->dead_roots,
2434 root_list) {
2435 if (dead_root->root_key.objectid ==
2436 found_key.objectid) {
2437 is_dead_root = 1;
2438 break;
2439 }
2440 }
2441 spin_unlock(&fs_info->trans_lock);
2442 if (is_dead_root) {
2443 /* prevent this orphan from being found again */
2444 key.offset = found_key.objectid - 1;
2445 continue;
2446 }
2447 }
Josef Bacika8c9e572011-09-21 16:55:59 -04002448 /*
2449 * Inode is already gone but the orphan item is still there,
2450 * kill the orphan item.
2451 */
2452 if (ret == -ESTALE) {
2453 trans = btrfs_start_transaction(root, 1);
2454 if (IS_ERR(trans)) {
2455 ret = PTR_ERR(trans);
2456 goto out;
2457 }
Josef Bacik8a35d952012-05-23 14:26:42 -04002458 printk(KERN_ERR "auto deleting %Lu\n",
2459 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04002460 ret = btrfs_del_orphan_item(trans, root,
2461 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002462 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04002463 btrfs_end_transaction(trans, root);
2464 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002465 }
Josef Bacik7b128762008-07-24 12:17:14 -04002466
Josef Bacik7b128762008-07-24 12:17:14 -04002467 /*
2468 * add this inode to the orphan list so btrfs_orphan_del does
2469 * the proper thing when we hit it
2470 */
Josef Bacik8a35d952012-05-23 14:26:42 -04002471 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2472 &BTRFS_I(inode)->runtime_flags);
Josef Bacik7b128762008-07-24 12:17:14 -04002473
Josef Bacik7b128762008-07-24 12:17:14 -04002474 /* if we have links, this was a truncate, lets do that */
2475 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002476 if (!S_ISREG(inode->i_mode)) {
2477 WARN_ON(1);
2478 iput(inode);
2479 continue;
2480 }
Josef Bacik7b128762008-07-24 12:17:14 -04002481 nr_truncate++;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002482 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002483 } else {
2484 nr_unlink++;
2485 }
2486
2487 /* this will do delete_inode and everything for us */
2488 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002489 if (ret)
2490 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002491 }
Miao Xie3254c872011-11-10 20:45:05 -05002492 /* release the path since we're done with it */
2493 btrfs_release_path(path);
2494
Yan, Zhengd68fc572010-05-16 10:49:58 -04002495 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2496
2497 if (root->orphan_block_rsv)
2498 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2499 (u64)-1);
2500
2501 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa402011-04-13 12:54:33 -04002502 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002503 if (!IS_ERR(trans))
2504 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002505 }
Josef Bacik7b128762008-07-24 12:17:14 -04002506
2507 if (nr_unlink)
2508 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2509 if (nr_truncate)
2510 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002511
2512out:
2513 if (ret)
2514 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2515 btrfs_free_path(path);
2516 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002517}
2518
Chris Masond352ac62008-09-29 15:18:18 -04002519/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002520 * very simple check to peek ahead in the leaf looking for xattrs. If we
2521 * don't find any xattrs, we know there can't be any acls.
2522 *
2523 * slot is the slot the inode is in, objectid is the objectid of the inode
2524 */
2525static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2526 int slot, u64 objectid)
2527{
2528 u32 nritems = btrfs_header_nritems(leaf);
2529 struct btrfs_key found_key;
2530 int scanned = 0;
2531
2532 slot++;
2533 while (slot < nritems) {
2534 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2535
2536 /* we found a different objectid, there must not be acls */
2537 if (found_key.objectid != objectid)
2538 return 0;
2539
2540 /* we found an xattr, assume we've got an acl */
2541 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2542 return 1;
2543
2544 /*
2545 * we found a key greater than an xattr key, there can't
2546 * be any acls later on
2547 */
2548 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2549 return 0;
2550
2551 slot++;
2552 scanned++;
2553
2554 /*
2555 * it goes inode, inode backrefs, xattrs, extents,
2556 * so if there are a ton of hard links to an inode there can
2557 * be a lot of backrefs. Don't waste time searching too hard,
2558 * this is just an optimization
2559 */
2560 if (scanned >= 8)
2561 break;
2562 }
2563 /* we hit the end of the leaf before we found an xattr or
2564 * something larger than an xattr. We have to assume the inode
2565 * has acls
2566 */
2567 return 1;
2568}
2569
2570/*
Chris Masond352ac62008-09-29 15:18:18 -04002571 * read an inode from the btree into the in-memory inode
2572 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002573static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002574{
2575 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002576 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002577 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002578 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002579 struct btrfs_root *root = BTRFS_I(inode)->root;
2580 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002581 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002582 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002583 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00002584 bool filled = false;
2585
2586 ret = btrfs_fill_inode(inode, &rdev);
2587 if (!ret)
2588 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04002589
2590 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07002591 if (!path)
2592 goto make_bad;
2593
Josef Bacikd90c7322011-05-17 09:50:54 -04002594 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002595 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002596
Chris Mason39279cc2007-06-12 06:35:45 -04002597 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002598 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002599 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002600
Chris Mason5f39d392007-10-15 16:14:19 -04002601 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00002602
2603 if (filled)
2604 goto cache_acl;
2605
Chris Mason5f39d392007-10-15 16:14:19 -04002606 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2607 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002608 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002609 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08002610 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
2611 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04002612 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002613
2614 tspec = btrfs_inode_atime(inode_item);
2615 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2616 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2617
2618 tspec = btrfs_inode_mtime(inode_item);
2619 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2620 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2621
2622 tspec = btrfs_inode_ctime(inode_item);
2623 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2624 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2625
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002626 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002627 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04002628 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
2629
2630 /*
2631 * If we were modified in the current generation and evicted from memory
2632 * and then re-read we need to do a full sync since we don't have any
2633 * idea about which extents were modified before we were evicted from
2634 * cache.
2635 */
2636 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
2637 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2638 &BTRFS_I(inode)->runtime_flags);
2639
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002640 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002641 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002642 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002643 rdev = btrfs_inode_rdev(leaf, inode_item);
2644
Josef Bacikaec74772008-07-24 12:12:38 -04002645 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002646 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00002647cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04002648 /*
2649 * try to precache a NULL acl entry for files that don't have
2650 * any xattrs or acls
2651 */
Li Zefan33345d012011-04-20 10:31:50 +08002652 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2653 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002654 if (!maybe_acls)
2655 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002656
Chris Mason39279cc2007-06-12 06:35:45 -04002657 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002658
Chris Mason39279cc2007-06-12 06:35:45 -04002659 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002660 case S_IFREG:
2661 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002662 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002663 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002664 inode->i_fop = &btrfs_file_operations;
2665 inode->i_op = &btrfs_file_inode_operations;
2666 break;
2667 case S_IFDIR:
2668 inode->i_fop = &btrfs_dir_file_operations;
2669 if (root == root->fs_info->tree_root)
2670 inode->i_op = &btrfs_dir_ro_inode_operations;
2671 else
2672 inode->i_op = &btrfs_dir_inode_operations;
2673 break;
2674 case S_IFLNK:
2675 inode->i_op = &btrfs_symlink_inode_operations;
2676 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002677 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002678 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002679 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002680 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002681 init_special_inode(inode, inode->i_mode, rdev);
2682 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002683 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002684
2685 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002686 return;
2687
2688make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002689 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002690 make_bad_inode(inode);
2691}
2692
Chris Masond352ac62008-09-29 15:18:18 -04002693/*
2694 * given a leaf and an inode, copy the inode fields into the leaf
2695 */
Chris Masone02119d2008-09-05 16:13:11 -04002696static void fill_inode_item(struct btrfs_trans_handle *trans,
2697 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002698 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002699 struct inode *inode)
2700{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08002701 btrfs_set_inode_uid(leaf, item, i_uid_read(inode));
2702 btrfs_set_inode_gid(leaf, item, i_gid_read(inode));
Chris Masondbe674a2008-07-17 12:54:05 -04002703 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002704 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2705 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2706
2707 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2708 inode->i_atime.tv_sec);
2709 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2710 inode->i_atime.tv_nsec);
2711
2712 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2713 inode->i_mtime.tv_sec);
2714 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2715 inode->i_mtime.tv_nsec);
2716
2717 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2718 inode->i_ctime.tv_sec);
2719 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2720 inode->i_ctime.tv_nsec);
2721
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002722 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002723 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002724 btrfs_set_inode_sequence(leaf, item, inode->i_version);
Chris Masone02119d2008-09-05 16:13:11 -04002725 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002726 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002727 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Josef Bacikd82a6f12011-05-11 15:26:06 -04002728 btrfs_set_inode_block_group(leaf, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002729}
2730
Chris Masond352ac62008-09-29 15:18:18 -04002731/*
2732 * copy everything in the in-memory inode into the btree.
2733 */
Chris Mason21151332011-11-10 20:39:08 -05002734static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05002735 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002736{
2737 struct btrfs_inode_item *inode_item;
2738 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002739 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002740 int ret;
2741
2742 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002743 if (!path)
2744 return -ENOMEM;
2745
Chris Masonb9473432009-03-13 11:00:37 -04002746 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002747 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2748 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002749 if (ret) {
2750 if (ret > 0)
2751 ret = -ENOENT;
2752 goto failed;
2753 }
2754
Chris Masonb4ce94d2009-02-04 09:25:08 -05002755 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002756 leaf = path->nodes[0];
2757 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002758 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002759
Chris Masone02119d2008-09-05 16:13:11 -04002760 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002761 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002762 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002763 ret = 0;
2764failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002765 btrfs_free_path(path);
2766 return ret;
2767}
2768
Chris Masond352ac62008-09-29 15:18:18 -04002769/*
Chris Mason21151332011-11-10 20:39:08 -05002770 * copy everything in the in-memory inode into the btree.
2771 */
2772noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2773 struct btrfs_root *root, struct inode *inode)
2774{
2775 int ret;
2776
2777 /*
2778 * If the inode is a free space inode, we can deadlock during commit
2779 * if we put it into the delayed code.
2780 *
2781 * The data relocation inode should also be directly updated
2782 * without delay
2783 */
Liu Bo83eea1f2012-07-10 05:28:39 -06002784 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05002785 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02002786 btrfs_update_root_times(trans, root);
2787
Chris Mason21151332011-11-10 20:39:08 -05002788 ret = btrfs_delayed_update_inode(trans, root, inode);
2789 if (!ret)
2790 btrfs_set_inode_last_trans(trans, inode);
2791 return ret;
2792 }
2793
2794 return btrfs_update_inode_item(trans, root, inode);
2795}
2796
Josef Bacikbe6aef62012-10-22 15:43:12 -04002797noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2798 struct btrfs_root *root,
2799 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05002800{
2801 int ret;
2802
2803 ret = btrfs_update_inode(trans, root, inode);
2804 if (ret == -ENOSPC)
2805 return btrfs_update_inode_item(trans, root, inode);
2806 return ret;
2807}
2808
2809/*
Chris Masond352ac62008-09-29 15:18:18 -04002810 * unlink helper that gets used here in inode.c and in the tree logging
2811 * recovery code. It remove a link in a directory with a given name, and
2812 * also drops the back refs in the inode to the directory
2813 */
Al Viro92986792011-03-04 17:14:37 +00002814static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2815 struct btrfs_root *root,
2816 struct inode *dir, struct inode *inode,
2817 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002818{
2819 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002820 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002821 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002822 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002823 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002824 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002825 u64 ino = btrfs_ino(inode);
2826 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002827
2828 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002829 if (!path) {
2830 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002831 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002832 }
2833
Chris Masonb9473432009-03-13 11:00:37 -04002834 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002835 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002836 name, name_len, -1);
2837 if (IS_ERR(di)) {
2838 ret = PTR_ERR(di);
2839 goto err;
2840 }
2841 if (!di) {
2842 ret = -ENOENT;
2843 goto err;
2844 }
Chris Mason5f39d392007-10-15 16:14:19 -04002845 leaf = path->nodes[0];
2846 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002847 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002848 if (ret)
2849 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02002850 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002851
Li Zefan33345d012011-04-20 10:31:50 +08002852 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2853 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002854 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002855 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002856 "inode %llu parent %llu\n", name_len, name,
2857 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002858 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002859 goto err;
2860 }
2861
Miao Xie16cdcec2011-04-22 18:12:22 +08002862 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002863 if (ret) {
2864 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002865 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002866 }
Chris Mason39279cc2007-06-12 06:35:45 -04002867
Chris Masone02119d2008-09-05 16:13:11 -04002868 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002869 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002870 if (ret != 0 && ret != -ENOENT) {
2871 btrfs_abort_transaction(trans, root, ret);
2872 goto err;
2873 }
Chris Masone02119d2008-09-05 16:13:11 -04002874
2875 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2876 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002877 if (ret == -ENOENT)
2878 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002879err:
2880 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002881 if (ret)
2882 goto out;
2883
2884 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002885 inode_inc_iversion(inode);
2886 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002887 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06002888 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002889out:
Chris Mason39279cc2007-06-12 06:35:45 -04002890 return ret;
2891}
2892
Al Viro92986792011-03-04 17:14:37 +00002893int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2894 struct btrfs_root *root,
2895 struct inode *dir, struct inode *inode,
2896 const char *name, int name_len)
2897{
2898 int ret;
2899 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2900 if (!ret) {
2901 btrfs_drop_nlink(inode);
2902 ret = btrfs_update_inode(trans, root, inode);
2903 }
2904 return ret;
2905}
2906
2907
Yan, Zhenga22285a2010-05-16 10:48:46 -04002908/* helper to check if there is any shared block in the path */
2909static int check_path_shared(struct btrfs_root *root,
2910 struct btrfs_path *path)
2911{
2912 struct extent_buffer *eb;
2913 int level;
Dan Carpenter0e4dcbe2010-06-01 08:23:11 +00002914 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002915
2916 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002917 int ret;
2918
Yan, Zhenga22285a2010-05-16 10:48:46 -04002919 if (!path->nodes[level])
2920 break;
2921 eb = path->nodes[level];
2922 if (!btrfs_block_can_be_shared(root, eb))
2923 continue;
2924 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2925 &refs, NULL);
2926 if (refs > 1)
2927 return 1;
2928 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002929 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002930}
2931
2932/*
2933 * helper to start transaction for unlink and rmdir.
2934 *
2935 * unlink and rmdir are special in btrfs, they do not always free space.
2936 * so in enospc case, we should make sure they will free space before
2937 * allowing them to use the global metadata reservation.
2938 */
2939static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2940 struct dentry *dentry)
2941{
2942 struct btrfs_trans_handle *trans;
2943 struct btrfs_root *root = BTRFS_I(dir)->root;
2944 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002945 struct btrfs_dir_item *di;
2946 struct inode *inode = dentry->d_inode;
2947 u64 index;
2948 int check_link = 1;
2949 int err = -ENOSPC;
2950 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002951 u64 ino = btrfs_ino(inode);
2952 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002953
Josef Bacike70bea52011-10-11 14:18:24 -04002954 /*
2955 * 1 for the possible orphan item
2956 * 1 for the dir item
2957 * 1 for the dir index
2958 * 1 for the inode ref
2959 * 1 for the inode ref in the tree log
2960 * 2 for the dir entries in the log
2961 * 1 for the inode
2962 */
2963 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002964 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2965 return trans;
2966
Li Zefan33345d012011-04-20 10:31:50 +08002967 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04002968 return ERR_PTR(-ENOSPC);
2969
2970 /* check if there is someone else holds reference */
2971 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2972 return ERR_PTR(-ENOSPC);
2973
2974 if (atomic_read(&inode->i_count) > 2)
2975 return ERR_PTR(-ENOSPC);
2976
2977 if (xchg(&root->fs_info->enospc_unlink, 1))
2978 return ERR_PTR(-ENOSPC);
2979
2980 path = btrfs_alloc_path();
2981 if (!path) {
2982 root->fs_info->enospc_unlink = 0;
2983 return ERR_PTR(-ENOMEM);
2984 }
2985
Josef Bacik3880a1b2011-10-14 14:46:51 -04002986 /* 1 for the orphan item */
2987 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002988 if (IS_ERR(trans)) {
2989 btrfs_free_path(path);
2990 root->fs_info->enospc_unlink = 0;
2991 return trans;
2992 }
2993
2994 path->skip_locking = 1;
2995 path->search_commit_root = 1;
2996
2997 ret = btrfs_lookup_inode(trans, root, path,
2998 &BTRFS_I(dir)->location, 0);
2999 if (ret < 0) {
3000 err = ret;
3001 goto out;
3002 }
3003 if (ret == 0) {
3004 if (check_path_shared(root, path))
3005 goto out;
3006 } else {
3007 check_link = 0;
3008 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003009 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003010
3011 ret = btrfs_lookup_inode(trans, root, path,
3012 &BTRFS_I(inode)->location, 0);
3013 if (ret < 0) {
3014 err = ret;
3015 goto out;
3016 }
3017 if (ret == 0) {
3018 if (check_path_shared(root, path))
3019 goto out;
3020 } else {
3021 check_link = 0;
3022 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003023 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003024
3025 if (ret == 0 && S_ISREG(inode->i_mode)) {
3026 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003027 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003028 if (ret < 0) {
3029 err = ret;
3030 goto out;
3031 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003032 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003033 if (check_path_shared(root, path))
3034 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003035 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003036 }
3037
3038 if (!check_link) {
3039 err = 0;
3040 goto out;
3041 }
3042
Li Zefan33345d012011-04-20 10:31:50 +08003043 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003044 dentry->d_name.name, dentry->d_name.len, 0);
3045 if (IS_ERR(di)) {
3046 err = PTR_ERR(di);
3047 goto out;
3048 }
3049 if (di) {
3050 if (check_path_shared(root, path))
3051 goto out;
3052 } else {
3053 err = 0;
3054 goto out;
3055 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003056 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003057
Mark Fashehf1863732012-08-08 11:32:27 -07003058 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3059 dentry->d_name.len, ino, dir_ino, 0,
3060 &index);
3061 if (ret) {
3062 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003063 goto out;
3064 }
Mark Fashehf1863732012-08-08 11:32:27 -07003065
Yan, Zhenga22285a2010-05-16 10:48:46 -04003066 if (check_path_shared(root, path))
3067 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003068
David Sterbab3b4aa72011-04-21 01:20:15 +02003069 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003070
Miao Xie16cdcec2011-04-22 18:12:22 +08003071 /*
3072 * This is a commit root search, if we can lookup inode item and other
3073 * relative items in the commit root, it means the transaction of
3074 * dir/file creation has been committed, and the dir index item that we
3075 * delay to insert has also been inserted into the commit root. So
3076 * we needn't worry about the delayed insertion of the dir index item
3077 * here.
3078 */
Li Zefan33345d012011-04-20 10:31:50 +08003079 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003080 dentry->d_name.name, dentry->d_name.len, 0);
3081 if (IS_ERR(di)) {
3082 err = PTR_ERR(di);
3083 goto out;
3084 }
3085 BUG_ON(ret == -ENOENT);
3086 if (check_path_shared(root, path))
3087 goto out;
3088
3089 err = 0;
3090out:
3091 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003092 /* Migrate the orphan reservation over */
3093 if (!err)
3094 err = btrfs_block_rsv_migrate(trans->block_rsv,
3095 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003096 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003097
Yan, Zhenga22285a2010-05-16 10:48:46 -04003098 if (err) {
3099 btrfs_end_transaction(trans, root);
3100 root->fs_info->enospc_unlink = 0;
3101 return ERR_PTR(err);
3102 }
3103
3104 trans->block_rsv = &root->fs_info->global_block_rsv;
3105 return trans;
3106}
3107
3108static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3109 struct btrfs_root *root)
3110{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003111 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003112 btrfs_block_rsv_release(root, trans->block_rsv,
3113 trans->bytes_reserved);
3114 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003115 BUG_ON(!root->fs_info->enospc_unlink);
3116 root->fs_info->enospc_unlink = 0;
3117 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003118 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003119}
3120
Chris Mason39279cc2007-06-12 06:35:45 -04003121static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3122{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003123 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003124 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003125 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003126 int ret;
3127
Yan, Zhenga22285a2010-05-16 10:48:46 -04003128 trans = __unlink_start_trans(dir, dentry);
3129 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003130 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003131
Chris Mason12fcfd22009-03-24 10:24:20 -04003132 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3133
Chris Masone02119d2008-09-05 16:13:11 -04003134 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3135 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003136 if (ret)
3137 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003138
Yan, Zhenga22285a2010-05-16 10:48:46 -04003139 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003140 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003141 if (ret)
3142 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003143 }
Josef Bacik7b128762008-07-24 12:17:14 -04003144
Tsutomu Itohb5324022011-07-19 07:27:20 +00003145out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003146 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003147 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003148 return ret;
3149}
3150
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003151int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3152 struct btrfs_root *root,
3153 struct inode *dir, u64 objectid,
3154 const char *name, int name_len)
3155{
3156 struct btrfs_path *path;
3157 struct extent_buffer *leaf;
3158 struct btrfs_dir_item *di;
3159 struct btrfs_key key;
3160 u64 index;
3161 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003162 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003163
3164 path = btrfs_alloc_path();
3165 if (!path)
3166 return -ENOMEM;
3167
Li Zefan33345d012011-04-20 10:31:50 +08003168 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003169 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003170 if (IS_ERR_OR_NULL(di)) {
3171 if (!di)
3172 ret = -ENOENT;
3173 else
3174 ret = PTR_ERR(di);
3175 goto out;
3176 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003177
3178 leaf = path->nodes[0];
3179 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3180 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3181 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003182 if (ret) {
3183 btrfs_abort_transaction(trans, root, ret);
3184 goto out;
3185 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003186 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003187
3188 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3189 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003190 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003191 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003192 if (ret != -ENOENT) {
3193 btrfs_abort_transaction(trans, root, ret);
3194 goto out;
3195 }
Li Zefan33345d012011-04-20 10:31:50 +08003196 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003197 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003198 if (IS_ERR_OR_NULL(di)) {
3199 if (!di)
3200 ret = -ENOENT;
3201 else
3202 ret = PTR_ERR(di);
3203 btrfs_abort_transaction(trans, root, ret);
3204 goto out;
3205 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003206
3207 leaf = path->nodes[0];
3208 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003209 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003210 index = key.offset;
3211 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003212 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003213
Miao Xie16cdcec2011-04-22 18:12:22 +08003214 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003215 if (ret) {
3216 btrfs_abort_transaction(trans, root, ret);
3217 goto out;
3218 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003219
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003220 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003221 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003222 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003223 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003224 if (ret)
3225 btrfs_abort_transaction(trans, root, ret);
3226out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003227 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003228 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003229}
3230
Chris Mason39279cc2007-06-12 06:35:45 -04003231static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3232{
3233 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003234 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003235 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003236 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003237
David Sterbab3ae2442012-09-13 16:04:34 -06003238 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003239 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003240 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3241 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003242
Yan, Zhenga22285a2010-05-16 10:48:46 -04003243 trans = __unlink_start_trans(dir, dentry);
3244 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003245 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003246
Li Zefan33345d012011-04-20 10:31:50 +08003247 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003248 err = btrfs_unlink_subvol(trans, root, dir,
3249 BTRFS_I(inode)->location.objectid,
3250 dentry->d_name.name,
3251 dentry->d_name.len);
3252 goto out;
3253 }
3254
Josef Bacik7b128762008-07-24 12:17:14 -04003255 err = btrfs_orphan_add(trans, inode);
3256 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003257 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003258
Chris Mason39279cc2007-06-12 06:35:45 -04003259 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003260 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3261 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003262 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003263 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003264out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003265 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003266 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003267
Chris Mason39279cc2007-06-12 06:35:45 -04003268 return err;
3269}
3270
Chris Mason323ac952008-10-01 19:05:46 -04003271/*
Chris Mason39279cc2007-06-12 06:35:45 -04003272 * this can truncate away extent items, csum items and directory items.
3273 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003274 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003275 *
3276 * csum items that cross the new i_size are truncated to the new size
3277 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003278 *
3279 * min_type is the minimum key type to truncate down to. If set to 0, this
3280 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003281 */
Yan, Zheng80825102009-11-12 09:35:36 +00003282int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3283 struct btrfs_root *root,
3284 struct inode *inode,
3285 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003286{
Chris Mason39279cc2007-06-12 06:35:45 -04003287 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003288 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003289 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003290 struct btrfs_key key;
3291 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003292 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003293 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003294 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003295 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003296 u64 mask = root->sectorsize - 1;
3297 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003298 int found_extent;
3299 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003300 int pending_del_nr = 0;
3301 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003302 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003303 int ret;
3304 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003305 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003306
3307 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003308
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003309 path = btrfs_alloc_path();
3310 if (!path)
3311 return -ENOMEM;
3312 path->reada = -1;
3313
Josef Bacik5dc562c2012-08-17 13:14:17 -04003314 /*
3315 * We want to drop from the next block forward in case this new size is
3316 * not block aligned since we will be keeping the last block of the
3317 * extent just the way it is.
3318 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003319 if (root->ref_cows || root == root->fs_info->tree_root)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003320 btrfs_drop_extent_cache(inode, (new_size + mask) & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003321
Miao Xie16cdcec2011-04-22 18:12:22 +08003322 /*
3323 * This function is also used to drop the items in the log tree before
3324 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3325 * it is used to drop the loged items. So we shouldn't kill the delayed
3326 * items.
3327 */
3328 if (min_type == 0 && root == BTRFS_I(inode)->root)
3329 btrfs_kill_delayed_inode_items(inode);
3330
Li Zefan33345d012011-04-20 10:31:50 +08003331 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003332 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003333 key.type = (u8)-1;
3334
Chris Mason85e21ba2008-01-29 15:11:36 -05003335search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003336 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003337 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003338 if (ret < 0) {
3339 err = ret;
3340 goto out;
3341 }
Chris Masond3977122009-01-05 21:25:51 -05003342
Chris Mason85e21ba2008-01-29 15:11:36 -05003343 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003344 /* there are no items in the tree for us to truncate, we're
3345 * done
3346 */
Yan, Zheng80825102009-11-12 09:35:36 +00003347 if (path->slots[0] == 0)
3348 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003349 path->slots[0]--;
3350 }
3351
Chris Masond3977122009-01-05 21:25:51 -05003352 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003353 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003354 leaf = path->nodes[0];
3355 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3356 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003357
Li Zefan33345d012011-04-20 10:31:50 +08003358 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003359 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003360
Chris Mason85e21ba2008-01-29 15:11:36 -05003361 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003362 break;
3363
Chris Mason5f39d392007-10-15 16:14:19 -04003364 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003365 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003366 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003367 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003368 extent_type = btrfs_file_extent_type(leaf, fi);
3369 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003370 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003371 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003372 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003373 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003374 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003375 }
Yan008630c2007-11-07 13:31:09 -05003376 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003377 }
Yan, Zheng80825102009-11-12 09:35:36 +00003378 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003379 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003380 } else {
3381 if (item_end < new_size)
3382 break;
3383 if (found_key.offset >= new_size)
3384 del_item = 1;
3385 else
3386 del_item = 0;
3387 }
Chris Mason39279cc2007-06-12 06:35:45 -04003388 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003389 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003390 if (found_type != BTRFS_EXTENT_DATA_KEY)
3391 goto delete;
3392
3393 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003394 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003395 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003396 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003397 u64 orig_num_bytes =
3398 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003399 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003400 found_key.offset + root->sectorsize - 1;
Yanb1632b102008-01-30 11:54:04 -05003401 extent_num_bytes = extent_num_bytes &
3402 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003403 btrfs_set_file_extent_num_bytes(leaf, fi,
3404 extent_num_bytes);
3405 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003406 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003407 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003408 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003409 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003410 } else {
Chris Masondb945352007-10-15 16:15:53 -04003411 extent_num_bytes =
3412 btrfs_file_extent_disk_num_bytes(leaf,
3413 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003414 extent_offset = found_key.offset -
3415 btrfs_file_extent_offset(leaf, fi);
3416
Chris Mason39279cc2007-06-12 06:35:45 -04003417 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003418 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003419 if (extent_start != 0) {
3420 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003421 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003422 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003423 }
3424 }
Chris Mason90692182008-02-08 13:49:28 -05003425 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003426 /*
3427 * we can't truncate inline items that have had
3428 * special encodings
3429 */
3430 if (!del_item &&
3431 btrfs_file_extent_compression(leaf, fi) == 0 &&
3432 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3433 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003434 u32 size = new_size - found_key.offset;
3435
3436 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003437 inode_sub_bytes(inode, item_end + 1 -
3438 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003439 }
3440 size =
3441 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003442 btrfs_truncate_item(trans, root, path,
3443 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003444 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003445 inode_sub_bytes(inode, item_end + 1 -
3446 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003447 }
Chris Mason39279cc2007-06-12 06:35:45 -04003448 }
Chris Mason179e29e2007-11-01 11:28:41 -04003449delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003450 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003451 if (!pending_del_nr) {
3452 /* no pending yet, add ourselves */
3453 pending_del_slot = path->slots[0];
3454 pending_del_nr = 1;
3455 } else if (pending_del_nr &&
3456 path->slots[0] + 1 == pending_del_slot) {
3457 /* hop on the pending chunk */
3458 pending_del_nr++;
3459 pending_del_slot = path->slots[0];
3460 } else {
Chris Masond3977122009-01-05 21:25:51 -05003461 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003462 }
Chris Mason39279cc2007-06-12 06:35:45 -04003463 } else {
3464 break;
3465 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003466 if (found_extent && (root->ref_cows ||
3467 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003468 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003469 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003470 extent_num_bytes, 0,
3471 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003472 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003473 BUG_ON(ret);
3474 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003475
Yan, Zheng80825102009-11-12 09:35:36 +00003476 if (found_type == BTRFS_INODE_ITEM_KEY)
3477 break;
3478
3479 if (path->slots[0] == 0 ||
3480 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00003481 if (pending_del_nr) {
3482 ret = btrfs_del_items(trans, root, path,
3483 pending_del_slot,
3484 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003485 if (ret) {
3486 btrfs_abort_transaction(trans,
3487 root, ret);
3488 goto error;
3489 }
Yan, Zheng80825102009-11-12 09:35:36 +00003490 pending_del_nr = 0;
3491 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003492 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003493 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003494 } else {
3495 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003496 }
Chris Mason39279cc2007-06-12 06:35:45 -04003497 }
Yan, Zheng80825102009-11-12 09:35:36 +00003498out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003499 if (pending_del_nr) {
3500 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3501 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003502 if (ret)
3503 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003504 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003505error:
Chris Mason39279cc2007-06-12 06:35:45 -04003506 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003507 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003508}
3509
Chris Masona52d9a82007-08-27 16:49:44 -04003510/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04003511 * btrfs_truncate_page - read, zero a chunk and write a page
3512 * @inode - inode that we're zeroing
3513 * @from - the offset to start zeroing
3514 * @len - the length to zero, 0 to zero the entire range respective to the
3515 * offset
3516 * @front - zero up to the offset instead of from the offset on
3517 *
3518 * This will find the page for the "from" offset and cow the page and zero the
3519 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04003520 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04003521int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
3522 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04003523{
Josef Bacik2aaa6652012-08-29 14:27:18 -04003524 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04003525 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003526 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3527 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003528 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003529 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003530 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003531 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3532 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3533 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003534 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04003535 int ret = 0;
3536 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003537 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003538
Josef Bacik2aaa6652012-08-29 14:27:18 -04003539 if ((offset & (blocksize - 1)) == 0 &&
3540 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04003541 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003542 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003543 if (ret)
3544 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003545
Chris Mason211c17f2008-05-15 09:13:45 -04003546again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003547 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003548 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003549 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00003550 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04003551 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003552 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003553
3554 page_start = page_offset(page);
3555 page_end = page_start + PAGE_CACHE_SIZE - 1;
3556
Chris Masona52d9a82007-08-27 16:49:44 -04003557 if (!PageUptodate(page)) {
3558 ret = btrfs_readpage(NULL, page);
3559 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003560 if (page->mapping != mapping) {
3561 unlock_page(page);
3562 page_cache_release(page);
3563 goto again;
3564 }
Chris Masona52d9a82007-08-27 16:49:44 -04003565 if (!PageUptodate(page)) {
3566 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003567 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003568 }
3569 }
Chris Mason211c17f2008-05-15 09:13:45 -04003570 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003571
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003572 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003573 set_page_extent_mapped(page);
3574
3575 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3576 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003577 unlock_extent_cached(io_tree, page_start, page_end,
3578 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003579 unlock_page(page);
3580 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003581 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003582 btrfs_put_ordered_extent(ordered);
3583 goto again;
3584 }
3585
Josef Bacik2ac55d42010-02-03 19:33:23 +00003586 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06003587 EXTENT_DIRTY | EXTENT_DELALLOC |
3588 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003589 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003590
Josef Bacik2ac55d42010-02-03 19:33:23 +00003591 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3592 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003593 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003594 unlock_extent_cached(io_tree, page_start, page_end,
3595 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003596 goto out_unlock;
3597 }
3598
Chris Masone6dcd2d2008-07-17 12:53:50 -04003599 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04003600 if (!len)
3601 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003602 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04003603 if (front)
3604 memset(kaddr, 0, offset);
3605 else
3606 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003607 flush_dcache_page(page);
3608 kunmap(page);
3609 }
Chris Mason247e7432008-07-17 12:53:51 -04003610 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003611 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003612 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3613 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003614
Chris Mason89642222008-07-24 09:41:53 -04003615out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003616 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003617 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003618 unlock_page(page);
3619 page_cache_release(page);
3620out:
3621 return ret;
3622}
3623
Josef Bacik695a0d02011-03-04 15:46:53 -05003624/*
3625 * This function puts in dummy file extents for the area we're creating a hole
3626 * for. So if we are truncating this file to a larger size we need to insert
3627 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3628 * the range between oldsize and size
3629 */
Josef Bacika41ad392011-01-31 15:30:16 -05003630int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003631{
3632 struct btrfs_trans_handle *trans;
3633 struct btrfs_root *root = BTRFS_I(inode)->root;
3634 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003635 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003636 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003637 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan Zheng9036c102008-10-30 14:19:41 -04003638 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003639 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003640 u64 block_end = (size + mask) & ~mask;
3641 u64 last_byte;
3642 u64 cur_offset;
3643 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003644 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003645
3646 if (size <= hole_start)
3647 return 0;
3648
Yan Zheng9036c102008-10-30 14:19:41 -04003649 while (1) {
3650 struct btrfs_ordered_extent *ordered;
3651 btrfs_wait_ordered_range(inode, hole_start,
3652 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003653 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003654 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04003655 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3656 if (!ordered)
3657 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003658 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3659 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003660 btrfs_put_ordered_extent(ordered);
3661 }
3662
Yan Zheng9036c102008-10-30 14:19:41 -04003663 cur_offset = hole_start;
3664 while (1) {
3665 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3666 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003667 if (IS_ERR(em)) {
3668 err = PTR_ERR(em);
3669 break;
3670 }
Yan Zheng9036c102008-10-30 14:19:41 -04003671 last_byte = min(extent_map_end(em), block_end);
3672 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003673 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003674 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04003675 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003676
Miao Xie36423202011-12-14 20:12:02 -05003677 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003678 if (IS_ERR(trans)) {
3679 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003680 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003681 }
Yan, Zheng80825102009-11-12 09:35:36 +00003682
Josef Bacik5dc562c2012-08-17 13:14:17 -04003683 err = btrfs_drop_extents(trans, root, inode,
3684 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04003685 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04003686 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003687 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003688 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003689 break;
Miao Xie5b397372011-09-11 10:52:24 -04003690 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003691
Yan Zheng9036c102008-10-30 14:19:41 -04003692 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003693 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003694 0, hole_size, 0, hole_size,
3695 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04003696 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003697 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003698 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003699 break;
Miao Xie5b397372011-09-11 10:52:24 -04003700 }
Yan, Zheng80825102009-11-12 09:35:36 +00003701
Josef Bacik5dc562c2012-08-17 13:14:17 -04003702 btrfs_drop_extent_cache(inode, cur_offset,
3703 cur_offset + hole_size - 1, 0);
3704 hole_em = alloc_extent_map();
3705 if (!hole_em) {
3706 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3707 &BTRFS_I(inode)->runtime_flags);
3708 goto next;
3709 }
3710 hole_em->start = cur_offset;
3711 hole_em->len = hole_size;
3712 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003713
Josef Bacik5dc562c2012-08-17 13:14:17 -04003714 hole_em->block_start = EXTENT_MAP_HOLE;
3715 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05003716 hole_em->orig_block_len = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003717 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
3718 hole_em->compress_type = BTRFS_COMPRESS_NONE;
3719 hole_em->generation = trans->transid;
3720
3721 while (1) {
3722 write_lock(&em_tree->lock);
3723 err = add_extent_mapping(em_tree, hole_em);
3724 if (!err)
3725 list_move(&hole_em->list,
3726 &em_tree->modified_extents);
3727 write_unlock(&em_tree->lock);
3728 if (err != -EEXIST)
3729 break;
3730 btrfs_drop_extent_cache(inode, cur_offset,
3731 cur_offset +
3732 hole_size - 1, 0);
3733 }
3734 free_extent_map(hole_em);
3735next:
Miao Xie36423202011-12-14 20:12:02 -05003736 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003737 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003738 }
3739 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003740 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003741 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003742 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003743 break;
3744 }
3745
Yan, Zhenga22285a2010-05-16 10:48:46 -04003746 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003747 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3748 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003749 return err;
3750}
3751
Josef Bacika41ad392011-01-31 15:30:16 -05003752static int btrfs_setsize(struct inode *inode, loff_t newsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003753{
Miao Xief4a2f4c2011-12-14 20:12:01 -05003754 struct btrfs_root *root = BTRFS_I(inode)->root;
3755 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05003756 loff_t oldsize = i_size_read(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003757 int ret;
3758
Josef Bacika41ad392011-01-31 15:30:16 -05003759 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003760 return 0;
3761
Josef Bacika41ad392011-01-31 15:30:16 -05003762 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05003763 truncate_pagecache(inode, oldsize, newsize);
3764 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05003765 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00003766 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003767
Miao Xief4a2f4c2011-12-14 20:12:01 -05003768 trans = btrfs_start_transaction(root, 1);
3769 if (IS_ERR(trans))
3770 return PTR_ERR(trans);
3771
3772 i_size_write(inode, newsize);
3773 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3774 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003775 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05003776 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003777
Josef Bacika41ad392011-01-31 15:30:16 -05003778 /*
3779 * We're truncating a file that used to have good data down to
3780 * zero. Make sure it gets into the ordered flush list so that
3781 * any new writes get down to disk quickly.
3782 */
3783 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04003784 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
3785 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00003786
Josef Bacika41ad392011-01-31 15:30:16 -05003787 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3788 truncate_setsize(inode, newsize);
3789 ret = btrfs_truncate(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003790 }
3791
Josef Bacika41ad392011-01-31 15:30:16 -05003792 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003793}
3794
Chris Mason39279cc2007-06-12 06:35:45 -04003795static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3796{
3797 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003798 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003799 int err;
3800
Li Zefanb83cc962010-12-20 16:04:08 +08003801 if (btrfs_root_readonly(root))
3802 return -EROFS;
3803
Chris Mason39279cc2007-06-12 06:35:45 -04003804 err = inode_change_ok(inode, attr);
3805 if (err)
3806 return err;
3807
Chris Mason5a3f23d2009-03-31 13:27:11 -04003808 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Josef Bacika41ad392011-01-31 15:30:16 -05003809 err = btrfs_setsize(inode, attr->ia_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003810 if (err)
3811 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003812 }
Yan Zheng9036c102008-10-30 14:19:41 -04003813
Christoph Hellwig10257742010-06-04 11:30:02 +02003814 if (attr->ia_valid) {
3815 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003816 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05003817 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003818
Josef Bacik22c44fe2011-11-30 10:45:38 -05003819 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02003820 err = btrfs_acl_chmod(inode);
3821 }
3822
Chris Mason39279cc2007-06-12 06:35:45 -04003823 return err;
3824}
Chris Mason61295eb2008-01-14 16:24:38 -05003825
Al Virobd555972010-06-07 11:35:40 -04003826void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003827{
3828 struct btrfs_trans_handle *trans;
3829 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04003830 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04003831 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003832 int ret;
3833
liubo1abe9b82011-03-24 11:18:59 +00003834 trace_btrfs_inode_evict(inode);
3835
Chris Mason39279cc2007-06-12 06:35:45 -04003836 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003837 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06003838 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04003839 goto no_delete;
3840
Chris Mason39279cc2007-06-12 06:35:45 -04003841 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003842 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003843 goto no_delete;
3844 }
Al Virobd555972010-06-07 11:35:40 -04003845 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003846 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003847
Yan, Zhengc71bf092009-11-12 09:34:40 +00003848 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06003849 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04003850 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00003851 goto no_delete;
3852 }
3853
Yan, Zheng76dda932009-09-21 16:00:26 -04003854 if (inode->i_nlink > 0) {
3855 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3856 goto no_delete;
3857 }
3858
Miao Xie66d8f3d2012-09-06 04:02:28 -06003859 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04003860 if (!rsv) {
3861 btrfs_orphan_del(NULL, inode);
3862 goto no_delete;
3863 }
Josef Bacik4a338542011-08-29 11:01:31 -04003864 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04003865 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04003866 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04003867
Chris Masondbe674a2008-07-17 12:54:05 -04003868 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003869
Josef Bacik4289a662011-08-05 13:22:24 -04003870 /*
Miao Xie8407aa42012-09-07 01:43:32 -06003871 * This is a bit simpler than btrfs_truncate since we've already
3872 * reserved our space for our orphan item in the unlink, so we just
3873 * need to reserve some slack space in case we add bytes and update
3874 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04003875 */
Yan, Zheng80825102009-11-12 09:35:36 +00003876 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00003877 ret = btrfs_block_rsv_refill(root, rsv, min_size,
3878 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00003879
Josef Bacik726c35f2011-09-26 15:46:06 -04003880 /*
3881 * Try and steal from the global reserve since we will
3882 * likely not use this space anyway, we want to try as
3883 * hard as possible to get this to work.
3884 */
3885 if (ret)
3886 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
3887
Yan, Zhengd68fc572010-05-16 10:49:58 -04003888 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04003889 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04003890 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04003891 btrfs_orphan_del(NULL, inode);
3892 btrfs_free_block_rsv(root, rsv);
3893 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003894 }
3895
Miao Xie08e007d2012-10-16 11:33:38 +00003896 trans = btrfs_start_transaction_lflush(root, 1);
Josef Bacik4289a662011-08-05 13:22:24 -04003897 if (IS_ERR(trans)) {
3898 btrfs_orphan_del(NULL, inode);
3899 btrfs_free_block_rsv(root, rsv);
3900 goto no_delete;
3901 }
3902
3903 trans->block_rsv = rsv;
3904
Yan, Zhengd68fc572010-05-16 10:49:58 -04003905 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04003906 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00003907 break;
3908
Miao Xie8407aa42012-09-07 01:43:32 -06003909 trans->block_rsv = &root->fs_info->trans_block_rsv;
3910 ret = btrfs_update_inode(trans, root, inode);
3911 BUG_ON(ret);
3912
Yan, Zheng80825102009-11-12 09:35:36 +00003913 btrfs_end_transaction(trans, root);
3914 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00003915 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04003916 }
3917
Josef Bacik4289a662011-08-05 13:22:24 -04003918 btrfs_free_block_rsv(root, rsv);
3919
Yan, Zheng80825102009-11-12 09:35:36 +00003920 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04003921 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003922 ret = btrfs_orphan_del(trans, inode);
3923 BUG_ON(ret);
3924 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003925
Josef Bacik4289a662011-08-05 13:22:24 -04003926 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08003927 if (!(root == root->fs_info->tree_root ||
3928 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08003929 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08003930
Chris Mason54aa1f42007-06-22 14:16:25 -04003931 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003932 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003933no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02003934 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003935 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003936}
3937
3938/*
3939 * this returns the key found in the dir entry in the location pointer.
3940 * If no dir entries were found, location->objectid is 0.
3941 */
3942static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3943 struct btrfs_key *location)
3944{
3945 const char *name = dentry->d_name.name;
3946 int namelen = dentry->d_name.len;
3947 struct btrfs_dir_item *di;
3948 struct btrfs_path *path;
3949 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003950 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003951
3952 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07003953 if (!path)
3954 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05003955
Li Zefan33345d012011-04-20 10:31:50 +08003956 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04003957 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003958 if (IS_ERR(di))
3959 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003960
David Sterbac7040052011-04-19 18:00:01 +02003961 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05003962 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003963
Chris Mason5f39d392007-10-15 16:14:19 -04003964 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003965out:
Chris Mason39279cc2007-06-12 06:35:45 -04003966 btrfs_free_path(path);
3967 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003968out_err:
3969 location->objectid = 0;
3970 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003971}
3972
3973/*
3974 * when we hit a tree root in a directory, the btrfs part of the inode
3975 * needs to be changed to reflect the root directory of the tree root. This
3976 * is kind of like crossing a mount point.
3977 */
3978static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003979 struct inode *dir,
3980 struct dentry *dentry,
3981 struct btrfs_key *location,
3982 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003983{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003984 struct btrfs_path *path;
3985 struct btrfs_root *new_root;
3986 struct btrfs_root_ref *ref;
3987 struct extent_buffer *leaf;
3988 int ret;
3989 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003990
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003991 path = btrfs_alloc_path();
3992 if (!path) {
3993 err = -ENOMEM;
3994 goto out;
3995 }
Chris Mason39279cc2007-06-12 06:35:45 -04003996
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003997 err = -ENOENT;
3998 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3999 BTRFS_I(dir)->root->root_key.objectid,
4000 location->objectid);
4001 if (ret) {
4002 if (ret < 0)
4003 err = ret;
4004 goto out;
4005 }
Chris Mason39279cc2007-06-12 06:35:45 -04004006
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004007 leaf = path->nodes[0];
4008 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004009 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004010 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4011 goto out;
4012
4013 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4014 (unsigned long)(ref + 1),
4015 dentry->d_name.len);
4016 if (ret)
4017 goto out;
4018
David Sterbab3b4aa72011-04-21 01:20:15 +02004019 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004020
4021 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4022 if (IS_ERR(new_root)) {
4023 err = PTR_ERR(new_root);
4024 goto out;
4025 }
4026
4027 if (btrfs_root_refs(&new_root->root_item) == 0) {
4028 err = -ENOENT;
4029 goto out;
4030 }
4031
4032 *sub_root = new_root;
4033 location->objectid = btrfs_root_dirid(&new_root->root_item);
4034 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004035 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004036 err = 0;
4037out:
4038 btrfs_free_path(path);
4039 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004040}
4041
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004042static void inode_tree_add(struct inode *inode)
4043{
4044 struct btrfs_root *root = BTRFS_I(inode)->root;
4045 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004046 struct rb_node **p;
4047 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004048 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004049again:
4050 p = &root->inode_tree.rb_node;
4051 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004052
Al Viro1d3382c2010-10-23 15:19:20 -04004053 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004054 return;
4055
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004056 spin_lock(&root->inode_lock);
4057 while (*p) {
4058 parent = *p;
4059 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4060
Li Zefan33345d012011-04-20 10:31:50 +08004061 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004062 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004063 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004064 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004065 else {
4066 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004067 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004068 rb_erase(parent, &root->inode_tree);
4069 RB_CLEAR_NODE(parent);
4070 spin_unlock(&root->inode_lock);
4071 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004072 }
4073 }
4074 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4075 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4076 spin_unlock(&root->inode_lock);
4077}
4078
4079static void inode_tree_del(struct inode *inode)
4080{
4081 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004082 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004083
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004084 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004085 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004086 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004087 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004088 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004089 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004090 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004091
Josef Bacik0af3d002010-06-21 14:48:16 -04004092 /*
4093 * Free space cache has inodes in the tree root, but the tree root has a
4094 * root_refs of 0, so this could end up dropping the tree root as a
4095 * snapshot, so we need the extra !root->fs_info->tree_root check to
4096 * make sure we don't drop it.
4097 */
4098 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4099 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004100 synchronize_srcu(&root->fs_info->subvol_srcu);
4101 spin_lock(&root->inode_lock);
4102 empty = RB_EMPTY_ROOT(&root->inode_tree);
4103 spin_unlock(&root->inode_lock);
4104 if (empty)
4105 btrfs_add_dead_root(root);
4106 }
4107}
4108
Jeff Mahoney143bede2012-03-01 14:56:26 +01004109void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004110{
4111 struct rb_node *node;
4112 struct rb_node *prev;
4113 struct btrfs_inode *entry;
4114 struct inode *inode;
4115 u64 objectid = 0;
4116
4117 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4118
4119 spin_lock(&root->inode_lock);
4120again:
4121 node = root->inode_tree.rb_node;
4122 prev = NULL;
4123 while (node) {
4124 prev = node;
4125 entry = rb_entry(node, struct btrfs_inode, rb_node);
4126
Li Zefan33345d012011-04-20 10:31:50 +08004127 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004128 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004129 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004130 node = node->rb_right;
4131 else
4132 break;
4133 }
4134 if (!node) {
4135 while (prev) {
4136 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004137 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004138 node = prev;
4139 break;
4140 }
4141 prev = rb_next(prev);
4142 }
4143 }
4144 while (node) {
4145 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004146 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004147 inode = igrab(&entry->vfs_inode);
4148 if (inode) {
4149 spin_unlock(&root->inode_lock);
4150 if (atomic_read(&inode->i_count) > 1)
4151 d_prune_aliases(inode);
4152 /*
Al Viro45321ac2010-06-07 13:43:19 -04004153 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004154 * the inode cache when its usage count
4155 * hits zero.
4156 */
4157 iput(inode);
4158 cond_resched();
4159 spin_lock(&root->inode_lock);
4160 goto again;
4161 }
4162
4163 if (cond_resched_lock(&root->inode_lock))
4164 goto again;
4165
4166 node = rb_next(node);
4167 }
4168 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004169}
4170
Chris Masone02119d2008-09-05 16:13:11 -04004171static int btrfs_init_locked_inode(struct inode *inode, void *p)
4172{
4173 struct btrfs_iget_args *args = p;
4174 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004175 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004176 return 0;
4177}
4178
4179static int btrfs_find_actor(struct inode *inode, void *opaque)
4180{
4181 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004182 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004183 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004184}
4185
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004186static struct inode *btrfs_iget_locked(struct super_block *s,
4187 u64 objectid,
4188 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004189{
4190 struct inode *inode;
4191 struct btrfs_iget_args args;
4192 args.ino = objectid;
4193 args.root = root;
4194
4195 inode = iget5_locked(s, objectid, btrfs_find_actor,
4196 btrfs_init_locked_inode,
4197 (void *)&args);
4198 return inode;
4199}
4200
Balaji Rao1a54ef82008-07-21 02:01:04 +05304201/* Get an inode object given its location and corresponding root.
4202 * Returns in *is_new if the inode was read from disk
4203 */
4204struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004205 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304206{
4207 struct inode *inode;
4208
4209 inode = btrfs_iget_locked(s, location->objectid, root);
4210 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004211 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304212
4213 if (inode->i_state & I_NEW) {
4214 BTRFS_I(inode)->root = root;
4215 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4216 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004217 if (!is_bad_inode(inode)) {
4218 inode_tree_add(inode);
4219 unlock_new_inode(inode);
4220 if (new)
4221 *new = 1;
4222 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004223 unlock_new_inode(inode);
4224 iput(inode);
4225 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004226 }
4227 }
4228
Balaji Rao1a54ef82008-07-21 02:01:04 +05304229 return inode;
4230}
4231
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004232static struct inode *new_simple_dir(struct super_block *s,
4233 struct btrfs_key *key,
4234 struct btrfs_root *root)
4235{
4236 struct inode *inode = new_inode(s);
4237
4238 if (!inode)
4239 return ERR_PTR(-ENOMEM);
4240
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004241 BTRFS_I(inode)->root = root;
4242 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004243 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004244
4245 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004246 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004247 inode->i_fop = &simple_dir_operations;
4248 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4249 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4250
4251 return inode;
4252}
4253
Chris Mason3de45862008-11-17 21:02:50 -05004254struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004255{
Chris Masond3977122009-01-05 21:25:51 -05004256 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004257 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004258 struct btrfs_root *sub_root = root;
4259 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004260 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004261 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004262
4263 if (dentry->d_name.len > BTRFS_NAME_LEN)
4264 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004265
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004266 if (unlikely(d_need_lookup(dentry))) {
4267 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4268 kfree(dentry->d_fsdata);
4269 dentry->d_fsdata = NULL;
Josef Bacika66e7cc2011-09-18 10:34:03 -04004270 /* This thing is hashed, drop it for now */
4271 d_drop(dentry);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004272 } else {
4273 ret = btrfs_inode_by_name(dir, dentry, &location);
4274 }
Chris Mason5f39d392007-10-15 16:14:19 -04004275
Chris Mason39279cc2007-06-12 06:35:45 -04004276 if (ret < 0)
4277 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004278
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004279 if (location.objectid == 0)
4280 return NULL;
4281
4282 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004283 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004284 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004285 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004286
4287 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4288
Yan, Zheng76dda932009-09-21 16:00:26 -04004289 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004290 ret = fixup_tree_root_location(root, dir, dentry,
4291 &location, &sub_root);
4292 if (ret < 0) {
4293 if (ret != -ENOENT)
4294 inode = ERR_PTR(ret);
4295 else
4296 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4297 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004298 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004299 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004300 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4301
Julia Lawall34d19ba2011-01-24 19:55:19 +00004302 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004303 down_read(&root->fs_info->cleanup_work_sem);
4304 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004305 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004306 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004307 if (ret)
4308 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004309 }
4310
Chris Mason3de45862008-11-17 21:02:50 -05004311 return inode;
4312}
4313
Nick Pigginfe15ce42011-01-07 17:49:23 +11004314static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004315{
4316 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004317 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004318
Li Zefan848cce02012-02-21 17:04:28 +08004319 if (!inode && !IS_ROOT(dentry))
4320 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004321
Li Zefan848cce02012-02-21 17:04:28 +08004322 if (inode) {
4323 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004324 if (btrfs_root_refs(&root->root_item) == 0)
4325 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004326
4327 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4328 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004329 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004330 return 0;
4331}
4332
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004333static void btrfs_dentry_release(struct dentry *dentry)
4334{
4335 if (dentry->d_fsdata)
4336 kfree(dentry->d_fsdata);
4337}
4338
Chris Mason3de45862008-11-17 21:02:50 -05004339static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004340 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004341{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004342 struct dentry *ret;
4343
4344 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4345 if (unlikely(d_need_lookup(dentry))) {
4346 spin_lock(&dentry->d_lock);
4347 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4348 spin_unlock(&dentry->d_lock);
4349 }
4350 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004351}
4352
Miao Xie16cdcec2011-04-22 18:12:22 +08004353unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004354 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4355};
4356
David Woodhousecbdf5a22008-08-06 19:42:33 +01004357static int btrfs_real_readdir(struct file *filp, void *dirent,
4358 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004359{
Chris Mason6da6aba2007-12-18 16:15:09 -05004360 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004361 struct btrfs_root *root = BTRFS_I(inode)->root;
4362 struct btrfs_item *item;
4363 struct btrfs_dir_item *di;
4364 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004365 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004366 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004367 struct list_head ins_list;
4368 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004369 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004370 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004371 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004372 unsigned char d_type;
4373 int over = 0;
4374 u32 di_cur;
4375 u32 di_total;
4376 u32 di_len;
4377 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004378 char tmp_name[32];
4379 char *name_ptr;
4380 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004381 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004382
4383 /* FIXME, use a real flag for deciding about the key type */
4384 if (root->fs_info->tree_root == root)
4385 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004386
Chris Mason39544012007-12-12 14:38:19 -05004387 /* special case for "." */
4388 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004389 over = filldir(dirent, ".", 1,
4390 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004391 if (over)
4392 return 0;
4393 filp->f_pos = 1;
4394 }
Chris Mason39544012007-12-12 14:38:19 -05004395 /* special case for .., just use the back ref */
4396 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004397 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004398 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004399 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004400 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004401 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004402 filp->f_pos = 2;
4403 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004404 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004405 if (!path)
4406 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004407
Josef Bacik026fd312011-05-13 10:32:11 -04004408 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004409
Miao Xie16cdcec2011-04-22 18:12:22 +08004410 if (key_type == BTRFS_DIR_INDEX_KEY) {
4411 INIT_LIST_HEAD(&ins_list);
4412 INIT_LIST_HEAD(&del_list);
4413 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4414 }
4415
Chris Mason39279cc2007-06-12 06:35:45 -04004416 btrfs_set_key_type(&key, key_type);
4417 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004418 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004419
Chris Mason39279cc2007-06-12 06:35:45 -04004420 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4421 if (ret < 0)
4422 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004423
4424 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004425 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004426 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004427 if (slot >= btrfs_header_nritems(leaf)) {
4428 ret = btrfs_next_leaf(root, path);
4429 if (ret < 0)
4430 goto err;
4431 else if (ret > 0)
4432 break;
4433 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004434 }
Chris Mason3de45862008-11-17 21:02:50 -05004435
Chris Mason5f39d392007-10-15 16:14:19 -04004436 item = btrfs_item_nr(leaf, slot);
4437 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4438
4439 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004440 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004441 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004442 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004443 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004444 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004445 if (key_type == BTRFS_DIR_INDEX_KEY &&
4446 btrfs_should_delete_dir_index(&del_list,
4447 found_key.offset))
4448 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004449
4450 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004451 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004452
Chris Mason39279cc2007-06-12 06:35:45 -04004453 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4454 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004455 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004456
4457 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004458 struct btrfs_key location;
4459
Josef Bacik22a94d42011-03-16 16:47:17 -04004460 if (verify_dir_item(root, leaf, di))
4461 break;
4462
Chris Mason5f39d392007-10-15 16:14:19 -04004463 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004464 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004465 name_ptr = tmp_name;
4466 } else {
4467 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004468 if (!name_ptr) {
4469 ret = -ENOMEM;
4470 goto err;
4471 }
Chris Mason5f39d392007-10-15 16:14:19 -04004472 }
4473 read_extent_buffer(leaf, name_ptr,
4474 (unsigned long)(di + 1), name_len);
4475
4476 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4477 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004478
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004479
Chris Mason3de45862008-11-17 21:02:50 -05004480 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01004481 * skip it.
4482 *
4483 * In contrast to old kernels, we insert the snapshot's
4484 * dir item and dir index after it has been created, so
4485 * we won't find a reference to our own snapshot. We
4486 * still keep the following code for backward
4487 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05004488 */
4489 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4490 location.objectid == root->root_key.objectid) {
4491 over = 0;
4492 goto skip;
4493 }
Chris Mason5f39d392007-10-15 16:14:19 -04004494 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004495 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004496 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004497
Chris Mason3de45862008-11-17 21:02:50 -05004498skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004499 if (name_ptr != tmp_name)
4500 kfree(name_ptr);
4501
Chris Mason39279cc2007-06-12 06:35:45 -04004502 if (over)
4503 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004504 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004505 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004506 di_cur += di_len;
4507 di = (struct btrfs_dir_item *)((char *)di + di_len);
4508 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004509next:
4510 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004511 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004512
Miao Xie16cdcec2011-04-22 18:12:22 +08004513 if (key_type == BTRFS_DIR_INDEX_KEY) {
4514 if (is_curr)
4515 filp->f_pos++;
4516 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4517 &ins_list);
4518 if (ret)
4519 goto nopos;
4520 }
4521
David Woodhouse49593bf2008-08-17 17:08:36 +01004522 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004523 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004524 /*
4525 * 32-bit glibc will use getdents64, but then strtol -
4526 * so the last number we can serve is this.
4527 */
4528 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004529 else
4530 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004531nopos:
4532 ret = 0;
4533err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004534 if (key_type == BTRFS_DIR_INDEX_KEY)
4535 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004536 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004537 return ret;
4538}
4539
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004540int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004541{
4542 struct btrfs_root *root = BTRFS_I(inode)->root;
4543 struct btrfs_trans_handle *trans;
4544 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004545 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004546
Josef Bacik72ac3c02012-05-23 14:13:11 -04004547 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04004548 return 0;
4549
Liu Bo83eea1f2012-07-10 05:28:39 -06004550 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08004551 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004552
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004553 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004554 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04004555 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004556 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04004557 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004558 if (IS_ERR(trans))
4559 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06004560 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004561 }
4562 return ret;
4563}
4564
4565/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004566 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004567 * inode changes. But, it is most likely to find the inode in cache.
4568 * FIXME, needs more benchmarking...there are no reasons other than performance
4569 * to keep or drop this code.
4570 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05004571int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004572{
4573 struct btrfs_root *root = BTRFS_I(inode)->root;
4574 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004575 int ret;
4576
Josef Bacik72ac3c02012-05-23 14:13:11 -04004577 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05004578 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004579
Josef Bacik7a7eaa402011-04-13 12:54:33 -04004580 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004581 if (IS_ERR(trans))
4582 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004583
4584 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004585 if (ret && ret == -ENOSPC) {
4586 /* whoops, lets try again with the full transaction */
4587 btrfs_end_transaction(trans, root);
4588 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004589 if (IS_ERR(trans))
4590 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004591
Chris Mason94b60442010-05-26 11:02:00 -04004592 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004593 }
Chris Mason39279cc2007-06-12 06:35:45 -04004594 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004595 if (BTRFS_I(inode)->delayed_node)
4596 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004597
4598 return ret;
4599}
4600
4601/*
4602 * This is a copy of file_update_time. We need this so we can return error on
4603 * ENOSPC for updating the inode in the case of file write and mmap writes.
4604 */
Josef Bacike41f9412012-03-26 09:46:47 -04004605static int btrfs_update_time(struct inode *inode, struct timespec *now,
4606 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004607{
Alexander Block2bc5565282012-06-15 09:49:33 +02004608 struct btrfs_root *root = BTRFS_I(inode)->root;
4609
4610 if (btrfs_root_readonly(root))
4611 return -EROFS;
4612
Josef Bacike41f9412012-03-26 09:46:47 -04004613 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004614 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04004615 if (flags & S_CTIME)
4616 inode->i_ctime = *now;
4617 if (flags & S_MTIME)
4618 inode->i_mtime = *now;
4619 if (flags & S_ATIME)
4620 inode->i_atime = *now;
4621 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004622}
4623
Chris Masond352ac62008-09-29 15:18:18 -04004624/*
4625 * find the highest existing sequence number in a directory
4626 * and then set the in-memory index_cnt variable to reflect
4627 * free sequence numbers
4628 */
Josef Bacikaec74772008-07-24 12:12:38 -04004629static int btrfs_set_inode_index_count(struct inode *inode)
4630{
4631 struct btrfs_root *root = BTRFS_I(inode)->root;
4632 struct btrfs_key key, found_key;
4633 struct btrfs_path *path;
4634 struct extent_buffer *leaf;
4635 int ret;
4636
Li Zefan33345d012011-04-20 10:31:50 +08004637 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004638 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4639 key.offset = (u64)-1;
4640
4641 path = btrfs_alloc_path();
4642 if (!path)
4643 return -ENOMEM;
4644
4645 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4646 if (ret < 0)
4647 goto out;
4648 /* FIXME: we should be able to handle this */
4649 if (ret == 0)
4650 goto out;
4651 ret = 0;
4652
4653 /*
4654 * MAGIC NUMBER EXPLANATION:
4655 * since we search a directory based on f_pos we have to start at 2
4656 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4657 * else has to start at 2
4658 */
4659 if (path->slots[0] == 0) {
4660 BTRFS_I(inode)->index_cnt = 2;
4661 goto out;
4662 }
4663
4664 path->slots[0]--;
4665
4666 leaf = path->nodes[0];
4667 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4668
Li Zefan33345d012011-04-20 10:31:50 +08004669 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004670 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4671 BTRFS_I(inode)->index_cnt = 2;
4672 goto out;
4673 }
4674
4675 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4676out:
4677 btrfs_free_path(path);
4678 return ret;
4679}
4680
Chris Masond352ac62008-09-29 15:18:18 -04004681/*
4682 * helper to find a free sequence number in a given directory. This current
4683 * code is very simple, later versions will do smarter things in the btree
4684 */
Chris Mason3de45862008-11-17 21:02:50 -05004685int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004686{
4687 int ret = 0;
4688
4689 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004690 ret = btrfs_inode_delayed_dir_index_count(dir);
4691 if (ret) {
4692 ret = btrfs_set_inode_index_count(dir);
4693 if (ret)
4694 return ret;
4695 }
Josef Bacikaec74772008-07-24 12:12:38 -04004696 }
4697
Chris Mason00e4e6b2008-08-05 11:18:09 -04004698 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004699 BTRFS_I(dir)->index_cnt++;
4700
4701 return ret;
4702}
4703
Chris Mason39279cc2007-06-12 06:35:45 -04004704static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4705 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004706 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004707 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04004708 u64 ref_objectid, u64 objectid,
4709 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004710{
4711 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004712 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004713 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004714 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004715 struct btrfs_inode_ref *ref;
4716 struct btrfs_key key[2];
4717 u32 sizes[2];
4718 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004719 int ret;
4720 int owner;
4721
Chris Mason5f39d392007-10-15 16:14:19 -04004722 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004723 if (!path)
4724 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04004725
Chris Mason39279cc2007-06-12 06:35:45 -04004726 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004727 if (!inode) {
4728 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004729 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004730 }
Chris Mason39279cc2007-06-12 06:35:45 -04004731
Li Zefan581bb052011-04-20 10:06:11 +08004732 /*
4733 * we have to initialize this early, so we can reclaim the inode
4734 * number if we fail afterwards in this function.
4735 */
4736 inode->i_ino = objectid;
4737
Josef Bacikaec74772008-07-24 12:12:38 -04004738 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004739 trace_btrfs_inode_request(dir);
4740
Chris Mason3de45862008-11-17 21:02:50 -05004741 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004742 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004743 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004744 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004745 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004746 }
Josef Bacikaec74772008-07-24 12:12:38 -04004747 }
4748 /*
4749 * index_cnt is ignored for everything but a dir,
4750 * btrfs_get_inode_index_count has an explanation for the magic
4751 * number
4752 */
4753 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004754 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004755 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004756 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04004757
Josef Bacik5dc562c2012-08-17 13:14:17 -04004758 /*
4759 * We could have gotten an inode number from somebody who was fsynced
4760 * and then removed in this same transaction, so let's just set full
4761 * sync since it will be a full sync anyway and this will blow away the
4762 * old info in the log.
4763 */
4764 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
4765
Al Viro569254b2011-07-24 17:08:40 -04004766 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04004767 owner = 0;
4768 else
4769 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004770
4771 key[0].objectid = objectid;
4772 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4773 key[0].offset = 0;
4774
Mark Fashehf1863732012-08-08 11:32:27 -07004775 /*
4776 * Start new inodes with an inode_ref. This is slightly more
4777 * efficient for small numbers of hard links since they will
4778 * be packed into one item. Extended refs will kick in if we
4779 * add more hard links than can fit in the ref item.
4780 */
Chris Mason9c583092008-01-29 15:15:18 -05004781 key[1].objectid = objectid;
4782 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4783 key[1].offset = ref_objectid;
4784
4785 sizes[0] = sizeof(struct btrfs_inode_item);
4786 sizes[1] = name_len + sizeof(*ref);
4787
Chris Masonb9473432009-03-13 11:00:37 -04004788 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004789 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4790 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004791 goto fail;
4792
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004793 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004794 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004795 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004796 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4797 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06004798 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
4799 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04004800 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004801
4802 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4803 struct btrfs_inode_ref);
4804 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004805 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004806 ptr = (unsigned long)(ref + 1);
4807 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4808
Chris Mason5f39d392007-10-15 16:14:19 -04004809 btrfs_mark_buffer_dirty(path->nodes[0]);
4810 btrfs_free_path(path);
4811
Chris Mason39279cc2007-06-12 06:35:45 -04004812 location = &BTRFS_I(inode)->location;
4813 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004814 location->offset = 0;
4815 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4816
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004817 btrfs_inherit_iflags(inode, dir);
4818
Al Viro569254b2011-07-24 17:08:40 -04004819 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04004820 if (btrfs_test_opt(root, NODATASUM))
4821 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06004822 if (btrfs_test_opt(root, NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004823 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4824 }
4825
Chris Mason39279cc2007-06-12 06:35:45 -04004826 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004827 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004828
4829 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04004830 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00004831
Alexander Block8ea05e32012-07-25 17:35:53 +02004832 btrfs_update_root_times(trans, root);
4833
Chris Mason39279cc2007-06-12 06:35:45 -04004834 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004835fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004836 if (dir)
4837 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004838 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004839 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004840 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004841}
4842
4843static inline u8 btrfs_inode_type(struct inode *inode)
4844{
4845 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4846}
4847
Chris Masond352ac62008-09-29 15:18:18 -04004848/*
4849 * utility function to add 'inode' into 'parent_inode' with
4850 * a give name and a given sequence number.
4851 * if 'add_backref' is true, also insert a backref from the
4852 * inode to the parent directory.
4853 */
Chris Masone02119d2008-09-05 16:13:11 -04004854int btrfs_add_link(struct btrfs_trans_handle *trans,
4855 struct inode *parent_inode, struct inode *inode,
4856 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004857{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004858 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004859 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004860 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004861 u64 ino = btrfs_ino(inode);
4862 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004863
Li Zefan33345d012011-04-20 10:31:50 +08004864 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004865 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4866 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004867 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004868 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4869 key.offset = 0;
4870 }
Chris Mason39279cc2007-06-12 06:35:45 -04004871
Li Zefan33345d012011-04-20 10:31:50 +08004872 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004873 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4874 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004875 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004876 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004877 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4878 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004879 }
4880
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004881 /* Nothing to clean up yet */
4882 if (ret)
4883 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004884
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004885 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4886 parent_inode, &key,
4887 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05004888 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004889 goto fail_dir_item;
4890 else if (ret) {
4891 btrfs_abort_transaction(trans, root, ret);
4892 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004893 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004894
4895 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4896 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004897 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004898 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4899 ret = btrfs_update_inode(trans, root, parent_inode);
4900 if (ret)
4901 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004902 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05004903
4904fail_dir_item:
4905 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4906 u64 local_index;
4907 int err;
4908 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4909 key.objectid, root->root_key.objectid,
4910 parent_ino, &local_index, name, name_len);
4911
4912 } else if (add_backref) {
4913 u64 local_index;
4914 int err;
4915
4916 err = btrfs_del_inode_ref(trans, root, name, name_len,
4917 ino, parent_ino, &local_index);
4918 }
4919 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004920}
4921
4922static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004923 struct inode *dir, struct dentry *dentry,
4924 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004925{
Josef Bacika1b075d2010-11-19 20:36:11 +00004926 int err = btrfs_add_link(trans, dir, inode,
4927 dentry->d_name.name, dentry->d_name.len,
4928 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004929 if (err > 0)
4930 err = -EEXIST;
4931 return err;
4932}
4933
Josef Bacik618e21d2007-07-11 10:18:17 -04004934static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04004935 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04004936{
4937 struct btrfs_trans_handle *trans;
4938 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004939 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004940 int err;
4941 int drop_inode = 0;
4942 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004943 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004944
4945 if (!new_valid_dev(rdev))
4946 return -EINVAL;
4947
Josef Bacik9ed74f22009-09-11 16:12:44 -04004948 /*
4949 * 2 for inode item and ref
4950 * 2 for dir items
4951 * 1 for xattr if selinux is on
4952 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004953 trans = btrfs_start_transaction(root, 5);
4954 if (IS_ERR(trans))
4955 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004956
Li Zefan581bb052011-04-20 10:06:11 +08004957 err = btrfs_find_free_ino(root, &objectid);
4958 if (err)
4959 goto out_unlock;
4960
Josef Bacikaec74772008-07-24 12:12:38 -04004961 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004962 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004963 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004964 if (IS_ERR(inode)) {
4965 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004966 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004967 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004968
Eric Paris2a7dba32011-02-01 11:05:39 -05004969 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004970 if (err) {
4971 drop_inode = 1;
4972 goto out_unlock;
4973 }
4974
Filipe Brandenburger2794ed02012-11-30 03:40:08 +00004975 err = btrfs_update_inode(trans, root, inode);
4976 if (err) {
4977 drop_inode = 1;
4978 goto out_unlock;
4979 }
4980
Casey Schauflerad19db72011-12-15 10:09:07 -05004981 /*
4982 * If the active LSM wants to access the inode during
4983 * d_instantiate it needs these. Smack checks to see
4984 * if the filesystem supports xattrs by looking at the
4985 * ops vector.
4986 */
4987
4988 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00004989 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004990 if (err)
4991 drop_inode = 1;
4992 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04004993 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004994 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05004995 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004996 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004997out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004998 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004999 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005000 if (drop_inode) {
5001 inode_dec_link_count(inode);
5002 iput(inode);
5003 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005004 return err;
5005}
5006
Chris Mason39279cc2007-06-12 06:35:45 -04005007static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005008 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005009{
5010 struct btrfs_trans_handle *trans;
5011 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005012 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005013 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005014 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005015 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005016 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005017
Josef Bacik9ed74f22009-09-11 16:12:44 -04005018 /*
5019 * 2 for inode item and ref
5020 * 2 for dir items
5021 * 1 for xattr if selinux is on
5022 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005023 trans = btrfs_start_transaction(root, 5);
5024 if (IS_ERR(trans))
5025 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005026
Li Zefan581bb052011-04-20 10:06:11 +08005027 err = btrfs_find_free_ino(root, &objectid);
5028 if (err)
5029 goto out_unlock;
5030
Josef Bacikaec74772008-07-24 12:12:38 -04005031 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005032 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005033 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005034 if (IS_ERR(inode)) {
5035 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005036 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005037 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005038 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005039
Eric Paris2a7dba32011-02-01 11:05:39 -05005040 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005041 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005042 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005043
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005044 err = btrfs_update_inode(trans, root, inode);
5045 if (err)
5046 goto out_unlock;
5047
Casey Schauflerad19db72011-12-15 10:09:07 -05005048 /*
5049 * If the active LSM wants to access the inode during
5050 * d_instantiate it needs these. Smack checks to see
5051 * if the filesystem supports xattrs by looking at the
5052 * ops vector.
5053 */
5054 inode->i_fop = &btrfs_file_operations;
5055 inode->i_op = &btrfs_file_inode_operations;
5056
Josef Bacika1b075d2010-11-19 20:36:11 +00005057 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005058 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005059 goto out_unlock;
5060
5061 inode->i_mapping->a_ops = &btrfs_aops;
5062 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5063 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5064 d_instantiate(dentry, inode);
5065
Chris Mason39279cc2007-06-12 06:35:45 -04005066out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005067 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005068 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005069 inode_dec_link_count(inode);
5070 iput(inode);
5071 }
Liu Bob53d3f52012-11-14 14:34:34 +00005072 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005073 return err;
5074}
5075
5076static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5077 struct dentry *dentry)
5078{
5079 struct btrfs_trans_handle *trans;
5080 struct btrfs_root *root = BTRFS_I(dir)->root;
5081 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005082 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005083 int err;
5084 int drop_inode = 0;
5085
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005086 /* do not allow sys_link's with other subvols of the same device */
5087 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005088 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005089
Mark Fashehf1863732012-08-08 11:32:27 -07005090 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005091 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005092
Chris Mason3de45862008-11-17 21:02:50 -05005093 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005094 if (err)
5095 goto fail;
5096
Yan, Zhenga22285a2010-05-16 10:48:46 -04005097 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005098 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005099 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005100 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005101 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005102 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005103 if (IS_ERR(trans)) {
5104 err = PTR_ERR(trans);
5105 goto fail;
5106 }
Chris Mason5f39d392007-10-15 16:14:19 -04005107
Miao Xie31534952011-04-13 13:19:21 +08005108 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005109 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005110 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6e2010-10-23 11:11:40 -04005111 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005112 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005113
Josef Bacika1b075d2010-11-19 20:36:11 +00005114 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005115
Yan, Zhenga5719522009-09-24 09:17:31 -04005116 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005117 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005118 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005119 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005120 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005121 if (err)
5122 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005123 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005124 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005125 }
Chris Mason39279cc2007-06-12 06:35:45 -04005126
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005127 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005128fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005129 if (drop_inode) {
5130 inode_dec_link_count(inode);
5131 iput(inode);
5132 }
Liu Bob53d3f52012-11-14 14:34:34 +00005133 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005134 return err;
5135}
5136
Al Viro18bb1db2011-07-26 01:41:39 -04005137static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005138{
Chris Masonb9d86662008-05-02 16:13:49 -04005139 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005140 struct btrfs_trans_handle *trans;
5141 struct btrfs_root *root = BTRFS_I(dir)->root;
5142 int err = 0;
5143 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005144 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005145 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005146
Josef Bacik9ed74f22009-09-11 16:12:44 -04005147 /*
5148 * 2 items for inode and ref
5149 * 2 items for dir items
5150 * 1 for xattr if selinux is on
5151 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005152 trans = btrfs_start_transaction(root, 5);
5153 if (IS_ERR(trans))
5154 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005155
Li Zefan581bb052011-04-20 10:06:11 +08005156 err = btrfs_find_free_ino(root, &objectid);
5157 if (err)
5158 goto out_fail;
5159
Josef Bacikaec74772008-07-24 12:12:38 -04005160 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005161 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005162 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005163 if (IS_ERR(inode)) {
5164 err = PTR_ERR(inode);
5165 goto out_fail;
5166 }
Chris Mason5f39d392007-10-15 16:14:19 -04005167
Chris Mason39279cc2007-06-12 06:35:45 -04005168 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005169
Eric Paris2a7dba32011-02-01 11:05:39 -05005170 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005171 if (err)
5172 goto out_fail;
5173
Chris Mason39279cc2007-06-12 06:35:45 -04005174 inode->i_op = &btrfs_dir_inode_operations;
5175 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005176
Chris Masondbe674a2008-07-17 12:54:05 -04005177 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005178 err = btrfs_update_inode(trans, root, inode);
5179 if (err)
5180 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005181
Josef Bacika1b075d2010-11-19 20:36:11 +00005182 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5183 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005184 if (err)
5185 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005186
Chris Mason39279cc2007-06-12 06:35:45 -04005187 d_instantiate(dentry, inode);
5188 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005189
5190out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005191 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005192 if (drop_on_err)
5193 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005194 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005195 return err;
5196}
5197
Chris Masond352ac62008-09-29 15:18:18 -04005198/* helper for btfs_get_extent. Given an existing extent in the tree,
5199 * and an extent that you want to insert, deal with overlap and insert
5200 * the new extent into the tree.
5201 */
Chris Mason3b951512008-04-17 11:29:12 -04005202static int merge_extent_mapping(struct extent_map_tree *em_tree,
5203 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005204 struct extent_map *em,
5205 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005206{
5207 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005208
Chris Masone6dcd2d2008-07-17 12:53:50 -04005209 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5210 start_diff = map_start - em->start;
5211 em->start = map_start;
5212 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005213 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5214 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005215 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005216 em->block_len -= start_diff;
5217 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005218 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005219}
5220
Chris Masonc8b97812008-10-29 14:49:59 -04005221static noinline int uncompress_inline(struct btrfs_path *path,
5222 struct inode *inode, struct page *page,
5223 size_t pg_offset, u64 extent_offset,
5224 struct btrfs_file_extent_item *item)
5225{
5226 int ret;
5227 struct extent_buffer *leaf = path->nodes[0];
5228 char *tmp;
5229 size_t max_size;
5230 unsigned long inline_size;
5231 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005232 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005233
5234 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005235 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005236 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5237 inline_size = btrfs_file_extent_inline_item_len(leaf,
5238 btrfs_item_nr(leaf, path->slots[0]));
5239 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005240 if (!tmp)
5241 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005242 ptr = btrfs_file_extent_inline_start(item);
5243
5244 read_extent_buffer(leaf, tmp, ptr, inline_size);
5245
Chris Mason5b050f02008-11-11 09:34:41 -05005246 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005247 ret = btrfs_decompress(compress_type, tmp, page,
5248 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005249 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005250 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005251 unsigned long copy_size = min_t(u64,
5252 PAGE_CACHE_SIZE - pg_offset,
5253 max_size - extent_offset);
5254 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005255 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005256 }
5257 kfree(tmp);
5258 return 0;
5259}
5260
Chris Masond352ac62008-09-29 15:18:18 -04005261/*
5262 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005263 * the ugly parts come from merging extents from the disk with the in-ram
5264 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005265 * where the in-ram extents might be locked pending data=ordered completion.
5266 *
5267 * This also copies inline extents directly into the page.
5268 */
Chris Masond3977122009-01-05 21:25:51 -05005269
Chris Masona52d9a82007-08-27 16:49:44 -04005270struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005271 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005272 int create)
5273{
5274 int ret;
5275 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005276 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005277 u64 extent_start = 0;
5278 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005279 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005280 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005281 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005282 struct btrfs_root *root = BTRFS_I(inode)->root;
5283 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005284 struct extent_buffer *leaf;
5285 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005286 struct extent_map *em = NULL;
5287 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005288 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005289 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005290 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005291
Chris Masona52d9a82007-08-27 16:49:44 -04005292again:
Chris Mason890871b2009-09-02 16:24:52 -04005293 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005294 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005295 if (em)
5296 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005297 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005298
Chris Masona52d9a82007-08-27 16:49:44 -04005299 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005300 if (em->start > start || em->start + em->len <= start)
5301 free_extent_map(em);
5302 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005303 free_extent_map(em);
5304 else
5305 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005306 }
David Sterba172ddd62011-04-21 00:48:27 +02005307 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005308 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005309 err = -ENOMEM;
5310 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005311 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005312 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005313 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005314 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005315 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005316 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005317
5318 if (!path) {
5319 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005320 if (!path) {
5321 err = -ENOMEM;
5322 goto out;
5323 }
5324 /*
5325 * Chances are we'll be called again, so go ahead and do
5326 * readahead
5327 */
5328 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005329 }
5330
Chris Mason179e29e2007-11-01 11:28:41 -04005331 ret = btrfs_lookup_file_extent(trans, root, path,
5332 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005333 if (ret < 0) {
5334 err = ret;
5335 goto out;
5336 }
5337
5338 if (ret != 0) {
5339 if (path->slots[0] == 0)
5340 goto not_found;
5341 path->slots[0]--;
5342 }
5343
Chris Mason5f39d392007-10-15 16:14:19 -04005344 leaf = path->nodes[0];
5345 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005346 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005347 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005348 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5349 found_type = btrfs_key_type(&found_key);
5350 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005351 found_type != BTRFS_EXTENT_DATA_KEY) {
5352 goto not_found;
5353 }
5354
Chris Mason5f39d392007-10-15 16:14:19 -04005355 found_type = btrfs_file_extent_type(leaf, item);
5356 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005357 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005358 if (found_type == BTRFS_FILE_EXTENT_REG ||
5359 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005360 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005361 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005362 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5363 size_t size;
5364 size = btrfs_file_extent_inline_len(leaf, item);
5365 extent_end = (extent_start + size + root->sectorsize - 1) &
5366 ~((u64)root->sectorsize - 1);
5367 }
5368
5369 if (start >= extent_end) {
5370 path->slots[0]++;
5371 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5372 ret = btrfs_next_leaf(root, path);
5373 if (ret < 0) {
5374 err = ret;
5375 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005376 }
Yan Zheng9036c102008-10-30 14:19:41 -04005377 if (ret > 0)
5378 goto not_found;
5379 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005380 }
Yan Zheng9036c102008-10-30 14:19:41 -04005381 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5382 if (found_key.objectid != objectid ||
5383 found_key.type != BTRFS_EXTENT_DATA_KEY)
5384 goto not_found;
5385 if (start + len <= found_key.offset)
5386 goto not_found;
5387 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04005388 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04005389 em->len = found_key.offset - start;
5390 goto not_found_em;
5391 }
5392
Yan Zhengd899e052008-10-30 14:25:28 -04005393 if (found_type == BTRFS_FILE_EXTENT_REG ||
5394 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005395 em->start = extent_start;
5396 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005397 em->orig_start = extent_start -
5398 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05005399 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
5400 item);
Chris Masondb945352007-10-15 16:15:53 -04005401 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5402 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005403 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005404 goto insert;
5405 }
Li Zefan261507a02010-12-17 14:21:50 +08005406 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005407 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005408 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005409 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05005410 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005411 } else {
5412 bytenr += btrfs_file_extent_offset(leaf, item);
5413 em->block_start = bytenr;
5414 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005415 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5416 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005417 }
Chris Masona52d9a82007-08-27 16:49:44 -04005418 goto insert;
5419 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005420 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005421 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005422 size_t size;
5423 size_t extent_offset;
5424 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005425
Chris Masona52d9a82007-08-27 16:49:44 -04005426 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005427 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005428 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005429 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005430 goto out;
5431 }
5432
Yan Zheng9036c102008-10-30 14:19:41 -04005433 size = btrfs_file_extent_inline_len(leaf, item);
5434 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005435 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005436 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005437 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005438 em->len = (copy_size + root->sectorsize - 1) &
5439 ~((u64)root->sectorsize - 1);
Josef Bacikb4939682012-12-03 10:31:19 -05005440 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04005441 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08005442 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005443 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005444 em->compress_type = compress_type;
5445 }
Yan689f9342007-10-29 11:41:07 -04005446 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005447 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005448 if (btrfs_file_extent_compression(leaf, item) !=
5449 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005450 ret = uncompress_inline(path, inode, page,
5451 pg_offset,
5452 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005453 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04005454 } else {
5455 map = kmap(page);
5456 read_extent_buffer(leaf, map + pg_offset, ptr,
5457 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005458 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5459 memset(map + pg_offset + copy_size, 0,
5460 PAGE_CACHE_SIZE - pg_offset -
5461 copy_size);
5462 }
Chris Masonc8b97812008-10-29 14:49:59 -04005463 kunmap(page);
5464 }
Chris Mason179e29e2007-11-01 11:28:41 -04005465 flush_dcache_page(page);
5466 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01005467 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04005468 if (!trans) {
5469 kunmap(page);
5470 free_extent_map(em);
5471 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04005472
David Sterbab3b4aa72011-04-21 01:20:15 +02005473 btrfs_release_path(path);
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005474 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04005475
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005476 if (IS_ERR(trans))
5477 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005478 goto again;
5479 }
Chris Masonc8b97812008-10-29 14:49:59 -04005480 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005481 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005482 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005483 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005484 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005485 }
Chris Masond1310b22008-01-24 16:13:08 -05005486 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005487 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005488 goto insert;
5489 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00005490 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005491 }
5492not_found:
5493 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04005494 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005495 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005496not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005497 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005498 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005499insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02005500 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05005501 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005502 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5503 "[%llu %llu]\n", (unsigned long long)em->start,
5504 (unsigned long long)em->len,
5505 (unsigned long long)start,
5506 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005507 err = -EIO;
5508 goto out;
5509 }
Chris Masond1310b22008-01-24 16:13:08 -05005510
5511 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005512 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005513 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005514 /* it is possible that someone inserted the extent into the tree
5515 * while we had the lock dropped. It is also possible that
5516 * an overlapping map exists in the tree
5517 */
Chris Masona52d9a82007-08-27 16:49:44 -04005518 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005519 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005520
5521 ret = 0;
5522
Chris Mason3b951512008-04-17 11:29:12 -04005523 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005524 if (existing && (existing->start > start ||
5525 existing->start + existing->len <= start)) {
5526 free_extent_map(existing);
5527 existing = NULL;
5528 }
Chris Mason3b951512008-04-17 11:29:12 -04005529 if (!existing) {
5530 existing = lookup_extent_mapping(em_tree, em->start,
5531 em->len);
5532 if (existing) {
5533 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005534 em, start,
5535 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005536 free_extent_map(existing);
5537 if (err) {
5538 free_extent_map(em);
5539 em = NULL;
5540 }
5541 } else {
5542 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005543 free_extent_map(em);
5544 em = NULL;
5545 }
5546 } else {
5547 free_extent_map(em);
5548 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005549 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005550 }
Chris Masona52d9a82007-08-27 16:49:44 -04005551 }
Chris Mason890871b2009-09-02 16:24:52 -04005552 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005553out:
liubo1abe9b82011-03-24 11:18:59 +00005554
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06005555 if (em)
5556 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00005557
Chris Masonf4219502008-07-22 11:18:09 -04005558 if (path)
5559 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005560 if (trans) {
5561 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005562 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005563 err = ret;
5564 }
Chris Masona52d9a82007-08-27 16:49:44 -04005565 if (err) {
5566 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005567 return ERR_PTR(err);
5568 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005569 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04005570 return em;
5571}
5572
Chris Masonec29ed52011-02-23 16:23:20 -05005573struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5574 size_t pg_offset, u64 start, u64 len,
5575 int create)
5576{
5577 struct extent_map *em;
5578 struct extent_map *hole_em = NULL;
5579 u64 range_start = start;
5580 u64 end;
5581 u64 found;
5582 u64 found_end;
5583 int err = 0;
5584
5585 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5586 if (IS_ERR(em))
5587 return em;
5588 if (em) {
5589 /*
5590 * if our em maps to a hole, there might
5591 * actually be delalloc bytes behind it
5592 */
5593 if (em->block_start != EXTENT_MAP_HOLE)
5594 return em;
5595 else
5596 hole_em = em;
5597 }
5598
5599 /* check to see if we've wrapped (len == -1 or similar) */
5600 end = start + len;
5601 if (end < start)
5602 end = (u64)-1;
5603 else
5604 end -= 1;
5605
5606 em = NULL;
5607
5608 /* ok, we didn't find anything, lets look for delalloc */
5609 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5610 end, len, EXTENT_DELALLOC, 1);
5611 found_end = range_start + found;
5612 if (found_end < range_start)
5613 found_end = (u64)-1;
5614
5615 /*
5616 * we didn't find anything useful, return
5617 * the original results from get_extent()
5618 */
5619 if (range_start > end || found_end <= start) {
5620 em = hole_em;
5621 hole_em = NULL;
5622 goto out;
5623 }
5624
5625 /* adjust the range_start to make sure it doesn't
5626 * go backwards from the start they passed in
5627 */
5628 range_start = max(start,range_start);
5629 found = found_end - range_start;
5630
5631 if (found > 0) {
5632 u64 hole_start = start;
5633 u64 hole_len = len;
5634
David Sterba172ddd62011-04-21 00:48:27 +02005635 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05005636 if (!em) {
5637 err = -ENOMEM;
5638 goto out;
5639 }
5640 /*
5641 * when btrfs_get_extent can't find anything it
5642 * returns one huge hole
5643 *
5644 * make sure what it found really fits our range, and
5645 * adjust to make sure it is based on the start from
5646 * the caller
5647 */
5648 if (hole_em) {
5649 u64 calc_end = extent_map_end(hole_em);
5650
5651 if (calc_end <= start || (hole_em->start > end)) {
5652 free_extent_map(hole_em);
5653 hole_em = NULL;
5654 } else {
5655 hole_start = max(hole_em->start, start);
5656 hole_len = calc_end - hole_start;
5657 }
5658 }
5659 em->bdev = NULL;
5660 if (hole_em && range_start > hole_start) {
5661 /* our hole starts before our delalloc, so we
5662 * have to return just the parts of the hole
5663 * that go until the delalloc starts
5664 */
5665 em->len = min(hole_len,
5666 range_start - hole_start);
5667 em->start = hole_start;
5668 em->orig_start = hole_start;
5669 /*
5670 * don't adjust block start at all,
5671 * it is fixed at EXTENT_MAP_HOLE
5672 */
5673 em->block_start = hole_em->block_start;
5674 em->block_len = hole_len;
5675 } else {
5676 em->start = range_start;
5677 em->len = found;
5678 em->orig_start = range_start;
5679 em->block_start = EXTENT_MAP_DELALLOC;
5680 em->block_len = found;
5681 }
5682 } else if (hole_em) {
5683 return hole_em;
5684 }
5685out:
5686
5687 free_extent_map(hole_em);
5688 if (err) {
5689 free_extent_map(em);
5690 return ERR_PTR(err);
5691 }
5692 return em;
5693}
5694
Josef Bacik4b46fce2010-05-23 11:00:55 -04005695static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5696 u64 start, u64 len)
5697{
5698 struct btrfs_root *root = BTRFS_I(inode)->root;
5699 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04005700 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005701 struct btrfs_key ins;
5702 u64 alloc_hint;
5703 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005704
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005705 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005706 if (IS_ERR(trans))
5707 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005708
5709 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5710
5711 alloc_hint = get_extent_allocation_hint(inode, start, len);
5712 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005713 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005714 if (ret) {
5715 em = ERR_PTR(ret);
5716 goto out;
5717 }
5718
Josef Bacik70c8a912012-10-11 16:54:30 -04005719 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
5720 ins.offset, ins.offset, 0);
5721 if (IS_ERR(em))
5722 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005723
5724 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5725 ins.offset, ins.offset, 0);
5726 if (ret) {
5727 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5728 em = ERR_PTR(ret);
5729 }
5730out:
5731 btrfs_end_transaction(trans, root);
5732 return em;
5733}
5734
Chris Mason46bfbb52010-05-26 11:04:10 -04005735/*
5736 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5737 * block must be cow'd
5738 */
5739static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5740 struct inode *inode, u64 offset, u64 len)
5741{
5742 struct btrfs_path *path;
5743 int ret;
5744 struct extent_buffer *leaf;
5745 struct btrfs_root *root = BTRFS_I(inode)->root;
5746 struct btrfs_file_extent_item *fi;
5747 struct btrfs_key key;
5748 u64 disk_bytenr;
5749 u64 backref_offset;
5750 u64 extent_end;
5751 u64 num_bytes;
5752 int slot;
5753 int found_type;
5754
5755 path = btrfs_alloc_path();
5756 if (!path)
5757 return -ENOMEM;
5758
Li Zefan33345d012011-04-20 10:31:50 +08005759 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005760 offset, 0);
5761 if (ret < 0)
5762 goto out;
5763
5764 slot = path->slots[0];
5765 if (ret == 1) {
5766 if (slot == 0) {
5767 /* can't find the item, must cow */
5768 ret = 0;
5769 goto out;
5770 }
5771 slot--;
5772 }
5773 ret = 0;
5774 leaf = path->nodes[0];
5775 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005776 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005777 key.type != BTRFS_EXTENT_DATA_KEY) {
5778 /* not our file or wrong item type, must cow */
5779 goto out;
5780 }
5781
5782 if (key.offset > offset) {
5783 /* Wrong offset, must cow */
5784 goto out;
5785 }
5786
5787 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5788 found_type = btrfs_file_extent_type(leaf, fi);
5789 if (found_type != BTRFS_FILE_EXTENT_REG &&
5790 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5791 /* not a regular extent, must cow */
5792 goto out;
5793 }
5794 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5795 backref_offset = btrfs_file_extent_offset(leaf, fi);
5796
5797 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5798 if (extent_end < offset + len) {
5799 /* extent doesn't include our full range, must cow */
5800 goto out;
5801 }
5802
5803 if (btrfs_extent_readonly(root, disk_bytenr))
5804 goto out;
5805
5806 /*
5807 * look for other files referencing this extent, if we
5808 * find any we must cow
5809 */
Li Zefan33345d012011-04-20 10:31:50 +08005810 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005811 key.offset - backref_offset, disk_bytenr))
5812 goto out;
5813
5814 /*
5815 * adjust disk_bytenr and num_bytes to cover just the bytes
5816 * in this extent we are about to write. If there
5817 * are any csums in that range we have to cow in order
5818 * to keep the csums correct
5819 */
5820 disk_bytenr += backref_offset;
5821 disk_bytenr += offset - key.offset;
5822 num_bytes = min(offset + len, extent_end) - offset;
5823 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5824 goto out;
5825 /*
5826 * all of the above have passed, it is safe to overwrite this extent
5827 * without cow
5828 */
5829 ret = 1;
5830out:
5831 btrfs_free_path(path);
5832 return ret;
5833}
5834
Josef Bacikeb838e72012-07-31 16:28:48 -04005835static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
5836 struct extent_state **cached_state, int writing)
5837{
5838 struct btrfs_ordered_extent *ordered;
5839 int ret = 0;
5840
5841 while (1) {
5842 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5843 0, cached_state);
5844 /*
5845 * We're concerned with the entire range that we're going to be
5846 * doing DIO to, so we need to make sure theres no ordered
5847 * extents in this range.
5848 */
5849 ordered = btrfs_lookup_ordered_range(inode, lockstart,
5850 lockend - lockstart + 1);
5851
5852 /*
5853 * We need to make sure there are no buffered pages in this
5854 * range either, we could have raced between the invalidate in
5855 * generic_file_direct_write and locking the extent. The
5856 * invalidate needs to happen so that reads after a write do not
5857 * get stale data.
5858 */
5859 if (!ordered && (!writing ||
5860 !test_range_bit(&BTRFS_I(inode)->io_tree,
5861 lockstart, lockend, EXTENT_UPTODATE, 0,
5862 *cached_state)))
5863 break;
5864
5865 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5866 cached_state, GFP_NOFS);
5867
5868 if (ordered) {
5869 btrfs_start_ordered_extent(inode, ordered, 1);
5870 btrfs_put_ordered_extent(ordered);
5871 } else {
5872 /* Screw you mmap */
5873 ret = filemap_write_and_wait_range(inode->i_mapping,
5874 lockstart,
5875 lockend);
5876 if (ret)
5877 break;
5878
5879 /*
5880 * If we found a page that couldn't be invalidated just
5881 * fall back to buffered.
5882 */
5883 ret = invalidate_inode_pages2_range(inode->i_mapping,
5884 lockstart >> PAGE_CACHE_SHIFT,
5885 lockend >> PAGE_CACHE_SHIFT);
5886 if (ret)
5887 break;
5888 }
5889
5890 cond_resched();
5891 }
5892
5893 return ret;
5894}
5895
Josef Bacik69ffb542012-09-11 15:40:07 -04005896static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
5897 u64 len, u64 orig_start,
5898 u64 block_start, u64 block_len,
Josef Bacikb4939682012-12-03 10:31:19 -05005899 u64 orig_block_len, int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04005900{
5901 struct extent_map_tree *em_tree;
5902 struct extent_map *em;
5903 struct btrfs_root *root = BTRFS_I(inode)->root;
5904 int ret;
5905
5906 em_tree = &BTRFS_I(inode)->extent_tree;
5907 em = alloc_extent_map();
5908 if (!em)
5909 return ERR_PTR(-ENOMEM);
5910
5911 em->start = start;
5912 em->orig_start = orig_start;
5913 em->len = len;
5914 em->block_len = block_len;
5915 em->block_start = block_start;
5916 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05005917 em->orig_block_len = orig_block_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04005918 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04005919 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5920 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05005921 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04005922
5923 do {
5924 btrfs_drop_extent_cache(inode, em->start,
5925 em->start + em->len - 1, 0);
5926 write_lock(&em_tree->lock);
5927 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04005928 if (!ret)
5929 list_move(&em->list,
5930 &em_tree->modified_extents);
Josef Bacik69ffb542012-09-11 15:40:07 -04005931 write_unlock(&em_tree->lock);
5932 } while (ret == -EEXIST);
5933
5934 if (ret) {
5935 free_extent_map(em);
5936 return ERR_PTR(ret);
5937 }
5938
5939 return em;
5940}
5941
5942
Josef Bacik4b46fce2010-05-23 11:00:55 -04005943static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5944 struct buffer_head *bh_result, int create)
5945{
5946 struct extent_map *em;
5947 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04005948 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005949 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04005950 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005951 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005952 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04005953 int unlock_bits = EXTENT_LOCKED;
5954 int ret;
5955
Josef Bacikeb838e72012-07-31 16:28:48 -04005956 if (create) {
5957 ret = btrfs_delalloc_reserve_space(inode, len);
5958 if (ret)
5959 return ret;
5960 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Josef Bacikc3298612012-08-03 16:49:19 -04005961 } else {
5962 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04005963 }
5964
Josef Bacikc3298612012-08-03 16:49:19 -04005965 lockstart = start;
5966 lockend = start + len - 1;
5967
Josef Bacikeb838e72012-07-31 16:28:48 -04005968 /*
5969 * If this errors out it's because we couldn't invalidate pagecache for
5970 * this range and we need to fallback to buffered.
5971 */
5972 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
5973 return -ENOTBLK;
5974
5975 if (create) {
5976 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
5977 lockend, EXTENT_DELALLOC, NULL,
5978 &cached_state, GFP_NOFS);
5979 if (ret)
5980 goto unlock_err;
5981 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005982
5983 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04005984 if (IS_ERR(em)) {
5985 ret = PTR_ERR(em);
5986 goto unlock_err;
5987 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005988
5989 /*
5990 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5991 * io. INLINE is special, and we could probably kludge it in here, but
5992 * it's still buffered so for safety lets just fall back to the generic
5993 * buffered path.
5994 *
5995 * For COMPRESSED we _have_ to read the entire extent in so we can
5996 * decompress it, so there will be buffering required no matter what we
5997 * do, so go ahead and fallback to buffered.
5998 *
5999 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6000 * to buffered IO. Don't blame me, this is the price we pay for using
6001 * the generic code.
6002 */
6003 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6004 em->block_start == EXTENT_MAP_INLINE) {
6005 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006006 ret = -ENOTBLK;
6007 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006008 }
6009
6010 /* Just a good old fashioned hole, return */
6011 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6012 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6013 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006014 ret = 0;
6015 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006016 }
6017
6018 /*
6019 * We don't allocate a new extent in the following cases
6020 *
6021 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6022 * existing extent.
6023 * 2) The extent is marked as PREALLOC. We're good to go here and can
6024 * just use the extent.
6025 *
6026 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006027 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006028 len = min(len, em->len - (start - em->start));
6029 lockstart = start + len;
6030 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006031 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006032
6033 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6034 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6035 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006036 int type;
6037 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04006038 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006039
6040 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6041 type = BTRFS_ORDERED_PREALLOC;
6042 else
6043 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006044 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006045 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006046
6047 /*
6048 * we're not going to log anything, but we do need
6049 * to make sure the current transaction stays open
6050 * while we look for nocow cross refs
6051 */
Josef Bacik7a7eaa402011-04-13 12:54:33 -04006052 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006053 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006054 goto must_cow;
6055
6056 if (can_nocow_odirect(trans, inode, start, len) == 1) {
Josef Bacik70c8a912012-10-11 16:54:30 -04006057 u64 orig_start = em->orig_start;
Josef Bacikb4939682012-12-03 10:31:19 -05006058 u64 orig_block_len = em->orig_block_len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006059
6060 if (type == BTRFS_ORDERED_PREALLOC) {
6061 free_extent_map(em);
6062 em = create_pinned_em(inode, start, len,
6063 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006064 block_start, len,
6065 orig_block_len, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006066 if (IS_ERR(em)) {
6067 btrfs_end_transaction(trans, root);
6068 goto unlock_err;
6069 }
6070 }
6071
Chris Mason46bfbb52010-05-26 11:04:10 -04006072 ret = btrfs_add_ordered_extent_dio(inode, start,
6073 block_start, len, len, type);
6074 btrfs_end_transaction(trans, root);
6075 if (ret) {
6076 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006077 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006078 }
6079 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006080 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006081 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006082 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006083must_cow:
6084 /*
6085 * this will cow the extent, reset the len in case we changed
6086 * it above
6087 */
6088 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006089 free_extent_map(em);
6090 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006091 if (IS_ERR(em)) {
6092 ret = PTR_ERR(em);
6093 goto unlock_err;
6094 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006095 len = min(len, em->len - (start - em->start));
6096unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006097 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6098 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006099 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006100 bh_result->b_bdev = em->bdev;
6101 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006102 if (create) {
6103 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6104 set_buffer_new(bh_result);
6105
6106 /*
6107 * Need to update the i_size under the extent lock so buffered
6108 * readers will get the updated i_size when we unlock.
6109 */
6110 if (start + len > i_size_read(inode))
6111 i_size_write(inode, start + len);
6112 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006113
Josef Bacikeb838e72012-07-31 16:28:48 -04006114 /*
6115 * In the case of write we need to clear and unlock the entire range,
6116 * in the case of read we need to unlock only the end area that we
6117 * aren't using if there is any left over space.
6118 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006119 if (lockstart < lockend) {
6120 if (create && len < lockend - lockstart) {
6121 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006122 lockstart + len - 1,
6123 unlock_bits | EXTENT_DEFRAG, 1, 0,
Liu Bo24c03fa2012-08-22 20:10:38 -06006124 &cached_state, GFP_NOFS);
6125 /*
6126 * Beside unlock, we also need to cleanup reserved space
6127 * for the left range by attaching EXTENT_DO_ACCOUNTING.
6128 */
6129 clear_extent_bit(&BTRFS_I(inode)->io_tree,
6130 lockstart + len, lockend,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006131 unlock_bits | EXTENT_DO_ACCOUNTING |
6132 EXTENT_DEFRAG, 1, 0, NULL, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006133 } else {
6134 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6135 lockend, unlock_bits, 1, 0,
6136 &cached_state, GFP_NOFS);
6137 }
6138 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006139 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006140 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006141
Josef Bacik4b46fce2010-05-23 11:00:55 -04006142 free_extent_map(em);
6143
6144 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006145
6146unlock_err:
6147 if (create)
6148 unlock_bits |= EXTENT_DO_ACCOUNTING;
6149
6150 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6151 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6152 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006153}
6154
6155struct btrfs_dio_private {
6156 struct inode *inode;
6157 u64 logical_offset;
6158 u64 disk_bytenr;
6159 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006160 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006161
6162 /* number of bios pending for this dio */
6163 atomic_t pending_bios;
6164
6165 /* IO errors */
6166 int errors;
6167
6168 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006169};
6170
6171static void btrfs_endio_direct_read(struct bio *bio, int err)
6172{
Miao Xiee65e1532010-11-22 03:04:43 +00006173 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006174 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6175 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006176 struct inode *inode = dip->inode;
6177 struct btrfs_root *root = BTRFS_I(inode)->root;
6178 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006179
6180 start = dip->logical_offset;
6181 do {
6182 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6183 struct page *page = bvec->bv_page;
6184 char *kaddr;
6185 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006186 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006187 unsigned long flags;
6188
Josef Bacikc3298612012-08-03 16:49:19 -04006189 if (get_state_private(&BTRFS_I(inode)->io_tree,
6190 start, &private))
6191 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006192 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006193 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006194 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
6195 csum, bvec->bv_len);
6196 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006197 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006198 local_irq_restore(flags);
6199
6200 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006201 if (csum != private) {
6202failed:
Li Zefan33345d012011-04-20 10:31:50 +08006203 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04006204 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08006205 (unsigned long long)btrfs_ino(inode),
6206 (unsigned long long)start,
Josef Bacikc3298612012-08-03 16:49:19 -04006207 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006208 err = -EIO;
6209 }
6210 }
6211
6212 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006213 bvec++;
6214 } while (bvec <= bvec_end);
6215
6216 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006217 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006218 bio->bi_private = dip->private;
6219
Josef Bacik4b46fce2010-05-23 11:00:55 -04006220 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006221
6222 /* If we had a csum failure make sure to clear the uptodate flag */
6223 if (err)
6224 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006225 dio_end_io(bio, err);
6226}
6227
6228static void btrfs_endio_direct_write(struct bio *bio, int err)
6229{
6230 struct btrfs_dio_private *dip = bio->bi_private;
6231 struct inode *inode = dip->inode;
6232 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006233 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006234 u64 ordered_offset = dip->logical_offset;
6235 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006236 int ret;
6237
6238 if (err)
6239 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006240again:
6241 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6242 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006243 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006244 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006245 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006246
Josef Bacik5fd02042012-05-02 14:00:54 -04006247 ordered->work.func = finish_ordered_fn;
6248 ordered->work.flags = 0;
6249 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6250 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006251out_test:
6252 /*
6253 * our bio might span multiple ordered extents. If we haven't
6254 * completed the accounting for the whole dio, go back and try again
6255 */
6256 if (ordered_offset < dip->logical_offset + dip->bytes) {
6257 ordered_bytes = dip->logical_offset + dip->bytes -
6258 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006259 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006260 goto again;
6261 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006262out_done:
6263 bio->bi_private = dip->private;
6264
Josef Bacik4b46fce2010-05-23 11:00:55 -04006265 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006266
6267 /* If we had an error make sure to clear the uptodate flag */
6268 if (err)
6269 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006270 dio_end_io(bio, err);
6271}
6272
Chris Masoneaf25d92010-05-25 09:48:28 -04006273static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6274 struct bio *bio, int mirror_num,
6275 unsigned long bio_flags, u64 offset)
6276{
6277 int ret;
6278 struct btrfs_root *root = BTRFS_I(inode)->root;
6279 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006280 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006281 return 0;
6282}
6283
Miao Xiee65e1532010-11-22 03:04:43 +00006284static void btrfs_end_dio_bio(struct bio *bio, int err)
6285{
6286 struct btrfs_dio_private *dip = bio->bi_private;
6287
6288 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006289 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006290 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006291 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006292 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006293 dip->errors = 1;
6294
6295 /*
6296 * before atomic variable goto zero, we must make sure
6297 * dip->errors is perceived to be set.
6298 */
6299 smp_mb__before_atomic_dec();
6300 }
6301
6302 /* if there are more bios still pending for this dio, just exit */
6303 if (!atomic_dec_and_test(&dip->pending_bios))
6304 goto out;
6305
6306 if (dip->errors)
6307 bio_io_error(dip->orig_bio);
6308 else {
6309 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6310 bio_endio(dip->orig_bio, 0);
6311 }
6312out:
6313 bio_put(bio);
6314}
6315
6316static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6317 u64 first_sector, gfp_t gfp_flags)
6318{
6319 int nr_vecs = bio_get_nr_vecs(bdev);
6320 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6321}
6322
6323static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6324 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006325 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006326{
6327 int write = rw & REQ_WRITE;
6328 struct btrfs_root *root = BTRFS_I(inode)->root;
6329 int ret;
6330
Josef Bacikb812ce22012-11-16 13:56:32 -05006331 if (async_submit)
6332 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6333
Miao Xiee65e1532010-11-22 03:04:43 +00006334 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006335
6336 if (!write) {
6337 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6338 if (ret)
6339 goto err;
6340 }
Miao Xiee65e1532010-11-22 03:04:43 +00006341
Josef Bacik1ae39932011-04-06 14:41:34 -04006342 if (skip_sum)
6343 goto map;
6344
6345 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006346 ret = btrfs_wq_submit_bio(root->fs_info,
6347 inode, rw, bio, 0, 0,
6348 file_offset,
6349 __btrfs_submit_bio_start_direct_io,
6350 __btrfs_submit_bio_done);
6351 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006352 } else if (write) {
6353 /*
6354 * If we aren't doing async submit, calculate the csum of the
6355 * bio now.
6356 */
6357 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6358 if (ret)
6359 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006360 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04006361 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006362 if (ret)
6363 goto err;
6364 }
Miao Xiee65e1532010-11-22 03:04:43 +00006365
Josef Bacik1ae39932011-04-06 14:41:34 -04006366map:
6367 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006368err:
6369 bio_put(bio);
6370 return ret;
6371}
6372
6373static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6374 int skip_sum)
6375{
6376 struct inode *inode = dip->inode;
6377 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00006378 struct bio *bio;
6379 struct bio *orig_bio = dip->orig_bio;
6380 struct bio_vec *bvec = orig_bio->bi_io_vec;
6381 u64 start_sector = orig_bio->bi_sector;
6382 u64 file_offset = dip->logical_offset;
6383 u64 submit_len = 0;
6384 u64 map_length;
6385 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006386 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006387 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006388
Miao Xiee65e1532010-11-22 03:04:43 +00006389 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05006390 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006391 &map_length, NULL, 0);
6392 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006393 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006394 return -EIO;
6395 }
Josef Bacik02f57c72011-04-06 14:25:44 -04006396 if (map_length >= orig_bio->bi_size) {
6397 bio = orig_bio;
6398 goto submit;
6399 }
6400
David Woodhouse53b381b2013-01-29 18:40:14 -05006401 /* async crcs make it difficult to collect full stripe writes. */
6402 if (btrfs_get_alloc_profile(root, 1) &
6403 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
6404 async_submit = 0;
6405 else
6406 async_submit = 1;
6407
Josef Bacik02f57c72011-04-06 14:25:44 -04006408 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6409 if (!bio)
6410 return -ENOMEM;
6411 bio->bi_private = dip;
6412 bio->bi_end_io = btrfs_end_dio_bio;
6413 atomic_inc(&dip->pending_bios);
6414
Miao Xiee65e1532010-11-22 03:04:43 +00006415 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6416 if (unlikely(map_length < submit_len + bvec->bv_len ||
6417 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6418 bvec->bv_offset) < bvec->bv_len)) {
6419 /*
6420 * inc the count before we submit the bio so
6421 * we know the end IO handler won't happen before
6422 * we inc the count. Otherwise, the dip might get freed
6423 * before we're done setting it up
6424 */
6425 atomic_inc(&dip->pending_bios);
6426 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6427 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006428 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006429 if (ret) {
6430 bio_put(bio);
6431 atomic_dec(&dip->pending_bios);
6432 goto out_err;
6433 }
6434
Miao Xiee65e1532010-11-22 03:04:43 +00006435 start_sector += submit_len >> 9;
6436 file_offset += submit_len;
6437
6438 submit_len = 0;
6439 nr_pages = 0;
6440
6441 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6442 start_sector, GFP_NOFS);
6443 if (!bio)
6444 goto out_err;
6445 bio->bi_private = dip;
6446 bio->bi_end_io = btrfs_end_dio_bio;
6447
6448 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05006449 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01006450 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006451 &map_length, NULL, 0);
6452 if (ret) {
6453 bio_put(bio);
6454 goto out_err;
6455 }
6456 } else {
6457 submit_len += bvec->bv_len;
6458 nr_pages ++;
6459 bvec++;
6460 }
6461 }
6462
Josef Bacik02f57c72011-04-06 14:25:44 -04006463submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006464 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006465 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006466 if (!ret)
6467 return 0;
6468
6469 bio_put(bio);
6470out_err:
6471 dip->errors = 1;
6472 /*
6473 * before atomic variable goto zero, we must
6474 * make sure dip->errors is perceived to be set.
6475 */
6476 smp_mb__before_atomic_dec();
6477 if (atomic_dec_and_test(&dip->pending_bios))
6478 bio_io_error(dip->orig_bio);
6479
6480 /* bio_end_io() will handle error, so we needn't return it */
6481 return 0;
6482}
6483
Josef Bacik4b46fce2010-05-23 11:00:55 -04006484static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6485 loff_t file_offset)
6486{
6487 struct btrfs_root *root = BTRFS_I(inode)->root;
6488 struct btrfs_dio_private *dip;
6489 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006490 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006491 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006492 int ret = 0;
6493
6494 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6495
6496 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6497 if (!dip) {
6498 ret = -ENOMEM;
6499 goto free_ordered;
6500 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006501
6502 dip->private = bio->bi_private;
6503 dip->inode = inode;
6504 dip->logical_offset = file_offset;
6505
Josef Bacik4b46fce2010-05-23 11:00:55 -04006506 dip->bytes = 0;
6507 do {
6508 dip->bytes += bvec->bv_len;
6509 bvec++;
6510 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6511
Chris Mason46bfbb52010-05-26 11:04:10 -04006512 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006513 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006514 dip->errors = 0;
6515 dip->orig_bio = bio;
6516 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006517
6518 if (write)
6519 bio->bi_end_io = btrfs_endio_direct_write;
6520 else
6521 bio->bi_end_io = btrfs_endio_direct_read;
6522
Miao Xiee65e1532010-11-22 03:04:43 +00006523 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6524 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006525 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006526free_ordered:
6527 /*
6528 * If this is a write, we need to clean up the reserved space and kill
6529 * the ordered extent.
6530 */
6531 if (write) {
6532 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006533 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006534 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6535 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6536 btrfs_free_reserved_extent(root, ordered->start,
6537 ordered->disk_len);
6538 btrfs_put_ordered_extent(ordered);
6539 btrfs_put_ordered_extent(ordered);
6540 }
6541 bio_endio(bio, ret);
6542}
6543
Chris Mason5a5f79b2010-05-26 21:33:37 -04006544static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6545 const struct iovec *iov, loff_t offset,
6546 unsigned long nr_segs)
6547{
6548 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006549 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006550 size_t size;
6551 unsigned long addr;
6552 unsigned blocksize_mask = root->sectorsize - 1;
6553 ssize_t retval = -EINVAL;
6554 loff_t end = offset;
6555
6556 if (offset & blocksize_mask)
6557 goto out;
6558
6559 /* Check the memory alignment. Blocks cannot straddle pages */
6560 for (seg = 0; seg < nr_segs; seg++) {
6561 addr = (unsigned long)iov[seg].iov_base;
6562 size = iov[seg].iov_len;
6563 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006564 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006565 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006566
6567 /* If this is a write we don't need to check anymore */
6568 if (rw & WRITE)
6569 continue;
6570
6571 /*
6572 * Check to make sure we don't have duplicate iov_base's in this
6573 * iovec, if so return EINVAL, otherwise we'll get csum errors
6574 * when reading back.
6575 */
6576 for (i = seg + 1; i < nr_segs; i++) {
6577 if (iov[seg].iov_base == iov[i].iov_base)
6578 goto out;
6579 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006580 }
6581 retval = 0;
6582out:
6583 return retval;
6584}
Josef Bacikeb838e72012-07-31 16:28:48 -04006585
Chris Mason16432982008-04-10 10:23:21 -04006586static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6587 const struct iovec *iov, loff_t offset,
6588 unsigned long nr_segs)
6589{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006590 struct file *file = iocb->ki_filp;
6591 struct inode *inode = file->f_mapping->host;
David Woodhouse53b381b2013-01-29 18:40:14 -05006592 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006593
Chris Mason5a5f79b2010-05-26 21:33:37 -04006594 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04006595 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006596 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006597
David Woodhouse53b381b2013-01-29 18:40:14 -05006598 ret = __blockdev_direct_IO(rw, iocb, inode,
Chris Mason5a5f79b2010-05-26 21:33:37 -04006599 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6600 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6601 btrfs_submit_direct, 0);
David Woodhouse53b381b2013-01-29 18:40:14 -05006602 return ret;
Chris Mason16432982008-04-10 10:23:21 -04006603}
6604
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00006605#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
6606
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006607static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6608 __u64 start, __u64 len)
6609{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00006610 int ret;
6611
6612 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
6613 if (ret)
6614 return ret;
6615
Chris Masonec29ed52011-02-23 16:23:20 -05006616 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006617}
6618
Chris Mason9ebefb182007-06-15 13:50:00 -04006619int btrfs_readpage(struct file *file, struct page *page)
6620{
Chris Masond1310b22008-01-24 16:13:08 -05006621 struct extent_io_tree *tree;
6622 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02006623 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006624}
Chris Mason1832a6d2007-12-21 16:27:21 -05006625
Chris Mason39279cc2007-06-12 06:35:45 -04006626static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6627{
Chris Masond1310b22008-01-24 16:13:08 -05006628 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006629
6630
6631 if (current->flags & PF_MEMALLOC) {
6632 redirty_page_for_writepage(wbc, page);
6633 unlock_page(page);
6634 return 0;
6635 }
Chris Masond1310b22008-01-24 16:13:08 -05006636 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006637 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6638}
Chris Mason39279cc2007-06-12 06:35:45 -04006639
Chris Masonf4219502008-07-22 11:18:09 -04006640int btrfs_writepages(struct address_space *mapping,
6641 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006642{
Chris Masond1310b22008-01-24 16:13:08 -05006643 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006644
Chris Masond1310b22008-01-24 16:13:08 -05006645 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006646 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6647}
6648
Chris Mason3ab2fb52007-11-08 10:59:22 -05006649static int
6650btrfs_readpages(struct file *file, struct address_space *mapping,
6651 struct list_head *pages, unsigned nr_pages)
6652{
Chris Masond1310b22008-01-24 16:13:08 -05006653 struct extent_io_tree *tree;
6654 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006655 return extent_readpages(tree, mapping, pages, nr_pages,
6656 btrfs_get_extent);
6657}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006658static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006659{
Chris Masond1310b22008-01-24 16:13:08 -05006660 struct extent_io_tree *tree;
6661 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006662 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006663
Chris Masond1310b22008-01-24 16:13:08 -05006664 tree = &BTRFS_I(page->mapping->host)->io_tree;
6665 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006666 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006667 if (ret == 1) {
6668 ClearPagePrivate(page);
6669 set_page_private(page, 0);
6670 page_cache_release(page);
6671 }
6672 return ret;
6673}
Chris Mason39279cc2007-06-12 06:35:45 -04006674
Chris Masone6dcd2d2008-07-17 12:53:50 -04006675static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6676{
Chris Mason98509cf2008-09-11 15:51:43 -04006677 if (PageWriteback(page) || PageDirty(page))
6678 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006679 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006680}
6681
Chris Masona52d9a82007-08-27 16:49:44 -04006682static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6683{
Josef Bacik5fd02042012-05-02 14:00:54 -04006684 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05006685 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006686 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006687 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006688 u64 page_start = page_offset(page);
6689 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006690
Chris Mason8b62b722009-09-02 16:53:46 -04006691 /*
6692 * we have the page locked, so new writeback can't start,
6693 * and the dirty bit won't be cleared while we are here.
6694 *
6695 * Wait for IO on this page so that we can safely clear
6696 * the PagePrivate2 bit and do ordered accounting
6697 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006698 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006699
Josef Bacik5fd02042012-05-02 14:00:54 -04006700 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006701 if (offset) {
6702 btrfs_releasepage(page, GFP_NOFS);
6703 return;
6704 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006705 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Josef Bacik5fd02042012-05-02 14:00:54 -04006706 ordered = btrfs_lookup_ordered_extent(inode,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006707 page_offset(page));
6708 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006709 /*
6710 * IO on this page will never be started, so we need
6711 * to account for any ordered extents now
6712 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006713 clear_extent_bit(tree, page_start, page_end,
6714 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006715 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
6716 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006717 /*
6718 * whoever cleared the private bit is responsible
6719 * for the finish_ordered_io
6720 */
Josef Bacik5fd02042012-05-02 14:00:54 -04006721 if (TestClearPagePrivate2(page) &&
6722 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
6723 PAGE_CACHE_SIZE, 1)) {
6724 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04006725 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006726 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006727 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006728 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006729 }
6730 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006731 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006732 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
6733 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006734 __btrfs_releasepage(page, GFP_NOFS);
6735
Chris Mason4a096752008-07-21 10:29:44 -04006736 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006737 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006738 ClearPagePrivate(page);
6739 set_page_private(page, 0);
6740 page_cache_release(page);
6741 }
Chris Mason39279cc2007-06-12 06:35:45 -04006742}
6743
Chris Mason9ebefb182007-06-15 13:50:00 -04006744/*
6745 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6746 * called from a page fault handler when a page is first dirtied. Hence we must
6747 * be careful to check for EOF conditions here. We set the page up correctly
6748 * for a written page which means we get ENOSPC checking when writing into
6749 * holes and correct delalloc and unwritten extent mapping on filesystems that
6750 * support these features.
6751 *
6752 * We are not allowed to take the i_mutex here so we have to play games to
6753 * protect against truncate races as the page could now be beyond EOF. Because
6754 * vmtruncate() writes the inode size before removing pages, once we have the
6755 * page lock we can determine safely if the page is beyond EOF. If it is not
6756 * beyond EOF, then the page is guaranteed safe against truncation until we
6757 * unlock the page.
6758 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006759int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006760{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006761 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006762 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006763 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006764 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6765 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006766 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006767 char *kaddr;
6768 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006769 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006770 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05006771 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006772 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006773 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006774
Jan Karab2b5ef52012-06-12 16:20:45 +02006775 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006776 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006777 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04006778 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05006779 reserved = 1;
6780 }
Nick Piggin56a76f82009-03-31 15:23:23 -07006781 if (ret) {
6782 if (ret == -ENOMEM)
6783 ret = VM_FAULT_OOM;
6784 else /* -ENOSPC, -EIO, etc */
6785 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05006786 if (reserved)
6787 goto out;
6788 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07006789 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006790
Nick Piggin56a76f82009-03-31 15:23:23 -07006791 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006792again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006793 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006794 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006795 page_start = page_offset(page);
6796 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006797
Chris Mason9ebefb182007-06-15 13:50:00 -04006798 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006799 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006800 /* page got truncated out from underneath us */
6801 goto out_unlock;
6802 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006803 wait_on_page_writeback(page);
6804
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006805 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006806 set_page_extent_mapped(page);
6807
Chris Masoneb84ae02008-07-17 13:53:27 -04006808 /*
6809 * we can't set the delalloc bits if there are pending ordered
6810 * extents. Drop our locks and wait for them to finish
6811 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006812 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6813 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006814 unlock_extent_cached(io_tree, page_start, page_end,
6815 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006816 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006817 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006818 btrfs_put_ordered_extent(ordered);
6819 goto again;
6820 }
6821
Josef Bacikfbf19082009-10-01 17:10:23 -04006822 /*
6823 * XXX - page_mkwrite gets called every time the page is dirtied, even
6824 * if it was already dirty, so for space accounting reasons we need to
6825 * clear any delalloc bits for the range we are fixing to save. There
6826 * is probably a better way to do this, but for now keep consistent with
6827 * prepare_pages in the normal write path.
6828 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006829 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006830 EXTENT_DIRTY | EXTENT_DELALLOC |
6831 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006832 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006833
Josef Bacik2ac55d42010-02-03 19:33:23 +00006834 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6835 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006836 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006837 unlock_extent_cached(io_tree, page_start, page_end,
6838 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006839 ret = VM_FAULT_SIGBUS;
6840 goto out_unlock;
6841 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006842 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006843
6844 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006845 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006846 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006847 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006848 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006849
Chris Masone6dcd2d2008-07-17 12:53:50 -04006850 if (zero_start != PAGE_CACHE_SIZE) {
6851 kaddr = kmap(page);
6852 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6853 flush_dcache_page(page);
6854 kunmap(page);
6855 }
Chris Mason247e7432008-07-17 12:53:51 -04006856 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006857 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006858 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006859
Chris Mason257c62e2009-10-13 13:21:08 -04006860 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6861 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06006862 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04006863
Josef Bacik2ac55d42010-02-03 19:33:23 +00006864 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006865
6866out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02006867 if (!ret) {
6868 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04006869 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02006870 }
Chris Mason9ebefb182007-06-15 13:50:00 -04006871 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05006872out:
Josef Bacikec39e182012-01-12 19:10:12 -05006873 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006874out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02006875 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04006876 return ret;
6877}
6878
Josef Bacika41ad392011-01-31 15:30:16 -05006879static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006880{
6881 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006882 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04006883 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006884 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006885 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04006886 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04006887 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04006888
Josef Bacik2aaa6652012-08-29 14:27:18 -04006889 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04006890 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006891 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006892
Chris Mason4a096752008-07-21 10:29:44 -04006893 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006894 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006895
Josef Bacikfcb80c22011-05-03 10:40:22 -04006896 /*
6897 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6898 * 3 things going on here
6899 *
6900 * 1) We need to reserve space for our orphan item and the space to
6901 * delete our orphan item. Lord knows we don't want to have a dangling
6902 * orphan item because we didn't reserve space to remove it.
6903 *
6904 * 2) We need to reserve space to update our inode.
6905 *
6906 * 3) We need to have something to cache all the space that is going to
6907 * be free'd up by the truncate operation, but also have some slack
6908 * space reserved in case it uses space during the truncate (thank you
6909 * very much snapshotting).
6910 *
6911 * And we need these to all be seperate. The fact is we can use alot of
6912 * space doing the truncate, and we have no earthly idea how much space
6913 * we will use, so we need the truncate reservation to be seperate so it
6914 * doesn't end up using space reserved for updating the inode or
6915 * removing the orphan item. We also need to be able to stop the
6916 * transaction and start a new one, which means we need to be able to
6917 * update the inode several times, and we have no idea of knowing how
6918 * many times that will be, so we can't just reserve 1 item for the
6919 * entirety of the opration, so that has to be done seperately as well.
6920 * Then there is the orphan item, which does indeed need to be held on
6921 * to for the whole operation, and we need nobody to touch this reserved
6922 * space except the orphan code.
6923 *
6924 * So that leaves us with
6925 *
6926 * 1) root->orphan_block_rsv - for the orphan deletion.
6927 * 2) rsv - for the truncate reservation, which we will steal from the
6928 * transaction reservation.
6929 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6930 * updating the inode.
6931 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06006932 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006933 if (!rsv)
6934 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04006935 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04006936 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006937
Josef Bacik907cbce2011-08-08 13:46:15 -04006938 /*
Josef Bacik07127182011-08-19 10:29:59 -04006939 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04006940 * 1 for the orphan item we're going to add
6941 * 1 for the orphan item deletion
6942 * 1 for updating the inode.
6943 */
Josef Bacikfcb80c22011-05-03 10:40:22 -04006944 trans = btrfs_start_transaction(root, 4);
6945 if (IS_ERR(trans)) {
6946 err = PTR_ERR(trans);
6947 goto out;
6948 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05006949
Josef Bacik907cbce2011-08-08 13:46:15 -04006950 /* Migrate the slack space for the truncate to our reserve */
6951 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
6952 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006953 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05006954
6955 ret = btrfs_orphan_add(trans, inode);
6956 if (ret) {
6957 btrfs_end_transaction(trans, root);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006958 goto out;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006959 }
6960
Chris Mason5a3f23d2009-03-31 13:27:11 -04006961 /*
6962 * setattr is responsible for setting the ordered_data_close flag,
6963 * but that is only tested during the last file release. That
6964 * could happen well after the next commit, leaving a great big
6965 * window where new writes may get lost if someone chooses to write
6966 * to this file after truncating to zero
6967 *
6968 * The inode doesn't have any dirty data here, and so if we commit
6969 * this is a noop. If someone immediately starts writing to the inode
6970 * it is very likely we'll catch some of their writes in this
6971 * transaction, and the commit will find this file on the ordered
6972 * data list with good things to send down.
6973 *
6974 * This is a best effort solution, there is still a window where
6975 * using truncate to replace the contents of the file will
6976 * end up with a zero length file after a crash.
6977 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04006978 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
6979 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04006980 btrfs_add_ordered_operation(trans, root, inode);
6981
Josef Bacik5dc562c2012-08-17 13:14:17 -04006982 /*
6983 * So if we truncate and then write and fsync we normally would just
6984 * write the extents that changed, which is a problem if we need to
6985 * first truncate that entire inode. So set this flag so we write out
6986 * all of the extents in the inode to the sync log so we're completely
6987 * safe.
6988 */
6989 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04006990 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04006991
Yan, Zheng80825102009-11-12 09:35:36 +00006992 while (1) {
6993 ret = btrfs_truncate_inode_items(trans, root, inode,
6994 inode->i_size,
6995 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04006996 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05006997 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006998 break;
Josef Bacik3893e332011-01-31 16:03:11 -05006999 }
Chris Mason39279cc2007-06-12 06:35:45 -04007000
Josef Bacikfcb80c22011-05-03 10:40:22 -04007001 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007002 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007003 if (ret) {
7004 err = ret;
7005 break;
7006 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007007
Yan, Zheng80825102009-11-12 09:35:36 +00007008 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007009 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007010
7011 trans = btrfs_start_transaction(root, 2);
7012 if (IS_ERR(trans)) {
7013 ret = err = PTR_ERR(trans);
7014 trans = NULL;
7015 break;
7016 }
7017
7018 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7019 rsv, min_size);
7020 BUG_ON(ret); /* shouldn't happen */
7021 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007022 }
7023
7024 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007025 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007026 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007027 if (ret)
7028 err = ret;
Josef Bacikded5db92011-03-04 14:09:46 -05007029 } else if (ret && inode->i_nlink > 0) {
7030 /*
7031 * Failed to do the truncate, remove us from the in memory
7032 * orphan list.
7033 */
7034 ret = btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00007035 }
7036
Chris Mason917c16b2011-11-08 14:49:59 -05007037 if (trans) {
7038 trans->block_rsv = &root->fs_info->trans_block_rsv;
7039 ret = btrfs_update_inode(trans, root, inode);
7040 if (ret && !err)
7041 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007042
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007043 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007044 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007045 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007046
7047out:
7048 btrfs_free_block_rsv(root, rsv);
7049
Josef Bacik3893e332011-01-31 16:03:11 -05007050 if (ret && !err)
7051 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007052
Josef Bacik3893e332011-01-31 16:03:11 -05007053 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007054}
7055
Sven Wegener3b963622008-06-09 21:57:42 -04007056/*
Chris Masond352ac62008-09-29 15:18:18 -04007057 * create a new subvolume directory/inode (helper for the ioctl).
7058 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007059int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007060 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007061{
Chris Mason39279cc2007-06-12 06:35:45 -04007062 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007063 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007064 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007065
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007066 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7067 new_dirid, new_dirid,
7068 S_IFDIR | (~current_umask() & S_IRWXUGO),
7069 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007070 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007071 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007072 inode->i_op = &btrfs_dir_inode_operations;
7073 inode->i_fop = &btrfs_dir_file_operations;
7074
Miklos Szeredibfe86842011-10-28 14:13:29 +02007075 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007076 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007077
Yan, Zheng76dda932009-09-21 16:00:26 -04007078 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007079
Yan, Zheng76dda932009-09-21 16:00:26 -04007080 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007081 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007082}
7083
Chris Mason39279cc2007-06-12 06:35:45 -04007084struct inode *btrfs_alloc_inode(struct super_block *sb)
7085{
7086 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007087 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007088
7089 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7090 if (!ei)
7091 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007092
7093 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007094 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007095 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007096 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007097 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007098 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007099 ei->disk_i_size = 0;
7100 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007101 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007102 ei->index_cnt = (u64)-1;
7103 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007104 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007105
Josef Bacik9e0baf62011-07-15 15:16:44 +00007106 spin_lock_init(&ei->lock);
7107 ei->outstanding_extents = 0;
7108 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007109
Josef Bacik72ac3c02012-05-23 14:13:11 -04007110 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007111 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007112
Miao Xie16cdcec2011-04-22 18:12:22 +08007113 ei->delayed_node = NULL;
7114
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007115 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007116 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007117 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7118 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007119 ei->io_tree.track_uptodate = 1;
7120 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007121 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007122 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007123 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007124 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007125 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007126 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007127 RB_CLEAR_NODE(&ei->rb_node);
7128
7129 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007130}
7131
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007132static void btrfs_i_callback(struct rcu_head *head)
7133{
7134 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007135 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7136}
7137
Chris Mason39279cc2007-06-12 06:35:45 -04007138void btrfs_destroy_inode(struct inode *inode)
7139{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007140 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007141 struct btrfs_root *root = BTRFS_I(inode)->root;
7142
Al Virob3d9b7a2012-06-09 13:51:19 -04007143 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007144 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007145 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7146 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007147 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7148 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007149
Chris Mason5a3f23d2009-03-31 13:27:11 -04007150 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007151 * This can happen where we create an inode, but somebody else also
7152 * created the same inode and we need to destroy the one we already
7153 * created.
7154 */
7155 if (!root)
7156 goto free;
7157
7158 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007159 * Make sure we're properly removed from the ordered operation
7160 * lists.
7161 */
7162 smp_mb();
7163 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7164 spin_lock(&root->fs_info->ordered_extent_lock);
7165 list_del_init(&BTRFS_I(inode)->ordered_operations);
7166 spin_unlock(&root->fs_info->ordered_extent_lock);
7167 }
7168
Josef Bacik8a35d952012-05-23 14:26:42 -04007169 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7170 &BTRFS_I(inode)->runtime_flags)) {
Li Zefan33345d012011-04-20 10:31:50 +08007171 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
7172 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007173 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007174 }
Josef Bacik7b128762008-07-24 12:17:14 -04007175
Chris Masond3977122009-01-05 21:25:51 -05007176 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007177 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7178 if (!ordered)
7179 break;
7180 else {
Chris Masond3977122009-01-05 21:25:51 -05007181 printk(KERN_ERR "btrfs found ordered "
7182 "extent %llu %llu on inode cleanup\n",
7183 (unsigned long long)ordered->file_offset,
7184 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007185 btrfs_remove_ordered_extent(inode, ordered);
7186 btrfs_put_ordered_extent(ordered);
7187 btrfs_put_ordered_extent(ordered);
7188 }
7189 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007190 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007191 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007192free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007193 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007194 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007195}
7196
Al Viro45321ac2010-06-07 13:43:19 -04007197int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007198{
7199 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007200
Josef Bacik0af3d002010-06-21 14:48:16 -04007201 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bo83eea1f2012-07-10 05:28:39 -06007202 !btrfs_is_free_space_inode(inode))
Al Viro45321ac2010-06-07 13:43:19 -04007203 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007204 else
Al Viro45321ac2010-06-07 13:43:19 -04007205 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007206}
7207
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007208static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007209{
7210 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7211
7212 inode_init_once(&ei->vfs_inode);
7213}
7214
7215void btrfs_destroy_cachep(void)
7216{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007217 /*
7218 * Make sure all delayed rcu free inodes are flushed before we
7219 * destroy cache.
7220 */
7221 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007222 if (btrfs_inode_cachep)
7223 kmem_cache_destroy(btrfs_inode_cachep);
7224 if (btrfs_trans_handle_cachep)
7225 kmem_cache_destroy(btrfs_trans_handle_cachep);
7226 if (btrfs_transaction_cachep)
7227 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007228 if (btrfs_path_cachep)
7229 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007230 if (btrfs_free_space_cachep)
7231 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007232 if (btrfs_delalloc_work_cachep)
7233 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007234}
7235
7236int btrfs_init_cachep(void)
7237{
David Sterba837e1972012-09-07 03:00:48 -06007238 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007239 sizeof(struct btrfs_inode), 0,
7240 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007241 if (!btrfs_inode_cachep)
7242 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007243
David Sterba837e1972012-09-07 03:00:48 -06007244 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007245 sizeof(struct btrfs_trans_handle), 0,
7246 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007247 if (!btrfs_trans_handle_cachep)
7248 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007249
David Sterba837e1972012-09-07 03:00:48 -06007250 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007251 sizeof(struct btrfs_transaction), 0,
7252 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007253 if (!btrfs_transaction_cachep)
7254 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007255
David Sterba837e1972012-09-07 03:00:48 -06007256 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007257 sizeof(struct btrfs_path), 0,
7258 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007259 if (!btrfs_path_cachep)
7260 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007261
David Sterba837e1972012-09-07 03:00:48 -06007262 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007263 sizeof(struct btrfs_free_space), 0,
7264 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7265 if (!btrfs_free_space_cachep)
7266 goto fail;
7267
Miao Xie8ccf6f192012-10-25 09:28:04 +00007268 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7269 sizeof(struct btrfs_delalloc_work), 0,
7270 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7271 NULL);
7272 if (!btrfs_delalloc_work_cachep)
7273 goto fail;
7274
Chris Mason39279cc2007-06-12 06:35:45 -04007275 return 0;
7276fail:
7277 btrfs_destroy_cachep();
7278 return -ENOMEM;
7279}
7280
7281static int btrfs_getattr(struct vfsmount *mnt,
7282 struct dentry *dentry, struct kstat *stat)
7283{
7284 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007285 u32 blocksize = inode->i_sb->s_blocksize;
7286
Chris Mason39279cc2007-06-12 06:35:45 -04007287 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007288 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007289 stat->blksize = PAGE_CACHE_SIZE;
David Sterbafadc0d82011-11-20 07:33:38 -05007290 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
7291 ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007292 return 0;
7293}
7294
Liu Bo75e7cb72011-03-22 10:12:20 +00007295/*
7296 * If a file is moved, it will inherit the cow and compression flags of the new
7297 * directory.
7298 */
7299static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7300{
7301 struct btrfs_inode *b_dir = BTRFS_I(dir);
7302 struct btrfs_inode *b_inode = BTRFS_I(inode);
7303
7304 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7305 b_inode->flags |= BTRFS_INODE_NODATACOW;
7306 else
7307 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7308
Liu Bobc178232012-06-14 02:23:18 -06007309 if (b_dir->flags & BTRFS_INODE_COMPRESS) {
Liu Bo75e7cb72011-03-22 10:12:20 +00007310 b_inode->flags |= BTRFS_INODE_COMPRESS;
Liu Bobc178232012-06-14 02:23:18 -06007311 b_inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
7312 } else {
7313 b_inode->flags &= ~(BTRFS_INODE_COMPRESS |
7314 BTRFS_INODE_NOCOMPRESS);
7315 }
Liu Bo75e7cb72011-03-22 10:12:20 +00007316}
7317
Chris Masond3977122009-01-05 21:25:51 -05007318static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7319 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007320{
7321 struct btrfs_trans_handle *trans;
7322 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007323 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007324 struct inode *new_inode = new_dentry->d_inode;
7325 struct inode *old_inode = old_dentry->d_inode;
7326 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007327 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007328 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007329 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007330 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007331
Li Zefan33345d012011-04-20 10:31:50 +08007332 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007333 return -EPERM;
7334
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007335 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007336 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007337 return -EXDEV;
7338
Li Zefan33345d012011-04-20 10:31:50 +08007339 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7340 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007341 return -ENOTEMPTY;
7342
Chris Mason39279cc2007-06-12 06:35:45 -04007343 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007344 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007345 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05007346
7347
7348 /* check for collisions, even if the name isn't there */
7349 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
7350 new_dentry->d_name.name,
7351 new_dentry->d_name.len);
7352
7353 if (ret) {
7354 if (ret == -EEXIST) {
7355 /* we shouldn't get
7356 * eexist without a new_inode */
7357 if (!new_inode) {
7358 WARN_ON(1);
7359 return ret;
7360 }
7361 } else {
7362 /* maybe -EOVERFLOW */
7363 return ret;
7364 }
7365 }
7366 ret = 0;
7367
Chris Mason5a3f23d2009-03-31 13:27:11 -04007368 /*
7369 * we're using rename to replace one file with another.
7370 * and the replacement file is large. Start IO on it now so
7371 * we don't add too much work to the end of the transaction
7372 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007373 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007374 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7375 filemap_flush(old_inode->i_mapping);
7376
Yan, Zheng76dda932009-09-21 16:00:26 -04007377 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007378 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007379 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007380 /*
7381 * We want to reserve the absolute worst case amount of items. So if
7382 * both inodes are subvols and we need to unlink them then that would
7383 * require 4 item modifications, but if they are both normal inodes it
7384 * would require 5 item modifications, so we'll assume their normal
7385 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7386 * should cover the worst case number of items we'll modify.
7387 */
7388 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007389 if (IS_ERR(trans)) {
7390 ret = PTR_ERR(trans);
7391 goto out_notrans;
7392 }
Chris Mason5f39d392007-10-15 16:14:19 -04007393
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007394 if (dest != root)
7395 btrfs_record_root_in_trans(trans, dest);
7396
Yan, Zhenga5719522009-09-24 09:17:31 -04007397 ret = btrfs_set_inode_index(new_dir, &index);
7398 if (ret)
7399 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007400
Li Zefan33345d012011-04-20 10:31:50 +08007401 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007402 /* force full log commit if subvolume involved. */
7403 root->fs_info->last_trans_log_full_commit = trans->transid;
7404 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007405 ret = btrfs_insert_inode_ref(trans, dest,
7406 new_dentry->d_name.name,
7407 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007408 old_ino,
7409 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007410 if (ret)
7411 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007412 /*
7413 * this is an ugly little race, but the rename is required
7414 * to make sure that if we crash, the inode is either at the
7415 * old name or the new one. pinning the log transaction lets
7416 * us make sure we don't allow a log commit to come in after
7417 * we unlink the name but before we add the new name back in.
7418 */
7419 btrfs_pin_log_trans(root);
7420 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007421 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007422 * make sure the inode gets flushed if it is replacing
7423 * something.
7424 */
Li Zefan33345d012011-04-20 10:31:50 +08007425 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007426 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007427
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007428 inode_inc_iversion(old_dir);
7429 inode_inc_iversion(new_dir);
7430 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007431 old_dir->i_ctime = old_dir->i_mtime = ctime;
7432 new_dir->i_ctime = new_dir->i_mtime = ctime;
7433 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007434
Chris Mason12fcfd22009-03-24 10:24:20 -04007435 if (old_dentry->d_parent != new_dentry->d_parent)
7436 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7437
Li Zefan33345d012011-04-20 10:31:50 +08007438 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007439 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7440 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7441 old_dentry->d_name.name,
7442 old_dentry->d_name.len);
7443 } else {
Al Viro92986792011-03-04 17:14:37 +00007444 ret = __btrfs_unlink_inode(trans, root, old_dir,
7445 old_dentry->d_inode,
7446 old_dentry->d_name.name,
7447 old_dentry->d_name.len);
7448 if (!ret)
7449 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007450 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007451 if (ret) {
7452 btrfs_abort_transaction(trans, root, ret);
7453 goto out_fail;
7454 }
Chris Mason39279cc2007-06-12 06:35:45 -04007455
7456 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007457 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007458 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007459 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007460 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7461 root_objectid = BTRFS_I(new_inode)->location.objectid;
7462 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7463 root_objectid,
7464 new_dentry->d_name.name,
7465 new_dentry->d_name.len);
7466 BUG_ON(new_inode->i_nlink == 0);
7467 } else {
7468 ret = btrfs_unlink_inode(trans, dest, new_dir,
7469 new_dentry->d_inode,
7470 new_dentry->d_name.name,
7471 new_dentry->d_name.len);
7472 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007473 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007474 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007475 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007476 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007477 if (ret) {
7478 btrfs_abort_transaction(trans, root, ret);
7479 goto out_fail;
7480 }
Chris Mason39279cc2007-06-12 06:35:45 -04007481 }
Josef Bacikaec74772008-07-24 12:12:38 -04007482
Liu Bo75e7cb72011-03-22 10:12:20 +00007483 fixup_inode_flags(new_dir, old_inode);
7484
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007485 ret = btrfs_add_link(trans, new_dir, old_inode,
7486 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007487 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007488 if (ret) {
7489 btrfs_abort_transaction(trans, root, ret);
7490 goto out_fail;
7491 }
Chris Mason39279cc2007-06-12 06:35:45 -04007492
Li Zefan33345d012011-04-20 10:31:50 +08007493 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04007494 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00007495 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007496 btrfs_end_log_trans(root);
7497 }
Chris Mason39279cc2007-06-12 06:35:45 -04007498out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007499 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007500out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007501 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007502 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007503
Chris Mason39279cc2007-06-12 06:35:45 -04007504 return ret;
7505}
7506
Miao Xie8ccf6f192012-10-25 09:28:04 +00007507static void btrfs_run_delalloc_work(struct btrfs_work *work)
7508{
7509 struct btrfs_delalloc_work *delalloc_work;
7510
7511 delalloc_work = container_of(work, struct btrfs_delalloc_work,
7512 work);
7513 if (delalloc_work->wait)
7514 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
7515 else
7516 filemap_flush(delalloc_work->inode->i_mapping);
7517
7518 if (delalloc_work->delay_iput)
7519 btrfs_add_delayed_iput(delalloc_work->inode);
7520 else
7521 iput(delalloc_work->inode);
7522 complete(&delalloc_work->completion);
7523}
7524
7525struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
7526 int wait, int delay_iput)
7527{
7528 struct btrfs_delalloc_work *work;
7529
7530 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
7531 if (!work)
7532 return NULL;
7533
7534 init_completion(&work->completion);
7535 INIT_LIST_HEAD(&work->list);
7536 work->inode = inode;
7537 work->wait = wait;
7538 work->delay_iput = delay_iput;
7539 work->work.func = btrfs_run_delalloc_work;
7540
7541 return work;
7542}
7543
7544void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
7545{
7546 wait_for_completion(&work->completion);
7547 kmem_cache_free(btrfs_delalloc_work_cachep, work);
7548}
7549
Chris Masond352ac62008-09-29 15:18:18 -04007550/*
7551 * some fairly slow code that needs optimization. This walks the list
7552 * of all the inodes with pending delalloc and forces them to disk.
7553 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007554int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007555{
7556 struct list_head *head = &root->fs_info->delalloc_inodes;
7557 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007558 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007559 struct btrfs_delalloc_work *work, *next;
7560 struct list_head works;
7561 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04007562
Yan Zhengc146afa2008-11-12 14:34:12 -05007563 if (root->fs_info->sb->s_flags & MS_RDONLY)
7564 return -EROFS;
7565
Miao Xie8ccf6f192012-10-25 09:28:04 +00007566 INIT_LIST_HEAD(&works);
7567
Chris Mason75eff682008-12-15 15:54:40 -05007568 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05007569 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04007570 binode = list_entry(head->next, struct btrfs_inode,
7571 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007572 inode = igrab(&binode->vfs_inode);
7573 if (!inode)
7574 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007575 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007576 if (inode) {
Miao Xie8ccf6f192012-10-25 09:28:04 +00007577 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
7578 if (!work) {
7579 ret = -ENOMEM;
7580 goto out;
7581 }
7582 list_add_tail(&work->list, &works);
7583 btrfs_queue_worker(&root->fs_info->flush_workers,
7584 &work->work);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007585 }
7586 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007587 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007588 }
Chris Mason75eff682008-12-15 15:54:40 -05007589 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007590
7591 /* the filemap_flush will queue IO into the worker threads, but
7592 * we have to make sure the IO is actually started and that
7593 * ordered extents get created before we return
7594 */
7595 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007596 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007597 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007598 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007599 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7600 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007601 }
7602 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007603out:
7604 list_for_each_entry_safe(work, next, &works, list) {
7605 list_del_init(&work->list);
7606 btrfs_wait_and_free_delalloc_work(work);
7607 }
7608 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04007609}
7610
Chris Mason39279cc2007-06-12 06:35:45 -04007611static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7612 const char *symname)
7613{
7614 struct btrfs_trans_handle *trans;
7615 struct btrfs_root *root = BTRFS_I(dir)->root;
7616 struct btrfs_path *path;
7617 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007618 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007619 int err;
7620 int drop_inode = 0;
7621 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007622 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007623 int name_len;
7624 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007625 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007626 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007627 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04007628
7629 name_len = strlen(symname) + 1;
7630 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7631 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007632
Josef Bacik9ed74f22009-09-11 16:12:44 -04007633 /*
7634 * 2 items for inode item and ref
7635 * 2 items for dir items
7636 * 1 item for xattr if selinux is on
7637 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007638 trans = btrfs_start_transaction(root, 5);
7639 if (IS_ERR(trans))
7640 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007641
Li Zefan581bb052011-04-20 10:06:11 +08007642 err = btrfs_find_free_ino(root, &objectid);
7643 if (err)
7644 goto out_unlock;
7645
Josef Bacikaec74772008-07-24 12:12:38 -04007646 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007647 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007648 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007649 if (IS_ERR(inode)) {
7650 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007651 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007652 }
Chris Mason39279cc2007-06-12 06:35:45 -04007653
Eric Paris2a7dba32011-02-01 11:05:39 -05007654 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007655 if (err) {
7656 drop_inode = 1;
7657 goto out_unlock;
7658 }
7659
Casey Schauflerad19db72011-12-15 10:09:07 -05007660 /*
7661 * If the active LSM wants to access the inode during
7662 * d_instantiate it needs these. Smack checks to see
7663 * if the filesystem supports xattrs by looking at the
7664 * ops vector.
7665 */
7666 inode->i_fop = &btrfs_file_operations;
7667 inode->i_op = &btrfs_file_inode_operations;
7668
Josef Bacika1b075d2010-11-19 20:36:11 +00007669 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007670 if (err)
7671 drop_inode = 1;
7672 else {
7673 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007674 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05007675 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007676 }
Chris Mason39279cc2007-06-12 06:35:45 -04007677 if (drop_inode)
7678 goto out_unlock;
7679
7680 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007681 if (!path) {
7682 err = -ENOMEM;
7683 drop_inode = 1;
7684 goto out_unlock;
7685 }
Li Zefan33345d012011-04-20 10:31:50 +08007686 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007687 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007688 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7689 datasize = btrfs_file_extent_calc_inline_size(name_len);
7690 err = btrfs_insert_empty_item(trans, root, path, &key,
7691 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007692 if (err) {
7693 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00007694 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04007695 goto out_unlock;
7696 }
Chris Mason5f39d392007-10-15 16:14:19 -04007697 leaf = path->nodes[0];
7698 ei = btrfs_item_ptr(leaf, path->slots[0],
7699 struct btrfs_file_extent_item);
7700 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7701 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007702 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007703 btrfs_set_file_extent_encryption(leaf, ei, 0);
7704 btrfs_set_file_extent_compression(leaf, ei, 0);
7705 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7706 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7707
Chris Mason39279cc2007-06-12 06:35:45 -04007708 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007709 write_extent_buffer(leaf, symname, ptr, name_len);
7710 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007711 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007712
Chris Mason39279cc2007-06-12 06:35:45 -04007713 inode->i_op = &btrfs_symlink_inode_operations;
7714 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007715 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007716 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007717 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007718 err = btrfs_update_inode(trans, root, inode);
7719 if (err)
7720 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007721
7722out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05007723 if (!err)
7724 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007725 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007726 if (drop_inode) {
7727 inode_dec_link_count(inode);
7728 iput(inode);
7729 }
Liu Bob53d3f52012-11-14 14:34:34 +00007730 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04007731 return err;
7732}
Chris Mason16432982008-04-10 10:23:21 -04007733
Josef Bacik0af3d002010-06-21 14:48:16 -04007734static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7735 u64 start, u64 num_bytes, u64 min_size,
7736 loff_t actual_len, u64 *alloc_hint,
7737 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007738{
Josef Bacik5dc562c2012-08-17 13:14:17 -04007739 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7740 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04007741 struct btrfs_root *root = BTRFS_I(inode)->root;
7742 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007743 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007744 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007745 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007746 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007747
Josef Bacik0af3d002010-06-21 14:48:16 -04007748 if (trans)
7749 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007750 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007751 if (own_trans) {
7752 trans = btrfs_start_transaction(root, 3);
7753 if (IS_ERR(trans)) {
7754 ret = PTR_ERR(trans);
7755 break;
7756 }
Yan Zhengd899e052008-10-30 14:25:28 -04007757 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007758
Yan, Zhengefa56462010-05-16 10:49:59 -04007759 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05007760 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007761 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007762 if (own_trans)
7763 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007764 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007765 }
7766
Yan Zhengd899e052008-10-30 14:25:28 -04007767 ret = insert_reserved_file_extent(trans, inode,
7768 cur_offset, ins.objectid,
7769 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007770 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007771 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007772 if (ret) {
7773 btrfs_abort_transaction(trans, root, ret);
7774 if (own_trans)
7775 btrfs_end_transaction(trans, root);
7776 break;
7777 }
Chris Masona1ed8352009-09-11 12:27:37 -04007778 btrfs_drop_extent_cache(inode, cur_offset,
7779 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007780
Josef Bacik5dc562c2012-08-17 13:14:17 -04007781 em = alloc_extent_map();
7782 if (!em) {
7783 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
7784 &BTRFS_I(inode)->runtime_flags);
7785 goto next;
7786 }
7787
7788 em->start = cur_offset;
7789 em->orig_start = cur_offset;
7790 em->len = ins.offset;
7791 em->block_start = ins.objectid;
7792 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05007793 em->orig_block_len = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007794 em->bdev = root->fs_info->fs_devices->latest_bdev;
7795 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7796 em->generation = trans->transid;
7797
7798 while (1) {
7799 write_lock(&em_tree->lock);
7800 ret = add_extent_mapping(em_tree, em);
7801 if (!ret)
7802 list_move(&em->list,
7803 &em_tree->modified_extents);
7804 write_unlock(&em_tree->lock);
7805 if (ret != -EEXIST)
7806 break;
7807 btrfs_drop_extent_cache(inode, cur_offset,
7808 cur_offset + ins.offset - 1,
7809 0);
7810 }
7811 free_extent_map(em);
7812next:
Yan Zhengd899e052008-10-30 14:25:28 -04007813 num_bytes -= ins.offset;
7814 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007815 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007816
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007817 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04007818 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007819 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007820 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007821 (actual_len > inode->i_size) &&
7822 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007823 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007824 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007825 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007826 i_size = cur_offset;
7827 i_size_write(inode, i_size);
7828 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007829 }
7830
Yan Zhengd899e052008-10-30 14:25:28 -04007831 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007832
7833 if (ret) {
7834 btrfs_abort_transaction(trans, root, ret);
7835 if (own_trans)
7836 btrfs_end_transaction(trans, root);
7837 break;
7838 }
Yan Zhengd899e052008-10-30 14:25:28 -04007839
Josef Bacik0af3d002010-06-21 14:48:16 -04007840 if (own_trans)
7841 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007842 }
Yan Zhengd899e052008-10-30 14:25:28 -04007843 return ret;
7844}
7845
Josef Bacik0af3d002010-06-21 14:48:16 -04007846int btrfs_prealloc_file_range(struct inode *inode, int mode,
7847 u64 start, u64 num_bytes, u64 min_size,
7848 loff_t actual_len, u64 *alloc_hint)
7849{
7850 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7851 min_size, actual_len, alloc_hint,
7852 NULL);
7853}
7854
7855int btrfs_prealloc_file_range_trans(struct inode *inode,
7856 struct btrfs_trans_handle *trans, int mode,
7857 u64 start, u64 num_bytes, u64 min_size,
7858 loff_t actual_len, u64 *alloc_hint)
7859{
7860 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7861 min_size, actual_len, alloc_hint, trans);
7862}
7863
Chris Masone6dcd2d2008-07-17 12:53:50 -04007864static int btrfs_set_page_dirty(struct page *page)
7865{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007866 return __set_page_dirty_nobuffers(page);
7867}
7868
Al Viro10556cb2011-06-20 19:28:19 -04007869static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007870{
Li Zefanb83cc962010-12-20 16:04:08 +08007871 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007872 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08007873
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007874 if (mask & MAY_WRITE &&
7875 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7876 if (btrfs_root_readonly(root))
7877 return -EROFS;
7878 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7879 return -EACCES;
7880 }
Al Viro2830ba72011-06-20 19:16:29 -04007881 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05007882}
Chris Mason39279cc2007-06-12 06:35:45 -04007883
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007884static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007885 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007886 .lookup = btrfs_lookup,
7887 .create = btrfs_create,
7888 .unlink = btrfs_unlink,
7889 .link = btrfs_link,
7890 .mkdir = btrfs_mkdir,
7891 .rmdir = btrfs_rmdir,
7892 .rename = btrfs_rename,
7893 .symlink = btrfs_symlink,
7894 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007895 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007896 .setxattr = btrfs_setxattr,
7897 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007898 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007899 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007900 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007901 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007902};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007903static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007904 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007905 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007906 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007907};
Yan, Zheng76dda932009-09-21 16:00:26 -04007908
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007909static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007910 .llseek = generic_file_llseek,
7911 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007912 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007913 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007914#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007915 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007916#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007917 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007918 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007919};
7920
Chris Masond1310b22008-01-24 16:13:08 -05007921static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007922 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007923 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007924 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007925 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007926 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007927 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007928 .set_bit_hook = btrfs_set_bit_hook,
7929 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007930 .merge_extent_hook = btrfs_merge_extent_hook,
7931 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007932};
7933
Chris Mason35054392009-01-21 13:11:13 -05007934/*
7935 * btrfs doesn't support the bmap operation because swapfiles
7936 * use bmap to make a mapping of extents in the file. They assume
7937 * these extents won't change over the life of the file and they
7938 * use the bmap result to do IO directly to the drive.
7939 *
7940 * the btrfs bmap call would return logical addresses that aren't
7941 * suitable for IO and they also will change frequently as COW
7942 * operations happen. So, swapfile + btrfs == corruption.
7943 *
7944 * For now we're avoiding this by dropping bmap.
7945 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007946static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007947 .readpage = btrfs_readpage,
7948 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04007949 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007950 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04007951 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007952 .invalidatepage = btrfs_invalidatepage,
7953 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007954 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007955 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007956};
7957
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007958static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007959 .readpage = btrfs_readpage,
7960 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007961 .invalidatepage = btrfs_invalidatepage,
7962 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007963};
7964
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007965static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007966 .getattr = btrfs_getattr,
7967 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007968 .setxattr = btrfs_setxattr,
7969 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007970 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007971 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007972 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007973 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007974 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007975 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04007976};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007977static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007978 .getattr = btrfs_getattr,
7979 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007980 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007981 .setxattr = btrfs_setxattr,
7982 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007983 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007984 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007985 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007986 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04007987};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007988static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007989 .readlink = generic_readlink,
7990 .follow_link = page_follow_link_light,
7991 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007992 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05007993 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007994 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007995 .setxattr = btrfs_setxattr,
7996 .getxattr = btrfs_getxattr,
7997 .listxattr = btrfs_listxattr,
7998 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007999 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008000 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008001};
Yan, Zheng76dda932009-09-21 16:00:26 -04008002
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008003const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008004 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008005 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008006};