blob: 64d57e032b4e520978ceb2f6716a3e6f16f30fe0 [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>
Chris Mason4b4e25f2008-11-20 10:22:27 -050040#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040041#include "ctree.h"
42#include "disk-io.h"
43#include "transaction.h"
44#include "btrfs_inode.h"
45#include "ioctl.h"
46#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040047#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040048#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040049#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040050#include "tree-log.h"
Chris Masonc8b97812008-10-29 14:49:59 -040051#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050052#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050053#include "free-space-cache.h"
Chris Mason39279cc2007-06-12 06:35:45 -040054
55struct btrfs_iget_args {
56 u64 ino;
57 struct btrfs_root *root;
58};
59
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070060static const struct inode_operations btrfs_dir_inode_operations;
61static const struct inode_operations btrfs_symlink_inode_operations;
62static const struct inode_operations btrfs_dir_ro_inode_operations;
63static const struct inode_operations btrfs_special_inode_operations;
64static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070065static const struct address_space_operations btrfs_aops;
66static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070067static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050068static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040069
70static struct kmem_cache *btrfs_inode_cachep;
71struct kmem_cache *btrfs_trans_handle_cachep;
72struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040073struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050074struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040075
76#define S_SHIFT 12
77static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
78 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
79 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
80 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
81 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
82 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
83 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
84 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
85};
86
Josef Bacika41ad392011-01-31 15:30:16 -050087static int btrfs_setsize(struct inode *inode, loff_t newsize);
88static int btrfs_truncate(struct inode *inode);
Chris Masonc8b97812008-10-29 14:49:59 -040089static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
Chris Mason771ed682008-11-06 22:02:51 -050090static noinline int cow_file_range(struct inode *inode,
91 struct page *locked_page,
92 u64 start, u64 end, int *page_started,
93 unsigned long *nr_written, int unlock);
Josef Bacik7b128762008-07-24 12:17:14 -040094
Yan, Zhengf34f57a2009-11-12 09:35:27 +000095static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
96 struct inode *inode, struct inode *dir)
Jim Owens0279b4c2009-02-04 09:29:13 -050097{
98 int err;
99
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000100 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500101 if (!err)
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000102 err = btrfs_xattr_security_init(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500103 return err;
104}
105
Chris Masond352ac62008-09-29 15:18:18 -0400106/*
Chris Masonc8b97812008-10-29 14:49:59 -0400107 * this does all the hard work for inserting an inline extent into
108 * the btree. The caller should have done a btrfs_drop_extents so that
109 * no overlapping inline items exist in the btree
110 */
Chris Masond3977122009-01-05 21:25:51 -0500111static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400112 struct btrfs_root *root, struct inode *inode,
113 u64 start, size_t size, size_t compressed_size,
114 struct page **compressed_pages)
115{
116 struct btrfs_key key;
117 struct btrfs_path *path;
118 struct extent_buffer *leaf;
119 struct page *page = NULL;
120 char *kaddr;
121 unsigned long ptr;
122 struct btrfs_file_extent_item *ei;
123 int err = 0;
124 int ret;
125 size_t cur_size = size;
126 size_t datasize;
127 unsigned long offset;
Li Zefan261507a02010-12-17 14:21:50 +0800128 int compress_type = BTRFS_COMPRESS_NONE;
Chris Masonc8b97812008-10-29 14:49:59 -0400129
130 if (compressed_size && compressed_pages) {
Li Zefan261507a02010-12-17 14:21:50 +0800131 compress_type = root->fs_info->compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -0400132 cur_size = compressed_size;
133 }
134
Chris Masond3977122009-01-05 21:25:51 -0500135 path = btrfs_alloc_path();
136 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400137 return -ENOMEM;
138
Chris Masonb9473432009-03-13 11:00:37 -0400139 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400140 btrfs_set_trans_block_group(trans, inode);
141
142 key.objectid = inode->i_ino;
143 key.offset = start;
144 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400145 datasize = btrfs_file_extent_calc_inline_size(cur_size);
146
147 inode_add_bytes(inode, size);
148 ret = btrfs_insert_empty_item(trans, root, path, &key,
149 datasize);
150 BUG_ON(ret);
151 if (ret) {
152 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400153 goto fail;
154 }
155 leaf = path->nodes[0];
156 ei = btrfs_item_ptr(leaf, path->slots[0],
157 struct btrfs_file_extent_item);
158 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
159 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
160 btrfs_set_file_extent_encryption(leaf, ei, 0);
161 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
162 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
163 ptr = btrfs_file_extent_inline_start(ei);
164
Li Zefan261507a02010-12-17 14:21:50 +0800165 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400166 struct page *cpage;
167 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500168 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400169 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500170 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400171 PAGE_CACHE_SIZE);
172
Chris Masonb9473432009-03-13 11:00:37 -0400173 kaddr = kmap_atomic(cpage, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400174 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Chris Masonb9473432009-03-13 11:00:37 -0400175 kunmap_atomic(kaddr, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400176
177 i++;
178 ptr += cur_size;
179 compressed_size -= cur_size;
180 }
181 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800182 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400183 } else {
184 page = find_get_page(inode->i_mapping,
185 start >> PAGE_CACHE_SHIFT);
186 btrfs_set_file_extent_compression(leaf, ei, 0);
187 kaddr = kmap_atomic(page, KM_USER0);
188 offset = start & (PAGE_CACHE_SIZE - 1);
189 write_extent_buffer(leaf, kaddr + offset, ptr, size);
190 kunmap_atomic(kaddr, KM_USER0);
191 page_cache_release(page);
192 }
193 btrfs_mark_buffer_dirty(leaf);
194 btrfs_free_path(path);
195
Yan, Zhengc2167752009-11-12 09:34:21 +0000196 /*
197 * we're an inline extent, so nobody can
198 * extend the file past i_size without locking
199 * a page we already have locked.
200 *
201 * We must do any isize and inode updates
202 * before we unlock the pages. Otherwise we
203 * could end up racing with unlink.
204 */
Chris Masonc8b97812008-10-29 14:49:59 -0400205 BTRFS_I(inode)->disk_i_size = inode->i_size;
206 btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000207
Chris Masonc8b97812008-10-29 14:49:59 -0400208 return 0;
209fail:
210 btrfs_free_path(path);
211 return err;
212}
213
214
215/*
216 * conditionally insert an inline extent into the file. This
217 * does the checks required to make sure the data is small enough
218 * to fit as an inline extent.
219 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400220static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400221 struct btrfs_root *root,
222 struct inode *inode, u64 start, u64 end,
223 size_t compressed_size,
224 struct page **compressed_pages)
225{
226 u64 isize = i_size_read(inode);
227 u64 actual_end = min(end + 1, isize);
228 u64 inline_len = actual_end - start;
229 u64 aligned_end = (end + root->sectorsize - 1) &
230 ~((u64)root->sectorsize - 1);
231 u64 hint_byte;
232 u64 data_len = inline_len;
233 int ret;
234
235 if (compressed_size)
236 data_len = compressed_size;
237
238 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400239 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400240 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
241 (!compressed_size &&
242 (actual_end & (root->sectorsize - 1)) == 0) ||
243 end + 1 < isize ||
244 data_len > root->fs_info->max_inline) {
245 return 1;
246 }
247
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000248 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
Chris Masona1ed8352009-09-11 12:27:37 -0400249 &hint_byte, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400250 BUG_ON(ret);
251
252 if (isize > actual_end)
253 inline_len = min_t(u64, isize, actual_end);
254 ret = insert_inline_extent(trans, root, inode, start,
255 inline_len, compressed_size,
256 compressed_pages);
257 BUG_ON(ret);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400258 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400259 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400260 return 0;
261}
262
Chris Mason771ed682008-11-06 22:02:51 -0500263struct async_extent {
264 u64 start;
265 u64 ram_size;
266 u64 compressed_size;
267 struct page **pages;
268 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800269 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500270 struct list_head list;
271};
272
273struct async_cow {
274 struct inode *inode;
275 struct btrfs_root *root;
276 struct page *locked_page;
277 u64 start;
278 u64 end;
279 struct list_head extents;
280 struct btrfs_work work;
281};
282
283static noinline int add_async_extent(struct async_cow *cow,
284 u64 start, u64 ram_size,
285 u64 compressed_size,
286 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800287 unsigned long nr_pages,
288 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500289{
290 struct async_extent *async_extent;
291
292 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
293 async_extent->start = start;
294 async_extent->ram_size = ram_size;
295 async_extent->compressed_size = compressed_size;
296 async_extent->pages = pages;
297 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800298 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500299 list_add_tail(&async_extent->list, &cow->extents);
300 return 0;
301}
302
Chris Masonc8b97812008-10-29 14:49:59 -0400303/*
Chris Mason771ed682008-11-06 22:02:51 -0500304 * we create compressed extents in two phases. The first
305 * phase compresses a range of pages that have already been
306 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400307 *
Chris Mason771ed682008-11-06 22:02:51 -0500308 * This is done inside an ordered work queue, and the compression
309 * is spread across many cpus. The actual IO submission is step
310 * two, and the ordered work queue takes care of making sure that
311 * happens in the same order things were put onto the queue by
312 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400313 *
Chris Mason771ed682008-11-06 22:02:51 -0500314 * If this code finds it can't get good compression, it puts an
315 * entry onto the work queue to write the uncompressed bytes. This
316 * makes sure that both compressed inodes and uncompressed inodes
317 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400318 */
Chris Mason771ed682008-11-06 22:02:51 -0500319static noinline int compress_file_range(struct inode *inode,
320 struct page *locked_page,
321 u64 start, u64 end,
322 struct async_cow *async_cow,
323 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400324{
325 struct btrfs_root *root = BTRFS_I(inode)->root;
326 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400327 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400328 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400329 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500330 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400331 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400332 struct page **pages = NULL;
333 unsigned long nr_pages;
334 unsigned long nr_pages_ret = 0;
335 unsigned long total_compressed = 0;
336 unsigned long total_in = 0;
337 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500338 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400339 int i;
340 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800341 int compress_type = root->fs_info->compress_type;
Chris Masonb888db22007-08-27 16:49:44 -0400342
Chris Mason42dc7ba2008-12-15 11:44:56 -0500343 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400344again:
345 will_compress = 0;
346 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
347 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
348
Chris Masonf03d9301f2009-02-04 09:31:06 -0500349 /*
350 * we don't want to send crud past the end of i_size through
351 * compression, that's just a waste of CPU time. So, if the
352 * end of the file is before the start of our current
353 * requested range of bytes, we bail out to the uncompressed
354 * cleanup code that can deal with all of this.
355 *
356 * It isn't really the fastest way to fix things, but this is a
357 * very uncommon corner.
358 */
359 if (actual_end <= start)
360 goto cleanup_and_bail_uncompressed;
361
Chris Masonc8b97812008-10-29 14:49:59 -0400362 total_compressed = actual_end - start;
363
364 /* we want to make sure that amount of ram required to uncompress
365 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500366 * of a compressed extent to 128k. This is a crucial number
367 * because it also controls how easily we can spread reads across
368 * cpus for decompression.
369 *
370 * We also want to make sure the amount of IO required to do
371 * a random read is reasonably small, so we limit the size of
372 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400373 */
374 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400375 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500376 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400377 total_in = 0;
378 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400379
Chris Mason771ed682008-11-06 22:02:51 -0500380 /*
381 * we do compression for mount -o compress and when the
382 * inode has not been flagged as nocompress. This flag can
383 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400384 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200385 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500386 (btrfs_test_opt(root, COMPRESS) ||
387 (BTRFS_I(inode)->force_compress))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400388 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400389 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400390
Li Zefan261507a02010-12-17 14:21:50 +0800391 if (BTRFS_I(inode)->force_compress)
392 compress_type = BTRFS_I(inode)->force_compress;
393
394 ret = btrfs_compress_pages(compress_type,
395 inode->i_mapping, start,
396 total_compressed, pages,
397 nr_pages, &nr_pages_ret,
398 &total_in,
399 &total_compressed,
400 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400401
402 if (!ret) {
403 unsigned long offset = total_compressed &
404 (PAGE_CACHE_SIZE - 1);
405 struct page *page = pages[nr_pages_ret - 1];
406 char *kaddr;
407
408 /* zero the tail end of the last page, we might be
409 * sending it down to disk
410 */
411 if (offset) {
412 kaddr = kmap_atomic(page, KM_USER0);
413 memset(kaddr + offset, 0,
414 PAGE_CACHE_SIZE - offset);
415 kunmap_atomic(kaddr, KM_USER0);
416 }
417 will_compress = 1;
418 }
419 }
420 if (start == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500421 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000422 BUG_ON(IS_ERR(trans));
Chris Mason771ed682008-11-06 22:02:51 -0500423 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400424 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500425
Chris Masonc8b97812008-10-29 14:49:59 -0400426 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500427 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400428 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500429 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400430 */
431 ret = cow_file_range_inline(trans, root, inode,
432 start, end, 0, NULL);
433 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500434 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400435 ret = cow_file_range_inline(trans, root, inode,
436 start, end,
437 total_compressed, pages);
438 }
439 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500440 /*
441 * inline extent creation worked, we don't need
442 * to create any more async work items. Unlock
443 * and free up our temp pages.
444 */
Chris Masonc8b97812008-10-29 14:49:59 -0400445 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400446 &BTRFS_I(inode)->io_tree,
447 start, end, NULL,
448 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400449 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400450 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000451
452 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400453 goto free_pages_out;
454 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000455 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400456 }
457
458 if (will_compress) {
459 /*
460 * we aren't doing an inline extent round the compressed size
461 * up to a block size boundary so the allocator does sane
462 * things
463 */
464 total_compressed = (total_compressed + blocksize - 1) &
465 ~(blocksize - 1);
466
467 /*
468 * one last check to make sure the compression is really a
469 * win, compare the page count read with the blocks on disk
470 */
471 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
472 ~(PAGE_CACHE_SIZE - 1);
473 if (total_compressed >= total_in) {
474 will_compress = 0;
475 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400476 num_bytes = total_in;
477 }
478 }
479 if (!will_compress && pages) {
480 /*
481 * the compression code ran but failed to make things smaller,
482 * free any pages it allocated and our page pointer array
483 */
484 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400485 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400486 page_cache_release(pages[i]);
487 }
488 kfree(pages);
489 pages = NULL;
490 total_compressed = 0;
491 nr_pages_ret = 0;
492
493 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500494 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
495 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500496 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500497 }
Chris Masonc8b97812008-10-29 14:49:59 -0400498 }
Chris Mason771ed682008-11-06 22:02:51 -0500499 if (will_compress) {
500 *num_added += 1;
501
502 /* the async work queues will take care of doing actual
503 * allocation on disk for these compressed pages,
504 * and will submit them to the elevator.
505 */
506 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800507 total_compressed, pages, nr_pages_ret,
508 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500509
Yan, Zheng24ae6362010-12-06 07:02:36 +0000510 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500511 start += num_bytes;
512 pages = NULL;
513 cond_resched();
514 goto again;
515 }
516 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500517cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500518 /*
519 * No compression, but we still need to write the pages in
520 * the file we've been given so far. redirty the locked
521 * page if it corresponds to our extent and set things up
522 * for the async work queue to run cow_file_range to do
523 * the normal delalloc dance
524 */
525 if (page_offset(locked_page) >= start &&
526 page_offset(locked_page) <= end) {
527 __set_page_dirty_nobuffers(locked_page);
528 /* unlocked later on in the async handlers */
529 }
Li Zefan261507a02010-12-17 14:21:50 +0800530 add_async_extent(async_cow, start, end - start + 1,
531 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500532 *num_added += 1;
533 }
534
535out:
536 return 0;
537
538free_pages_out:
539 for (i = 0; i < nr_pages_ret; i++) {
540 WARN_ON(pages[i]->mapping);
541 page_cache_release(pages[i]);
542 }
Chris Masond3977122009-01-05 21:25:51 -0500543 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500544
545 goto out;
546}
547
548/*
549 * phase two of compressed writeback. This is the ordered portion
550 * of the code, which only gets called in the order the work was
551 * queued. We walk all the async extents created by compress_file_range
552 * and send them down to the disk.
553 */
554static noinline int submit_compressed_extents(struct inode *inode,
555 struct async_cow *async_cow)
556{
557 struct async_extent *async_extent;
558 u64 alloc_hint = 0;
559 struct btrfs_trans_handle *trans;
560 struct btrfs_key ins;
561 struct extent_map *em;
562 struct btrfs_root *root = BTRFS_I(inode)->root;
563 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
564 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500565 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500566
567 if (list_empty(&async_cow->extents))
568 return 0;
569
Chris Mason771ed682008-11-06 22:02:51 -0500570
Chris Masond3977122009-01-05 21:25:51 -0500571 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500572 async_extent = list_entry(async_cow->extents.next,
573 struct async_extent, list);
574 list_del(&async_extent->list);
575
576 io_tree = &BTRFS_I(inode)->io_tree;
577
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500578retry:
Chris Mason771ed682008-11-06 22:02:51 -0500579 /* did the compression code fall back to uncompressed IO? */
580 if (!async_extent->pages) {
581 int page_started = 0;
582 unsigned long nr_written = 0;
583
584 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000585 async_extent->start +
586 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500587
588 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500589 ret = cow_file_range(inode, async_cow->locked_page,
590 async_extent->start,
591 async_extent->start +
592 async_extent->ram_size - 1,
593 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500594
595 /*
596 * if page_started, cow_file_range inserted an
597 * inline extent and took care of all the unlocking
598 * and IO for us. Otherwise, we need to submit
599 * all those pages down to the drive.
600 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500601 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500602 extent_write_locked_range(io_tree,
603 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500604 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500605 async_extent->ram_size - 1,
606 btrfs_get_extent,
607 WB_SYNC_ALL);
608 kfree(async_extent);
609 cond_resched();
610 continue;
611 }
612
613 lock_extent(io_tree, async_extent->start,
614 async_extent->start + async_extent->ram_size - 1,
615 GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500616
Yan, Zhengc2167752009-11-12 09:34:21 +0000617 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000618 BUG_ON(IS_ERR(trans));
Chris Mason771ed682008-11-06 22:02:51 -0500619 ret = btrfs_reserve_extent(trans, root,
620 async_extent->compressed_size,
621 async_extent->compressed_size,
622 0, alloc_hint,
623 (u64)-1, &ins, 1);
Yan, Zhengc2167752009-11-12 09:34:21 +0000624 btrfs_end_transaction(trans, root);
625
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500626 if (ret) {
627 int i;
628 for (i = 0; i < async_extent->nr_pages; i++) {
629 WARN_ON(async_extent->pages[i]->mapping);
630 page_cache_release(async_extent->pages[i]);
631 }
632 kfree(async_extent->pages);
633 async_extent->nr_pages = 0;
634 async_extent->pages = NULL;
635 unlock_extent(io_tree, async_extent->start,
636 async_extent->start +
637 async_extent->ram_size - 1, GFP_NOFS);
638 goto retry;
639 }
640
Yan, Zhengc2167752009-11-12 09:34:21 +0000641 /*
642 * here we're doing allocation and writeback of the
643 * compressed pages
644 */
645 btrfs_drop_extent_cache(inode, async_extent->start,
646 async_extent->start +
647 async_extent->ram_size - 1, 0);
648
Chris Mason771ed682008-11-06 22:02:51 -0500649 em = alloc_extent_map(GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +0000650 BUG_ON(!em);
Chris Mason771ed682008-11-06 22:02:51 -0500651 em->start = async_extent->start;
652 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500653 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500654
655 em->block_start = ins.objectid;
656 em->block_len = ins.offset;
657 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800658 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500659 set_bit(EXTENT_FLAG_PINNED, &em->flags);
660 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
661
Chris Masond3977122009-01-05 21:25:51 -0500662 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400663 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500664 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400665 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500666 if (ret != -EEXIST) {
667 free_extent_map(em);
668 break;
669 }
670 btrfs_drop_extent_cache(inode, async_extent->start,
671 async_extent->start +
672 async_extent->ram_size - 1, 0);
673 }
674
Li Zefan261507a02010-12-17 14:21:50 +0800675 ret = btrfs_add_ordered_extent_compress(inode,
676 async_extent->start,
677 ins.objectid,
678 async_extent->ram_size,
679 ins.offset,
680 BTRFS_ORDERED_COMPRESSED,
681 async_extent->compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500682 BUG_ON(ret);
683
Chris Mason771ed682008-11-06 22:02:51 -0500684 /*
685 * clear dirty, set writeback and unlock the pages.
686 */
687 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400688 &BTRFS_I(inode)->io_tree,
689 async_extent->start,
690 async_extent->start +
691 async_extent->ram_size - 1,
692 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
693 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400694 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400695 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500696
697 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500698 async_extent->start,
699 async_extent->ram_size,
700 ins.objectid,
701 ins.offset, async_extent->pages,
702 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500703
704 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -0500705 alloc_hint = ins.objectid + ins.offset;
706 kfree(async_extent);
707 cond_resched();
708 }
709
Chris Mason771ed682008-11-06 22:02:51 -0500710 return 0;
711}
712
Josef Bacik4b46fce2010-05-23 11:00:55 -0400713static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
714 u64 num_bytes)
715{
716 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
717 struct extent_map *em;
718 u64 alloc_hint = 0;
719
720 read_lock(&em_tree->lock);
721 em = search_extent_mapping(em_tree, start, num_bytes);
722 if (em) {
723 /*
724 * if block start isn't an actual block number then find the
725 * first block in this inode and use that as a hint. If that
726 * block is also bogus then just don't worry about it.
727 */
728 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
729 free_extent_map(em);
730 em = search_extent_mapping(em_tree, 0, 0);
731 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
732 alloc_hint = em->block_start;
733 if (em)
734 free_extent_map(em);
735 } else {
736 alloc_hint = em->block_start;
737 free_extent_map(em);
738 }
739 }
740 read_unlock(&em_tree->lock);
741
742 return alloc_hint;
743}
744
Chris Mason771ed682008-11-06 22:02:51 -0500745/*
746 * when extent_io.c finds a delayed allocation range in the file,
747 * the call backs end up in this code. The basic idea is to
748 * allocate extents on disk for the range, and create ordered data structs
749 * in ram to track those extents.
750 *
751 * locked_page is the page that writepage had locked already. We use
752 * it to make sure we don't do extra locks or unlocks.
753 *
754 * *page_started is set to one if we unlock locked_page and do everything
755 * required to start IO on it. It may be clean and already done with
756 * IO when we return.
757 */
758static noinline int cow_file_range(struct inode *inode,
759 struct page *locked_page,
760 u64 start, u64 end, int *page_started,
761 unsigned long *nr_written,
762 int unlock)
763{
764 struct btrfs_root *root = BTRFS_I(inode)->root;
765 struct btrfs_trans_handle *trans;
766 u64 alloc_hint = 0;
767 u64 num_bytes;
768 unsigned long ram_size;
769 u64 disk_num_bytes;
770 u64 cur_alloc_size;
771 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500772 struct btrfs_key ins;
773 struct extent_map *em;
774 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
775 int ret = 0;
776
Josef Bacik0cb59c92010-07-02 12:14:14 -0400777 BUG_ON(root == root->fs_info->tree_root);
Chris Mason771ed682008-11-06 22:02:51 -0500778 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000779 BUG_ON(IS_ERR(trans));
Chris Mason771ed682008-11-06 22:02:51 -0500780 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400781 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500782
Chris Mason771ed682008-11-06 22:02:51 -0500783 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
784 num_bytes = max(blocksize, num_bytes);
785 disk_num_bytes = num_bytes;
786 ret = 0;
787
788 if (start == 0) {
789 /* lets try to make an inline extent */
790 ret = cow_file_range_inline(trans, root, inode,
791 start, end, 0, NULL);
792 if (ret == 0) {
793 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400794 &BTRFS_I(inode)->io_tree,
795 start, end, NULL,
796 EXTENT_CLEAR_UNLOCK_PAGE |
797 EXTENT_CLEAR_UNLOCK |
798 EXTENT_CLEAR_DELALLOC |
799 EXTENT_CLEAR_DIRTY |
800 EXTENT_SET_WRITEBACK |
801 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000802
Chris Mason771ed682008-11-06 22:02:51 -0500803 *nr_written = *nr_written +
804 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
805 *page_started = 1;
806 ret = 0;
807 goto out;
808 }
809 }
Chris Masonc8b97812008-10-29 14:49:59 -0400810
811 BUG_ON(disk_num_bytes >
812 btrfs_super_total_bytes(&root->fs_info->super_copy));
813
Josef Bacik4b46fce2010-05-23 11:00:55 -0400814 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400815 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400816
Chris Masond3977122009-01-05 21:25:51 -0500817 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400818 unsigned long op;
819
Josef Bacik287a0ab2010-03-19 18:07:23 +0000820 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400821 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500822 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400823 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500824 BUG_ON(ret);
825
Chris Masone6dcd2d2008-07-17 12:53:50 -0400826 em = alloc_extent_map(GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +0000827 BUG_ON(!em);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400828 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500829 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500830 ram_size = ins.offset;
831 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400832
Chris Masone6dcd2d2008-07-17 12:53:50 -0400833 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400834 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400835 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400836 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400837
Chris Masond3977122009-01-05 21:25:51 -0500838 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400839 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400840 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400841 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400842 if (ret != -EEXIST) {
843 free_extent_map(em);
844 break;
845 }
846 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400847 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400848 }
849
Chris Mason98d20f62008-04-14 09:46:10 -0400850 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400851 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500852 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400853 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400854
Yan Zheng17d217f2008-12-12 10:03:38 -0500855 if (root->root_key.objectid ==
856 BTRFS_DATA_RELOC_TREE_OBJECTID) {
857 ret = btrfs_reloc_clone_csums(inode, start,
858 cur_alloc_size);
859 BUG_ON(ret);
860 }
861
Chris Masond3977122009-01-05 21:25:51 -0500862 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400863 break;
Chris Masond3977122009-01-05 21:25:51 -0500864
Chris Masonc8b97812008-10-29 14:49:59 -0400865 /* we're not doing compressed IO, don't unlock the first
866 * page (which the caller expects to stay locked), don't
867 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400868 *
869 * Do set the Private2 bit so we know this page was properly
870 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400871 */
Chris Masona791e352009-10-08 11:27:10 -0400872 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
873 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
874 EXTENT_SET_PRIVATE2;
875
Chris Masonc8b97812008-10-29 14:49:59 -0400876 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
877 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400878 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400879 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500880 num_bytes -= cur_alloc_size;
881 alloc_hint = ins.objectid + ins.offset;
882 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400883 }
Chris Masonb888db22007-08-27 16:49:44 -0400884out:
Chris Mason771ed682008-11-06 22:02:51 -0500885 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400886 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400887
Chris Masonbe20aa92007-12-17 20:14:01 -0500888 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500889}
Chris Masonc8b97812008-10-29 14:49:59 -0400890
Chris Mason771ed682008-11-06 22:02:51 -0500891/*
892 * work queue call back to started compression on a file and pages
893 */
894static noinline void async_cow_start(struct btrfs_work *work)
895{
896 struct async_cow *async_cow;
897 int num_added = 0;
898 async_cow = container_of(work, struct async_cow, work);
899
900 compress_file_range(async_cow->inode, async_cow->locked_page,
901 async_cow->start, async_cow->end, async_cow,
902 &num_added);
903 if (num_added == 0)
904 async_cow->inode = NULL;
905}
906
907/*
908 * work queue call back to submit previously compressed pages
909 */
910static noinline void async_cow_submit(struct btrfs_work *work)
911{
912 struct async_cow *async_cow;
913 struct btrfs_root *root;
914 unsigned long nr_pages;
915
916 async_cow = container_of(work, struct async_cow, work);
917
918 root = async_cow->root;
919 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
920 PAGE_CACHE_SHIFT;
921
922 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
923
924 if (atomic_read(&root->fs_info->async_delalloc_pages) <
925 5 * 1042 * 1024 &&
926 waitqueue_active(&root->fs_info->async_submit_wait))
927 wake_up(&root->fs_info->async_submit_wait);
928
Chris Masond3977122009-01-05 21:25:51 -0500929 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500930 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500931}
Chris Masonc8b97812008-10-29 14:49:59 -0400932
Chris Mason771ed682008-11-06 22:02:51 -0500933static noinline void async_cow_free(struct btrfs_work *work)
934{
935 struct async_cow *async_cow;
936 async_cow = container_of(work, struct async_cow, work);
937 kfree(async_cow);
938}
939
940static int cow_file_range_async(struct inode *inode, struct page *locked_page,
941 u64 start, u64 end, int *page_started,
942 unsigned long *nr_written)
943{
944 struct async_cow *async_cow;
945 struct btrfs_root *root = BTRFS_I(inode)->root;
946 unsigned long nr_pages;
947 u64 cur_end;
948 int limit = 10 * 1024 * 1042;
949
Chris Masona3429ab2009-10-08 12:30:20 -0400950 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
951 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500952 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500953 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
954 async_cow->inode = inode;
955 async_cow->root = root;
956 async_cow->locked_page = locked_page;
957 async_cow->start = start;
958
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200959 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -0500960 cur_end = end;
961 else
962 cur_end = min(end, start + 512 * 1024 - 1);
963
964 async_cow->end = cur_end;
965 INIT_LIST_HEAD(&async_cow->extents);
966
967 async_cow->work.func = async_cow_start;
968 async_cow->work.ordered_func = async_cow_submit;
969 async_cow->work.ordered_free = async_cow_free;
970 async_cow->work.flags = 0;
971
Chris Mason771ed682008-11-06 22:02:51 -0500972 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
973 PAGE_CACHE_SHIFT;
974 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
975
976 btrfs_queue_worker(&root->fs_info->delalloc_workers,
977 &async_cow->work);
978
979 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
980 wait_event(root->fs_info->async_submit_wait,
981 (atomic_read(&root->fs_info->async_delalloc_pages) <
982 limit));
983 }
984
Chris Masond3977122009-01-05 21:25:51 -0500985 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500986 atomic_read(&root->fs_info->async_delalloc_pages)) {
987 wait_event(root->fs_info->async_submit_wait,
988 (atomic_read(&root->fs_info->async_delalloc_pages) ==
989 0));
990 }
991
992 *nr_written += nr_pages;
993 start = cur_end + 1;
994 }
995 *page_started = 1;
996 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500997}
998
Chris Masond3977122009-01-05 21:25:51 -0500999static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001000 u64 bytenr, u64 num_bytes)
1001{
1002 int ret;
1003 struct btrfs_ordered_sum *sums;
1004 LIST_HEAD(list);
1005
Yan Zheng07d400a2009-01-06 11:42:00 -05001006 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
1007 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -05001008 if (ret == 0 && list_empty(&list))
1009 return 0;
1010
1011 while (!list_empty(&list)) {
1012 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1013 list_del(&sums->list);
1014 kfree(sums);
1015 }
1016 return 1;
1017}
1018
Chris Masond352ac62008-09-29 15:18:18 -04001019/*
1020 * when nowcow writeback call back. This checks for snapshots or COW copies
1021 * of the extents that exist in the file, and COWs the file as required.
1022 *
1023 * If no cow copies or snapshots exist, we write directly to the existing
1024 * blocks on disk
1025 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001026static noinline int run_delalloc_nocow(struct inode *inode,
1027 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001028 u64 start, u64 end, int *page_started, int force,
1029 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001030{
Chris Masonbe20aa92007-12-17 20:14:01 -05001031 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001032 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001033 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001034 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001035 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001036 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001037 u64 cow_start;
1038 u64 cur_offset;
1039 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001040 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001041 u64 disk_bytenr;
1042 u64 num_bytes;
1043 int extent_type;
1044 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001045 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001046 int nocow;
1047 int check_prev = 1;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001048 bool nolock = false;
Chris Masonbe20aa92007-12-17 20:14:01 -05001049
1050 path = btrfs_alloc_path();
1051 BUG_ON(!path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001052 if (root == root->fs_info->tree_root) {
1053 nolock = true;
1054 trans = btrfs_join_transaction_nolock(root, 1);
1055 } else {
1056 trans = btrfs_join_transaction(root, 1);
1057 }
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001058 BUG_ON(IS_ERR(trans));
Chris Masonbe20aa92007-12-17 20:14:01 -05001059
Yan Zheng80ff3852008-10-30 14:20:02 -04001060 cow_start = (u64)-1;
1061 cur_offset = start;
1062 while (1) {
1063 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1064 cur_offset, 0);
1065 BUG_ON(ret < 0);
1066 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1067 leaf = path->nodes[0];
1068 btrfs_item_key_to_cpu(leaf, &found_key,
1069 path->slots[0] - 1);
1070 if (found_key.objectid == inode->i_ino &&
1071 found_key.type == BTRFS_EXTENT_DATA_KEY)
1072 path->slots[0]--;
1073 }
1074 check_prev = 0;
1075next_slot:
1076 leaf = path->nodes[0];
1077 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1078 ret = btrfs_next_leaf(root, path);
1079 if (ret < 0)
1080 BUG_ON(1);
1081 if (ret > 0)
1082 break;
1083 leaf = path->nodes[0];
1084 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001085
Yan Zheng80ff3852008-10-30 14:20:02 -04001086 nocow = 0;
1087 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001088 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001089 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001090
Yan Zheng80ff3852008-10-30 14:20:02 -04001091 if (found_key.objectid > inode->i_ino ||
1092 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1093 found_key.offset > end)
1094 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001095
Yan Zheng80ff3852008-10-30 14:20:02 -04001096 if (found_key.offset > cur_offset) {
1097 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001098 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001099 goto out_check;
1100 }
Chris Masonc31f8832008-01-08 15:46:31 -05001101
Yan Zheng80ff3852008-10-30 14:20:02 -04001102 fi = btrfs_item_ptr(leaf, path->slots[0],
1103 struct btrfs_file_extent_item);
1104 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001105
Yan Zhengd899e052008-10-30 14:25:28 -04001106 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1107 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001108 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001109 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001110 extent_end = found_key.offset +
1111 btrfs_file_extent_num_bytes(leaf, fi);
1112 if (extent_end <= start) {
1113 path->slots[0]++;
1114 goto next_slot;
1115 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001116 if (disk_bytenr == 0)
1117 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001118 if (btrfs_file_extent_compression(leaf, fi) ||
1119 btrfs_file_extent_encryption(leaf, fi) ||
1120 btrfs_file_extent_other_encoding(leaf, fi))
1121 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001122 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1123 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001124 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001125 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001126 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001127 found_key.offset -
1128 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001129 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001130 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001131 disk_bytenr += cur_offset - found_key.offset;
1132 num_bytes = min(end + 1, extent_end) - cur_offset;
1133 /*
1134 * force cow if csum exists in the range.
1135 * this ensure that csum for a given extent are
1136 * either valid or do not exist.
1137 */
1138 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1139 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001140 nocow = 1;
1141 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1142 extent_end = found_key.offset +
1143 btrfs_file_extent_inline_len(leaf, fi);
1144 extent_end = ALIGN(extent_end, root->sectorsize);
1145 } else {
1146 BUG_ON(1);
1147 }
1148out_check:
1149 if (extent_end <= start) {
1150 path->slots[0]++;
1151 goto next_slot;
1152 }
1153 if (!nocow) {
1154 if (cow_start == (u64)-1)
1155 cow_start = cur_offset;
1156 cur_offset = extent_end;
1157 if (cur_offset > end)
1158 break;
1159 path->slots[0]++;
1160 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001161 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001162
Yan Zheng7ea394f2008-08-05 13:05:02 -04001163 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001164 if (cow_start != (u64)-1) {
1165 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001166 found_key.offset - 1, page_started,
1167 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001168 BUG_ON(ret);
1169 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001170 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001171
Yan Zhengd899e052008-10-30 14:25:28 -04001172 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1173 struct extent_map *em;
1174 struct extent_map_tree *em_tree;
1175 em_tree = &BTRFS_I(inode)->extent_tree;
1176 em = alloc_extent_map(GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +00001177 BUG_ON(!em);
Yan Zhengd899e052008-10-30 14:25:28 -04001178 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001179 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001180 em->len = num_bytes;
1181 em->block_len = num_bytes;
1182 em->block_start = disk_bytenr;
1183 em->bdev = root->fs_info->fs_devices->latest_bdev;
1184 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1185 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001186 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001187 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001188 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001189 if (ret != -EEXIST) {
1190 free_extent_map(em);
1191 break;
1192 }
1193 btrfs_drop_extent_cache(inode, em->start,
1194 em->start + em->len - 1, 0);
1195 }
1196 type = BTRFS_ORDERED_PREALLOC;
1197 } else {
1198 type = BTRFS_ORDERED_NOCOW;
1199 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001200
1201 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001202 num_bytes, num_bytes, type);
1203 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001204
Yan, Zhengefa56462010-05-16 10:49:59 -04001205 if (root->root_key.objectid ==
1206 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1207 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1208 num_bytes);
1209 BUG_ON(ret);
1210 }
1211
Yan Zhengd899e052008-10-30 14:25:28 -04001212 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001213 cur_offset, cur_offset + num_bytes - 1,
1214 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1215 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1216 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001217 cur_offset = extent_end;
1218 if (cur_offset > end)
1219 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001220 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001221 btrfs_release_path(root, path);
1222
1223 if (cur_offset <= end && cow_start == (u64)-1)
1224 cow_start = cur_offset;
1225 if (cow_start != (u64)-1) {
1226 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001227 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001228 BUG_ON(ret);
1229 }
1230
Josef Bacik0cb59c92010-07-02 12:14:14 -04001231 if (nolock) {
1232 ret = btrfs_end_transaction_nolock(trans, root);
1233 BUG_ON(ret);
1234 } else {
1235 ret = btrfs_end_transaction(trans, root);
1236 BUG_ON(ret);
1237 }
Yan Zheng7ea394f2008-08-05 13:05:02 -04001238 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001239 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001240}
1241
Chris Masond352ac62008-09-29 15:18:18 -04001242/*
1243 * extent_io.c call back to do delayed allocation processing
1244 */
Chris Masonc8b97812008-10-29 14:49:59 -04001245static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001246 u64 start, u64 end, int *page_started,
1247 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001248{
Chris Masonbe20aa92007-12-17 20:14:01 -05001249 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001250 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001251
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001252 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001253 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001254 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001255 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001256 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001257 page_started, 0, nr_written);
Chris Mason1e701a32010-03-11 09:42:04 -05001258 else if (!btrfs_test_opt(root, COMPRESS) &&
1259 !(BTRFS_I(inode)->force_compress))
Chris Mason7f366cf2009-03-12 20:12:45 -04001260 ret = cow_file_range(inode, locked_page, start, end,
1261 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001262 else
Chris Mason771ed682008-11-06 22:02:51 -05001263 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001264 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001265 return ret;
1266}
1267
Josef Bacik9ed74f22009-09-11 16:12:44 -04001268static int btrfs_split_extent_hook(struct inode *inode,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001269 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001270{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001271 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001272 if (!(orig->state & EXTENT_DELALLOC))
1273 return 0;
1274
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001275 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001276 return 0;
1277}
1278
1279/*
1280 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1281 * extents so we can keep track of new extents that are just merged onto old
1282 * extents, such as when we are doing sequential writes, so we can properly
1283 * account for the metadata space we'll need.
1284 */
1285static int btrfs_merge_extent_hook(struct inode *inode,
1286 struct extent_state *new,
1287 struct extent_state *other)
1288{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001289 /* not delalloc, ignore it */
1290 if (!(other->state & EXTENT_DELALLOC))
1291 return 0;
1292
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001293 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001294 return 0;
1295}
1296
Chris Masond352ac62008-09-29 15:18:18 -04001297/*
1298 * extent_io.c set_bit_hook, used to track delayed allocation
1299 * bytes in this file, and to maintain the list of inodes that
1300 * have pending delalloc work to be done.
1301 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001302static int btrfs_set_bit_hook(struct inode *inode,
1303 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001304{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001305
Chris Mason75eff682008-12-15 15:54:40 -05001306 /*
1307 * set_bit and clear bit hooks normally require _irqsave/restore
1308 * but in this case, we are only testeing for the DELALLOC
1309 * bit, which is only set or cleared with irqs on
1310 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001311 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001312 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001313 u64 len = state->end + 1 - state->start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001314 int do_list = (root->root_key.objectid !=
1315 BTRFS_ROOT_TREE_OBJECTID);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001316
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001317 if (*bits & EXTENT_FIRST_DELALLOC)
1318 *bits &= ~EXTENT_FIRST_DELALLOC;
1319 else
1320 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001321
Chris Mason75eff682008-12-15 15:54:40 -05001322 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001323 BTRFS_I(inode)->delalloc_bytes += len;
1324 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001325 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001326 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1327 &root->fs_info->delalloc_inodes);
1328 }
Chris Mason75eff682008-12-15 15:54:40 -05001329 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001330 }
1331 return 0;
1332}
1333
Chris Masond352ac62008-09-29 15:18:18 -04001334/*
1335 * extent_io.c clear_bit_hook, see set_bit_hook for why
1336 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001337static int btrfs_clear_bit_hook(struct inode *inode,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001338 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001339{
Chris Mason75eff682008-12-15 15:54:40 -05001340 /*
1341 * set_bit and clear bit hooks normally require _irqsave/restore
1342 * but in this case, we are only testeing for the DELALLOC
1343 * bit, which is only set or cleared with irqs on
1344 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001345 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001346 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001347 u64 len = state->end + 1 - state->start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001348 int do_list = (root->root_key.objectid !=
1349 BTRFS_ROOT_TREE_OBJECTID);
Chris Masonbcbfce82008-04-22 13:26:47 -04001350
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001351 if (*bits & EXTENT_FIRST_DELALLOC)
1352 *bits &= ~EXTENT_FIRST_DELALLOC;
1353 else if (!(*bits & EXTENT_DO_ACCOUNTING))
1354 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
1355
1356 if (*bits & EXTENT_DO_ACCOUNTING)
1357 btrfs_delalloc_release_metadata(inode, len);
1358
Josef Bacik0cb59c92010-07-02 12:14:14 -04001359 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1360 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001361 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001362
Chris Mason75eff682008-12-15 15:54:40 -05001363 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001364 root->fs_info->delalloc_bytes -= len;
1365 BTRFS_I(inode)->delalloc_bytes -= len;
1366
Josef Bacik0cb59c92010-07-02 12:14:14 -04001367 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001368 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1369 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1370 }
Chris Mason75eff682008-12-15 15:54:40 -05001371 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001372 }
1373 return 0;
1374}
1375
Chris Masond352ac62008-09-29 15:18:18 -04001376/*
1377 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1378 * we don't create bios that span stripes or chunks
1379 */
Chris Mason239b14b2008-03-24 15:02:07 -04001380int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001381 size_t size, struct bio *bio,
1382 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001383{
1384 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1385 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001386 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001387 u64 length = 0;
1388 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001389 int ret;
1390
Chris Mason771ed682008-11-06 22:02:51 -05001391 if (bio_flags & EXTENT_BIO_COMPRESSED)
1392 return 0;
1393
Chris Masonf2d8d742008-04-21 10:03:05 -04001394 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001395 map_tree = &root->fs_info->mapping_tree;
1396 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001397 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001398 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001399
Chris Masond3977122009-01-05 21:25:51 -05001400 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001401 return 1;
Andi Kleen411fc6b2010-10-29 15:14:31 -04001402 return ret;
Chris Mason239b14b2008-03-24 15:02:07 -04001403}
1404
Chris Masond352ac62008-09-29 15:18:18 -04001405/*
1406 * in order to insert checksums into the metadata in large chunks,
1407 * we wait until bio submission time. All the pages in the bio are
1408 * checksummed and sums are attached onto the ordered extent record.
1409 *
1410 * At IO completion time the cums attached on the ordered extent record
1411 * are inserted into the btree
1412 */
Chris Masond3977122009-01-05 21:25:51 -05001413static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1414 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001415 unsigned long bio_flags,
1416 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001417{
Chris Mason065631f2008-02-20 12:07:25 -05001418 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001419 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001420
Chris Masond20f7042008-12-08 16:58:54 -05001421 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001422 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001423 return 0;
1424}
Chris Masone0156402008-04-16 11:15:20 -04001425
Chris Mason4a69a412008-11-06 22:03:00 -05001426/*
1427 * in order to insert checksums into the metadata in large chunks,
1428 * we wait until bio submission time. All the pages in the bio are
1429 * checksummed and sums are attached onto the ordered extent record.
1430 *
1431 * At IO completion time the cums attached on the ordered extent record
1432 * are inserted into the btree
1433 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001434static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001435 int mirror_num, unsigned long bio_flags,
1436 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001437{
1438 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001439 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001440}
1441
Chris Masond352ac62008-09-29 15:18:18 -04001442/*
Chris Masoncad321a2008-12-17 14:51:42 -05001443 * extent_io.c submission hook. This does the right thing for csum calculation
1444 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001445 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001446static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001447 int mirror_num, unsigned long bio_flags,
1448 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001449{
1450 struct btrfs_root *root = BTRFS_I(inode)->root;
1451 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001452 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001453
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001454 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001455
Josef Bacik0cb59c92010-07-02 12:14:14 -04001456 if (root == root->fs_info->tree_root)
1457 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2);
1458 else
1459 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001460 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001461
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001462 if (!(rw & REQ_WRITE)) {
Chris Masond20f7042008-12-08 16:58:54 -05001463 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001464 return btrfs_submit_compressed_read(inode, bio,
1465 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001466 } else if (!skip_sum)
1467 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001468 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001469 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001470 /* csum items have already been cloned */
1471 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1472 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001473 /* we're doing a write, do the async checksumming */
1474 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001475 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001476 bio_flags, bio_offset,
1477 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001478 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001479 }
1480
Chris Mason0b86a832008-03-24 15:01:56 -04001481mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001482 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001483}
Chris Mason6885f302008-02-20 16:11:05 -05001484
Chris Masond352ac62008-09-29 15:18:18 -04001485/*
1486 * given a list of ordered sums record them in the inode. This happens
1487 * at IO completion time based on sums calculated at bio submission time.
1488 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001489static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001490 struct inode *inode, u64 file_offset,
1491 struct list_head *list)
1492{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001493 struct btrfs_ordered_sum *sum;
1494
1495 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001496
1497 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001498 btrfs_csum_file_blocks(trans,
1499 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001500 }
1501 return 0;
1502}
1503
Josef Bacik2ac55d42010-02-03 19:33:23 +00001504int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1505 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001506{
Chris Masond3977122009-01-05 21:25:51 -05001507 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001508 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001509 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001510 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001511}
1512
Chris Masond352ac62008-09-29 15:18:18 -04001513/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001514struct btrfs_writepage_fixup {
1515 struct page *page;
1516 struct btrfs_work work;
1517};
1518
Christoph Hellwigb2950862008-12-02 09:54:17 -05001519static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001520{
1521 struct btrfs_writepage_fixup *fixup;
1522 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001523 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001524 struct page *page;
1525 struct inode *inode;
1526 u64 page_start;
1527 u64 page_end;
1528
1529 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1530 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001531again:
Chris Mason247e7432008-07-17 12:53:51 -04001532 lock_page(page);
1533 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1534 ClearPageChecked(page);
1535 goto out_page;
1536 }
1537
1538 inode = page->mapping->host;
1539 page_start = page_offset(page);
1540 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1541
Josef Bacik2ac55d42010-02-03 19:33:23 +00001542 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1543 &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001544
1545 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001546 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001547 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001548
1549 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1550 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001551 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1552 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001553 unlock_page(page);
1554 btrfs_start_ordered_extent(inode, ordered, 1);
1555 goto again;
1556 }
Chris Mason247e7432008-07-17 12:53:51 -04001557
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001558 BUG();
Josef Bacik2ac55d42010-02-03 19:33:23 +00001559 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001560 ClearPageChecked(page);
1561out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001562 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1563 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001564out_page:
1565 unlock_page(page);
1566 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001567 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001568}
1569
1570/*
1571 * There are a few paths in the higher layers of the kernel that directly
1572 * set the page dirty bit without asking the filesystem if it is a
1573 * good idea. This causes problems because we want to make sure COW
1574 * properly happens and the data=ordered rules are followed.
1575 *
Chris Masonc8b97812008-10-29 14:49:59 -04001576 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001577 * hasn't been properly setup for IO. We kick off an async process
1578 * to fix it up. The async helper will wait for ordered extents, set
1579 * the delalloc bit and make it safe to write the page.
1580 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001581static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001582{
1583 struct inode *inode = page->mapping->host;
1584 struct btrfs_writepage_fixup *fixup;
1585 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001586
Chris Mason8b62b722009-09-02 16:53:46 -04001587 /* this page is properly in the ordered list */
1588 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001589 return 0;
1590
1591 if (PageChecked(page))
1592 return -EAGAIN;
1593
1594 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1595 if (!fixup)
1596 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001597
Chris Mason247e7432008-07-17 12:53:51 -04001598 SetPageChecked(page);
1599 page_cache_get(page);
1600 fixup->work.func = btrfs_writepage_fixup_worker;
1601 fixup->page = page;
1602 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1603 return -EAGAIN;
1604}
1605
Yan Zhengd899e052008-10-30 14:25:28 -04001606static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1607 struct inode *inode, u64 file_pos,
1608 u64 disk_bytenr, u64 disk_num_bytes,
1609 u64 num_bytes, u64 ram_bytes,
1610 u8 compression, u8 encryption,
1611 u16 other_encoding, int extent_type)
1612{
1613 struct btrfs_root *root = BTRFS_I(inode)->root;
1614 struct btrfs_file_extent_item *fi;
1615 struct btrfs_path *path;
1616 struct extent_buffer *leaf;
1617 struct btrfs_key ins;
1618 u64 hint;
1619 int ret;
1620
1621 path = btrfs_alloc_path();
1622 BUG_ON(!path);
1623
Chris Masonb9473432009-03-13 11:00:37 -04001624 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001625
1626 /*
1627 * we may be replacing one extent in the tree with another.
1628 * The new extent is pinned in the extent map, and we don't want
1629 * to drop it from the cache until it is completely in the btree.
1630 *
1631 * So, tell btrfs_drop_extents to leave this extent in the cache.
1632 * the caller is expected to unpin it and allow it to be merged
1633 * with the others.
1634 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001635 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1636 &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001637 BUG_ON(ret);
1638
1639 ins.objectid = inode->i_ino;
1640 ins.offset = file_pos;
1641 ins.type = BTRFS_EXTENT_DATA_KEY;
1642 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1643 BUG_ON(ret);
1644 leaf = path->nodes[0];
1645 fi = btrfs_item_ptr(leaf, path->slots[0],
1646 struct btrfs_file_extent_item);
1647 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1648 btrfs_set_file_extent_type(leaf, fi, extent_type);
1649 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1650 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1651 btrfs_set_file_extent_offset(leaf, fi, 0);
1652 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1653 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1654 btrfs_set_file_extent_compression(leaf, fi, compression);
1655 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1656 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001657
1658 btrfs_unlock_up_safe(path, 1);
1659 btrfs_set_lock_blocking(leaf);
1660
Yan Zhengd899e052008-10-30 14:25:28 -04001661 btrfs_mark_buffer_dirty(leaf);
1662
1663 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001664
1665 ins.objectid = disk_bytenr;
1666 ins.offset = disk_num_bytes;
1667 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001668 ret = btrfs_alloc_reserved_file_extent(trans, root,
1669 root->root_key.objectid,
1670 inode->i_ino, file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001671 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001672 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001673
Yan Zhengd899e052008-10-30 14:25:28 -04001674 return 0;
1675}
1676
Chris Mason5d13a982009-03-13 11:41:46 -04001677/*
1678 * helper function for btrfs_finish_ordered_io, this
1679 * just reads in some of the csum leaves to prime them into ram
1680 * before we start the transaction. It limits the amount of btree
1681 * reads required while inside the transaction.
1682 */
Chris Masond352ac62008-09-29 15:18:18 -04001683/* as ordered data IO finishes, this gets called so we can finish
1684 * an ordered extent if the range of bytes in the file it covers are
1685 * fully written.
1686 */
Chris Mason211f90e2008-07-18 11:56:15 -04001687static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001688{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001689 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001690 struct btrfs_trans_handle *trans = NULL;
Chris Mason5d13a982009-03-13 11:41:46 -04001691 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001692 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001693 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001694 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001695 int ret;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001696 bool nolock = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001697
Josef Bacik5a1a3df2010-02-02 20:51:14 +00001698 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1699 end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001700 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001701 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001702 BUG_ON(!ordered_extent);
Josef Bacikefd049f2010-02-02 20:50:10 +00001703
Josef Bacik0cb59c92010-07-02 12:14:14 -04001704 nolock = (root == root->fs_info->tree_root);
1705
Yan, Zhengc2167752009-11-12 09:34:21 +00001706 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1707 BUG_ON(!list_empty(&ordered_extent->list));
1708 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1709 if (!ret) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001710 if (nolock)
1711 trans = btrfs_join_transaction_nolock(root, 1);
1712 else
1713 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001714 BUG_ON(IS_ERR(trans));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001715 btrfs_set_trans_block_group(trans, inode);
1716 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001717 ret = btrfs_update_inode(trans, root, inode);
1718 BUG_ON(ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001719 }
1720 goto out;
1721 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001722
Josef Bacik2ac55d42010-02-03 19:33:23 +00001723 lock_extent_bits(io_tree, ordered_extent->file_offset,
1724 ordered_extent->file_offset + ordered_extent->len - 1,
1725 0, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001726
Josef Bacik0cb59c92010-07-02 12:14:14 -04001727 if (nolock)
1728 trans = btrfs_join_transaction_nolock(root, 1);
1729 else
1730 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001731 BUG_ON(IS_ERR(trans));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001732 btrfs_set_trans_block_group(trans, inode);
1733 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001734
Chris Masonc8b97812008-10-29 14:49:59 -04001735 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001736 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001737 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001738 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001739 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001740 ordered_extent->file_offset,
1741 ordered_extent->file_offset +
1742 ordered_extent->len);
1743 BUG_ON(ret);
1744 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001745 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001746 ret = insert_reserved_file_extent(trans, inode,
1747 ordered_extent->file_offset,
1748 ordered_extent->start,
1749 ordered_extent->disk_len,
1750 ordered_extent->len,
1751 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001752 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001753 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001754 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1755 ordered_extent->file_offset,
1756 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001757 BUG_ON(ret);
1758 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001759 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1760 ordered_extent->file_offset +
1761 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1762
Chris Masone6dcd2d2008-07-17 12:53:50 -04001763 add_pending_csums(trans, inode, ordered_extent->file_offset,
1764 &ordered_extent->list);
1765
Yan, Zhengc2167752009-11-12 09:34:21 +00001766 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1767 ret = btrfs_update_inode(trans, root, inode);
1768 BUG_ON(ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001769out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04001770 if (nolock) {
1771 if (trans)
1772 btrfs_end_transaction_nolock(trans, root);
1773 } else {
1774 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
1775 if (trans)
1776 btrfs_end_transaction(trans, root);
1777 }
1778
Chris Masone6dcd2d2008-07-17 12:53:50 -04001779 /* once for us */
1780 btrfs_put_ordered_extent(ordered_extent);
1781 /* once for the tree */
1782 btrfs_put_ordered_extent(ordered_extent);
1783
Chris Masone6dcd2d2008-07-17 12:53:50 -04001784 return 0;
1785}
1786
Christoph Hellwigb2950862008-12-02 09:54:17 -05001787static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001788 struct extent_state *state, int uptodate)
1789{
Chris Mason8b62b722009-09-02 16:53:46 -04001790 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001791 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1792}
1793
Chris Masond352ac62008-09-29 15:18:18 -04001794/*
1795 * When IO fails, either with EIO or csum verification fails, we
1796 * try other mirrors that might have a good copy of the data. This
1797 * io_failure_record is used to record state as we go through all the
1798 * mirrors. If another mirror has good data, the page is set up to date
1799 * and things continue. If a good mirror can't be found, the original
1800 * bio end_io callback is called to indicate things have failed.
1801 */
Chris Mason7e383262008-04-09 16:28:12 -04001802struct io_failure_record {
1803 struct page *page;
1804 u64 start;
1805 u64 len;
1806 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001807 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001808 int last_mirror;
1809};
1810
Christoph Hellwigb2950862008-12-02 09:54:17 -05001811static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001812 struct page *page, u64 start, u64 end,
1813 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001814{
1815 struct io_failure_record *failrec = NULL;
1816 u64 private;
1817 struct extent_map *em;
1818 struct inode *inode = page->mapping->host;
1819 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001820 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001821 struct bio *bio;
1822 int num_copies;
1823 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001824 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001825 u64 logical;
1826
1827 ret = get_state_private(failure_tree, start, &private);
1828 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001829 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1830 if (!failrec)
1831 return -ENOMEM;
1832 failrec->start = start;
1833 failrec->len = end - start + 1;
1834 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001835 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001836
Chris Mason890871b2009-09-02 16:24:52 -04001837 read_lock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -04001838 em = lookup_extent_mapping(em_tree, start, failrec->len);
1839 if (em->start > start || em->start + em->len < start) {
1840 free_extent_map(em);
1841 em = NULL;
1842 }
Chris Mason890871b2009-09-02 16:24:52 -04001843 read_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001844
1845 if (!em || IS_ERR(em)) {
1846 kfree(failrec);
1847 return -EIO;
1848 }
1849 logical = start - em->start;
1850 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001851 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1852 logical = em->block_start;
1853 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08001854 extent_set_compress_type(&failrec->bio_flags,
1855 em->compress_type);
Chris Masond20f7042008-12-08 16:58:54 -05001856 }
Chris Mason7e383262008-04-09 16:28:12 -04001857 failrec->logical = logical;
1858 free_extent_map(em);
1859 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1860 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001861 set_state_private(failure_tree, start,
1862 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001863 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001864 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001865 }
1866 num_copies = btrfs_num_copies(
1867 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1868 failrec->logical, failrec->len);
1869 failrec->last_mirror++;
1870 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001871 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001872 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1873 failrec->start,
1874 EXTENT_LOCKED);
1875 if (state && state->start != failrec->start)
1876 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001877 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001878 }
1879 if (!state || failrec->last_mirror > num_copies) {
1880 set_state_private(failure_tree, failrec->start, 0);
1881 clear_extent_bits(failure_tree, failrec->start,
1882 failrec->start + failrec->len - 1,
1883 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1884 kfree(failrec);
1885 return -EIO;
1886 }
1887 bio = bio_alloc(GFP_NOFS, 1);
1888 bio->bi_private = state;
1889 bio->bi_end_io = failed_bio->bi_end_io;
1890 bio->bi_sector = failrec->logical >> 9;
1891 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001892 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001893
Chris Mason7e383262008-04-09 16:28:12 -04001894 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001895 if (failed_bio->bi_rw & REQ_WRITE)
Chris Mason1259ab72008-05-12 13:39:03 -04001896 rw = WRITE;
1897 else
1898 rw = READ;
1899
1900 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001901 failrec->last_mirror,
Chris Masoneaf25d92010-05-25 09:48:28 -04001902 failrec->bio_flags, 0);
Chris Mason1259ab72008-05-12 13:39:03 -04001903 return 0;
1904}
1905
Chris Masond352ac62008-09-29 15:18:18 -04001906/*
1907 * each time an IO finishes, we do a fast check in the IO failure tree
1908 * to see if we need to process or clean up an io_failure_record
1909 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001910static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001911{
1912 u64 private;
1913 u64 private_failure;
1914 struct io_failure_record *failure;
1915 int ret;
1916
1917 private = 0;
1918 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
Chris Masonec29ed52011-02-23 16:23:20 -05001919 (u64)-1, 1, EXTENT_DIRTY, 0)) {
Chris Mason1259ab72008-05-12 13:39:03 -04001920 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1921 start, &private_failure);
1922 if (ret == 0) {
1923 failure = (struct io_failure_record *)(unsigned long)
1924 private_failure;
1925 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1926 failure->start, 0);
1927 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1928 failure->start,
1929 failure->start + failure->len - 1,
1930 EXTENT_DIRTY | EXTENT_LOCKED,
1931 GFP_NOFS);
1932 kfree(failure);
1933 }
1934 }
Chris Mason7e383262008-04-09 16:28:12 -04001935 return 0;
1936}
1937
Chris Masond352ac62008-09-29 15:18:18 -04001938/*
1939 * when reads are done, we need to check csums to verify the data is correct
1940 * if there's a match, we allow the bio to finish. If not, we go through
1941 * the io_failure_record routines to find good copies
1942 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001943static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001944 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001945{
Chris Mason35ebb932007-10-30 16:56:53 -04001946 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001947 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001948 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001949 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001950 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001951 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001952 struct btrfs_root *root = BTRFS_I(inode)->root;
1953 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001954
Chris Masond20f7042008-12-08 16:58:54 -05001955 if (PageChecked(page)) {
1956 ClearPageChecked(page);
1957 goto good;
1958 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001959
1960 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Chris Masonb6cda9b2007-12-14 15:30:32 -05001961 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001962
Yan Zheng17d217f2008-12-12 10:03:38 -05001963 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001964 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001965 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1966 GFP_NOFS);
1967 return 0;
1968 }
1969
Yanc2e639f2008-02-04 08:57:25 -05001970 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001971 private = state->private;
1972 ret = 0;
1973 } else {
1974 ret = get_state_private(io_tree, start, &private);
1975 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001976 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001977 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001978 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001979
Chris Masonff79f812007-10-15 16:22:25 -04001980 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1981 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001982 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001983 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001984
Chris Mason9ab86c82009-01-07 09:48:51 -05001985 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001986good:
Chris Mason7e383262008-04-09 16:28:12 -04001987 /* if the io failure tree for this inode is non-empty,
1988 * check to see if we've recovered from a failed IO
1989 */
Chris Mason1259ab72008-05-12 13:39:03 -04001990 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001991 return 0;
1992
1993zeroit:
Chris Mason193f2842009-04-27 07:29:05 -04001994 if (printk_ratelimit()) {
1995 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1996 "private %llu\n", page->mapping->host->i_ino,
1997 (unsigned long long)start, csum,
1998 (unsigned long long)private);
1999 }
Chris Masondb945352007-10-15 16:15:53 -04002000 memset(kaddr + offset, 1, end - start + 1);
2001 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05002002 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04002003 if (private == 0)
2004 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002005 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002006}
Chris Masonb888db22007-08-27 16:49:44 -04002007
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002008struct delayed_iput {
2009 struct list_head list;
2010 struct inode *inode;
2011};
2012
2013void btrfs_add_delayed_iput(struct inode *inode)
2014{
2015 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2016 struct delayed_iput *delayed;
2017
2018 if (atomic_add_unless(&inode->i_count, -1, 1))
2019 return;
2020
2021 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2022 delayed->inode = inode;
2023
2024 spin_lock(&fs_info->delayed_iput_lock);
2025 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2026 spin_unlock(&fs_info->delayed_iput_lock);
2027}
2028
2029void btrfs_run_delayed_iputs(struct btrfs_root *root)
2030{
2031 LIST_HEAD(list);
2032 struct btrfs_fs_info *fs_info = root->fs_info;
2033 struct delayed_iput *delayed;
2034 int empty;
2035
2036 spin_lock(&fs_info->delayed_iput_lock);
2037 empty = list_empty(&fs_info->delayed_iputs);
2038 spin_unlock(&fs_info->delayed_iput_lock);
2039 if (empty)
2040 return;
2041
2042 down_read(&root->fs_info->cleanup_work_sem);
2043 spin_lock(&fs_info->delayed_iput_lock);
2044 list_splice_init(&fs_info->delayed_iputs, &list);
2045 spin_unlock(&fs_info->delayed_iput_lock);
2046
2047 while (!list_empty(&list)) {
2048 delayed = list_entry(list.next, struct delayed_iput, list);
2049 list_del(&delayed->list);
2050 iput(delayed->inode);
2051 kfree(delayed);
2052 }
2053 up_read(&root->fs_info->cleanup_work_sem);
2054}
2055
Josef Bacik7b128762008-07-24 12:17:14 -04002056/*
Yan, Zhengd68fc572010-05-16 10:49:58 -04002057 * calculate extra metadata reservation when snapshotting a subvolume
2058 * contains orphan files.
2059 */
2060void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans,
2061 struct btrfs_pending_snapshot *pending,
2062 u64 *bytes_to_reserve)
2063{
2064 struct btrfs_root *root;
2065 struct btrfs_block_rsv *block_rsv;
2066 u64 num_bytes;
2067 int index;
2068
2069 root = pending->root;
2070 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2071 return;
2072
2073 block_rsv = root->orphan_block_rsv;
2074
2075 /* orphan block reservation for the snapshot */
2076 num_bytes = block_rsv->size;
2077
2078 /*
2079 * after the snapshot is created, COWing tree blocks may use more
2080 * space than it frees. So we should make sure there is enough
2081 * reserved space.
2082 */
2083 index = trans->transid & 0x1;
2084 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2085 num_bytes += block_rsv->size -
2086 (block_rsv->reserved + block_rsv->freed[index]);
2087 }
2088
2089 *bytes_to_reserve += num_bytes;
2090}
2091
2092void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans,
2093 struct btrfs_pending_snapshot *pending)
2094{
2095 struct btrfs_root *root = pending->root;
2096 struct btrfs_root *snap = pending->snap;
2097 struct btrfs_block_rsv *block_rsv;
2098 u64 num_bytes;
2099 int index;
2100 int ret;
2101
2102 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2103 return;
2104
2105 /* refill source subvolume's orphan block reservation */
2106 block_rsv = root->orphan_block_rsv;
2107 index = trans->transid & 0x1;
2108 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2109 num_bytes = block_rsv->size -
2110 (block_rsv->reserved + block_rsv->freed[index]);
2111 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2112 root->orphan_block_rsv,
2113 num_bytes);
2114 BUG_ON(ret);
2115 }
2116
2117 /* setup orphan block reservation for the snapshot */
2118 block_rsv = btrfs_alloc_block_rsv(snap);
2119 BUG_ON(!block_rsv);
2120
2121 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2122 snap->orphan_block_rsv = block_rsv;
2123
2124 num_bytes = root->orphan_block_rsv->size;
2125 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2126 block_rsv, num_bytes);
2127 BUG_ON(ret);
2128
2129#if 0
2130 /* insert orphan item for the snapshot */
2131 WARN_ON(!root->orphan_item_inserted);
2132 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2133 snap->root_key.objectid);
2134 BUG_ON(ret);
2135 snap->orphan_item_inserted = 1;
2136#endif
2137}
2138
2139enum btrfs_orphan_cleanup_state {
2140 ORPHAN_CLEANUP_STARTED = 1,
2141 ORPHAN_CLEANUP_DONE = 2,
2142};
2143
2144/*
2145 * This is called in transaction commmit time. If there are no orphan
2146 * files in the subvolume, it removes orphan item and frees block_rsv
2147 * structure.
2148 */
2149void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2150 struct btrfs_root *root)
2151{
2152 int ret;
2153
2154 if (!list_empty(&root->orphan_list) ||
2155 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2156 return;
2157
2158 if (root->orphan_item_inserted &&
2159 btrfs_root_refs(&root->root_item) > 0) {
2160 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2161 root->root_key.objectid);
2162 BUG_ON(ret);
2163 root->orphan_item_inserted = 0;
2164 }
2165
2166 if (root->orphan_block_rsv) {
2167 WARN_ON(root->orphan_block_rsv->size > 0);
2168 btrfs_free_block_rsv(root, root->orphan_block_rsv);
2169 root->orphan_block_rsv = NULL;
2170 }
2171}
2172
2173/*
Josef Bacik7b128762008-07-24 12:17:14 -04002174 * This creates an orphan entry for the given inode in case something goes
2175 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002176 *
2177 * NOTE: caller of this function should reserve 5 units of metadata for
2178 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002179 */
2180int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2181{
2182 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002183 struct btrfs_block_rsv *block_rsv = NULL;
2184 int reserve = 0;
2185 int insert = 0;
2186 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002187
Yan, Zhengd68fc572010-05-16 10:49:58 -04002188 if (!root->orphan_block_rsv) {
2189 block_rsv = btrfs_alloc_block_rsv(root);
2190 BUG_ON(!block_rsv);
Josef Bacik7b128762008-07-24 12:17:14 -04002191 }
2192
Yan, Zhengd68fc572010-05-16 10:49:58 -04002193 spin_lock(&root->orphan_lock);
2194 if (!root->orphan_block_rsv) {
2195 root->orphan_block_rsv = block_rsv;
2196 } else if (block_rsv) {
2197 btrfs_free_block_rsv(root, block_rsv);
2198 block_rsv = NULL;
2199 }
Josef Bacik7b128762008-07-24 12:17:14 -04002200
Yan, Zhengd68fc572010-05-16 10:49:58 -04002201 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2202 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2203#if 0
2204 /*
2205 * For proper ENOSPC handling, we should do orphan
2206 * cleanup when mounting. But this introduces backward
2207 * compatibility issue.
2208 */
2209 if (!xchg(&root->orphan_item_inserted, 1))
2210 insert = 2;
2211 else
2212 insert = 1;
2213#endif
2214 insert = 1;
2215 } else {
2216 WARN_ON(!BTRFS_I(inode)->orphan_meta_reserved);
2217 }
Josef Bacik7b128762008-07-24 12:17:14 -04002218
Yan, Zhengd68fc572010-05-16 10:49:58 -04002219 if (!BTRFS_I(inode)->orphan_meta_reserved) {
2220 BTRFS_I(inode)->orphan_meta_reserved = 1;
2221 reserve = 1;
2222 }
2223 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002224
Yan, Zhengd68fc572010-05-16 10:49:58 -04002225 if (block_rsv)
2226 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2227
2228 /* grab metadata reservation from transaction handle */
2229 if (reserve) {
2230 ret = btrfs_orphan_reserve_metadata(trans, inode);
2231 BUG_ON(ret);
2232 }
2233
2234 /* insert an orphan item to track this unlinked/truncated file */
2235 if (insert >= 1) {
2236 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
2237 BUG_ON(ret);
2238 }
2239
2240 /* insert an orphan item to track subvolume contains orphan files */
2241 if (insert >= 2) {
2242 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2243 root->root_key.objectid);
2244 BUG_ON(ret);
2245 }
2246 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002247}
2248
2249/*
2250 * We have done the truncate/delete so we can go ahead and remove the orphan
2251 * item for this particular inode.
2252 */
2253int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2254{
2255 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002256 int delete_item = 0;
2257 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002258 int ret = 0;
2259
Yan, Zhengd68fc572010-05-16 10:49:58 -04002260 spin_lock(&root->orphan_lock);
2261 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2262 list_del_init(&BTRFS_I(inode)->i_orphan);
2263 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002264 }
2265
Yan, Zhengd68fc572010-05-16 10:49:58 -04002266 if (BTRFS_I(inode)->orphan_meta_reserved) {
2267 BTRFS_I(inode)->orphan_meta_reserved = 0;
2268 release_rsv = 1;
2269 }
2270 spin_unlock(&root->orphan_lock);
2271
2272 if (trans && delete_item) {
2273 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2274 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002275 }
2276
Yan, Zhengd68fc572010-05-16 10:49:58 -04002277 if (release_rsv)
2278 btrfs_orphan_release_metadata(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002279
Yan, Zhengd68fc572010-05-16 10:49:58 -04002280 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002281}
2282
2283/*
2284 * this cleans up any orphans that may be left on the list from the last use
2285 * of this root.
2286 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002287int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002288{
2289 struct btrfs_path *path;
2290 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002291 struct btrfs_key key, found_key;
2292 struct btrfs_trans_handle *trans;
2293 struct inode *inode;
2294 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2295
Yan, Zhengd68fc572010-05-16 10:49:58 -04002296 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002297 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002298
2299 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002300 if (!path) {
2301 ret = -ENOMEM;
2302 goto out;
2303 }
Josef Bacik7b128762008-07-24 12:17:14 -04002304 path->reada = -1;
2305
2306 key.objectid = BTRFS_ORPHAN_OBJECTID;
2307 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2308 key.offset = (u64)-1;
2309
Josef Bacik7b128762008-07-24 12:17:14 -04002310 while (1) {
2311 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002312 if (ret < 0)
2313 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002314
2315 /*
2316 * if ret == 0 means we found what we were searching for, which
2317 * is weird, but possible, so only screw with path if we didnt
2318 * find the key and see if we have stuff that matches
2319 */
2320 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002321 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002322 if (path->slots[0] == 0)
2323 break;
2324 path->slots[0]--;
2325 }
2326
2327 /* pull out the item */
2328 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002329 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2330
2331 /* make sure the item matches what we want */
2332 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2333 break;
2334 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2335 break;
2336
2337 /* release the path since we're done with it */
2338 btrfs_release_path(root, path);
2339
2340 /*
2341 * this is where we are basically btrfs_lookup, without the
2342 * crossing root thing. we store the inode number in the
2343 * offset of the orphan item.
2344 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002345 found_key.objectid = found_key.offset;
2346 found_key.type = BTRFS_INODE_ITEM_KEY;
2347 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002348 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002349 if (IS_ERR(inode)) {
2350 ret = PTR_ERR(inode);
2351 goto out;
2352 }
Josef Bacik7b128762008-07-24 12:17:14 -04002353
Josef Bacik7b128762008-07-24 12:17:14 -04002354 /*
2355 * add this inode to the orphan list so btrfs_orphan_del does
2356 * the proper thing when we hit it
2357 */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002358 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002359 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002360 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002361
2362 /*
2363 * if this is a bad inode, means we actually succeeded in
2364 * removing the inode, but not the orphan record, which means
2365 * we need to manually delete the orphan since iput will just
2366 * do a destroy_inode
2367 */
2368 if (is_bad_inode(inode)) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04002369 trans = btrfs_start_transaction(root, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002370 if (IS_ERR(trans)) {
2371 ret = PTR_ERR(trans);
2372 goto out;
2373 }
Josef Bacik7b128762008-07-24 12:17:14 -04002374 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002375 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04002376 iput(inode);
2377 continue;
2378 }
2379
2380 /* if we have links, this was a truncate, lets do that */
2381 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002382 if (!S_ISREG(inode->i_mode)) {
2383 WARN_ON(1);
2384 iput(inode);
2385 continue;
2386 }
Josef Bacik7b128762008-07-24 12:17:14 -04002387 nr_truncate++;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002388 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002389 } else {
2390 nr_unlink++;
2391 }
2392
2393 /* this will do delete_inode and everything for us */
2394 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002395 if (ret)
2396 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002397 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002398 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2399
2400 if (root->orphan_block_rsv)
2401 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2402 (u64)-1);
2403
2404 if (root->orphan_block_rsv || root->orphan_item_inserted) {
2405 trans = btrfs_join_transaction(root, 1);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002406 if (!IS_ERR(trans))
2407 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002408 }
Josef Bacik7b128762008-07-24 12:17:14 -04002409
2410 if (nr_unlink)
2411 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2412 if (nr_truncate)
2413 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002414
2415out:
2416 if (ret)
2417 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2418 btrfs_free_path(path);
2419 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002420}
2421
Chris Masond352ac62008-09-29 15:18:18 -04002422/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002423 * very simple check to peek ahead in the leaf looking for xattrs. If we
2424 * don't find any xattrs, we know there can't be any acls.
2425 *
2426 * slot is the slot the inode is in, objectid is the objectid of the inode
2427 */
2428static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2429 int slot, u64 objectid)
2430{
2431 u32 nritems = btrfs_header_nritems(leaf);
2432 struct btrfs_key found_key;
2433 int scanned = 0;
2434
2435 slot++;
2436 while (slot < nritems) {
2437 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2438
2439 /* we found a different objectid, there must not be acls */
2440 if (found_key.objectid != objectid)
2441 return 0;
2442
2443 /* we found an xattr, assume we've got an acl */
2444 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2445 return 1;
2446
2447 /*
2448 * we found a key greater than an xattr key, there can't
2449 * be any acls later on
2450 */
2451 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2452 return 0;
2453
2454 slot++;
2455 scanned++;
2456
2457 /*
2458 * it goes inode, inode backrefs, xattrs, extents,
2459 * so if there are a ton of hard links to an inode there can
2460 * be a lot of backrefs. Don't waste time searching too hard,
2461 * this is just an optimization
2462 */
2463 if (scanned >= 8)
2464 break;
2465 }
2466 /* we hit the end of the leaf before we found an xattr or
2467 * something larger than an xattr. We have to assume the inode
2468 * has acls
2469 */
2470 return 1;
2471}
2472
2473/*
Chris Masond352ac62008-09-29 15:18:18 -04002474 * read an inode from the btree into the in-memory inode
2475 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002476static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002477{
2478 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002479 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002480 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002481 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002482 struct btrfs_root *root = BTRFS_I(inode)->root;
2483 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002484 int maybe_acls;
Chris Mason39279cc2007-06-12 06:35:45 -04002485 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04002486 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002487 int ret;
2488
2489 path = btrfs_alloc_path();
2490 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002491 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002492
Chris Mason39279cc2007-06-12 06:35:45 -04002493 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002494 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002495 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002496
Chris Mason5f39d392007-10-15 16:14:19 -04002497 leaf = path->nodes[0];
2498 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2499 struct btrfs_inode_item);
2500
2501 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2502 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2503 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2504 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002505 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002506
2507 tspec = btrfs_inode_atime(inode_item);
2508 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2509 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2510
2511 tspec = btrfs_inode_mtime(inode_item);
2512 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2513 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2514
2515 tspec = btrfs_inode_ctime(inode_item);
2516 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2517 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2518
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002519 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002520 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002521 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002522 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002523 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002524 rdev = btrfs_inode_rdev(leaf, inode_item);
2525
Josef Bacikaec74772008-07-24 12:12:38 -04002526 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002527 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002528
Chris Mason5f39d392007-10-15 16:14:19 -04002529 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002530
Chris Mason46a53cc2009-04-27 11:47:50 -04002531 /*
2532 * try to precache a NULL acl entry for files that don't have
2533 * any xattrs or acls
2534 */
2535 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
Al Viro72c04902009-06-24 16:58:48 -04002536 if (!maybe_acls)
2537 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002538
Yan Zhengd2fb3432008-12-11 16:30:39 -05002539 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2540 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002541 btrfs_free_path(path);
2542 inode_item = NULL;
2543
Chris Mason39279cc2007-06-12 06:35:45 -04002544 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002545 case S_IFREG:
2546 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002547 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002548 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002549 inode->i_fop = &btrfs_file_operations;
2550 inode->i_op = &btrfs_file_inode_operations;
2551 break;
2552 case S_IFDIR:
2553 inode->i_fop = &btrfs_dir_file_operations;
2554 if (root == root->fs_info->tree_root)
2555 inode->i_op = &btrfs_dir_ro_inode_operations;
2556 else
2557 inode->i_op = &btrfs_dir_inode_operations;
2558 break;
2559 case S_IFLNK:
2560 inode->i_op = &btrfs_symlink_inode_operations;
2561 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002562 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002563 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002564 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002565 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002566 init_special_inode(inode, inode->i_mode, rdev);
2567 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002568 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002569
2570 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002571 return;
2572
2573make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002574 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002575 make_bad_inode(inode);
2576}
2577
Chris Masond352ac62008-09-29 15:18:18 -04002578/*
2579 * given a leaf and an inode, copy the inode fields into the leaf
2580 */
Chris Masone02119d2008-09-05 16:13:11 -04002581static void fill_inode_item(struct btrfs_trans_handle *trans,
2582 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002583 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002584 struct inode *inode)
2585{
Chris Mason5f39d392007-10-15 16:14:19 -04002586 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2587 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002588 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002589 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2590 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2591
2592 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2593 inode->i_atime.tv_sec);
2594 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2595 inode->i_atime.tv_nsec);
2596
2597 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2598 inode->i_mtime.tv_sec);
2599 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2600 inode->i_mtime.tv_nsec);
2601
2602 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2603 inode->i_ctime.tv_sec);
2604 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2605 inode->i_ctime.tv_nsec);
2606
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002607 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002608 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002609 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002610 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002611 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002612 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002613 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002614}
2615
Chris Masond352ac62008-09-29 15:18:18 -04002616/*
2617 * copy everything in the in-memory inode into the btree.
2618 */
Chris Masond3977122009-01-05 21:25:51 -05002619noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2620 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002621{
2622 struct btrfs_inode_item *inode_item;
2623 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002624 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002625 int ret;
2626
2627 path = btrfs_alloc_path();
2628 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002629 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002630 ret = btrfs_lookup_inode(trans, root, path,
2631 &BTRFS_I(inode)->location, 1);
2632 if (ret) {
2633 if (ret > 0)
2634 ret = -ENOENT;
2635 goto failed;
2636 }
2637
Chris Masonb4ce94d2009-02-04 09:25:08 -05002638 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002639 leaf = path->nodes[0];
2640 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002641 struct btrfs_inode_item);
2642
Chris Masone02119d2008-09-05 16:13:11 -04002643 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002644 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002645 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002646 ret = 0;
2647failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002648 btrfs_free_path(path);
2649 return ret;
2650}
2651
2652
Chris Masond352ac62008-09-29 15:18:18 -04002653/*
2654 * unlink helper that gets used here in inode.c and in the tree logging
2655 * recovery code. It remove a link in a directory with a given name, and
2656 * also drops the back refs in the inode to the directory
2657 */
Chris Masone02119d2008-09-05 16:13:11 -04002658int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2659 struct btrfs_root *root,
2660 struct inode *dir, struct inode *inode,
2661 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002662{
2663 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002664 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002665 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002666 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002667 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002668 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002669
2670 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002671 if (!path) {
2672 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002673 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002674 }
2675
Chris Masonb9473432009-03-13 11:00:37 -04002676 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002677 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2678 name, name_len, -1);
2679 if (IS_ERR(di)) {
2680 ret = PTR_ERR(di);
2681 goto err;
2682 }
2683 if (!di) {
2684 ret = -ENOENT;
2685 goto err;
2686 }
Chris Mason5f39d392007-10-15 16:14:19 -04002687 leaf = path->nodes[0];
2688 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002689 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002690 if (ret)
2691 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002692 btrfs_release_path(root, path);
2693
Josef Bacikaec74772008-07-24 12:12:38 -04002694 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002695 inode->i_ino,
2696 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002697 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002698 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002699 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002700 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002701 goto err;
2702 }
2703
Chris Mason39279cc2007-06-12 06:35:45 -04002704 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002705 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002706 if (IS_ERR(di)) {
2707 ret = PTR_ERR(di);
2708 goto err;
2709 }
2710 if (!di) {
2711 ret = -ENOENT;
2712 goto err;
2713 }
2714 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002715 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002716
Chris Masone02119d2008-09-05 16:13:11 -04002717 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2718 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002719 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002720
2721 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2722 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002723 if (ret == -ENOENT)
2724 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002725err:
2726 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002727 if (ret)
2728 goto out;
2729
2730 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2731 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2732 btrfs_update_inode(trans, root, dir);
2733 btrfs_drop_nlink(inode);
2734 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002735out:
Chris Mason39279cc2007-06-12 06:35:45 -04002736 return ret;
2737}
2738
Yan, Zhenga22285a2010-05-16 10:48:46 -04002739/* helper to check if there is any shared block in the path */
2740static int check_path_shared(struct btrfs_root *root,
2741 struct btrfs_path *path)
2742{
2743 struct extent_buffer *eb;
2744 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00002745 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002746
2747 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002748 int ret;
2749
Yan, Zhenga22285a2010-05-16 10:48:46 -04002750 if (!path->nodes[level])
2751 break;
2752 eb = path->nodes[level];
2753 if (!btrfs_block_can_be_shared(root, eb))
2754 continue;
2755 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2756 &refs, NULL);
2757 if (refs > 1)
2758 return 1;
2759 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002760 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002761}
2762
2763/*
2764 * helper to start transaction for unlink and rmdir.
2765 *
2766 * unlink and rmdir are special in btrfs, they do not always free space.
2767 * so in enospc case, we should make sure they will free space before
2768 * allowing them to use the global metadata reservation.
2769 */
2770static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2771 struct dentry *dentry)
2772{
2773 struct btrfs_trans_handle *trans;
2774 struct btrfs_root *root = BTRFS_I(dir)->root;
2775 struct btrfs_path *path;
2776 struct btrfs_inode_ref *ref;
2777 struct btrfs_dir_item *di;
2778 struct inode *inode = dentry->d_inode;
2779 u64 index;
2780 int check_link = 1;
2781 int err = -ENOSPC;
2782 int ret;
2783
2784 trans = btrfs_start_transaction(root, 10);
2785 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2786 return trans;
2787
2788 if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2789 return ERR_PTR(-ENOSPC);
2790
2791 /* check if there is someone else holds reference */
2792 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2793 return ERR_PTR(-ENOSPC);
2794
2795 if (atomic_read(&inode->i_count) > 2)
2796 return ERR_PTR(-ENOSPC);
2797
2798 if (xchg(&root->fs_info->enospc_unlink, 1))
2799 return ERR_PTR(-ENOSPC);
2800
2801 path = btrfs_alloc_path();
2802 if (!path) {
2803 root->fs_info->enospc_unlink = 0;
2804 return ERR_PTR(-ENOMEM);
2805 }
2806
2807 trans = btrfs_start_transaction(root, 0);
2808 if (IS_ERR(trans)) {
2809 btrfs_free_path(path);
2810 root->fs_info->enospc_unlink = 0;
2811 return trans;
2812 }
2813
2814 path->skip_locking = 1;
2815 path->search_commit_root = 1;
2816
2817 ret = btrfs_lookup_inode(trans, root, path,
2818 &BTRFS_I(dir)->location, 0);
2819 if (ret < 0) {
2820 err = ret;
2821 goto out;
2822 }
2823 if (ret == 0) {
2824 if (check_path_shared(root, path))
2825 goto out;
2826 } else {
2827 check_link = 0;
2828 }
2829 btrfs_release_path(root, path);
2830
2831 ret = btrfs_lookup_inode(trans, root, path,
2832 &BTRFS_I(inode)->location, 0);
2833 if (ret < 0) {
2834 err = ret;
2835 goto out;
2836 }
2837 if (ret == 0) {
2838 if (check_path_shared(root, path))
2839 goto out;
2840 } else {
2841 check_link = 0;
2842 }
2843 btrfs_release_path(root, path);
2844
2845 if (ret == 0 && S_ISREG(inode->i_mode)) {
2846 ret = btrfs_lookup_file_extent(trans, root, path,
2847 inode->i_ino, (u64)-1, 0);
2848 if (ret < 0) {
2849 err = ret;
2850 goto out;
2851 }
2852 BUG_ON(ret == 0);
2853 if (check_path_shared(root, path))
2854 goto out;
2855 btrfs_release_path(root, path);
2856 }
2857
2858 if (!check_link) {
2859 err = 0;
2860 goto out;
2861 }
2862
2863 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2864 dentry->d_name.name, dentry->d_name.len, 0);
2865 if (IS_ERR(di)) {
2866 err = PTR_ERR(di);
2867 goto out;
2868 }
2869 if (di) {
2870 if (check_path_shared(root, path))
2871 goto out;
2872 } else {
2873 err = 0;
2874 goto out;
2875 }
2876 btrfs_release_path(root, path);
2877
2878 ref = btrfs_lookup_inode_ref(trans, root, path,
2879 dentry->d_name.name, dentry->d_name.len,
2880 inode->i_ino, dir->i_ino, 0);
2881 if (IS_ERR(ref)) {
2882 err = PTR_ERR(ref);
2883 goto out;
2884 }
2885 BUG_ON(!ref);
2886 if (check_path_shared(root, path))
2887 goto out;
2888 index = btrfs_inode_ref_index(path->nodes[0], ref);
2889 btrfs_release_path(root, path);
2890
2891 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index,
2892 dentry->d_name.name, dentry->d_name.len, 0);
2893 if (IS_ERR(di)) {
2894 err = PTR_ERR(di);
2895 goto out;
2896 }
2897 BUG_ON(ret == -ENOENT);
2898 if (check_path_shared(root, path))
2899 goto out;
2900
2901 err = 0;
2902out:
2903 btrfs_free_path(path);
2904 if (err) {
2905 btrfs_end_transaction(trans, root);
2906 root->fs_info->enospc_unlink = 0;
2907 return ERR_PTR(err);
2908 }
2909
2910 trans->block_rsv = &root->fs_info->global_block_rsv;
2911 return trans;
2912}
2913
2914static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2915 struct btrfs_root *root)
2916{
2917 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2918 BUG_ON(!root->fs_info->enospc_unlink);
2919 root->fs_info->enospc_unlink = 0;
2920 }
2921 btrfs_end_transaction_throttle(trans, root);
2922}
2923
Chris Mason39279cc2007-06-12 06:35:45 -04002924static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2925{
Yan, Zhenga22285a2010-05-16 10:48:46 -04002926 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002927 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002928 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002929 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002930 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002931
Yan, Zhenga22285a2010-05-16 10:48:46 -04002932 trans = __unlink_start_trans(dir, dentry);
2933 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002934 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04002935
Chris Mason39279cc2007-06-12 06:35:45 -04002936 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04002937
2938 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2939
Chris Masone02119d2008-09-05 16:13:11 -04002940 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2941 dentry->d_name.name, dentry->d_name.len);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002942 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002943
Yan, Zhenga22285a2010-05-16 10:48:46 -04002944 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04002945 ret = btrfs_orphan_add(trans, inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002946 BUG_ON(ret);
2947 }
Josef Bacik7b128762008-07-24 12:17:14 -04002948
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002949 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002950 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002951 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002952 return ret;
2953}
2954
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002955int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2956 struct btrfs_root *root,
2957 struct inode *dir, u64 objectid,
2958 const char *name, int name_len)
2959{
2960 struct btrfs_path *path;
2961 struct extent_buffer *leaf;
2962 struct btrfs_dir_item *di;
2963 struct btrfs_key key;
2964 u64 index;
2965 int ret;
2966
2967 path = btrfs_alloc_path();
2968 if (!path)
2969 return -ENOMEM;
2970
2971 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2972 name, name_len, -1);
2973 BUG_ON(!di || IS_ERR(di));
2974
2975 leaf = path->nodes[0];
2976 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2977 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2978 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2979 BUG_ON(ret);
2980 btrfs_release_path(root, path);
2981
2982 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2983 objectid, root->root_key.objectid,
2984 dir->i_ino, &index, name, name_len);
2985 if (ret < 0) {
2986 BUG_ON(ret != -ENOENT);
2987 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2988 name, name_len);
2989 BUG_ON(!di || IS_ERR(di));
2990
2991 leaf = path->nodes[0];
2992 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2993 btrfs_release_path(root, path);
2994 index = key.offset;
2995 }
2996
2997 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2998 index, name, name_len, -1);
2999 BUG_ON(!di || IS_ERR(di));
3000
3001 leaf = path->nodes[0];
3002 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3003 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3004 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3005 BUG_ON(ret);
3006 btrfs_release_path(root, path);
3007
3008 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3009 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3010 ret = btrfs_update_inode(trans, root, dir);
3011 BUG_ON(ret);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003012
3013 btrfs_free_path(path);
3014 return 0;
3015}
3016
Chris Mason39279cc2007-06-12 06:35:45 -04003017static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3018{
3019 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003020 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003021 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003022 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05003023 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003024
Chris Mason3394e162008-11-17 20:42:26 -05003025 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003026 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04003027 return -ENOTEMPTY;
3028
Yan, Zhenga22285a2010-05-16 10:48:46 -04003029 trans = __unlink_start_trans(dir, dentry);
3030 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003031 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003032
Chris Mason39279cc2007-06-12 06:35:45 -04003033 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003034
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003035 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
3036 err = btrfs_unlink_subvol(trans, root, dir,
3037 BTRFS_I(inode)->location.objectid,
3038 dentry->d_name.name,
3039 dentry->d_name.len);
3040 goto out;
3041 }
3042
Josef Bacik7b128762008-07-24 12:17:14 -04003043 err = btrfs_orphan_add(trans, inode);
3044 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003045 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003046
Chris Mason39279cc2007-06-12 06:35:45 -04003047 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003048 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3049 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003050 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003051 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003052out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003053 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003054 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003055 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05003056
Chris Mason39279cc2007-06-12 06:35:45 -04003057 return err;
3058}
3059
Chris Masond20f7042008-12-08 16:58:54 -05003060#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04003061/*
Chris Mason323ac952008-10-01 19:05:46 -04003062 * when truncating bytes in a file, it is possible to avoid reading
3063 * the leaves that contain only checksum items. This can be the
3064 * majority of the IO required to delete a large file, but it must
3065 * be done carefully.
3066 *
3067 * The keys in the level just above the leaves are checked to make sure
3068 * the lowest key in a given leaf is a csum key, and starts at an offset
3069 * after the new size.
3070 *
3071 * Then the key for the next leaf is checked to make sure it also has
3072 * a checksum item for the same file. If it does, we know our target leaf
3073 * contains only checksum items, and it can be safely freed without reading
3074 * it.
3075 *
3076 * This is just an optimization targeted at large files. It may do
3077 * nothing. It will return 0 unless things went badly.
3078 */
3079static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
3080 struct btrfs_root *root,
3081 struct btrfs_path *path,
3082 struct inode *inode, u64 new_size)
3083{
3084 struct btrfs_key key;
3085 int ret;
3086 int nritems;
3087 struct btrfs_key found_key;
3088 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003089 struct btrfs_leaf_ref *ref;
3090 u64 leaf_gen;
3091 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04003092
3093 path->lowest_level = 1;
3094 key.objectid = inode->i_ino;
3095 key.type = BTRFS_CSUM_ITEM_KEY;
3096 key.offset = new_size;
3097again:
3098 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3099 if (ret < 0)
3100 goto out;
3101
3102 if (path->nodes[1] == NULL) {
3103 ret = 0;
3104 goto out;
3105 }
3106 ret = 0;
3107 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
3108 nritems = btrfs_header_nritems(path->nodes[1]);
3109
3110 if (!nritems)
3111 goto out;
3112
3113 if (path->slots[1] >= nritems)
3114 goto next_node;
3115
3116 /* did we find a key greater than anything we want to delete? */
3117 if (found_key.objectid > inode->i_ino ||
3118 (found_key.objectid == inode->i_ino && found_key.type > key.type))
3119 goto out;
3120
3121 /* we check the next key in the node to make sure the leave contains
3122 * only checksum items. This comparison doesn't work if our
3123 * leaf is the last one in the node
3124 */
3125 if (path->slots[1] + 1 >= nritems) {
3126next_node:
3127 /* search forward from the last key in the node, this
3128 * will bring us into the next node in the tree
3129 */
3130 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
3131
3132 /* unlikely, but we inc below, so check to be safe */
3133 if (found_key.offset == (u64)-1)
3134 goto out;
3135
3136 /* search_forward needs a path with locks held, do the
3137 * search again for the original key. It is possible
3138 * this will race with a balance and return a path that
3139 * we could modify, but this drop is just an optimization
3140 * and is allowed to miss some leaves.
3141 */
3142 btrfs_release_path(root, path);
3143 found_key.offset++;
3144
3145 /* setup a max key for search_forward */
3146 other_key.offset = (u64)-1;
3147 other_key.type = key.type;
3148 other_key.objectid = key.objectid;
3149
3150 path->keep_locks = 1;
3151 ret = btrfs_search_forward(root, &found_key, &other_key,
3152 path, 0, 0);
3153 path->keep_locks = 0;
3154 if (ret || found_key.objectid != key.objectid ||
3155 found_key.type != key.type) {
3156 ret = 0;
3157 goto out;
3158 }
3159
3160 key.offset = found_key.offset;
3161 btrfs_release_path(root, path);
3162 cond_resched();
3163 goto again;
3164 }
3165
3166 /* we know there's one more slot after us in the tree,
3167 * read that key so we can verify it is also a checksum item
3168 */
3169 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
3170
3171 if (found_key.objectid < inode->i_ino)
3172 goto next_key;
3173
3174 if (found_key.type != key.type || found_key.offset < new_size)
3175 goto next_key;
3176
3177 /*
3178 * if the key for the next leaf isn't a csum key from this objectid,
3179 * we can't be sure there aren't good items inside this leaf.
3180 * Bail out
3181 */
3182 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
3183 goto out;
3184
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003185 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
3186 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003187 /*
3188 * it is safe to delete this leaf, it contains only
3189 * csum items from this inode at an offset >= new_size
3190 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003191 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04003192 BUG_ON(ret);
3193
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003194 if (root->ref_cows && leaf_gen < trans->transid) {
3195 ref = btrfs_alloc_leaf_ref(root, 0);
3196 if (ref) {
3197 ref->root_gen = root->root_key.offset;
3198 ref->bytenr = leaf_start;
3199 ref->owner = 0;
3200 ref->generation = leaf_gen;
3201 ref->nritems = 0;
3202
Chris Masonbd56b302009-02-04 09:27:02 -05003203 btrfs_sort_leaf_ref(ref);
3204
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003205 ret = btrfs_add_leaf_ref(root, ref, 0);
3206 WARN_ON(ret);
3207 btrfs_free_leaf_ref(root, ref);
3208 } else {
3209 WARN_ON(1);
3210 }
3211 }
Chris Mason323ac952008-10-01 19:05:46 -04003212next_key:
3213 btrfs_release_path(root, path);
3214
3215 if (other_key.objectid == inode->i_ino &&
3216 other_key.type == key.type && other_key.offset > key.offset) {
3217 key.offset = other_key.offset;
3218 cond_resched();
3219 goto again;
3220 }
3221 ret = 0;
3222out:
3223 /* fixup any changes we've made to the path */
3224 path->lowest_level = 0;
3225 path->keep_locks = 0;
3226 btrfs_release_path(root, path);
3227 return ret;
3228}
3229
Chris Masond20f7042008-12-08 16:58:54 -05003230#endif
3231
Chris Mason323ac952008-10-01 19:05:46 -04003232/*
Chris Mason39279cc2007-06-12 06:35:45 -04003233 * this can truncate away extent items, csum items and directory items.
3234 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003235 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003236 *
3237 * csum items that cross the new i_size are truncated to the new size
3238 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003239 *
3240 * min_type is the minimum key type to truncate down to. If set to 0, this
3241 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003242 */
Yan, Zheng80825102009-11-12 09:35:36 +00003243int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3244 struct btrfs_root *root,
3245 struct inode *inode,
3246 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003247{
Chris Mason39279cc2007-06-12 06:35:45 -04003248 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003249 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003250 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003251 struct btrfs_key key;
3252 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003253 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003254 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003255 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003256 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003257 u64 mask = root->sectorsize - 1;
3258 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003259 int found_extent;
3260 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003261 int pending_del_nr = 0;
3262 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003263 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05003264 int encoding;
Yan, Zheng80825102009-11-12 09:35:36 +00003265 int ret;
3266 int err = 0;
3267
3268 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003269
Josef Bacik0af3d002010-06-21 14:48:16 -04003270 if (root->ref_cows || root == root->fs_info->tree_root)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003271 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003272
Chris Mason39279cc2007-06-12 06:35:45 -04003273 path = btrfs_alloc_path();
3274 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04003275 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04003276
Chris Mason39279cc2007-06-12 06:35:45 -04003277 key.objectid = inode->i_ino;
3278 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003279 key.type = (u8)-1;
3280
Chris Mason85e21ba2008-01-29 15:11:36 -05003281search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003282 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003283 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003284 if (ret < 0) {
3285 err = ret;
3286 goto out;
3287 }
Chris Masond3977122009-01-05 21:25:51 -05003288
Chris Mason85e21ba2008-01-29 15:11:36 -05003289 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003290 /* there are no items in the tree for us to truncate, we're
3291 * done
3292 */
Yan, Zheng80825102009-11-12 09:35:36 +00003293 if (path->slots[0] == 0)
3294 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003295 path->slots[0]--;
3296 }
3297
Chris Masond3977122009-01-05 21:25:51 -05003298 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003299 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003300 leaf = path->nodes[0];
3301 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3302 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05003303 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003304
Chris Mason5f39d392007-10-15 16:14:19 -04003305 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003306 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003307
Chris Mason85e21ba2008-01-29 15:11:36 -05003308 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003309 break;
3310
Chris Mason5f39d392007-10-15 16:14:19 -04003311 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003312 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003313 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003314 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003315 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003316 encoding = btrfs_file_extent_compression(leaf, fi);
3317 encoding |= btrfs_file_extent_encryption(leaf, fi);
3318 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
3319
Chris Mason179e29e2007-11-01 11:28:41 -04003320 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003321 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003322 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003323 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003324 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003325 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003326 }
Yan008630c2007-11-07 13:31:09 -05003327 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003328 }
Yan, Zheng80825102009-11-12 09:35:36 +00003329 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003330 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003331 } else {
3332 if (item_end < new_size)
3333 break;
3334 if (found_key.offset >= new_size)
3335 del_item = 1;
3336 else
3337 del_item = 0;
3338 }
Chris Mason39279cc2007-06-12 06:35:45 -04003339 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003340 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003341 if (found_type != BTRFS_EXTENT_DATA_KEY)
3342 goto delete;
3343
3344 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003345 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003346 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003347 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04003348 u64 orig_num_bytes =
3349 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003350 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003351 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003352 extent_num_bytes = extent_num_bytes &
3353 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003354 btrfs_set_file_extent_num_bytes(leaf, fi,
3355 extent_num_bytes);
3356 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003357 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003358 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003359 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003360 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003361 } else {
Chris Masondb945352007-10-15 16:15:53 -04003362 extent_num_bytes =
3363 btrfs_file_extent_disk_num_bytes(leaf,
3364 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003365 extent_offset = found_key.offset -
3366 btrfs_file_extent_offset(leaf, fi);
3367
Chris Mason39279cc2007-06-12 06:35:45 -04003368 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003369 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003370 if (extent_start != 0) {
3371 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003372 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003373 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003374 }
3375 }
Chris Mason90692182008-02-08 13:49:28 -05003376 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003377 /*
3378 * we can't truncate inline items that have had
3379 * special encodings
3380 */
3381 if (!del_item &&
3382 btrfs_file_extent_compression(leaf, fi) == 0 &&
3383 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3384 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003385 u32 size = new_size - found_key.offset;
3386
3387 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003388 inode_sub_bytes(inode, item_end + 1 -
3389 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003390 }
3391 size =
3392 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05003393 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04003394 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05003395 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04003396 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003397 inode_sub_bytes(inode, item_end + 1 -
3398 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003399 }
Chris Mason39279cc2007-06-12 06:35:45 -04003400 }
Chris Mason179e29e2007-11-01 11:28:41 -04003401delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003402 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003403 if (!pending_del_nr) {
3404 /* no pending yet, add ourselves */
3405 pending_del_slot = path->slots[0];
3406 pending_del_nr = 1;
3407 } else if (pending_del_nr &&
3408 path->slots[0] + 1 == pending_del_slot) {
3409 /* hop on the pending chunk */
3410 pending_del_nr++;
3411 pending_del_slot = path->slots[0];
3412 } else {
Chris Masond3977122009-01-05 21:25:51 -05003413 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003414 }
Chris Mason39279cc2007-06-12 06:35:45 -04003415 } else {
3416 break;
3417 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003418 if (found_extent && (root->ref_cows ||
3419 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003420 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003421 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003422 extent_num_bytes, 0,
3423 btrfs_header_owner(leaf),
3424 inode->i_ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04003425 BUG_ON(ret);
3426 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003427
Yan, Zheng80825102009-11-12 09:35:36 +00003428 if (found_type == BTRFS_INODE_ITEM_KEY)
3429 break;
3430
3431 if (path->slots[0] == 0 ||
3432 path->slots[0] != pending_del_slot) {
3433 if (root->ref_cows) {
3434 err = -EAGAIN;
3435 goto out;
3436 }
3437 if (pending_del_nr) {
3438 ret = btrfs_del_items(trans, root, path,
3439 pending_del_slot,
3440 pending_del_nr);
3441 BUG_ON(ret);
3442 pending_del_nr = 0;
3443 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003444 btrfs_release_path(root, path);
3445 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003446 } else {
3447 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003448 }
Chris Mason39279cc2007-06-12 06:35:45 -04003449 }
Yan, Zheng80825102009-11-12 09:35:36 +00003450out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003451 if (pending_del_nr) {
3452 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3453 pending_del_nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003454 BUG_ON(ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003455 }
Chris Mason39279cc2007-06-12 06:35:45 -04003456 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003457 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003458}
3459
Chris Masona52d9a82007-08-27 16:49:44 -04003460/*
3461 * taken from block_truncate_page, but does cow as it zeros out
3462 * any bytes left in the last page in the file.
3463 */
3464static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3465{
3466 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003467 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003468 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3469 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003470 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003471 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003472 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003473 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3474 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3475 struct page *page;
3476 int ret = 0;
3477 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003478 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003479
3480 if ((offset & (blocksize - 1)) == 0)
3481 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003482 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003483 if (ret)
3484 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003485
3486 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003487again:
Chris Masona52d9a82007-08-27 16:49:44 -04003488 page = grab_cache_page(mapping, index);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003489 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003490 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003491 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003492 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003493
3494 page_start = page_offset(page);
3495 page_end = page_start + PAGE_CACHE_SIZE - 1;
3496
Chris Masona52d9a82007-08-27 16:49:44 -04003497 if (!PageUptodate(page)) {
3498 ret = btrfs_readpage(NULL, page);
3499 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003500 if (page->mapping != mapping) {
3501 unlock_page(page);
3502 page_cache_release(page);
3503 goto again;
3504 }
Chris Masona52d9a82007-08-27 16:49:44 -04003505 if (!PageUptodate(page)) {
3506 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003507 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003508 }
3509 }
Chris Mason211c17f2008-05-15 09:13:45 -04003510 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003511
Josef Bacik2ac55d42010-02-03 19:33:23 +00003512 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
3513 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003514 set_page_extent_mapped(page);
3515
3516 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3517 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003518 unlock_extent_cached(io_tree, page_start, page_end,
3519 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003520 unlock_page(page);
3521 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003522 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003523 btrfs_put_ordered_extent(ordered);
3524 goto again;
3525 }
3526
Josef Bacik2ac55d42010-02-03 19:33:23 +00003527 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003528 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003529 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003530
Josef Bacik2ac55d42010-02-03 19:33:23 +00003531 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3532 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003533 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003534 unlock_extent_cached(io_tree, page_start, page_end,
3535 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003536 goto out_unlock;
3537 }
3538
Chris Masone6dcd2d2008-07-17 12:53:50 -04003539 ret = 0;
3540 if (offset != PAGE_CACHE_SIZE) {
3541 kaddr = kmap(page);
3542 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3543 flush_dcache_page(page);
3544 kunmap(page);
3545 }
Chris Mason247e7432008-07-17 12:53:51 -04003546 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003547 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003548 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3549 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003550
Chris Mason89642222008-07-24 09:41:53 -04003551out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003552 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003553 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003554 unlock_page(page);
3555 page_cache_release(page);
3556out:
3557 return ret;
3558}
3559
Josef Bacika41ad392011-01-31 15:30:16 -05003560int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003561{
3562 struct btrfs_trans_handle *trans;
3563 struct btrfs_root *root = BTRFS_I(inode)->root;
3564 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003565 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003566 struct extent_state *cached_state = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003567 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003568 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003569 u64 block_end = (size + mask) & ~mask;
3570 u64 last_byte;
3571 u64 cur_offset;
3572 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003573 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003574
3575 if (size <= hole_start)
3576 return 0;
3577
Yan Zheng9036c102008-10-30 14:19:41 -04003578 while (1) {
3579 struct btrfs_ordered_extent *ordered;
3580 btrfs_wait_ordered_range(inode, hole_start,
3581 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003582 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3583 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003584 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3585 if (!ordered)
3586 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003587 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3588 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003589 btrfs_put_ordered_extent(ordered);
3590 }
3591
Yan Zheng9036c102008-10-30 14:19:41 -04003592 cur_offset = hole_start;
3593 while (1) {
3594 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3595 block_end - cur_offset, 0);
3596 BUG_ON(IS_ERR(em) || !em);
3597 last_byte = min(extent_map_end(em), block_end);
3598 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003599 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003600 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003601 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003602
Yan, Zhenga22285a2010-05-16 10:48:46 -04003603 trans = btrfs_start_transaction(root, 2);
3604 if (IS_ERR(trans)) {
3605 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003606 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003607 }
Yan, Zheng80825102009-11-12 09:35:36 +00003608 btrfs_set_trans_block_group(trans, inode);
3609
3610 err = btrfs_drop_extents(trans, inode, cur_offset,
3611 cur_offset + hole_size,
3612 &hint_byte, 1);
Josef Bacik3893e332011-01-31 16:03:11 -05003613 if (err)
3614 break;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003615
Yan Zheng9036c102008-10-30 14:19:41 -04003616 err = btrfs_insert_file_extent(trans, root,
3617 inode->i_ino, cur_offset, 0,
3618 0, hole_size, 0, hole_size,
3619 0, 0, 0);
Josef Bacik3893e332011-01-31 16:03:11 -05003620 if (err)
3621 break;
Yan, Zheng80825102009-11-12 09:35:36 +00003622
Yan Zheng9036c102008-10-30 14:19:41 -04003623 btrfs_drop_extent_cache(inode, hole_start,
3624 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003625
3626 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003627 }
3628 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003629 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003630 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003631 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003632 break;
3633 }
3634
Yan, Zhenga22285a2010-05-16 10:48:46 -04003635 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003636 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3637 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003638 return err;
3639}
3640
Josef Bacika41ad392011-01-31 15:30:16 -05003641static int btrfs_setsize(struct inode *inode, loff_t newsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003642{
Josef Bacika41ad392011-01-31 15:30:16 -05003643 loff_t oldsize = i_size_read(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003644 int ret;
3645
Josef Bacika41ad392011-01-31 15:30:16 -05003646 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003647 return 0;
3648
Josef Bacika41ad392011-01-31 15:30:16 -05003649 if (newsize > oldsize) {
3650 i_size_write(inode, newsize);
3651 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3652 truncate_pagecache(inode, oldsize, newsize);
3653 ret = btrfs_cont_expand(inode, oldsize, newsize);
Yan, Zheng80825102009-11-12 09:35:36 +00003654 if (ret) {
Josef Bacika41ad392011-01-31 15:30:16 -05003655 btrfs_setsize(inode, oldsize);
Yan, Zheng80825102009-11-12 09:35:36 +00003656 return ret;
3657 }
3658
Josef Bacik930f0282011-03-04 14:41:41 -05003659 mark_inode_dirty(inode);
Josef Bacika41ad392011-01-31 15:30:16 -05003660 } else {
3661
3662 /*
3663 * We're truncating a file that used to have good data down to
3664 * zero. Make sure it gets into the ordered flush list so that
3665 * any new writes get down to disk quickly.
3666 */
3667 if (newsize == 0)
3668 BTRFS_I(inode)->ordered_data_close = 1;
3669
3670 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3671 truncate_setsize(inode, newsize);
3672 ret = btrfs_truncate(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003673 }
3674
Josef Bacika41ad392011-01-31 15:30:16 -05003675 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003676}
3677
Chris Mason39279cc2007-06-12 06:35:45 -04003678static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3679{
3680 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003681 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003682 int err;
3683
Li Zefanb83cc962010-12-20 16:04:08 +08003684 if (btrfs_root_readonly(root))
3685 return -EROFS;
3686
Chris Mason39279cc2007-06-12 06:35:45 -04003687 err = inode_change_ok(inode, attr);
3688 if (err)
3689 return err;
3690
Chris Mason5a3f23d2009-03-31 13:27:11 -04003691 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Josef Bacika41ad392011-01-31 15:30:16 -05003692 err = btrfs_setsize(inode, attr->ia_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003693 if (err)
3694 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003695 }
Yan Zheng9036c102008-10-30 14:19:41 -04003696
Christoph Hellwig10257742010-06-04 11:30:02 +02003697 if (attr->ia_valid) {
3698 setattr_copy(inode, attr);
3699 mark_inode_dirty(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003700
Christoph Hellwig10257742010-06-04 11:30:02 +02003701 if (attr->ia_valid & ATTR_MODE)
3702 err = btrfs_acl_chmod(inode);
3703 }
3704
Chris Mason39279cc2007-06-12 06:35:45 -04003705 return err;
3706}
Chris Mason61295eb2008-01-14 16:24:38 -05003707
Al Virobd555972010-06-07 11:35:40 -04003708void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003709{
3710 struct btrfs_trans_handle *trans;
3711 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003712 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003713 int ret;
3714
3715 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003716 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
3717 root == root->fs_info->tree_root))
Al Virobd555972010-06-07 11:35:40 -04003718 goto no_delete;
3719
Chris Mason39279cc2007-06-12 06:35:45 -04003720 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003721 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003722 goto no_delete;
3723 }
Al Virobd555972010-06-07 11:35:40 -04003724 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003725 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003726
Yan, Zhengc71bf092009-11-12 09:34:40 +00003727 if (root->fs_info->log_root_recovering) {
3728 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3729 goto no_delete;
3730 }
3731
Yan, Zheng76dda932009-09-21 16:00:26 -04003732 if (inode->i_nlink > 0) {
3733 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3734 goto no_delete;
3735 }
3736
Chris Masondbe674a2008-07-17 12:54:05 -04003737 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003738
Yan, Zheng80825102009-11-12 09:35:36 +00003739 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003740 trans = btrfs_start_transaction(root, 0);
3741 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00003742 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003743 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003744
Yan, Zhengd68fc572010-05-16 10:49:58 -04003745 ret = btrfs_block_rsv_check(trans, root,
3746 root->orphan_block_rsv, 0, 5);
3747 if (ret) {
3748 BUG_ON(ret != -EAGAIN);
3749 ret = btrfs_commit_transaction(trans, root);
3750 BUG_ON(ret);
3751 continue;
3752 }
3753
3754 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003755 if (ret != -EAGAIN)
3756 break;
3757
3758 nr = trans->blocks_used;
3759 btrfs_end_transaction(trans, root);
3760 trans = NULL;
3761 btrfs_btree_balance_dirty(root, nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003762
Josef Bacik7b128762008-07-24 12:17:14 -04003763 }
3764
Yan, Zheng80825102009-11-12 09:35:36 +00003765 if (ret == 0) {
3766 ret = btrfs_orphan_del(trans, inode);
3767 BUG_ON(ret);
3768 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003769
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003770 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003771 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003772 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003773no_delete:
Al Virobd555972010-06-07 11:35:40 -04003774 end_writeback(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003775 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003776}
3777
3778/*
3779 * this returns the key found in the dir entry in the location pointer.
3780 * If no dir entries were found, location->objectid is 0.
3781 */
3782static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3783 struct btrfs_key *location)
3784{
3785 const char *name = dentry->d_name.name;
3786 int namelen = dentry->d_name.len;
3787 struct btrfs_dir_item *di;
3788 struct btrfs_path *path;
3789 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003790 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003791
3792 path = btrfs_alloc_path();
3793 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003794
Chris Mason39279cc2007-06-12 06:35:45 -04003795 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3796 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003797 if (IS_ERR(di))
3798 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003799
3800 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003801 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003802
Chris Mason5f39d392007-10-15 16:14:19 -04003803 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003804out:
Chris Mason39279cc2007-06-12 06:35:45 -04003805 btrfs_free_path(path);
3806 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003807out_err:
3808 location->objectid = 0;
3809 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003810}
3811
3812/*
3813 * when we hit a tree root in a directory, the btrfs part of the inode
3814 * needs to be changed to reflect the root directory of the tree root. This
3815 * is kind of like crossing a mount point.
3816 */
3817static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003818 struct inode *dir,
3819 struct dentry *dentry,
3820 struct btrfs_key *location,
3821 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003822{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003823 struct btrfs_path *path;
3824 struct btrfs_root *new_root;
3825 struct btrfs_root_ref *ref;
3826 struct extent_buffer *leaf;
3827 int ret;
3828 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003829
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003830 path = btrfs_alloc_path();
3831 if (!path) {
3832 err = -ENOMEM;
3833 goto out;
3834 }
Chris Mason39279cc2007-06-12 06:35:45 -04003835
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003836 err = -ENOENT;
3837 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3838 BTRFS_I(dir)->root->root_key.objectid,
3839 location->objectid);
3840 if (ret) {
3841 if (ret < 0)
3842 err = ret;
3843 goto out;
3844 }
Chris Mason39279cc2007-06-12 06:35:45 -04003845
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003846 leaf = path->nodes[0];
3847 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3848 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3849 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3850 goto out;
3851
3852 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3853 (unsigned long)(ref + 1),
3854 dentry->d_name.len);
3855 if (ret)
3856 goto out;
3857
3858 btrfs_release_path(root->fs_info->tree_root, path);
3859
3860 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3861 if (IS_ERR(new_root)) {
3862 err = PTR_ERR(new_root);
3863 goto out;
3864 }
3865
3866 if (btrfs_root_refs(&new_root->root_item) == 0) {
3867 err = -ENOENT;
3868 goto out;
3869 }
3870
3871 *sub_root = new_root;
3872 location->objectid = btrfs_root_dirid(&new_root->root_item);
3873 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003874 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003875 err = 0;
3876out:
3877 btrfs_free_path(path);
3878 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003879}
3880
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003881static void inode_tree_add(struct inode *inode)
3882{
3883 struct btrfs_root *root = BTRFS_I(inode)->root;
3884 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003885 struct rb_node **p;
3886 struct rb_node *parent;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003887again:
3888 p = &root->inode_tree.rb_node;
3889 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003890
Al Viro1d3382cb2010-10-23 15:19:20 -04003891 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003892 return;
3893
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003894 spin_lock(&root->inode_lock);
3895 while (*p) {
3896 parent = *p;
3897 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3898
3899 if (inode->i_ino < entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003900 p = &parent->rb_left;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003901 else if (inode->i_ino > entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003902 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003903 else {
3904 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04003905 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003906 rb_erase(parent, &root->inode_tree);
3907 RB_CLEAR_NODE(parent);
3908 spin_unlock(&root->inode_lock);
3909 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003910 }
3911 }
3912 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3913 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3914 spin_unlock(&root->inode_lock);
3915}
3916
3917static void inode_tree_del(struct inode *inode)
3918{
3919 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003920 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003921
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003922 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003923 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003924 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003925 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003926 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003927 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003928 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003929
Josef Bacik0af3d002010-06-21 14:48:16 -04003930 /*
3931 * Free space cache has inodes in the tree root, but the tree root has a
3932 * root_refs of 0, so this could end up dropping the tree root as a
3933 * snapshot, so we need the extra !root->fs_info->tree_root check to
3934 * make sure we don't drop it.
3935 */
3936 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
3937 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003938 synchronize_srcu(&root->fs_info->subvol_srcu);
3939 spin_lock(&root->inode_lock);
3940 empty = RB_EMPTY_ROOT(&root->inode_tree);
3941 spin_unlock(&root->inode_lock);
3942 if (empty)
3943 btrfs_add_dead_root(root);
3944 }
3945}
3946
3947int btrfs_invalidate_inodes(struct btrfs_root *root)
3948{
3949 struct rb_node *node;
3950 struct rb_node *prev;
3951 struct btrfs_inode *entry;
3952 struct inode *inode;
3953 u64 objectid = 0;
3954
3955 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3956
3957 spin_lock(&root->inode_lock);
3958again:
3959 node = root->inode_tree.rb_node;
3960 prev = NULL;
3961 while (node) {
3962 prev = node;
3963 entry = rb_entry(node, struct btrfs_inode, rb_node);
3964
3965 if (objectid < entry->vfs_inode.i_ino)
3966 node = node->rb_left;
3967 else if (objectid > entry->vfs_inode.i_ino)
3968 node = node->rb_right;
3969 else
3970 break;
3971 }
3972 if (!node) {
3973 while (prev) {
3974 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3975 if (objectid <= entry->vfs_inode.i_ino) {
3976 node = prev;
3977 break;
3978 }
3979 prev = rb_next(prev);
3980 }
3981 }
3982 while (node) {
3983 entry = rb_entry(node, struct btrfs_inode, rb_node);
3984 objectid = entry->vfs_inode.i_ino + 1;
3985 inode = igrab(&entry->vfs_inode);
3986 if (inode) {
3987 spin_unlock(&root->inode_lock);
3988 if (atomic_read(&inode->i_count) > 1)
3989 d_prune_aliases(inode);
3990 /*
Al Viro45321ac2010-06-07 13:43:19 -04003991 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04003992 * the inode cache when its usage count
3993 * hits zero.
3994 */
3995 iput(inode);
3996 cond_resched();
3997 spin_lock(&root->inode_lock);
3998 goto again;
3999 }
4000
4001 if (cond_resched_lock(&root->inode_lock))
4002 goto again;
4003
4004 node = rb_next(node);
4005 }
4006 spin_unlock(&root->inode_lock);
4007 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004008}
4009
Chris Masone02119d2008-09-05 16:13:11 -04004010static int btrfs_init_locked_inode(struct inode *inode, void *p)
4011{
4012 struct btrfs_iget_args *args = p;
4013 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004014 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05004015 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004016 return 0;
4017}
4018
4019static int btrfs_find_actor(struct inode *inode, void *opaque)
4020{
4021 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05004022 return args->ino == inode->i_ino &&
4023 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004024}
4025
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004026static struct inode *btrfs_iget_locked(struct super_block *s,
4027 u64 objectid,
4028 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004029{
4030 struct inode *inode;
4031 struct btrfs_iget_args args;
4032 args.ino = objectid;
4033 args.root = root;
4034
4035 inode = iget5_locked(s, objectid, btrfs_find_actor,
4036 btrfs_init_locked_inode,
4037 (void *)&args);
4038 return inode;
4039}
4040
Balaji Rao1a54ef82008-07-21 02:01:04 +05304041/* Get an inode object given its location and corresponding root.
4042 * Returns in *is_new if the inode was read from disk
4043 */
4044struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004045 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304046{
4047 struct inode *inode;
4048
4049 inode = btrfs_iget_locked(s, location->objectid, root);
4050 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004051 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304052
4053 if (inode->i_state & I_NEW) {
4054 BTRFS_I(inode)->root = root;
4055 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4056 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004057
4058 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304059 unlock_new_inode(inode);
Josef Bacik73f73412009-12-04 17:38:27 +00004060 if (new)
4061 *new = 1;
Balaji Rao1a54ef82008-07-21 02:01:04 +05304062 }
4063
4064 return inode;
4065}
4066
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004067static struct inode *new_simple_dir(struct super_block *s,
4068 struct btrfs_key *key,
4069 struct btrfs_root *root)
4070{
4071 struct inode *inode = new_inode(s);
4072
4073 if (!inode)
4074 return ERR_PTR(-ENOMEM);
4075
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004076 BTRFS_I(inode)->root = root;
4077 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
4078 BTRFS_I(inode)->dummy_inode = 1;
4079
4080 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
4081 inode->i_op = &simple_dir_inode_operations;
4082 inode->i_fop = &simple_dir_operations;
4083 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4084 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4085
4086 return inode;
4087}
4088
Chris Mason3de45862008-11-17 21:02:50 -05004089struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004090{
Chris Masond3977122009-01-05 21:25:51 -05004091 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004092 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004093 struct btrfs_root *sub_root = root;
4094 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004095 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004096 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004097
4098 if (dentry->d_name.len > BTRFS_NAME_LEN)
4099 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004100
Chris Mason39279cc2007-06-12 06:35:45 -04004101 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04004102
Chris Mason39279cc2007-06-12 06:35:45 -04004103 if (ret < 0)
4104 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004105
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004106 if (location.objectid == 0)
4107 return NULL;
4108
4109 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004110 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004111 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004112 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004113
4114 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4115
Yan, Zheng76dda932009-09-21 16:00:26 -04004116 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004117 ret = fixup_tree_root_location(root, dir, dentry,
4118 &location, &sub_root);
4119 if (ret < 0) {
4120 if (ret != -ENOENT)
4121 inode = ERR_PTR(ret);
4122 else
4123 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4124 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004125 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004126 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004127 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4128
Julia Lawall34d19ba2011-01-24 19:55:19 +00004129 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004130 down_read(&root->fs_info->cleanup_work_sem);
4131 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004132 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004133 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004134 if (ret)
4135 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004136 }
4137
Chris Mason3de45862008-11-17 21:02:50 -05004138 return inode;
4139}
4140
Nick Pigginfe15ce42011-01-07 17:49:23 +11004141static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004142{
4143 struct btrfs_root *root;
4144
Yan, Zhengefefb142009-10-09 09:25:16 -04004145 if (!dentry->d_inode && !IS_ROOT(dentry))
4146 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04004147
Yan, Zhengefefb142009-10-09 09:25:16 -04004148 if (dentry->d_inode) {
4149 root = BTRFS_I(dentry->d_inode)->root;
4150 if (btrfs_root_refs(&root->root_item) == 0)
4151 return 1;
4152 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004153 return 0;
4154}
4155
Chris Mason3de45862008-11-17 21:02:50 -05004156static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4157 struct nameidata *nd)
4158{
4159 struct inode *inode;
4160
Chris Mason3de45862008-11-17 21:02:50 -05004161 inode = btrfs_lookup_dentry(dir, dentry);
4162 if (IS_ERR(inode))
4163 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04004164
Chris Mason39279cc2007-06-12 06:35:45 -04004165 return d_splice_alias(inode, dentry);
4166}
4167
Chris Mason39279cc2007-06-12 06:35:45 -04004168static unsigned char btrfs_filetype_table[] = {
4169 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4170};
4171
David Woodhousecbdf5a22008-08-06 19:42:33 +01004172static int btrfs_real_readdir(struct file *filp, void *dirent,
4173 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004174{
Chris Mason6da6aba2007-12-18 16:15:09 -05004175 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004176 struct btrfs_root *root = BTRFS_I(inode)->root;
4177 struct btrfs_item *item;
4178 struct btrfs_dir_item *di;
4179 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004180 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004181 struct btrfs_path *path;
4182 int ret;
4183 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04004184 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004185 int slot;
4186 int advance;
4187 unsigned char d_type;
4188 int over = 0;
4189 u32 di_cur;
4190 u32 di_total;
4191 u32 di_len;
4192 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004193 char tmp_name[32];
4194 char *name_ptr;
4195 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04004196
4197 /* FIXME, use a real flag for deciding about the key type */
4198 if (root->fs_info->tree_root == root)
4199 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004200
Chris Mason39544012007-12-12 14:38:19 -05004201 /* special case for "." */
4202 if (filp->f_pos == 0) {
4203 over = filldir(dirent, ".", 1,
4204 1, inode->i_ino,
4205 DT_DIR);
4206 if (over)
4207 return 0;
4208 filp->f_pos = 1;
4209 }
Chris Mason39544012007-12-12 14:38:19 -05004210 /* special case for .., just use the back ref */
4211 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004212 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004213 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004214 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004215 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004216 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004217 filp->f_pos = 2;
4218 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004219 path = btrfs_alloc_path();
4220 path->reada = 2;
4221
Chris Mason39279cc2007-06-12 06:35:45 -04004222 btrfs_set_key_type(&key, key_type);
4223 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01004224 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04004225
Chris Mason39279cc2007-06-12 06:35:45 -04004226 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4227 if (ret < 0)
4228 goto err;
4229 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01004230
4231 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004232 leaf = path->nodes[0];
4233 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004234 slot = path->slots[0];
4235 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01004236 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004237 ret = btrfs_next_leaf(root, path);
4238 if (ret)
4239 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004240 leaf = path->nodes[0];
4241 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004242 slot = path->slots[0];
4243 } else {
4244 slot++;
4245 path->slots[0]++;
4246 }
4247 }
Chris Mason3de45862008-11-17 21:02:50 -05004248
Chris Mason39279cc2007-06-12 06:35:45 -04004249 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04004250 item = btrfs_item_nr(leaf, slot);
4251 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4252
4253 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004254 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004255 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004256 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004257 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04004258 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04004259
4260 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01004261
Chris Mason39279cc2007-06-12 06:35:45 -04004262 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4263 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004264 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004265
4266 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004267 struct btrfs_key location;
4268
4269 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004270 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004271 name_ptr = tmp_name;
4272 } else {
4273 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004274 if (!name_ptr) {
4275 ret = -ENOMEM;
4276 goto err;
4277 }
Chris Mason5f39d392007-10-15 16:14:19 -04004278 }
4279 read_extent_buffer(leaf, name_ptr,
4280 (unsigned long)(di + 1), name_len);
4281
4282 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4283 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004284
4285 /* is this a reference to our own snapshot? If so
4286 * skip it
4287 */
4288 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4289 location.objectid == root->root_key.objectid) {
4290 over = 0;
4291 goto skip;
4292 }
Chris Mason5f39d392007-10-15 16:14:19 -04004293 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004294 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004295 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004296
Chris Mason3de45862008-11-17 21:02:50 -05004297skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004298 if (name_ptr != tmp_name)
4299 kfree(name_ptr);
4300
Chris Mason39279cc2007-06-12 06:35:45 -04004301 if (over)
4302 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004303 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004304 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004305 di_cur += di_len;
4306 di = (struct btrfs_dir_item *)((char *)di + di_len);
4307 }
4308 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004309
4310 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004311 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004312 /*
4313 * 32-bit glibc will use getdents64, but then strtol -
4314 * so the last number we can serve is this.
4315 */
4316 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004317 else
4318 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004319nopos:
4320 ret = 0;
4321err:
Chris Mason39279cc2007-06-12 06:35:45 -04004322 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004323 return ret;
4324}
4325
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004326int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004327{
4328 struct btrfs_root *root = BTRFS_I(inode)->root;
4329 struct btrfs_trans_handle *trans;
4330 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004331 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004332
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004333 if (BTRFS_I(inode)->dummy_inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04004334 return 0;
4335
Josef Bacik0af3d002010-06-21 14:48:16 -04004336 smp_mb();
4337 nolock = (root->fs_info->closing && root == root->fs_info->tree_root);
4338
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004339 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004340 if (nolock)
4341 trans = btrfs_join_transaction_nolock(root, 1);
4342 else
4343 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004344 if (IS_ERR(trans))
4345 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04004346 btrfs_set_trans_block_group(trans, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04004347 if (nolock)
4348 ret = btrfs_end_transaction_nolock(trans, root);
4349 else
4350 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004351 }
4352 return ret;
4353}
4354
4355/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004356 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004357 * inode changes. But, it is most likely to find the inode in cache.
4358 * FIXME, needs more benchmarking...there are no reasons other than performance
4359 * to keep or drop this code.
4360 */
4361void btrfs_dirty_inode(struct inode *inode)
4362{
4363 struct btrfs_root *root = BTRFS_I(inode)->root;
4364 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004365 int ret;
4366
4367 if (BTRFS_I(inode)->dummy_inode)
4368 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004369
Chris Masonf9295742008-07-17 12:54:14 -04004370 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004371 BUG_ON(IS_ERR(trans));
Chris Mason39279cc2007-06-12 06:35:45 -04004372 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004373
4374 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004375 if (ret && ret == -ENOSPC) {
4376 /* whoops, lets try again with the full transaction */
4377 btrfs_end_transaction(trans, root);
4378 trans = btrfs_start_transaction(root, 1);
Chris Mason9aeead72010-05-27 10:23:00 -04004379 if (IS_ERR(trans)) {
4380 if (printk_ratelimit()) {
4381 printk(KERN_ERR "btrfs: fail to "
4382 "dirty inode %lu error %ld\n",
4383 inode->i_ino, PTR_ERR(trans));
4384 }
4385 return;
4386 }
Chris Mason94b60442010-05-26 11:02:00 -04004387 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004388
Chris Mason94b60442010-05-26 11:02:00 -04004389 ret = btrfs_update_inode(trans, root, inode);
4390 if (ret) {
Chris Mason9aeead72010-05-27 10:23:00 -04004391 if (printk_ratelimit()) {
4392 printk(KERN_ERR "btrfs: fail to "
4393 "dirty inode %lu error %d\n",
4394 inode->i_ino, ret);
4395 }
Chris Mason94b60442010-05-26 11:02:00 -04004396 }
4397 }
Chris Mason39279cc2007-06-12 06:35:45 -04004398 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004399}
4400
Chris Masond352ac62008-09-29 15:18:18 -04004401/*
4402 * find the highest existing sequence number in a directory
4403 * and then set the in-memory index_cnt variable to reflect
4404 * free sequence numbers
4405 */
Josef Bacikaec74772008-07-24 12:12:38 -04004406static int btrfs_set_inode_index_count(struct inode *inode)
4407{
4408 struct btrfs_root *root = BTRFS_I(inode)->root;
4409 struct btrfs_key key, found_key;
4410 struct btrfs_path *path;
4411 struct extent_buffer *leaf;
4412 int ret;
4413
4414 key.objectid = inode->i_ino;
4415 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4416 key.offset = (u64)-1;
4417
4418 path = btrfs_alloc_path();
4419 if (!path)
4420 return -ENOMEM;
4421
4422 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4423 if (ret < 0)
4424 goto out;
4425 /* FIXME: we should be able to handle this */
4426 if (ret == 0)
4427 goto out;
4428 ret = 0;
4429
4430 /*
4431 * MAGIC NUMBER EXPLANATION:
4432 * since we search a directory based on f_pos we have to start at 2
4433 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4434 * else has to start at 2
4435 */
4436 if (path->slots[0] == 0) {
4437 BTRFS_I(inode)->index_cnt = 2;
4438 goto out;
4439 }
4440
4441 path->slots[0]--;
4442
4443 leaf = path->nodes[0];
4444 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4445
4446 if (found_key.objectid != inode->i_ino ||
4447 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4448 BTRFS_I(inode)->index_cnt = 2;
4449 goto out;
4450 }
4451
4452 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4453out:
4454 btrfs_free_path(path);
4455 return ret;
4456}
4457
Chris Masond352ac62008-09-29 15:18:18 -04004458/*
4459 * helper to find a free sequence number in a given directory. This current
4460 * code is very simple, later versions will do smarter things in the btree
4461 */
Chris Mason3de45862008-11-17 21:02:50 -05004462int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004463{
4464 int ret = 0;
4465
4466 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4467 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05004468 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04004469 return ret;
4470 }
4471
Chris Mason00e4e6b2008-08-05 11:18:09 -04004472 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004473 BTRFS_I(dir)->index_cnt++;
4474
4475 return ret;
4476}
4477
Chris Mason39279cc2007-06-12 06:35:45 -04004478static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4479 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004480 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004481 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004482 u64 ref_objectid, u64 objectid,
4483 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004484{
4485 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004486 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004487 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004488 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004489 struct btrfs_inode_ref *ref;
4490 struct btrfs_key key[2];
4491 u32 sizes[2];
4492 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004493 int ret;
4494 int owner;
4495
Chris Mason5f39d392007-10-15 16:14:19 -04004496 path = btrfs_alloc_path();
4497 BUG_ON(!path);
4498
Chris Mason39279cc2007-06-12 06:35:45 -04004499 inode = new_inode(root->fs_info->sb);
4500 if (!inode)
4501 return ERR_PTR(-ENOMEM);
4502
Josef Bacikaec74772008-07-24 12:12:38 -04004503 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05004504 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004505 if (ret) {
4506 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004507 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004508 }
Josef Bacikaec74772008-07-24 12:12:38 -04004509 }
4510 /*
4511 * index_cnt is ignored for everything but a dir,
4512 * btrfs_get_inode_index_count has an explanation for the magic
4513 * number
4514 */
4515 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004516 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004517 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004518 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik6a632092009-02-20 11:00:09 -05004519 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004520
Chris Mason39279cc2007-06-12 06:35:45 -04004521 if (mode & S_IFDIR)
4522 owner = 0;
4523 else
4524 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004525 BTRFS_I(inode)->block_group =
4526 btrfs_find_block_group(root, 0, alloc_hint, owner);
Chris Mason9c583092008-01-29 15:15:18 -05004527
4528 key[0].objectid = objectid;
4529 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4530 key[0].offset = 0;
4531
4532 key[1].objectid = objectid;
4533 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4534 key[1].offset = ref_objectid;
4535
4536 sizes[0] = sizeof(struct btrfs_inode_item);
4537 sizes[1] = name_len + sizeof(*ref);
4538
Chris Masonb9473432009-03-13 11:00:37 -04004539 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004540 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4541 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004542 goto fail;
4543
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004544 inode_init_owner(inode, dir, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04004545 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004546 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004547 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004548 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4549 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004550 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004551
4552 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4553 struct btrfs_inode_ref);
4554 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004555 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004556 ptr = (unsigned long)(ref + 1);
4557 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4558
Chris Mason5f39d392007-10-15 16:14:19 -04004559 btrfs_mark_buffer_dirty(path->nodes[0]);
4560 btrfs_free_path(path);
4561
Chris Mason39279cc2007-06-12 06:35:45 -04004562 location = &BTRFS_I(inode)->location;
4563 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004564 location->offset = 0;
4565 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4566
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004567 btrfs_inherit_iflags(inode, dir);
4568
Chris Mason94272162009-07-02 12:26:06 -04004569 if ((mode & S_IFREG)) {
4570 if (btrfs_test_opt(root, NODATASUM))
4571 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4572 if (btrfs_test_opt(root, NODATACOW))
4573 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4574 }
4575
Chris Mason39279cc2007-06-12 06:35:45 -04004576 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004577 inode_tree_add(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004578 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004579fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004580 if (dir)
4581 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004582 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004583 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004584 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004585}
4586
4587static inline u8 btrfs_inode_type(struct inode *inode)
4588{
4589 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4590}
4591
Chris Masond352ac62008-09-29 15:18:18 -04004592/*
4593 * utility function to add 'inode' into 'parent_inode' with
4594 * a give name and a given sequence number.
4595 * if 'add_backref' is true, also insert a backref from the
4596 * inode to the parent directory.
4597 */
Chris Masone02119d2008-09-05 16:13:11 -04004598int btrfs_add_link(struct btrfs_trans_handle *trans,
4599 struct inode *parent_inode, struct inode *inode,
4600 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004601{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004602 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004603 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004604 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04004605
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004606 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4607 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4608 } else {
4609 key.objectid = inode->i_ino;
4610 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4611 key.offset = 0;
4612 }
Chris Mason39279cc2007-06-12 06:35:45 -04004613
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004614 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4615 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4616 key.objectid, root->root_key.objectid,
4617 parent_inode->i_ino,
4618 index, name, name_len);
4619 } else if (add_backref) {
4620 ret = btrfs_insert_inode_ref(trans, root,
4621 name, name_len, inode->i_ino,
4622 parent_inode->i_ino, index);
4623 }
4624
Chris Mason39279cc2007-06-12 06:35:45 -04004625 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004626 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4627 parent_inode->i_ino, &key,
4628 btrfs_inode_type(inode), index);
4629 BUG_ON(ret);
4630
Chris Masondbe674a2008-07-17 12:54:05 -04004631 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004632 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004633 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004634 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004635 }
4636 return ret;
4637}
4638
4639static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004640 struct inode *dir, struct dentry *dentry,
4641 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004642{
Josef Bacika1b075d2010-11-19 20:36:11 +00004643 int err = btrfs_add_link(trans, dir, inode,
4644 dentry->d_name.name, dentry->d_name.len,
4645 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004646 if (!err) {
4647 d_instantiate(dentry, inode);
4648 return 0;
4649 }
4650 if (err > 0)
4651 err = -EEXIST;
4652 return err;
4653}
4654
Josef Bacik618e21d2007-07-11 10:18:17 -04004655static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4656 int mode, dev_t rdev)
4657{
4658 struct btrfs_trans_handle *trans;
4659 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004660 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004661 int err;
4662 int drop_inode = 0;
4663 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004664 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004665 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004666
4667 if (!new_valid_dev(rdev))
4668 return -EINVAL;
4669
Yan, Zhenga22285a2010-05-16 10:48:46 -04004670 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4671 if (err)
4672 return err;
4673
Josef Bacik9ed74f22009-09-11 16:12:44 -04004674 /*
4675 * 2 for inode item and ref
4676 * 2 for dir items
4677 * 1 for xattr if selinux is on
4678 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004679 trans = btrfs_start_transaction(root, 5);
4680 if (IS_ERR(trans))
4681 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004682
Josef Bacik618e21d2007-07-11 10:18:17 -04004683 btrfs_set_trans_block_group(trans, dir);
4684
Josef Bacikaec74772008-07-24 12:12:38 -04004685 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00004686 dentry->d_name.len, dir->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004687 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004688 err = PTR_ERR(inode);
4689 if (IS_ERR(inode))
4690 goto out_unlock;
4691
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004692 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004693 if (err) {
4694 drop_inode = 1;
4695 goto out_unlock;
4696 }
4697
Josef Bacik618e21d2007-07-11 10:18:17 -04004698 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00004699 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004700 if (err)
4701 drop_inode = 1;
4702 else {
4703 inode->i_op = &btrfs_special_inode_operations;
4704 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004705 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004706 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004707 btrfs_update_inode_block_group(trans, inode);
4708 btrfs_update_inode_block_group(trans, dir);
4709out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004710 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004711 btrfs_end_transaction_throttle(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004712 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004713 if (drop_inode) {
4714 inode_dec_link_count(inode);
4715 iput(inode);
4716 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004717 return err;
4718}
4719
Chris Mason39279cc2007-06-12 06:35:45 -04004720static int btrfs_create(struct inode *dir, struct dentry *dentry,
4721 int mode, struct nameidata *nd)
4722{
4723 struct btrfs_trans_handle *trans;
4724 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004725 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004726 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004727 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004728 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004729 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004730 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004731
Yan, Zhenga22285a2010-05-16 10:48:46 -04004732 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4733 if (err)
4734 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004735 /*
4736 * 2 for inode item and ref
4737 * 2 for dir items
4738 * 1 for xattr if selinux is on
4739 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004740 trans = btrfs_start_transaction(root, 5);
4741 if (IS_ERR(trans))
4742 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004743
Chris Mason39279cc2007-06-12 06:35:45 -04004744 btrfs_set_trans_block_group(trans, dir);
4745
Josef Bacikaec74772008-07-24 12:12:38 -04004746 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00004747 dentry->d_name.len, dir->i_ino, objectid,
4748 BTRFS_I(dir)->block_group, mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004749 err = PTR_ERR(inode);
4750 if (IS_ERR(inode))
4751 goto out_unlock;
4752
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004753 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004754 if (err) {
4755 drop_inode = 1;
4756 goto out_unlock;
4757 }
4758
Chris Mason39279cc2007-06-12 06:35:45 -04004759 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00004760 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004761 if (err)
4762 drop_inode = 1;
4763 else {
4764 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004765 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004766 inode->i_fop = &btrfs_file_operations;
4767 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004768 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004769 }
Chris Mason39279cc2007-06-12 06:35:45 -04004770 btrfs_update_inode_block_group(trans, inode);
4771 btrfs_update_inode_block_group(trans, dir);
4772out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004773 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004774 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004775 if (drop_inode) {
4776 inode_dec_link_count(inode);
4777 iput(inode);
4778 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004779 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004780 return err;
4781}
4782
4783static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4784 struct dentry *dentry)
4785{
4786 struct btrfs_trans_handle *trans;
4787 struct btrfs_root *root = BTRFS_I(dir)->root;
4788 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004789 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004790 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004791 int err;
4792 int drop_inode = 0;
4793
4794 if (inode->i_nlink == 0)
4795 return -ENOENT;
4796
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004797 /* do not allow sys_link's with other subvols of the same device */
4798 if (root->objectid != BTRFS_I(inode)->root->objectid)
4799 return -EPERM;
4800
Josef Bacik9ed74f22009-09-11 16:12:44 -04004801 btrfs_inc_nlink(inode);
Josef Bacikbc1cbf12010-11-23 19:50:59 +00004802 inode->i_ctime = CURRENT_TIME;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004803
Chris Mason3de45862008-11-17 21:02:50 -05004804 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004805 if (err)
4806 goto fail;
4807
Yan, Zhenga22285a2010-05-16 10:48:46 -04004808 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00004809 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04004810 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00004811 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04004812 */
Miao Xie7e6b6462011-02-18 09:21:17 +00004813 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004814 if (IS_ERR(trans)) {
4815 err = PTR_ERR(trans);
4816 goto fail;
4817 }
Chris Mason5f39d392007-10-15 16:14:19 -04004818
Chris Mason39279cc2007-06-12 06:35:45 -04004819 btrfs_set_trans_block_group(trans, dir);
Al Viro7de9c6ee2010-10-23 11:11:40 -04004820 ihold(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004821
Josef Bacika1b075d2010-11-19 20:36:11 +00004822 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004823
Yan, Zhenga5719522009-09-24 09:17:31 -04004824 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004825 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004826 } else {
Josef Bacik6a912212010-11-20 09:48:00 +00004827 struct dentry *parent = dget_parent(dentry);
Yan, Zhenga5719522009-09-24 09:17:31 -04004828 btrfs_update_inode_block_group(trans, dir);
4829 err = btrfs_update_inode(trans, root, inode);
4830 BUG_ON(err);
Josef Bacik6a912212010-11-20 09:48:00 +00004831 btrfs_log_new_name(trans, inode, NULL, parent);
4832 dput(parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04004833 }
Chris Mason39279cc2007-06-12 06:35:45 -04004834
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004835 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004836 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004837fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004838 if (drop_inode) {
4839 inode_dec_link_count(inode);
4840 iput(inode);
4841 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004842 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004843 return err;
4844}
4845
Chris Mason39279cc2007-06-12 06:35:45 -04004846static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4847{
Chris Masonb9d86662008-05-02 16:13:49 -04004848 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004849 struct btrfs_trans_handle *trans;
4850 struct btrfs_root *root = BTRFS_I(dir)->root;
4851 int err = 0;
4852 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004853 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004854 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004855 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004856
Yan, Zhenga22285a2010-05-16 10:48:46 -04004857 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4858 if (err)
4859 return err;
4860
Josef Bacik9ed74f22009-09-11 16:12:44 -04004861 /*
4862 * 2 items for inode and ref
4863 * 2 items for dir items
4864 * 1 for xattr if selinux is on
4865 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004866 trans = btrfs_start_transaction(root, 5);
4867 if (IS_ERR(trans))
4868 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004869 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004870
Josef Bacikaec74772008-07-24 12:12:38 -04004871 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00004872 dentry->d_name.len, dir->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004873 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4874 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004875 if (IS_ERR(inode)) {
4876 err = PTR_ERR(inode);
4877 goto out_fail;
4878 }
Chris Mason5f39d392007-10-15 16:14:19 -04004879
Chris Mason39279cc2007-06-12 06:35:45 -04004880 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004881
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004882 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004883 if (err)
4884 goto out_fail;
4885
Chris Mason39279cc2007-06-12 06:35:45 -04004886 inode->i_op = &btrfs_dir_inode_operations;
4887 inode->i_fop = &btrfs_dir_file_operations;
4888 btrfs_set_trans_block_group(trans, inode);
4889
Chris Masondbe674a2008-07-17 12:54:05 -04004890 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004891 err = btrfs_update_inode(trans, root, inode);
4892 if (err)
4893 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004894
Josef Bacika1b075d2010-11-19 20:36:11 +00004895 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
4896 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004897 if (err)
4898 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004899
Chris Mason39279cc2007-06-12 06:35:45 -04004900 d_instantiate(dentry, inode);
4901 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004902 btrfs_update_inode_block_group(trans, inode);
4903 btrfs_update_inode_block_group(trans, dir);
4904
4905out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004906 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004907 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004908 if (drop_on_err)
4909 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004910 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004911 return err;
4912}
4913
Chris Masond352ac62008-09-29 15:18:18 -04004914/* helper for btfs_get_extent. Given an existing extent in the tree,
4915 * and an extent that you want to insert, deal with overlap and insert
4916 * the new extent into the tree.
4917 */
Chris Mason3b951512008-04-17 11:29:12 -04004918static int merge_extent_mapping(struct extent_map_tree *em_tree,
4919 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004920 struct extent_map *em,
4921 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04004922{
4923 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04004924
Chris Masone6dcd2d2008-07-17 12:53:50 -04004925 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4926 start_diff = map_start - em->start;
4927 em->start = map_start;
4928 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04004929 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4930 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004931 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04004932 em->block_len -= start_diff;
4933 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004934 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004935}
4936
Chris Masonc8b97812008-10-29 14:49:59 -04004937static noinline int uncompress_inline(struct btrfs_path *path,
4938 struct inode *inode, struct page *page,
4939 size_t pg_offset, u64 extent_offset,
4940 struct btrfs_file_extent_item *item)
4941{
4942 int ret;
4943 struct extent_buffer *leaf = path->nodes[0];
4944 char *tmp;
4945 size_t max_size;
4946 unsigned long inline_size;
4947 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08004948 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04004949
4950 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08004951 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04004952 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4953 inline_size = btrfs_file_extent_inline_item_len(leaf,
4954 btrfs_item_nr(leaf, path->slots[0]));
4955 tmp = kmalloc(inline_size, GFP_NOFS);
4956 ptr = btrfs_file_extent_inline_start(item);
4957
4958 read_extent_buffer(leaf, tmp, ptr, inline_size);
4959
Chris Mason5b050f02008-11-11 09:34:41 -05004960 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08004961 ret = btrfs_decompress(compress_type, tmp, page,
4962 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004963 if (ret) {
4964 char *kaddr = kmap_atomic(page, KM_USER0);
4965 unsigned long copy_size = min_t(u64,
4966 PAGE_CACHE_SIZE - pg_offset,
4967 max_size - extent_offset);
4968 memset(kaddr + pg_offset, 0, copy_size);
4969 kunmap_atomic(kaddr, KM_USER0);
4970 }
4971 kfree(tmp);
4972 return 0;
4973}
4974
Chris Masond352ac62008-09-29 15:18:18 -04004975/*
4976 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05004977 * the ugly parts come from merging extents from the disk with the in-ram
4978 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04004979 * where the in-ram extents might be locked pending data=ordered completion.
4980 *
4981 * This also copies inline extents directly into the page.
4982 */
Chris Masond3977122009-01-05 21:25:51 -05004983
Chris Masona52d9a82007-08-27 16:49:44 -04004984struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05004985 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04004986 int create)
4987{
4988 int ret;
4989 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04004990 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04004991 u64 extent_start = 0;
4992 u64 extent_end = 0;
4993 u64 objectid = inode->i_ino;
4994 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04004995 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04004996 struct btrfs_root *root = BTRFS_I(inode)->root;
4997 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04004998 struct extent_buffer *leaf;
4999 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005000 struct extent_map *em = NULL;
5001 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005002 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005003 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005004 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005005
Chris Masona52d9a82007-08-27 16:49:44 -04005006again:
Chris Mason890871b2009-09-02 16:24:52 -04005007 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005008 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005009 if (em)
5010 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005011 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005012
Chris Masona52d9a82007-08-27 16:49:44 -04005013 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005014 if (em->start > start || em->start + em->len <= start)
5015 free_extent_map(em);
5016 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005017 free_extent_map(em);
5018 else
5019 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005020 }
Chris Masond1310b22008-01-24 16:13:08 -05005021 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005022 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005023 err = -ENOMEM;
5024 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005025 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005026 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005027 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005028 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005029 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005030 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005031
5032 if (!path) {
5033 path = btrfs_alloc_path();
5034 BUG_ON(!path);
5035 }
5036
Chris Mason179e29e2007-11-01 11:28:41 -04005037 ret = btrfs_lookup_file_extent(trans, root, path,
5038 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005039 if (ret < 0) {
5040 err = ret;
5041 goto out;
5042 }
5043
5044 if (ret != 0) {
5045 if (path->slots[0] == 0)
5046 goto not_found;
5047 path->slots[0]--;
5048 }
5049
Chris Mason5f39d392007-10-15 16:14:19 -04005050 leaf = path->nodes[0];
5051 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005052 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005053 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005054 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5055 found_type = btrfs_key_type(&found_key);
5056 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005057 found_type != BTRFS_EXTENT_DATA_KEY) {
5058 goto not_found;
5059 }
5060
Chris Mason5f39d392007-10-15 16:14:19 -04005061 found_type = btrfs_file_extent_type(leaf, item);
5062 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005063 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005064 if (found_type == BTRFS_FILE_EXTENT_REG ||
5065 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005066 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005067 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005068 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5069 size_t size;
5070 size = btrfs_file_extent_inline_len(leaf, item);
5071 extent_end = (extent_start + size + root->sectorsize - 1) &
5072 ~((u64)root->sectorsize - 1);
5073 }
5074
5075 if (start >= extent_end) {
5076 path->slots[0]++;
5077 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5078 ret = btrfs_next_leaf(root, path);
5079 if (ret < 0) {
5080 err = ret;
5081 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005082 }
Yan Zheng9036c102008-10-30 14:19:41 -04005083 if (ret > 0)
5084 goto not_found;
5085 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005086 }
Yan Zheng9036c102008-10-30 14:19:41 -04005087 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5088 if (found_key.objectid != objectid ||
5089 found_key.type != BTRFS_EXTENT_DATA_KEY)
5090 goto not_found;
5091 if (start + len <= found_key.offset)
5092 goto not_found;
5093 em->start = start;
5094 em->len = found_key.offset - start;
5095 goto not_found_em;
5096 }
5097
Yan Zhengd899e052008-10-30 14:25:28 -04005098 if (found_type == BTRFS_FILE_EXTENT_REG ||
5099 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005100 em->start = extent_start;
5101 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005102 em->orig_start = extent_start -
5103 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005104 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5105 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005106 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005107 goto insert;
5108 }
Li Zefan261507a02010-12-17 14:21:50 +08005109 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005110 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005111 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005112 em->block_start = bytenr;
5113 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5114 item);
5115 } else {
5116 bytenr += btrfs_file_extent_offset(leaf, item);
5117 em->block_start = bytenr;
5118 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005119 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5120 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005121 }
Chris Masona52d9a82007-08-27 16:49:44 -04005122 goto insert;
5123 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005124 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005125 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005126 size_t size;
5127 size_t extent_offset;
5128 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005129
Chris Masona52d9a82007-08-27 16:49:44 -04005130 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005131 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005132 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005133 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005134 goto out;
5135 }
5136
Yan Zheng9036c102008-10-30 14:19:41 -04005137 size = btrfs_file_extent_inline_len(leaf, item);
5138 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005139 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005140 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005141 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005142 em->len = (copy_size + root->sectorsize - 1) &
5143 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005144 em->orig_start = EXTENT_MAP_INLINE;
Li Zefan261507a02010-12-17 14:21:50 +08005145 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005146 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005147 em->compress_type = compress_type;
5148 }
Yan689f9342007-10-29 11:41:07 -04005149 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005150 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005151 if (btrfs_file_extent_compression(leaf, item) !=
5152 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005153 ret = uncompress_inline(path, inode, page,
5154 pg_offset,
5155 extent_offset, item);
5156 BUG_ON(ret);
5157 } else {
5158 map = kmap(page);
5159 read_extent_buffer(leaf, map + pg_offset, ptr,
5160 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005161 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5162 memset(map + pg_offset + copy_size, 0,
5163 PAGE_CACHE_SIZE - pg_offset -
5164 copy_size);
5165 }
Chris Masonc8b97812008-10-29 14:49:59 -04005166 kunmap(page);
5167 }
Chris Mason179e29e2007-11-01 11:28:41 -04005168 flush_dcache_page(page);
5169 } else if (create && PageUptodate(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005170 WARN_ON(1);
Chris Mason179e29e2007-11-01 11:28:41 -04005171 if (!trans) {
5172 kunmap(page);
5173 free_extent_map(em);
5174 em = NULL;
5175 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04005176 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005177 if (IS_ERR(trans))
5178 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005179 goto again;
5180 }
Chris Masonc8b97812008-10-29 14:49:59 -04005181 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005182 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005183 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005184 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005185 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005186 }
Chris Masond1310b22008-01-24 16:13:08 -05005187 set_extent_uptodate(io_tree, em->start,
5188 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005189 goto insert;
5190 } else {
Chris Masond3977122009-01-05 21:25:51 -05005191 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005192 WARN_ON(1);
5193 }
5194not_found:
5195 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005196 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005197not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005198 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005199 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005200insert:
5201 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05005202 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005203 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5204 "[%llu %llu]\n", (unsigned long long)em->start,
5205 (unsigned long long)em->len,
5206 (unsigned long long)start,
5207 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005208 err = -EIO;
5209 goto out;
5210 }
Chris Masond1310b22008-01-24 16:13:08 -05005211
5212 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005213 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005214 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005215 /* it is possible that someone inserted the extent into the tree
5216 * while we had the lock dropped. It is also possible that
5217 * an overlapping map exists in the tree
5218 */
Chris Masona52d9a82007-08-27 16:49:44 -04005219 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005220 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005221
5222 ret = 0;
5223
Chris Mason3b951512008-04-17 11:29:12 -04005224 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005225 if (existing && (existing->start > start ||
5226 existing->start + existing->len <= start)) {
5227 free_extent_map(existing);
5228 existing = NULL;
5229 }
Chris Mason3b951512008-04-17 11:29:12 -04005230 if (!existing) {
5231 existing = lookup_extent_mapping(em_tree, em->start,
5232 em->len);
5233 if (existing) {
5234 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005235 em, start,
5236 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005237 free_extent_map(existing);
5238 if (err) {
5239 free_extent_map(em);
5240 em = NULL;
5241 }
5242 } else {
5243 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005244 free_extent_map(em);
5245 em = NULL;
5246 }
5247 } else {
5248 free_extent_map(em);
5249 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005250 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005251 }
Chris Masona52d9a82007-08-27 16:49:44 -04005252 }
Chris Mason890871b2009-09-02 16:24:52 -04005253 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005254out:
Chris Masonf4219502008-07-22 11:18:09 -04005255 if (path)
5256 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005257 if (trans) {
5258 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005259 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005260 err = ret;
5261 }
Chris Masona52d9a82007-08-27 16:49:44 -04005262 if (err) {
5263 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005264 return ERR_PTR(err);
5265 }
5266 return em;
5267}
5268
Chris Masonec29ed52011-02-23 16:23:20 -05005269struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5270 size_t pg_offset, u64 start, u64 len,
5271 int create)
5272{
5273 struct extent_map *em;
5274 struct extent_map *hole_em = NULL;
5275 u64 range_start = start;
5276 u64 end;
5277 u64 found;
5278 u64 found_end;
5279 int err = 0;
5280
5281 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5282 if (IS_ERR(em))
5283 return em;
5284 if (em) {
5285 /*
5286 * if our em maps to a hole, there might
5287 * actually be delalloc bytes behind it
5288 */
5289 if (em->block_start != EXTENT_MAP_HOLE)
5290 return em;
5291 else
5292 hole_em = em;
5293 }
5294
5295 /* check to see if we've wrapped (len == -1 or similar) */
5296 end = start + len;
5297 if (end < start)
5298 end = (u64)-1;
5299 else
5300 end -= 1;
5301
5302 em = NULL;
5303
5304 /* ok, we didn't find anything, lets look for delalloc */
5305 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5306 end, len, EXTENT_DELALLOC, 1);
5307 found_end = range_start + found;
5308 if (found_end < range_start)
5309 found_end = (u64)-1;
5310
5311 /*
5312 * we didn't find anything useful, return
5313 * the original results from get_extent()
5314 */
5315 if (range_start > end || found_end <= start) {
5316 em = hole_em;
5317 hole_em = NULL;
5318 goto out;
5319 }
5320
5321 /* adjust the range_start to make sure it doesn't
5322 * go backwards from the start they passed in
5323 */
5324 range_start = max(start,range_start);
5325 found = found_end - range_start;
5326
5327 if (found > 0) {
5328 u64 hole_start = start;
5329 u64 hole_len = len;
5330
5331 em = alloc_extent_map(GFP_NOFS);
5332 if (!em) {
5333 err = -ENOMEM;
5334 goto out;
5335 }
5336 /*
5337 * when btrfs_get_extent can't find anything it
5338 * returns one huge hole
5339 *
5340 * make sure what it found really fits our range, and
5341 * adjust to make sure it is based on the start from
5342 * the caller
5343 */
5344 if (hole_em) {
5345 u64 calc_end = extent_map_end(hole_em);
5346
5347 if (calc_end <= start || (hole_em->start > end)) {
5348 free_extent_map(hole_em);
5349 hole_em = NULL;
5350 } else {
5351 hole_start = max(hole_em->start, start);
5352 hole_len = calc_end - hole_start;
5353 }
5354 }
5355 em->bdev = NULL;
5356 if (hole_em && range_start > hole_start) {
5357 /* our hole starts before our delalloc, so we
5358 * have to return just the parts of the hole
5359 * that go until the delalloc starts
5360 */
5361 em->len = min(hole_len,
5362 range_start - hole_start);
5363 em->start = hole_start;
5364 em->orig_start = hole_start;
5365 /*
5366 * don't adjust block start at all,
5367 * it is fixed at EXTENT_MAP_HOLE
5368 */
5369 em->block_start = hole_em->block_start;
5370 em->block_len = hole_len;
5371 } else {
5372 em->start = range_start;
5373 em->len = found;
5374 em->orig_start = range_start;
5375 em->block_start = EXTENT_MAP_DELALLOC;
5376 em->block_len = found;
5377 }
5378 } else if (hole_em) {
5379 return hole_em;
5380 }
5381out:
5382
5383 free_extent_map(hole_em);
5384 if (err) {
5385 free_extent_map(em);
5386 return ERR_PTR(err);
5387 }
5388 return em;
5389}
5390
Josef Bacik4b46fce2010-05-23 11:00:55 -04005391static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5392 u64 start, u64 len)
5393{
5394 struct btrfs_root *root = BTRFS_I(inode)->root;
5395 struct btrfs_trans_handle *trans;
5396 struct extent_map *em;
5397 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5398 struct btrfs_key ins;
5399 u64 alloc_hint;
5400 int ret;
5401
5402 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5403
5404 trans = btrfs_join_transaction(root, 0);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005405 if (IS_ERR(trans))
5406 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005407
5408 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5409
5410 alloc_hint = get_extent_allocation_hint(inode, start, len);
5411 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
5412 alloc_hint, (u64)-1, &ins, 1);
5413 if (ret) {
5414 em = ERR_PTR(ret);
5415 goto out;
5416 }
5417
5418 em = alloc_extent_map(GFP_NOFS);
5419 if (!em) {
5420 em = ERR_PTR(-ENOMEM);
5421 goto out;
5422 }
5423
5424 em->start = start;
5425 em->orig_start = em->start;
5426 em->len = ins.offset;
5427
5428 em->block_start = ins.objectid;
5429 em->block_len = ins.offset;
5430 em->bdev = root->fs_info->fs_devices->latest_bdev;
5431 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5432
5433 while (1) {
5434 write_lock(&em_tree->lock);
5435 ret = add_extent_mapping(em_tree, em);
5436 write_unlock(&em_tree->lock);
5437 if (ret != -EEXIST)
5438 break;
5439 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5440 }
5441
5442 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5443 ins.offset, ins.offset, 0);
5444 if (ret) {
5445 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5446 em = ERR_PTR(ret);
5447 }
5448out:
5449 btrfs_end_transaction(trans, root);
5450 return em;
5451}
5452
Chris Mason46bfbb52010-05-26 11:04:10 -04005453/*
5454 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5455 * block must be cow'd
5456 */
5457static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5458 struct inode *inode, u64 offset, u64 len)
5459{
5460 struct btrfs_path *path;
5461 int ret;
5462 struct extent_buffer *leaf;
5463 struct btrfs_root *root = BTRFS_I(inode)->root;
5464 struct btrfs_file_extent_item *fi;
5465 struct btrfs_key key;
5466 u64 disk_bytenr;
5467 u64 backref_offset;
5468 u64 extent_end;
5469 u64 num_bytes;
5470 int slot;
5471 int found_type;
5472
5473 path = btrfs_alloc_path();
5474 if (!path)
5475 return -ENOMEM;
5476
5477 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
5478 offset, 0);
5479 if (ret < 0)
5480 goto out;
5481
5482 slot = path->slots[0];
5483 if (ret == 1) {
5484 if (slot == 0) {
5485 /* can't find the item, must cow */
5486 ret = 0;
5487 goto out;
5488 }
5489 slot--;
5490 }
5491 ret = 0;
5492 leaf = path->nodes[0];
5493 btrfs_item_key_to_cpu(leaf, &key, slot);
5494 if (key.objectid != inode->i_ino ||
5495 key.type != BTRFS_EXTENT_DATA_KEY) {
5496 /* not our file or wrong item type, must cow */
5497 goto out;
5498 }
5499
5500 if (key.offset > offset) {
5501 /* Wrong offset, must cow */
5502 goto out;
5503 }
5504
5505 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5506 found_type = btrfs_file_extent_type(leaf, fi);
5507 if (found_type != BTRFS_FILE_EXTENT_REG &&
5508 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5509 /* not a regular extent, must cow */
5510 goto out;
5511 }
5512 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5513 backref_offset = btrfs_file_extent_offset(leaf, fi);
5514
5515 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5516 if (extent_end < offset + len) {
5517 /* extent doesn't include our full range, must cow */
5518 goto out;
5519 }
5520
5521 if (btrfs_extent_readonly(root, disk_bytenr))
5522 goto out;
5523
5524 /*
5525 * look for other files referencing this extent, if we
5526 * find any we must cow
5527 */
5528 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
5529 key.offset - backref_offset, disk_bytenr))
5530 goto out;
5531
5532 /*
5533 * adjust disk_bytenr and num_bytes to cover just the bytes
5534 * in this extent we are about to write. If there
5535 * are any csums in that range we have to cow in order
5536 * to keep the csums correct
5537 */
5538 disk_bytenr += backref_offset;
5539 disk_bytenr += offset - key.offset;
5540 num_bytes = min(offset + len, extent_end) - offset;
5541 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5542 goto out;
5543 /*
5544 * all of the above have passed, it is safe to overwrite this extent
5545 * without cow
5546 */
5547 ret = 1;
5548out:
5549 btrfs_free_path(path);
5550 return ret;
5551}
5552
Josef Bacik4b46fce2010-05-23 11:00:55 -04005553static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5554 struct buffer_head *bh_result, int create)
5555{
5556 struct extent_map *em;
5557 struct btrfs_root *root = BTRFS_I(inode)->root;
5558 u64 start = iblock << inode->i_blkbits;
5559 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005560 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005561
5562 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5563 if (IS_ERR(em))
5564 return PTR_ERR(em);
5565
5566 /*
5567 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5568 * io. INLINE is special, and we could probably kludge it in here, but
5569 * it's still buffered so for safety lets just fall back to the generic
5570 * buffered path.
5571 *
5572 * For COMPRESSED we _have_ to read the entire extent in so we can
5573 * decompress it, so there will be buffering required no matter what we
5574 * do, so go ahead and fallback to buffered.
5575 *
5576 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5577 * to buffered IO. Don't blame me, this is the price we pay for using
5578 * the generic code.
5579 */
5580 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5581 em->block_start == EXTENT_MAP_INLINE) {
5582 free_extent_map(em);
5583 return -ENOTBLK;
5584 }
5585
5586 /* Just a good old fashioned hole, return */
5587 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5588 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5589 free_extent_map(em);
5590 /* DIO will do one hole at a time, so just unlock a sector */
5591 unlock_extent(&BTRFS_I(inode)->io_tree, start,
5592 start + root->sectorsize - 1, GFP_NOFS);
5593 return 0;
5594 }
5595
5596 /*
5597 * We don't allocate a new extent in the following cases
5598 *
5599 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5600 * existing extent.
5601 * 2) The extent is marked as PREALLOC. We're good to go here and can
5602 * just use the extent.
5603 *
5604 */
Chris Mason46bfbb52010-05-26 11:04:10 -04005605 if (!create) {
5606 len = em->len - (start - em->start);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005607 goto map;
Chris Mason46bfbb52010-05-26 11:04:10 -04005608 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005609
5610 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5611 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5612 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005613 int type;
5614 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04005615 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005616
5617 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5618 type = BTRFS_ORDERED_PREALLOC;
5619 else
5620 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04005621 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04005622 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04005623
5624 /*
5625 * we're not going to log anything, but we do need
5626 * to make sure the current transaction stays open
5627 * while we look for nocow cross refs
5628 */
5629 trans = btrfs_join_transaction(root, 0);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005630 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04005631 goto must_cow;
5632
5633 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5634 ret = btrfs_add_ordered_extent_dio(inode, start,
5635 block_start, len, len, type);
5636 btrfs_end_transaction(trans, root);
5637 if (ret) {
5638 free_extent_map(em);
5639 return ret;
5640 }
5641 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005642 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005643 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005644 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005645must_cow:
5646 /*
5647 * this will cow the extent, reset the len in case we changed
5648 * it above
5649 */
5650 len = bh_result->b_size;
5651 free_extent_map(em);
5652 em = btrfs_new_extent_direct(inode, start, len);
5653 if (IS_ERR(em))
5654 return PTR_ERR(em);
5655 len = min(len, em->len - (start - em->start));
5656unlock:
Chris Mason4845e442010-05-25 20:56:50 -04005657 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5658 EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5659 0, NULL, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005660map:
5661 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5662 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04005663 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005664 bh_result->b_bdev = em->bdev;
5665 set_buffer_mapped(bh_result);
5666 if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5667 set_buffer_new(bh_result);
5668
5669 free_extent_map(em);
5670
5671 return 0;
5672}
5673
5674struct btrfs_dio_private {
5675 struct inode *inode;
5676 u64 logical_offset;
5677 u64 disk_bytenr;
5678 u64 bytes;
5679 u32 *csums;
5680 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00005681
5682 /* number of bios pending for this dio */
5683 atomic_t pending_bios;
5684
5685 /* IO errors */
5686 int errors;
5687
5688 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005689};
5690
5691static void btrfs_endio_direct_read(struct bio *bio, int err)
5692{
Miao Xiee65e1532010-11-22 03:04:43 +00005693 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005694 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5695 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005696 struct inode *inode = dip->inode;
5697 struct btrfs_root *root = BTRFS_I(inode)->root;
5698 u64 start;
5699 u32 *private = dip->csums;
5700
5701 start = dip->logical_offset;
5702 do {
5703 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5704 struct page *page = bvec->bv_page;
5705 char *kaddr;
5706 u32 csum = ~(u32)0;
5707 unsigned long flags;
5708
5709 local_irq_save(flags);
5710 kaddr = kmap_atomic(page, KM_IRQ0);
5711 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5712 csum, bvec->bv_len);
5713 btrfs_csum_final(csum, (char *)&csum);
5714 kunmap_atomic(kaddr, KM_IRQ0);
5715 local_irq_restore(flags);
5716
5717 flush_dcache_page(bvec->bv_page);
5718 if (csum != *private) {
5719 printk(KERN_ERR "btrfs csum failed ino %lu off"
5720 " %llu csum %u private %u\n",
5721 inode->i_ino, (unsigned long long)start,
5722 csum, *private);
5723 err = -EIO;
5724 }
5725 }
5726
5727 start += bvec->bv_len;
5728 private++;
5729 bvec++;
5730 } while (bvec <= bvec_end);
5731
5732 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
5733 dip->logical_offset + dip->bytes - 1, GFP_NOFS);
5734 bio->bi_private = dip->private;
5735
5736 kfree(dip->csums);
5737 kfree(dip);
5738 dio_end_io(bio, err);
5739}
5740
5741static void btrfs_endio_direct_write(struct bio *bio, int err)
5742{
5743 struct btrfs_dio_private *dip = bio->bi_private;
5744 struct inode *inode = dip->inode;
5745 struct btrfs_root *root = BTRFS_I(inode)->root;
5746 struct btrfs_trans_handle *trans;
5747 struct btrfs_ordered_extent *ordered = NULL;
5748 struct extent_state *cached_state = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05005749 u64 ordered_offset = dip->logical_offset;
5750 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005751 int ret;
5752
5753 if (err)
5754 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05005755again:
5756 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
5757 &ordered_offset,
5758 ordered_bytes);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005759 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05005760 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005761
5762 BUG_ON(!ordered);
5763
5764 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005765 if (IS_ERR(trans)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005766 err = -ENOMEM;
5767 goto out;
5768 }
5769 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5770
5771 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
5772 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5773 if (!ret)
5774 ret = btrfs_update_inode(trans, root, inode);
5775 err = ret;
5776 goto out;
5777 }
5778
5779 lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5780 ordered->file_offset + ordered->len - 1, 0,
5781 &cached_state, GFP_NOFS);
5782
5783 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
5784 ret = btrfs_mark_extent_written(trans, inode,
5785 ordered->file_offset,
5786 ordered->file_offset +
5787 ordered->len);
5788 if (ret) {
5789 err = ret;
5790 goto out_unlock;
5791 }
5792 } else {
5793 ret = insert_reserved_file_extent(trans, inode,
5794 ordered->file_offset,
5795 ordered->start,
5796 ordered->disk_len,
5797 ordered->len,
5798 ordered->len,
5799 0, 0, 0,
5800 BTRFS_FILE_EXTENT_REG);
5801 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
5802 ordered->file_offset, ordered->len);
5803 if (ret) {
5804 err = ret;
5805 WARN_ON(1);
5806 goto out_unlock;
5807 }
5808 }
5809
5810 add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
5811 btrfs_ordered_update_i_size(inode, 0, ordered);
5812 btrfs_update_inode(trans, root, inode);
5813out_unlock:
5814 unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5815 ordered->file_offset + ordered->len - 1,
5816 &cached_state, GFP_NOFS);
5817out:
5818 btrfs_delalloc_release_metadata(inode, ordered->len);
5819 btrfs_end_transaction(trans, root);
Chris Mason163cf092010-11-28 19:56:33 -05005820 ordered_offset = ordered->file_offset + ordered->len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005821 btrfs_put_ordered_extent(ordered);
5822 btrfs_put_ordered_extent(ordered);
Chris Mason163cf092010-11-28 19:56:33 -05005823
5824out_test:
5825 /*
5826 * our bio might span multiple ordered extents. If we haven't
5827 * completed the accounting for the whole dio, go back and try again
5828 */
5829 if (ordered_offset < dip->logical_offset + dip->bytes) {
5830 ordered_bytes = dip->logical_offset + dip->bytes -
5831 ordered_offset;
5832 goto again;
5833 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005834out_done:
5835 bio->bi_private = dip->private;
5836
5837 kfree(dip->csums);
5838 kfree(dip);
5839 dio_end_io(bio, err);
5840}
5841
Chris Masoneaf25d92010-05-25 09:48:28 -04005842static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
5843 struct bio *bio, int mirror_num,
5844 unsigned long bio_flags, u64 offset)
5845{
5846 int ret;
5847 struct btrfs_root *root = BTRFS_I(inode)->root;
5848 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
5849 BUG_ON(ret);
5850 return 0;
5851}
5852
Miao Xiee65e1532010-11-22 03:04:43 +00005853static void btrfs_end_dio_bio(struct bio *bio, int err)
5854{
5855 struct btrfs_dio_private *dip = bio->bi_private;
5856
5857 if (err) {
5858 printk(KERN_ERR "btrfs direct IO failed ino %lu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00005859 "sector %#Lx len %u err no %d\n",
5860 dip->inode->i_ino, bio->bi_rw,
5861 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00005862 dip->errors = 1;
5863
5864 /*
5865 * before atomic variable goto zero, we must make sure
5866 * dip->errors is perceived to be set.
5867 */
5868 smp_mb__before_atomic_dec();
5869 }
5870
5871 /* if there are more bios still pending for this dio, just exit */
5872 if (!atomic_dec_and_test(&dip->pending_bios))
5873 goto out;
5874
5875 if (dip->errors)
5876 bio_io_error(dip->orig_bio);
5877 else {
5878 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
5879 bio_endio(dip->orig_bio, 0);
5880 }
5881out:
5882 bio_put(bio);
5883}
5884
5885static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
5886 u64 first_sector, gfp_t gfp_flags)
5887{
5888 int nr_vecs = bio_get_nr_vecs(bdev);
5889 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
5890}
5891
5892static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
5893 int rw, u64 file_offset, int skip_sum,
5894 u32 *csums)
5895{
5896 int write = rw & REQ_WRITE;
5897 struct btrfs_root *root = BTRFS_I(inode)->root;
5898 int ret;
5899
5900 bio_get(bio);
5901 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
5902 if (ret)
5903 goto err;
5904
5905 if (write && !skip_sum) {
5906 ret = btrfs_wq_submit_bio(root->fs_info,
5907 inode, rw, bio, 0, 0,
5908 file_offset,
5909 __btrfs_submit_bio_start_direct_io,
5910 __btrfs_submit_bio_done);
5911 goto err;
5912 } else if (!skip_sum)
5913 btrfs_lookup_bio_sums_dio(root, inode, bio,
5914 file_offset, csums);
5915
5916 ret = btrfs_map_bio(root, rw, bio, 0, 1);
5917err:
5918 bio_put(bio);
5919 return ret;
5920}
5921
5922static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
5923 int skip_sum)
5924{
5925 struct inode *inode = dip->inode;
5926 struct btrfs_root *root = BTRFS_I(inode)->root;
5927 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5928 struct bio *bio;
5929 struct bio *orig_bio = dip->orig_bio;
5930 struct bio_vec *bvec = orig_bio->bi_io_vec;
5931 u64 start_sector = orig_bio->bi_sector;
5932 u64 file_offset = dip->logical_offset;
5933 u64 submit_len = 0;
5934 u64 map_length;
5935 int nr_pages = 0;
5936 u32 *csums = dip->csums;
5937 int ret = 0;
5938
5939 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
5940 if (!bio)
5941 return -ENOMEM;
5942 bio->bi_private = dip;
5943 bio->bi_end_io = btrfs_end_dio_bio;
5944 atomic_inc(&dip->pending_bios);
5945
5946 map_length = orig_bio->bi_size;
5947 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
5948 &map_length, NULL, 0);
5949 if (ret) {
5950 bio_put(bio);
5951 return -EIO;
5952 }
5953
5954 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
5955 if (unlikely(map_length < submit_len + bvec->bv_len ||
5956 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5957 bvec->bv_offset) < bvec->bv_len)) {
5958 /*
5959 * inc the count before we submit the bio so
5960 * we know the end IO handler won't happen before
5961 * we inc the count. Otherwise, the dip might get freed
5962 * before we're done setting it up
5963 */
5964 atomic_inc(&dip->pending_bios);
5965 ret = __btrfs_submit_dio_bio(bio, inode, rw,
5966 file_offset, skip_sum,
5967 csums);
5968 if (ret) {
5969 bio_put(bio);
5970 atomic_dec(&dip->pending_bios);
5971 goto out_err;
5972 }
5973
5974 if (!skip_sum)
5975 csums = csums + nr_pages;
5976 start_sector += submit_len >> 9;
5977 file_offset += submit_len;
5978
5979 submit_len = 0;
5980 nr_pages = 0;
5981
5982 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
5983 start_sector, GFP_NOFS);
5984 if (!bio)
5985 goto out_err;
5986 bio->bi_private = dip;
5987 bio->bi_end_io = btrfs_end_dio_bio;
5988
5989 map_length = orig_bio->bi_size;
5990 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
5991 &map_length, NULL, 0);
5992 if (ret) {
5993 bio_put(bio);
5994 goto out_err;
5995 }
5996 } else {
5997 submit_len += bvec->bv_len;
5998 nr_pages ++;
5999 bvec++;
6000 }
6001 }
6002
6003 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
6004 csums);
6005 if (!ret)
6006 return 0;
6007
6008 bio_put(bio);
6009out_err:
6010 dip->errors = 1;
6011 /*
6012 * before atomic variable goto zero, we must
6013 * make sure dip->errors is perceived to be set.
6014 */
6015 smp_mb__before_atomic_dec();
6016 if (atomic_dec_and_test(&dip->pending_bios))
6017 bio_io_error(dip->orig_bio);
6018
6019 /* bio_end_io() will handle error, so we needn't return it */
6020 return 0;
6021}
6022
Josef Bacik4b46fce2010-05-23 11:00:55 -04006023static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6024 loff_t file_offset)
6025{
6026 struct btrfs_root *root = BTRFS_I(inode)->root;
6027 struct btrfs_dio_private *dip;
6028 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006029 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006030 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006031 int ret = 0;
6032
6033 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6034
6035 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6036 if (!dip) {
6037 ret = -ENOMEM;
6038 goto free_ordered;
6039 }
6040 dip->csums = NULL;
6041
6042 if (!skip_sum) {
6043 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
6044 if (!dip->csums) {
Daniel J Bluemanb4966b72011-03-09 16:46:42 +00006045 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006046 ret = -ENOMEM;
6047 goto free_ordered;
6048 }
6049 }
6050
6051 dip->private = bio->bi_private;
6052 dip->inode = inode;
6053 dip->logical_offset = file_offset;
6054
Josef Bacik4b46fce2010-05-23 11:00:55 -04006055 dip->bytes = 0;
6056 do {
6057 dip->bytes += bvec->bv_len;
6058 bvec++;
6059 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6060
Chris Mason46bfbb52010-05-26 11:04:10 -04006061 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006062 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006063 dip->errors = 0;
6064 dip->orig_bio = bio;
6065 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006066
6067 if (write)
6068 bio->bi_end_io = btrfs_endio_direct_write;
6069 else
6070 bio->bi_end_io = btrfs_endio_direct_read;
6071
Miao Xiee65e1532010-11-22 03:04:43 +00006072 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6073 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006074 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006075free_ordered:
6076 /*
6077 * If this is a write, we need to clean up the reserved space and kill
6078 * the ordered extent.
6079 */
6080 if (write) {
6081 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006082 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006083 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6084 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6085 btrfs_free_reserved_extent(root, ordered->start,
6086 ordered->disk_len);
6087 btrfs_put_ordered_extent(ordered);
6088 btrfs_put_ordered_extent(ordered);
6089 }
6090 bio_endio(bio, ret);
6091}
6092
Chris Mason5a5f79b2010-05-26 21:33:37 -04006093static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6094 const struct iovec *iov, loff_t offset,
6095 unsigned long nr_segs)
6096{
6097 int seg;
6098 size_t size;
6099 unsigned long addr;
6100 unsigned blocksize_mask = root->sectorsize - 1;
6101 ssize_t retval = -EINVAL;
6102 loff_t end = offset;
6103
6104 if (offset & blocksize_mask)
6105 goto out;
6106
6107 /* Check the memory alignment. Blocks cannot straddle pages */
6108 for (seg = 0; seg < nr_segs; seg++) {
6109 addr = (unsigned long)iov[seg].iov_base;
6110 size = iov[seg].iov_len;
6111 end += size;
6112 if ((addr & blocksize_mask) || (size & blocksize_mask))
6113 goto out;
6114 }
6115 retval = 0;
6116out:
6117 return retval;
6118}
Chris Mason16432982008-04-10 10:23:21 -04006119static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6120 const struct iovec *iov, loff_t offset,
6121 unsigned long nr_segs)
6122{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006123 struct file *file = iocb->ki_filp;
6124 struct inode *inode = file->f_mapping->host;
6125 struct btrfs_ordered_extent *ordered;
Chris Mason4845e442010-05-25 20:56:50 -04006126 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006127 u64 lockstart, lockend;
6128 ssize_t ret;
Chris Mason4845e442010-05-25 20:56:50 -04006129 int writing = rw & WRITE;
6130 int write_bits = 0;
Chris Mason3f7c5792010-05-26 10:59:53 -04006131 size_t count = iov_length(iov, nr_segs);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006132
Chris Mason5a5f79b2010-05-26 21:33:37 -04006133 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
6134 offset, nr_segs)) {
6135 return 0;
6136 }
6137
Josef Bacik4b46fce2010-05-23 11:00:55 -04006138 lockstart = offset;
Chris Mason3f7c5792010-05-26 10:59:53 -04006139 lockend = offset + count - 1;
6140
6141 if (writing) {
6142 ret = btrfs_delalloc_reserve_space(inode, count);
6143 if (ret)
6144 goto out;
6145 }
Chris Mason4845e442010-05-25 20:56:50 -04006146
Josef Bacik4b46fce2010-05-23 11:00:55 -04006147 while (1) {
Chris Mason4845e442010-05-25 20:56:50 -04006148 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6149 0, &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006150 /*
6151 * We're concerned with the entire range that we're going to be
6152 * doing DIO to, so we need to make sure theres no ordered
6153 * extents in this range.
6154 */
6155 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6156 lockend - lockstart + 1);
6157 if (!ordered)
6158 break;
Chris Mason4845e442010-05-25 20:56:50 -04006159 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6160 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006161 btrfs_start_ordered_extent(inode, ordered, 1);
6162 btrfs_put_ordered_extent(ordered);
6163 cond_resched();
6164 }
6165
Chris Mason4845e442010-05-25 20:56:50 -04006166 /*
6167 * we don't use btrfs_set_extent_delalloc because we don't want
6168 * the dirty or uptodate bits
6169 */
6170 if (writing) {
6171 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
6172 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6173 EXTENT_DELALLOC, 0, NULL, &cached_state,
6174 GFP_NOFS);
6175 if (ret) {
6176 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6177 lockend, EXTENT_LOCKED | write_bits,
6178 1, 0, &cached_state, GFP_NOFS);
6179 goto out;
6180 }
6181 }
6182
6183 free_extent_state(cached_state);
6184 cached_state = NULL;
6185
Chris Mason5a5f79b2010-05-26 21:33:37 -04006186 ret = __blockdev_direct_IO(rw, iocb, inode,
6187 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6188 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6189 btrfs_submit_direct, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006190
6191 if (ret < 0 && ret != -EIOCBQUEUED) {
Chris Mason4845e442010-05-25 20:56:50 -04006192 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
6193 offset + iov_length(iov, nr_segs) - 1,
6194 EXTENT_LOCKED | write_bits, 1, 0,
6195 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006196 } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
6197 /*
6198 * We're falling back to buffered, unlock the section we didn't
6199 * do IO on.
6200 */
Chris Mason4845e442010-05-25 20:56:50 -04006201 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
6202 offset + iov_length(iov, nr_segs) - 1,
6203 EXTENT_LOCKED | write_bits, 1, 0,
6204 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006205 }
Chris Mason4845e442010-05-25 20:56:50 -04006206out:
6207 free_extent_state(cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006208 return ret;
Chris Mason16432982008-04-10 10:23:21 -04006209}
6210
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006211static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6212 __u64 start, __u64 len)
6213{
Chris Masonec29ed52011-02-23 16:23:20 -05006214 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006215}
6216
Chris Mason9ebefb182007-06-15 13:50:00 -04006217int btrfs_readpage(struct file *file, struct page *page)
6218{
Chris Masond1310b22008-01-24 16:13:08 -05006219 struct extent_io_tree *tree;
6220 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006221 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04006222}
Chris Mason1832a6d2007-12-21 16:27:21 -05006223
Chris Mason39279cc2007-06-12 06:35:45 -04006224static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6225{
Chris Masond1310b22008-01-24 16:13:08 -05006226 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006227
6228
6229 if (current->flags & PF_MEMALLOC) {
6230 redirty_page_for_writepage(wbc, page);
6231 unlock_page(page);
6232 return 0;
6233 }
Chris Masond1310b22008-01-24 16:13:08 -05006234 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006235 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6236}
Chris Mason39279cc2007-06-12 06:35:45 -04006237
Chris Masonf4219502008-07-22 11:18:09 -04006238int btrfs_writepages(struct address_space *mapping,
6239 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006240{
Chris Masond1310b22008-01-24 16:13:08 -05006241 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006242
Chris Masond1310b22008-01-24 16:13:08 -05006243 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006244 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6245}
6246
Chris Mason3ab2fb52007-11-08 10:59:22 -05006247static int
6248btrfs_readpages(struct file *file, struct address_space *mapping,
6249 struct list_head *pages, unsigned nr_pages)
6250{
Chris Masond1310b22008-01-24 16:13:08 -05006251 struct extent_io_tree *tree;
6252 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006253 return extent_readpages(tree, mapping, pages, nr_pages,
6254 btrfs_get_extent);
6255}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006256static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006257{
Chris Masond1310b22008-01-24 16:13:08 -05006258 struct extent_io_tree *tree;
6259 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006260 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006261
Chris Masond1310b22008-01-24 16:13:08 -05006262 tree = &BTRFS_I(page->mapping->host)->io_tree;
6263 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006264 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006265 if (ret == 1) {
6266 ClearPagePrivate(page);
6267 set_page_private(page, 0);
6268 page_cache_release(page);
6269 }
6270 return ret;
6271}
Chris Mason39279cc2007-06-12 06:35:45 -04006272
Chris Masone6dcd2d2008-07-17 12:53:50 -04006273static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6274{
Chris Mason98509cf2008-09-11 15:51:43 -04006275 if (PageWriteback(page) || PageDirty(page))
6276 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006277 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006278}
6279
Chris Masona52d9a82007-08-27 16:49:44 -04006280static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6281{
Chris Masond1310b22008-01-24 16:13:08 -05006282 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006283 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006284 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006285 u64 page_start = page_offset(page);
6286 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006287
Chris Mason8b62b722009-09-02 16:53:46 -04006288
6289 /*
6290 * we have the page locked, so new writeback can't start,
6291 * and the dirty bit won't be cleared while we are here.
6292 *
6293 * Wait for IO on this page so that we can safely clear
6294 * the PagePrivate2 bit and do ordered accounting
6295 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006296 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006297
Chris Masond1310b22008-01-24 16:13:08 -05006298 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006299 if (offset) {
6300 btrfs_releasepage(page, GFP_NOFS);
6301 return;
6302 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00006303 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6304 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006305 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
6306 page_offset(page));
6307 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006308 /*
6309 * IO on this page will never be started, so we need
6310 * to account for any ordered extents now
6311 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006312 clear_extent_bit(tree, page_start, page_end,
6313 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04006314 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006315 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006316 /*
6317 * whoever cleared the private bit is responsible
6318 * for the finish_ordered_io
6319 */
6320 if (TestClearPagePrivate2(page)) {
6321 btrfs_finish_ordered_io(page->mapping->host,
6322 page_start, page_end);
6323 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006324 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006325 cached_state = NULL;
6326 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6327 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006328 }
6329 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006330 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00006331 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006332 __btrfs_releasepage(page, GFP_NOFS);
6333
Chris Mason4a096752008-07-21 10:29:44 -04006334 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006335 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006336 ClearPagePrivate(page);
6337 set_page_private(page, 0);
6338 page_cache_release(page);
6339 }
Chris Mason39279cc2007-06-12 06:35:45 -04006340}
6341
Chris Mason9ebefb182007-06-15 13:50:00 -04006342/*
6343 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6344 * called from a page fault handler when a page is first dirtied. Hence we must
6345 * be careful to check for EOF conditions here. We set the page up correctly
6346 * for a written page which means we get ENOSPC checking when writing into
6347 * holes and correct delalloc and unwritten extent mapping on filesystems that
6348 * support these features.
6349 *
6350 * We are not allowed to take the i_mutex here so we have to play games to
6351 * protect against truncate races as the page could now be beyond EOF. Because
6352 * vmtruncate() writes the inode size before removing pages, once we have the
6353 * page lock we can determine safely if the page is beyond EOF. If it is not
6354 * beyond EOF, then the page is guaranteed safe against truncation until we
6355 * unlock the page.
6356 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006357int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006358{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006359 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006360 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006361 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006362 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6363 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006364 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006365 char *kaddr;
6366 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006367 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006368 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04006369 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006370 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006371
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006372 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07006373 if (ret) {
6374 if (ret == -ENOMEM)
6375 ret = VM_FAULT_OOM;
6376 else /* -ENOSPC, -EIO, etc */
6377 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05006378 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07006379 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006380
Nick Piggin56a76f82009-03-31 15:23:23 -07006381 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006382again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006383 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006384 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006385 page_start = page_offset(page);
6386 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006387
Chris Mason9ebefb182007-06-15 13:50:00 -04006388 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006389 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006390 /* page got truncated out from underneath us */
6391 goto out_unlock;
6392 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006393 wait_on_page_writeback(page);
6394
Josef Bacik2ac55d42010-02-03 19:33:23 +00006395 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
6396 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006397 set_page_extent_mapped(page);
6398
Chris Masoneb84ae02008-07-17 13:53:27 -04006399 /*
6400 * we can't set the delalloc bits if there are pending ordered
6401 * extents. Drop our locks and wait for them to finish
6402 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006403 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6404 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006405 unlock_extent_cached(io_tree, page_start, page_end,
6406 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006407 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006408 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006409 btrfs_put_ordered_extent(ordered);
6410 goto again;
6411 }
6412
Josef Bacikfbf19082009-10-01 17:10:23 -04006413 /*
6414 * XXX - page_mkwrite gets called every time the page is dirtied, even
6415 * if it was already dirty, so for space accounting reasons we need to
6416 * clear any delalloc bits for the range we are fixing to save. There
6417 * is probably a better way to do this, but for now keep consistent with
6418 * prepare_pages in the normal write path.
6419 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006420 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006421 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006422 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006423
Josef Bacik2ac55d42010-02-03 19:33:23 +00006424 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6425 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006426 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006427 unlock_extent_cached(io_tree, page_start, page_end,
6428 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006429 ret = VM_FAULT_SIGBUS;
6430 goto out_unlock;
6431 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006432 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006433
6434 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006435 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006436 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006437 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006438 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006439
Chris Masone6dcd2d2008-07-17 12:53:50 -04006440 if (zero_start != PAGE_CACHE_SIZE) {
6441 kaddr = kmap(page);
6442 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6443 flush_dcache_page(page);
6444 kunmap(page);
6445 }
Chris Mason247e7432008-07-17 12:53:51 -04006446 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006447 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006448 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006449
Chris Mason257c62e2009-10-13 13:21:08 -04006450 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6451 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6452
Josef Bacik2ac55d42010-02-03 19:33:23 +00006453 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006454
6455out_unlock:
Chris Mason50a9b212009-09-11 12:33:12 -04006456 if (!ret)
6457 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04006458 unlock_page(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006459 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason1832a6d2007-12-21 16:27:21 -05006460out:
Chris Mason9ebefb182007-06-15 13:50:00 -04006461 return ret;
6462}
6463
Josef Bacika41ad392011-01-31 15:30:16 -05006464static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006465{
6466 struct btrfs_root *root = BTRFS_I(inode)->root;
6467 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006468 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006469 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006470 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04006471 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04006472
Josef Bacik5d5e1032009-10-13 16:46:49 -04006473 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6474 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006475 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006476
Chris Mason4a096752008-07-21 10:29:44 -04006477 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006478 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006479
Josef Bacikf0cd8462011-03-04 14:37:08 -05006480 trans = btrfs_start_transaction(root, 5);
6481 if (IS_ERR(trans))
6482 return PTR_ERR(trans);
6483
6484 btrfs_set_trans_block_group(trans, inode);
6485
6486 ret = btrfs_orphan_add(trans, inode);
6487 if (ret) {
6488 btrfs_end_transaction(trans, root);
6489 return ret;
6490 }
6491
6492 nr = trans->blocks_used;
6493 btrfs_end_transaction(trans, root);
6494 btrfs_btree_balance_dirty(root, nr);
6495
6496 /* Now start a transaction for the truncate */
Yan, Zhengd68fc572010-05-16 10:49:58 -04006497 trans = btrfs_start_transaction(root, 0);
Josef Bacik3893e332011-01-31 16:03:11 -05006498 if (IS_ERR(trans))
6499 return PTR_ERR(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00006500 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006501 trans->block_rsv = root->orphan_block_rsv;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006502
6503 /*
6504 * setattr is responsible for setting the ordered_data_close flag,
6505 * but that is only tested during the last file release. That
6506 * could happen well after the next commit, leaving a great big
6507 * window where new writes may get lost if someone chooses to write
6508 * to this file after truncating to zero
6509 *
6510 * The inode doesn't have any dirty data here, and so if we commit
6511 * this is a noop. If someone immediately starts writing to the inode
6512 * it is very likely we'll catch some of their writes in this
6513 * transaction, and the commit will find this file on the ordered
6514 * data list with good things to send down.
6515 *
6516 * This is a best effort solution, there is still a window where
6517 * using truncate to replace the contents of the file will
6518 * end up with a zero length file after a crash.
6519 */
6520 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
6521 btrfs_add_ordered_operation(trans, root, inode);
6522
Yan, Zheng80825102009-11-12 09:35:36 +00006523 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04006524 if (!trans) {
6525 trans = btrfs_start_transaction(root, 0);
Josef Bacik3893e332011-01-31 16:03:11 -05006526 if (IS_ERR(trans))
6527 return PTR_ERR(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006528 btrfs_set_trans_block_group(trans, inode);
6529 trans->block_rsv = root->orphan_block_rsv;
6530 }
6531
6532 ret = btrfs_block_rsv_check(trans, root,
6533 root->orphan_block_rsv, 0, 5);
Josef Bacik3893e332011-01-31 16:03:11 -05006534 if (ret == -EAGAIN) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04006535 ret = btrfs_commit_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05006536 if (ret)
6537 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006538 trans = NULL;
6539 continue;
Josef Bacik3893e332011-01-31 16:03:11 -05006540 } else if (ret) {
6541 err = ret;
6542 break;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006543 }
6544
Yan, Zheng80825102009-11-12 09:35:36 +00006545 ret = btrfs_truncate_inode_items(trans, root, inode,
6546 inode->i_size,
6547 BTRFS_EXTENT_DATA_KEY);
Josef Bacik3893e332011-01-31 16:03:11 -05006548 if (ret != -EAGAIN) {
6549 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006550 break;
Josef Bacik3893e332011-01-31 16:03:11 -05006551 }
Chris Mason39279cc2007-06-12 06:35:45 -04006552
Yan, Zheng80825102009-11-12 09:35:36 +00006553 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006554 if (ret) {
6555 err = ret;
6556 break;
6557 }
Chris Mason5f39d392007-10-15 16:14:19 -04006558
Yan, Zheng80825102009-11-12 09:35:36 +00006559 nr = trans->blocks_used;
6560 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006561 trans = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00006562 btrfs_btree_balance_dirty(root, nr);
Yan, Zheng80825102009-11-12 09:35:36 +00006563 }
6564
6565 if (ret == 0 && inode->i_nlink > 0) {
6566 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006567 if (ret)
6568 err = ret;
Josef Bacikded5db92011-03-04 14:09:46 -05006569 } else if (ret && inode->i_nlink > 0) {
6570 /*
6571 * Failed to do the truncate, remove us from the in memory
6572 * orphan list.
6573 */
6574 ret = btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00006575 }
6576
6577 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006578 if (ret && !err)
6579 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04006580
Josef Bacik7b128762008-07-24 12:17:14 -04006581 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04006582 ret = btrfs_end_transaction_throttle(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05006583 if (ret && !err)
6584 err = ret;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006585 btrfs_btree_balance_dirty(root, nr);
Josef Bacika41ad392011-01-31 15:30:16 -05006586
Josef Bacik3893e332011-01-31 16:03:11 -05006587 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04006588}
6589
Sven Wegener3b963622008-06-09 21:57:42 -04006590/*
Chris Masond352ac62008-09-29 15:18:18 -04006591 * create a new subvolume directory/inode (helper for the ioctl).
6592 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05006593int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Yan, Zheng76dda932009-09-21 16:00:26 -04006594 struct btrfs_root *new_root,
Yan Zhengd2fb3432008-12-11 16:30:39 -05006595 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04006596{
Chris Mason39279cc2007-06-12 06:35:45 -04006597 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04006598 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006599 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006600
Josef Bacikaec74772008-07-24 12:12:38 -04006601 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05006602 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04006603 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04006604 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006605 inode->i_op = &btrfs_dir_inode_operations;
6606 inode->i_fop = &btrfs_dir_file_operations;
6607
Chris Mason39279cc2007-06-12 06:35:45 -04006608 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04006609 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04006610
Yan, Zheng76dda932009-09-21 16:00:26 -04006611 err = btrfs_update_inode(trans, new_root, inode);
6612 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006613
Yan, Zheng76dda932009-09-21 16:00:26 -04006614 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006615 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006616}
6617
Chris Masond352ac62008-09-29 15:18:18 -04006618/* helper function for file defrag and space balancing. This
6619 * forces readahead on a given range of bytes in an inode
6620 */
Chris Masonedbd8d42007-12-21 16:27:24 -05006621unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04006622 struct file_ra_state *ra, struct file *file,
6623 pgoff_t offset, pgoff_t last_index)
6624{
Chris Mason8e7bf942008-04-28 09:02:36 -04006625 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04006626
Chris Mason86479a02007-09-10 19:58:16 -04006627 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
6628 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04006629}
6630
Chris Mason39279cc2007-06-12 06:35:45 -04006631struct inode *btrfs_alloc_inode(struct super_block *sb)
6632{
6633 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006634 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006635
6636 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6637 if (!ei)
6638 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006639
6640 ei->root = NULL;
6641 ei->space_info = NULL;
6642 ei->generation = 0;
6643 ei->sequence = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04006644 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04006645 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04006646 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006647 ei->delalloc_bytes = 0;
6648 ei->reserved_bytes = 0;
6649 ei->disk_i_size = 0;
6650 ei->flags = 0;
6651 ei->index_cnt = (u64)-1;
6652 ei->last_unlink_trans = 0;
6653
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006654 atomic_set(&ei->outstanding_extents, 0);
Josef Bacik57a45ced2011-01-25 16:30:38 -05006655 atomic_set(&ei->reserved_extents, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006656
6657 ei->ordered_data_close = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006658 ei->orphan_meta_reserved = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006659 ei->dummy_inode = 0;
Li Zefan261507a02010-12-17 14:21:50 +08006660 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006661
6662 inode = &ei->vfs_inode;
6663 extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
6664 extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
6665 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
6666 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006667 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04006668 INIT_LIST_HEAD(&ei->i_orphan);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006669 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006670 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006671 RB_CLEAR_NODE(&ei->rb_node);
6672
6673 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006674}
6675
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11006676static void btrfs_i_callback(struct rcu_head *head)
6677{
6678 struct inode *inode = container_of(head, struct inode, i_rcu);
6679 INIT_LIST_HEAD(&inode->i_dentry);
6680 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6681}
6682
Chris Mason39279cc2007-06-12 06:35:45 -04006683void btrfs_destroy_inode(struct inode *inode)
6684{
Chris Masone6dcd2d2008-07-17 12:53:50 -04006685 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006686 struct btrfs_root *root = BTRFS_I(inode)->root;
6687
Chris Mason39279cc2007-06-12 06:35:45 -04006688 WARN_ON(!list_empty(&inode->i_dentry));
6689 WARN_ON(inode->i_data.nrpages);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006690 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
Josef Bacik57a45ced2011-01-25 16:30:38 -05006691 WARN_ON(atomic_read(&BTRFS_I(inode)->reserved_extents));
Chris Mason39279cc2007-06-12 06:35:45 -04006692
Chris Mason5a3f23d2009-03-31 13:27:11 -04006693 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05006694 * This can happen where we create an inode, but somebody else also
6695 * created the same inode and we need to destroy the one we already
6696 * created.
6697 */
6698 if (!root)
6699 goto free;
6700
6701 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006702 * Make sure we're properly removed from the ordered operation
6703 * lists.
6704 */
6705 smp_mb();
6706 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6707 spin_lock(&root->fs_info->ordered_extent_lock);
6708 list_del_init(&BTRFS_I(inode)->ordered_operations);
6709 spin_unlock(&root->fs_info->ordered_extent_lock);
6710 }
6711
Josef Bacik0af3d002010-06-21 14:48:16 -04006712 if (root == root->fs_info->tree_root) {
6713 struct btrfs_block_group_cache *block_group;
6714
6715 block_group = btrfs_lookup_block_group(root->fs_info,
6716 BTRFS_I(inode)->block_group);
6717 if (block_group && block_group->inode == inode) {
6718 spin_lock(&block_group->lock);
6719 block_group->inode = NULL;
6720 spin_unlock(&block_group->lock);
6721 btrfs_put_block_group(block_group);
6722 } else if (block_group) {
6723 btrfs_put_block_group(block_group);
6724 }
6725 }
6726
Yan, Zhengd68fc572010-05-16 10:49:58 -04006727 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006728 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yan, Zheng80825102009-11-12 09:35:36 +00006729 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
6730 inode->i_ino);
6731 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04006732 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006733 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006734
Chris Masond3977122009-01-05 21:25:51 -05006735 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006736 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
6737 if (!ordered)
6738 break;
6739 else {
Chris Masond3977122009-01-05 21:25:51 -05006740 printk(KERN_ERR "btrfs found ordered "
6741 "extent %llu %llu on inode cleanup\n",
6742 (unsigned long long)ordered->file_offset,
6743 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006744 btrfs_remove_ordered_extent(inode, ordered);
6745 btrfs_put_ordered_extent(ordered);
6746 btrfs_put_ordered_extent(ordered);
6747 }
6748 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006749 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006750 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05006751free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11006752 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04006753}
6754
Al Viro45321ac2010-06-07 13:43:19 -04006755int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04006756{
6757 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04006758
Josef Bacik0af3d002010-06-21 14:48:16 -04006759 if (btrfs_root_refs(&root->root_item) == 0 &&
6760 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04006761 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04006762 else
Al Viro45321ac2010-06-07 13:43:19 -04006763 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04006764}
6765
Sven Wegener0ee0fda2008-07-30 16:54:26 -04006766static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04006767{
6768 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
6769
6770 inode_init_once(&ei->vfs_inode);
6771}
6772
6773void btrfs_destroy_cachep(void)
6774{
6775 if (btrfs_inode_cachep)
6776 kmem_cache_destroy(btrfs_inode_cachep);
6777 if (btrfs_trans_handle_cachep)
6778 kmem_cache_destroy(btrfs_trans_handle_cachep);
6779 if (btrfs_transaction_cachep)
6780 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04006781 if (btrfs_path_cachep)
6782 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05006783 if (btrfs_free_space_cachep)
6784 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04006785}
6786
6787int btrfs_init_cachep(void)
6788{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006789 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
6790 sizeof(struct btrfs_inode), 0,
6791 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04006792 if (!btrfs_inode_cachep)
6793 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006794
6795 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
6796 sizeof(struct btrfs_trans_handle), 0,
6797 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006798 if (!btrfs_trans_handle_cachep)
6799 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006800
6801 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
6802 sizeof(struct btrfs_transaction), 0,
6803 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006804 if (!btrfs_transaction_cachep)
6805 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006806
6807 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
6808 sizeof(struct btrfs_path), 0,
6809 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006810 if (!btrfs_path_cachep)
6811 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006812
Josef Bacikdc89e982011-01-28 17:05:48 -05006813 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
6814 sizeof(struct btrfs_free_space), 0,
6815 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
6816 if (!btrfs_free_space_cachep)
6817 goto fail;
6818
Chris Mason39279cc2007-06-12 06:35:45 -04006819 return 0;
6820fail:
6821 btrfs_destroy_cachep();
6822 return -ENOMEM;
6823}
6824
6825static int btrfs_getattr(struct vfsmount *mnt,
6826 struct dentry *dentry, struct kstat *stat)
6827{
6828 struct inode *inode = dentry->d_inode;
6829 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05006830 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05006831 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006832 stat->blocks = (inode_get_bytes(inode) +
6833 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04006834 return 0;
6835}
6836
Chris Masond3977122009-01-05 21:25:51 -05006837static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6838 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04006839{
6840 struct btrfs_trans_handle *trans;
6841 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006842 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04006843 struct inode *new_inode = new_dentry->d_inode;
6844 struct inode *old_inode = old_dentry->d_inode;
6845 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006846 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006847 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04006848 int ret;
6849
Yan, Zhengf679a842009-09-24 09:17:31 -04006850 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
6851 return -EPERM;
6852
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006853 /* we only allow rename subvolume link between subvolumes */
6854 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05006855 return -EXDEV;
6856
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006857 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
6858 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
6859 return -ENOTEMPTY;
6860
Chris Mason39279cc2007-06-12 06:35:45 -04006861 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006862 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04006863 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006864 /*
6865 * we're using rename to replace one file with another.
6866 * and the replacement file is large. Start IO on it now so
6867 * we don't add too much work to the end of the transaction
6868 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04006869 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04006870 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
6871 filemap_flush(old_inode->i_mapping);
6872
Yan, Zheng76dda932009-09-21 16:00:26 -04006873 /* close the racy window with snapshot create/destroy ioctl */
6874 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
6875 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006876 /*
6877 * We want to reserve the absolute worst case amount of items. So if
6878 * both inodes are subvols and we need to unlink them then that would
6879 * require 4 item modifications, but if they are both normal inodes it
6880 * would require 5 item modifications, so we'll assume their normal
6881 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
6882 * should cover the worst case number of items we'll modify.
6883 */
6884 trans = btrfs_start_transaction(root, 20);
6885 if (IS_ERR(trans))
6886 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04006887
Yan, Zhenga5719522009-09-24 09:17:31 -04006888 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04006889
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006890 if (dest != root)
6891 btrfs_record_root_in_trans(trans, dest);
6892
Yan, Zhenga5719522009-09-24 09:17:31 -04006893 ret = btrfs_set_inode_index(new_dir, &index);
6894 if (ret)
6895 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006896
Yan, Zhenga5719522009-09-24 09:17:31 -04006897 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006898 /* force full log commit if subvolume involved. */
6899 root->fs_info->last_trans_log_full_commit = trans->transid;
6900 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04006901 ret = btrfs_insert_inode_ref(trans, dest,
6902 new_dentry->d_name.name,
6903 new_dentry->d_name.len,
6904 old_inode->i_ino,
6905 new_dir->i_ino, index);
6906 if (ret)
6907 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006908 /*
6909 * this is an ugly little race, but the rename is required
6910 * to make sure that if we crash, the inode is either at the
6911 * old name or the new one. pinning the log transaction lets
6912 * us make sure we don't allow a log commit to come in after
6913 * we unlink the name but before we add the new name back in.
6914 */
6915 btrfs_pin_log_trans(root);
6916 }
Chris Mason12fcfd22009-03-24 10:24:20 -04006917 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006918 * make sure the inode gets flushed if it is replacing
6919 * something.
6920 */
6921 if (new_inode && new_inode->i_size &&
6922 old_inode && S_ISREG(old_inode->i_mode)) {
6923 btrfs_add_ordered_operation(trans, root, old_inode);
6924 }
6925
Chris Mason39279cc2007-06-12 06:35:45 -04006926 old_dir->i_ctime = old_dir->i_mtime = ctime;
6927 new_dir->i_ctime = new_dir->i_mtime = ctime;
6928 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04006929
Chris Mason12fcfd22009-03-24 10:24:20 -04006930 if (old_dentry->d_parent != new_dentry->d_parent)
6931 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
6932
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006933 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
6934 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
6935 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
6936 old_dentry->d_name.name,
6937 old_dentry->d_name.len);
6938 } else {
6939 btrfs_inc_nlink(old_dentry->d_inode);
6940 ret = btrfs_unlink_inode(trans, root, old_dir,
6941 old_dentry->d_inode,
6942 old_dentry->d_name.name,
6943 old_dentry->d_name.len);
6944 }
6945 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006946
6947 if (new_inode) {
6948 new_inode->i_ctime = CURRENT_TIME;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006949 if (unlikely(new_inode->i_ino ==
6950 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
6951 root_objectid = BTRFS_I(new_inode)->location.objectid;
6952 ret = btrfs_unlink_subvol(trans, dest, new_dir,
6953 root_objectid,
6954 new_dentry->d_name.name,
6955 new_dentry->d_name.len);
6956 BUG_ON(new_inode->i_nlink == 0);
6957 } else {
6958 ret = btrfs_unlink_inode(trans, dest, new_dir,
6959 new_dentry->d_inode,
6960 new_dentry->d_name.name,
6961 new_dentry->d_name.len);
6962 }
6963 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04006964 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04006965 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006966 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04006967 }
Chris Mason39279cc2007-06-12 06:35:45 -04006968 }
Josef Bacikaec74772008-07-24 12:12:38 -04006969
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006970 ret = btrfs_add_link(trans, new_dir, old_inode,
6971 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04006972 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006973 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006974
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006975 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik6a912212010-11-20 09:48:00 +00006976 struct dentry *parent = dget_parent(new_dentry);
6977 btrfs_log_new_name(trans, old_inode, old_dir, parent);
6978 dput(parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006979 btrfs_end_log_trans(root);
6980 }
Chris Mason39279cc2007-06-12 06:35:45 -04006981out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04006982 btrfs_end_transaction_throttle(trans, root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006983
Yan, Zheng76dda932009-09-21 16:00:26 -04006984 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
6985 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006986
Chris Mason39279cc2007-06-12 06:35:45 -04006987 return ret;
6988}
6989
Chris Masond352ac62008-09-29 15:18:18 -04006990/*
6991 * some fairly slow code that needs optimization. This walks the list
6992 * of all the inodes with pending delalloc and forces them to disk.
6993 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00006994int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04006995{
6996 struct list_head *head = &root->fs_info->delalloc_inodes;
6997 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006998 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04006999
Yan Zhengc146afa2008-11-12 14:34:12 -05007000 if (root->fs_info->sb->s_flags & MS_RDONLY)
7001 return -EROFS;
7002
Chris Mason75eff682008-12-15 15:54:40 -05007003 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05007004 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04007005 binode = list_entry(head->next, struct btrfs_inode,
7006 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007007 inode = igrab(&binode->vfs_inode);
7008 if (!inode)
7009 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007010 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007011 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007012 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007013 if (delay_iput)
7014 btrfs_add_delayed_iput(inode);
7015 else
7016 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007017 }
7018 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007019 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007020 }
Chris Mason75eff682008-12-15 15:54:40 -05007021 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007022
7023 /* the filemap_flush will queue IO into the worker threads, but
7024 * we have to make sure the IO is actually started and that
7025 * ordered extents get created before we return
7026 */
7027 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007028 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007029 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007030 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007031 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7032 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007033 }
7034 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04007035 return 0;
7036}
7037
Josef Bacik0019f102010-10-15 15:18:40 -04007038int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput,
7039 int sync)
Yan, Zheng5da9d012010-05-16 10:46:25 -04007040{
7041 struct btrfs_inode *binode;
7042 struct inode *inode = NULL;
7043
7044 spin_lock(&root->fs_info->delalloc_lock);
7045 while (!list_empty(&root->fs_info->delalloc_inodes)) {
7046 binode = list_entry(root->fs_info->delalloc_inodes.next,
7047 struct btrfs_inode, delalloc_inodes);
7048 inode = igrab(&binode->vfs_inode);
7049 if (inode) {
7050 list_move_tail(&binode->delalloc_inodes,
7051 &root->fs_info->delalloc_inodes);
7052 break;
7053 }
7054
7055 list_del_init(&binode->delalloc_inodes);
7056 cond_resched_lock(&root->fs_info->delalloc_lock);
7057 }
7058 spin_unlock(&root->fs_info->delalloc_lock);
7059
7060 if (inode) {
Josef Bacik0019f102010-10-15 15:18:40 -04007061 if (sync) {
7062 filemap_write_and_wait(inode->i_mapping);
7063 /*
7064 * We have to do this because compression doesn't
7065 * actually set PG_writeback until it submits the pages
7066 * for IO, which happens in an async thread, so we could
7067 * race and not actually wait for any writeback pages
7068 * because they've not been submitted yet. Technically
7069 * this could still be the case for the ordered stuff
7070 * since the async thread may not have started to do its
7071 * work yet. If this becomes the case then we need to
7072 * figure out a way to make sure that in writepage we
7073 * wait for any async pages to be submitted before
7074 * returning so that fdatawait does what its supposed to
7075 * do.
7076 */
7077 btrfs_wait_ordered_range(inode, 0, (u64)-1);
7078 } else {
7079 filemap_flush(inode->i_mapping);
7080 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04007081 if (delay_iput)
7082 btrfs_add_delayed_iput(inode);
7083 else
7084 iput(inode);
7085 return 1;
7086 }
7087 return 0;
7088}
7089
Chris Mason39279cc2007-06-12 06:35:45 -04007090static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7091 const char *symname)
7092{
7093 struct btrfs_trans_handle *trans;
7094 struct btrfs_root *root = BTRFS_I(dir)->root;
7095 struct btrfs_path *path;
7096 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007097 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007098 int err;
7099 int drop_inode = 0;
7100 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007101 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007102 int name_len;
7103 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007104 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007105 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007106 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05007107 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007108
7109 name_len = strlen(symname) + 1;
7110 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7111 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007112
Yan, Zhenga22285a2010-05-16 10:48:46 -04007113 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
7114 if (err)
7115 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -04007116 /*
7117 * 2 items for inode item and ref
7118 * 2 items for dir items
7119 * 1 item for xattr if selinux is on
7120 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007121 trans = btrfs_start_transaction(root, 5);
7122 if (IS_ERR(trans))
7123 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007124
Chris Mason39279cc2007-06-12 06:35:45 -04007125 btrfs_set_trans_block_group(trans, dir);
7126
Josef Bacikaec74772008-07-24 12:12:38 -04007127 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00007128 dentry->d_name.len, dir->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04007129 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
7130 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04007131 err = PTR_ERR(inode);
7132 if (IS_ERR(inode))
7133 goto out_unlock;
7134
Yan, Zhengf34f57a2009-11-12 09:35:27 +00007135 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04007136 if (err) {
7137 drop_inode = 1;
7138 goto out_unlock;
7139 }
7140
Chris Mason39279cc2007-06-12 06:35:45 -04007141 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00007142 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007143 if (err)
7144 drop_inode = 1;
7145 else {
7146 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007147 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04007148 inode->i_fop = &btrfs_file_operations;
7149 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05007150 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007151 }
Chris Mason39279cc2007-06-12 06:35:45 -04007152 btrfs_update_inode_block_group(trans, inode);
7153 btrfs_update_inode_block_group(trans, dir);
7154 if (drop_inode)
7155 goto out_unlock;
7156
7157 path = btrfs_alloc_path();
7158 BUG_ON(!path);
7159 key.objectid = inode->i_ino;
7160 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007161 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7162 datasize = btrfs_file_extent_calc_inline_size(name_len);
7163 err = btrfs_insert_empty_item(trans, root, path, &key,
7164 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007165 if (err) {
7166 drop_inode = 1;
7167 goto out_unlock;
7168 }
Chris Mason5f39d392007-10-15 16:14:19 -04007169 leaf = path->nodes[0];
7170 ei = btrfs_item_ptr(leaf, path->slots[0],
7171 struct btrfs_file_extent_item);
7172 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7173 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007174 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007175 btrfs_set_file_extent_encryption(leaf, ei, 0);
7176 btrfs_set_file_extent_compression(leaf, ei, 0);
7177 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7178 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7179
Chris Mason39279cc2007-06-12 06:35:45 -04007180 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007181 write_extent_buffer(leaf, symname, ptr, name_len);
7182 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007183 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007184
Chris Mason39279cc2007-06-12 06:35:45 -04007185 inode->i_op = &btrfs_symlink_inode_operations;
7186 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007187 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007188 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007189 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007190 err = btrfs_update_inode(trans, root, inode);
7191 if (err)
7192 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007193
7194out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007195 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04007196 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007197 if (drop_inode) {
7198 inode_dec_link_count(inode);
7199 iput(inode);
7200 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007201 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04007202 return err;
7203}
Chris Mason16432982008-04-10 10:23:21 -04007204
Josef Bacik0af3d002010-06-21 14:48:16 -04007205static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7206 u64 start, u64 num_bytes, u64 min_size,
7207 loff_t actual_len, u64 *alloc_hint,
7208 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007209{
Yan Zhengd899e052008-10-30 14:25:28 -04007210 struct btrfs_root *root = BTRFS_I(inode)->root;
7211 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007212 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007213 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007214 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007215 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007216
Josef Bacik0af3d002010-06-21 14:48:16 -04007217 if (trans)
7218 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007219 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007220 if (own_trans) {
7221 trans = btrfs_start_transaction(root, 3);
7222 if (IS_ERR(trans)) {
7223 ret = PTR_ERR(trans);
7224 break;
7225 }
Yan Zhengd899e052008-10-30 14:25:28 -04007226 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007227
Yan, Zhengefa56462010-05-16 10:49:59 -04007228 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
7229 0, *alloc_hint, (u64)-1, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007230 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007231 if (own_trans)
7232 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007233 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007234 }
7235
Yan Zhengd899e052008-10-30 14:25:28 -04007236 ret = insert_reserved_file_extent(trans, inode,
7237 cur_offset, ins.objectid,
7238 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007239 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007240 BTRFS_FILE_EXTENT_PREALLOC);
7241 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04007242 btrfs_drop_extent_cache(inode, cur_offset,
7243 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007244
Yan Zhengd899e052008-10-30 14:25:28 -04007245 num_bytes -= ins.offset;
7246 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007247 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007248
Yan Zhengd899e052008-10-30 14:25:28 -04007249 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007250 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007251 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007252 (actual_len > inode->i_size) &&
7253 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007254 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007255 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007256 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007257 i_size = cur_offset;
7258 i_size_write(inode, i_size);
7259 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007260 }
7261
Yan Zhengd899e052008-10-30 14:25:28 -04007262 ret = btrfs_update_inode(trans, root, inode);
7263 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04007264
Josef Bacik0af3d002010-06-21 14:48:16 -04007265 if (own_trans)
7266 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007267 }
Yan Zhengd899e052008-10-30 14:25:28 -04007268 return ret;
7269}
7270
Josef Bacik0af3d002010-06-21 14:48:16 -04007271int btrfs_prealloc_file_range(struct inode *inode, int mode,
7272 u64 start, u64 num_bytes, u64 min_size,
7273 loff_t actual_len, u64 *alloc_hint)
7274{
7275 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7276 min_size, actual_len, alloc_hint,
7277 NULL);
7278}
7279
7280int btrfs_prealloc_file_range_trans(struct inode *inode,
7281 struct btrfs_trans_handle *trans, int mode,
7282 u64 start, u64 num_bytes, u64 min_size,
7283 loff_t actual_len, u64 *alloc_hint)
7284{
7285 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7286 min_size, actual_len, alloc_hint, trans);
7287}
7288
Chris Masone6dcd2d2008-07-17 12:53:50 -04007289static int btrfs_set_page_dirty(struct page *page)
7290{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007291 return __set_page_dirty_nobuffers(page);
7292}
7293
Nick Pigginb74c79e2011-01-07 17:49:58 +11007294static int btrfs_permission(struct inode *inode, int mask, unsigned int flags)
Yanfdebe2b2008-01-14 13:26:08 -05007295{
Li Zefanb83cc962010-12-20 16:04:08 +08007296 struct btrfs_root *root = BTRFS_I(inode)->root;
7297
7298 if (btrfs_root_readonly(root) && (mask & MAY_WRITE))
7299 return -EROFS;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007300 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05007301 return -EACCES;
Nick Pigginb74c79e2011-01-07 17:49:58 +11007302 return generic_permission(inode, mask, flags, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05007303}
Chris Mason39279cc2007-06-12 06:35:45 -04007304
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007305static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007306 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007307 .lookup = btrfs_lookup,
7308 .create = btrfs_create,
7309 .unlink = btrfs_unlink,
7310 .link = btrfs_link,
7311 .mkdir = btrfs_mkdir,
7312 .rmdir = btrfs_rmdir,
7313 .rename = btrfs_rename,
7314 .symlink = btrfs_symlink,
7315 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007316 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007317 .setxattr = btrfs_setxattr,
7318 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007319 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007320 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007321 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04007322};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007323static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007324 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007325 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04007326};
Yan, Zheng76dda932009-09-21 16:00:26 -04007327
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007328static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007329 .llseek = generic_file_llseek,
7330 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007331 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007332 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007333#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007334 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007335#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007336 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007337 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007338};
7339
Chris Masond1310b22008-01-24 16:13:08 -05007340static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007341 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007342 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007343 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007344 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007345 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007346 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04007347 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007348 .set_bit_hook = btrfs_set_bit_hook,
7349 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007350 .merge_extent_hook = btrfs_merge_extent_hook,
7351 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007352};
7353
Chris Mason35054392009-01-21 13:11:13 -05007354/*
7355 * btrfs doesn't support the bmap operation because swapfiles
7356 * use bmap to make a mapping of extents in the file. They assume
7357 * these extents won't change over the life of the file and they
7358 * use the bmap result to do IO directly to the drive.
7359 *
7360 * the btrfs bmap call would return logical addresses that aren't
7361 * suitable for IO and they also will change frequently as COW
7362 * operations happen. So, swapfile + btrfs == corruption.
7363 *
7364 * For now we're avoiding this by dropping bmap.
7365 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007366static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007367 .readpage = btrfs_readpage,
7368 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04007369 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007370 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04007371 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04007372 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007373 .invalidatepage = btrfs_invalidatepage,
7374 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007375 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007376 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007377};
7378
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007379static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007380 .readpage = btrfs_readpage,
7381 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007382 .invalidatepage = btrfs_invalidatepage,
7383 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007384};
7385
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007386static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007387 .getattr = btrfs_getattr,
7388 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007389 .setxattr = btrfs_setxattr,
7390 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007391 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007392 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007393 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007394 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04007395};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007396static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007397 .getattr = btrfs_getattr,
7398 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007399 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007400 .setxattr = btrfs_setxattr,
7401 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007402 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007403 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007404};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007405static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007406 .readlink = generic_readlink,
7407 .follow_link = page_follow_link_light,
7408 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007409 .getattr = btrfs_getattr,
Yanfdebe2b2008-01-14 13:26:08 -05007410 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007411 .setxattr = btrfs_setxattr,
7412 .getxattr = btrfs_getxattr,
7413 .listxattr = btrfs_listxattr,
7414 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007415};
Yan, Zheng76dda932009-09-21 16:00:26 -04007416
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007417const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007418 .d_delete = btrfs_dentry_delete,
7419};